javascript - Flot: how to style numbers within bar-charts? - Stack Overflow

I'm using FlotCharts and it's plugin flot.barnumbers (Demo) to display charts.I need to displ

I'm using FlotCharts and it's plugin flot.barnumbers (Demo) to display charts.

I need to display the numbers within the bar, it works. Unfortunately I've no idea how to style the numbers and cannot find anything in the Docs or via Google.

I'd like to use something like (apparently not possible):

bars: {
    numbers: {
        numberFormatter: function(v, bar) {
            return '<div class="pimp-my-number-class">'+ v +'</div>';               
        }
    }
}

Does anyone have an idea, how to fix this problem?

Thank's in advance!

I'm using FlotCharts and it's plugin flot.barnumbers (Demo) to display charts.

I need to display the numbers within the bar, it works. Unfortunately I've no idea how to style the numbers and cannot find anything in the Docs or via Google.

I'd like to use something like (apparently not possible):

bars: {
    numbers: {
        numberFormatter: function(v, bar) {
            return '<div class="pimp-my-number-class">'+ v +'</div>';               
        }
    }
}

Does anyone have an idea, how to fix this problem?

Thank's in advance!

Share Improve this question asked Aug 30, 2013 at 1:52 Mr. B.Mr. B. 8,72516 gold badges75 silver badges120 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

One of the nicest things about flot is that it provides the basics and then gets out of your way. Here's a quick code example where I've implemented this myself (ie no plugins). It's short and sweet and you have plete control over appearance.

$(function() {

    dsHook = function(plot, canvascontext, series){
        for (var i = 0; i < series.data.length; i++){ // loop the series
           var offset = plot.offset(); // offset of canvas to body
           var dP = series.data[i]; // our data point
           var pos = plot.p2c({x: dP[0], y: dP[1]}); // position of point           
           var barWidth = plot.p2c({x: dP[0]+series.bars.barWidth, y: dP[1]}).left - pos.left; // calc width of bar
           pos.left += offset.left; pos.top += offset.top; //adjust position for offset
           var aDiv = $('<div></div>').css({'width':barWidth, 'background-color':'green','color':'white','text-align':'center','position':'absolute','left': pos.left,'top':pos.top}).text(dP[1]).appendTo("body"); // add an absolute div with the number
        }
    }

    var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

    somePlot = $.plot("#placeholder", [{
            data: d2,
            bars: { show: true }
        }],
        { hooks: { drawSeries: [dsHook] } }
    );
});

flot.barnumbers doesn't support styling of the inserted numbers. You have to add that yourself to the code. Have a look at Drawing text using a canvas.

I would try to achive your desired layout by using a bination of annotations like here and a second data series behind your actual series. The second series then should have equal x values as your original series. To the corresponding y values you add a constant (e.g. 2).

You can style the annotations and the second series as you like. The fiddly part would be to place the annotations at correct positions.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信