javascript-水平条形图上的Flot数据标签
作者:互联网
Too much code to paste here so....
有关当前代码,请参见http://jsfiddle.net/1a2s35m2/的JS Fiddle.
我正在尝试使用flot创建水平条形图.正如您从小提琴中看到的那样,这很好用,但我想在条形图本身中显示条形图的值,而不是在Y轴上显示为标签,如下图所示…
我尝试使用“标签”插件和barnumbers插件,但这些似乎不起作用. (条码编号接近,但显示0 1 2 3作为值.
有任何想法吗?
解决方法:
我的确开始听起来像是broken record,但是随着您的图表变得非常复杂,请忘记插件并自己做.
以下是我上面链接中的修改后的代码,可满足您绘制绘图的需要:
// after initial plot draw, then loop the data, add the labels
// I'm drawing these directly on the canvas, NO HTML DIVS!
// code is un-necessarily verbose for demonstration purposes
var ctx = somePlot.getCanvas().getContext("2d"); // get the context
var allSeries = somePlot.getData(); // get your series data
var xaxis = somePlot.getXAxes()[0]; // xAxis
var yaxis = somePlot.getYAxes()[0]; // yAxis
var offset = somePlot.getPlotOffset(); // plots offset
ctx.font = "12px 'Segoe UI'"; // set a pretty label font
ctx.fillStyle = "black";
for (var i = 0; i < allSeries.length; i++){
var series = allSeries[i];
var dataPoint = series.datapoints.points; // one point per series
var x = dataPoint[0];
var y = dataPoint[1];
var text = x + '%';
var metrics = ctx.measureText(text);
var xPos = xaxis.p2c(x)+offset.left - metrics.width; // place at end of bar
var yPos = yaxis.p2c(y) + offset.top - 2;
ctx.fillText(text, xPos, yPos);
}
更新了fiddle.
标签:javascript,jquery,flot 来源: https://codeday.me/bug/20191009/1878971.html