Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center bars in Flot charts?

I'm using Flot library with angular-flot. I have a fixed sized chart which doesn't look nice when having f.e. 1 or 2 bars. You can see the example below for 2 bars:

2 bars

The bars try to spread over the whole length of the x axis. I want the to be centered - they should "start" in the middle and spread horizontally. Is that possible in Flot?

My options for bars:

options: {
    series: {
        bars: {
            show: true,
            barWidth: 0.4,
            lineWidth: 1,
            align: 'center',
            fillColor: {
                colors: [{
                    opacity: 0.5
                }, {
                    opacity: 0.1
                }]
            }
        }
    },
    xaxis: {
       mode: 'categories',
       axisLabel: 'Machines',
       axisLabelUseCanvas: true,
       axisLabelFontSizePixels: 14,
       axisLabelFontFamily: 'RobotoLight, Helvetica Neue, Helvetica',
       axisLabelPadding: 10
    },
    yaxis: {
       axisLabel: metricName,
       axisLabelUseCanvas: true,
       axisLabelFontSizePixels: 14,
       axisLabelFontFamily: 'RobotoLight, Helvetica Neue, Helvetica',
       axisLabelPadding: 10
    },
    grid: {
       hoverable: true,
       clickable: false,
       borderWidth: 1
    }
}
like image 647
pmichna Avatar asked Feb 12 '23 19:02

pmichna


1 Answers

Just set an intelligent min/max on the xAxis options. For instance, on a plot with two bars:

min: -0.5,
max: 1.5,

For each additional bar increase the max by one.

Here's a fiddle.

enter image description here

like image 148
Mark Avatar answered Feb 15 '23 10:02

Mark