javascript - Flot Data Labels on Horizontal Bar Chart - Stack Overflow

Too much code to paste here so....Please see JS Fiddle atfor current code.I am attempting to create

Too much code to paste here so....

Please see JS Fiddle at / for current code.

I am attempting to create a horizontal bar chart using flot. As you will see from the Fiddle, this works fine but I want to display the values of the bars within the bars themselves and not as labels in the Y Axis, as in the picture below...

I have attempted to use the "labels" plugin and also the barnumbers plugin but these don't seem to work. (Barnumbers es close but displays 0 1 2 3 as the values.

Any ideas?

Too much code to paste here so....

Please see JS Fiddle at http://jsfiddle/1a2s35m2/ for current code.

I am attempting to create a horizontal bar chart using flot. As you will see from the Fiddle, this works fine but I want to display the values of the bars within the bars themselves and not as labels in the Y Axis, as in the picture below...

I have attempted to use the "labels" plugin and also the barnumbers plugin but these don't seem to work. (Barnumbers es close but displays 0 1 2 3 as the values.

Any ideas?

Share Improve this question asked Nov 17, 2014 at 15:34 Pandy LegendPandy Legend 1,1113 gold badges15 silver badges31 bronze badges 1
  • 1 Have a look at this link. Here is a good example – Blake Commented Nov 17, 2014 at 15:43
Add a ment  | 

1 Answer 1

Reset to default 8

I'm really starting to sound like a broken record on here, but as your charts bee really plicated forget the plugins and do it yourself.

Here's modified code from my links above catered for how you drew your plots:

    // 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);
    }

Updated fiddle.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744259699a4565570.html

相关推荐

  • javascript - Flot Data Labels on Horizontal Bar Chart - Stack Overflow

    Too much code to paste here so....Please see JS Fiddle atfor current code.I am attempting to create

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信