JQPlot - 将点标签移动到图形的起点

发布于 2024-11-01 10:33:07 字数 1243 浏览 8 评论 0原文

我一直在研究 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%,则不会显示任何内容。并且点数显示为整数。

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

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

示例输出 1

示例输出 2

谢谢:)

I have been working on a jqplot horizontal bar graph.

I WANTED TO HAVE THIS OUTPUT (I wanted the Point Labels to be in percentage and should be placed at the starting point of the graph)

Expected Outpt

Here is my code.....

$.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\%'}
        }
    }
});

Right now the point labels are displayed after the end of the bar graph and if the point value is somewhere near 100% it won't display anything. And the Points are displayed as a whole number.

Is there a way that I could move the points near the starting point of the bar graph?

The code above displays these sample outputs… I hop you can help me fix my problem :(

Sample Output 1

Sample Output 2

Thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-11-08 10:33:07

不知何故,我在@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');

Somehow I managed to solve my problem with the help of @Matt and also I add a few lines of code to meet the requirements I wantend. Instead of placing the point labels on the starting point of the bar graph, I placed it at the center to make it more readable and presentable.

Here is my code: (I'm open for suggestions if you feel you have a better solution) thanks

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');
鲜肉鲜肉永远不皱 2024-11-08 10:33:07

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

边缘容差

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


当我评论您的问题时,我不确定您是否确实发布了用于生成这些图表的代码。 这里是该代码的直接转储(已修复拼写错误), 这里是我能找到的最接近你的照片的地方,那就是对原始代码进行了大量更改。

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

To allow the labels to show when the bar is near 100%, use the PointLabels edgeTolerance option.

edgeTolerance

Number of pixels that the label must be away from an axis boundary in order to be drawn. Negative values will allow overlap with the grid boundaries.


As I commented on your question, I'm not sure that you actually posted the code you used to generate those charts. Here is a direct dump of that code (with typos fixed), and here is the closest I could get to your pictures, and that's with a lot of changes to your original code.

Mind posting a jsfiddle that shows how you generated those examples?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文