【问题标题】:JQPlot - Move the point label to the starting point of the graphJQPlot - 将点标签移动到图形的起点
【发布时间】:2011-04-15 03:44:03
【问题描述】:

我一直在研究 jqplot 水平条形图。

我想要这个输出(我希望点标签是百分比,并且应该放在图表的起点)

这是我的代码.....

$.jqplot.config.enablePlugins = true;

voteResults = [[Chinabank,0],['Citibank',100], ['UOB',0]['POSB',0],['OCBC',0]];
// voteResults = [[Chinabank,50],['Citibank',50], ['UOB',0]['POSB',0],['OCBC',0]];

plot = $.jqplot('chart1', [voteResults], {
    seriesDefaults:{
        renderer:$.jqplot.BarRenderer,
        shadowAngle: 135,
        rendererOptions: {
            barDirection: 'horizontal',
            barWidth:15,
            barMargin: 25
        }
    },
    axes: {
        yaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            tickOptions:{
                showGridline:true,
                markSize:0
            }
        },
        xaxis:{
            ticks:[0,100],
            tickOptions:{formatString:'%d\%'}
        }
    }
});

现在点标签显示在条形图的末尾之后,如果点值接近 100%,则不会显示任何内容。点数显示为整数。

有没有办法可以移动条形图起点附近的点?

上面的代码显示了这些示例输出......我希望你能帮我解决我的问题:(

谢谢你:)

【问题讨论】:

  • 对于初学者,\% 不是 JavaScript 中的有效转义序列,因此您可能需要改用 {formatString:'%d%'}
  • 另外,我无法看到该代码如何生成示例输出。

标签: jquery asp.net jquery-plugins graph jqplot


【解决方案1】:

不知何故,我设法在@Matt 的帮助下解决了我的问题,并且我添加了几行代码来满足我想要的要求。我没有将点标签放在条形图的起点上,而是将其放在中心以使其更具可读性和可呈现性。

这是我的代码:(如果您认为自己有更好的解决方案,我愿意提供建议)谢谢

var plot = $.jqplot('PollChart', [voteResults], {
    seriesDefaults: {
        renderer: $.jqplot.BarRenderer,
        pointLabels: { show: true, location: 'e', edgeTolerance: -50 },
        shadowAngle: 135,
        rendererOptions: {
            barDirection: 'horizontal',
            barWidth: 15,
            barMargin: 25
        }
    },
    axes: {
        yaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            tickOptions: {
                showGridline: true,
                markSize: 0
            }
        },
        xaxis: {
            ticks: [0, 100],
            tickOptions: {
                formatString: '%d%',
                showGridline: false
            }
        }
    }
});

// these lines here positions the Point Labels at the center of the graph
var ChartStartingPoint = parseInt($('#PollChart .jqplot-series-canvas').css('left'),10);
var ChartWidth = parseInt($('#PollChart .jqplot-series-canvas').css('width'),10);
var PointLabelLocation = (ChartWidth/2)+ ChartStartingPoint;
$('#PollChart .jqplot-point-label').css('left',PointLabelLocation+'px');

【讨论】:

    【解决方案2】:

    要在条形图接近 100% 时显示标签,请使用 PointLabels edgeTolerance 选项。

    edgeTolerance

    标签必须远离轴边界才能绘制的像素数。负值将允许与网格边界重叠。


    正如我对您的问题的评论,我不确定您是否实际发布了用于生成这些图表的代码。 Here 是该代码的直接转储(修正了拼写错误),here 是我能找到的最接近您的图片的,并且对您的原始代码进行了很多更改。

    介意发布一个展示您如何生成这些示例的 jsfiddle 吗?

    【讨论】:

    • 嗨,马特,我发布的代码实际上显示了我拥有的示例输出的第一张图片。我刚刚编辑了 voteResults 的值,我注意到我传递的参数都是字符串,这就是百分比没有生效的原因。我现在也修改了我的代码,我目前正在使用你的代码,谢谢你。我将发布工作代码。
    • 此外,edgeTolerance 似乎并不能解决我的问题,所以我尝试通过选择 jqplot-point-label 容器并将其定位到中心来将 div 定位在中心
    猜你喜欢
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多