PineCone
PineCone

Reputation: 2343

How to get a simple line for average on a bar chart in jqPlot

I am working with a bar chart in jqPlot where I need to show the average with a separate line on the chart. My question is how to do that? Would I need to use a Line chart for this? I have looked at few Line chart examples but they are shown as a trend in the bar chart which starts from the very first bar on the chart rather than showing the average.What I need is plotting a line using the average of all those bars displayed on the chart (screen shots as below)

enter image description here

My JSON string to plot the data is as follows:

        var commonOption= {    
            title: '' 
            ,stackSeries: true    
            ,captureRightClick: true    
            ,seriesDefaults:{      
                renderer:$.jqplot.BarRenderer      
                ,rendererOptions: {          
                    barMargin: 15          
                    ,highlightMouseDown: true   
                    ,fillToZero: true               
                },      
                pointLabels: {
                    show: true
                    ,formatString: '%.1f'
                    ,seriesLabelIndex:1
                    ,hideZeros:false
                }
            }   
            ,seriesColors: ['#A9CB5E']
            ,axes: {      
                xaxis: {          
                    tickOptions:{angle:-45}
                    ,tickRenderer: $.jqplot.CanvasAxisTickRenderer
                    ,renderer: $.jqplot.CategoryAxisRenderer
                    ,ticks: []
                },      
                yaxis: {   
                    labelRenderer: $.jqplot.CanvasAxisLabelRenderer
                    ,padMin: 0
                    ,pad: 1.1
                    , label: 'Percentage (%)'
                    ,rendererOptions: { forceTickAt0: true}
                    //,min: 0
                    //,tickOptions:{formatString: '%.0f'},
                } 
            }    
            ,negativeSeriesColors:['#F08080']   
            /*,legend: {
                show: true 
                ,location: 'e'
                ,placement: 'outsideGrid'
            }*/
            ,highlighter:{
                show: true
                ,tooltipLocation: 's'
                ,yvalues: 2
                ,bringSeriesToFront:true
                ,showMarker:false
                ,tooltipAxes: 'y'
                ,formatString: "%n%s"
            }
            ,cursor:{
                show: true
                ,zoom:true
                ,showTooltip:false
                ,constrainZoomTo: 'y'
            }
            ,grid:{
                background: '#f8f8f8'
            }               
        };  

Upvotes: 2

Views: 2210

Answers (2)

meccanismo.complesso
meccanismo.complesso

Reputation: 381

Hi I think it is better to implement a function that automatically calculates the average of the data points into the array. In fact the average of your bar char is about 18 and not 20!! I suggest to implement a function for doing thi. See this jsFiddle example. There is an article where it is shown how to draw and calculate the statistics for a bar chart: average, median, mode and standard deviation at this link: http://www.meccanismocomplesso.org/en/mean-mode-median-barchart/ .

Array.prototype.average=function(){
    var sum=0;
    var j=0;
    for(var i=0;i<this.length;i++){
        if(isFinite(this[i])){
          sum=sum+parseFloat(this[i]);
           j++;
        }
    }
    if(j===0){
        return 0;
    }else{
        return sum/j;
    }
}

...

canvasOverlay: {
                    show: true,
                    objects: [
                        {dashedHorizontalLine: {
                            name: 'average',
                            y: data.average(),
                            lineWidth: 3,
                            color: 'black',
                            shadow: false
                        }}                  
                   ]             
              }

Upvotes: 2

Ovi Faur
Ovi Faur

Reputation: 528

I believe what you are looking for is the jqplot CanvasOverlay functionality http://www.jqplot.com/deploy/dist/examples/canvas-overlay.html After declaring all of you options and data in the function (in your example after "grid" option):

grid:{
    background: '#f8f8f8'
},
canvasOverlay: {
                    show: true,
                    objects: [
                        {horizontalLine: {
                            name: 'avergae',
                            y: 20.8, //**AVERAGE_FLOAT_VALUE**
                            lineWidth: 2,
                            color: 'black',
                            shadow: false
                        }}
                    ]
                }

EDIT: Yes, sorry about that. be sure not to forget to include "jqplot.canvasOverlay.min.js "

Upvotes: 4

Related Questions