Two bars per one x-axis item and second bar must show the percentage like value(percentage) in jqplot chart

StackOverflow https://stackoverflow.com/questions/19633165

Question

I got the bar chart with two bars per one x-axis item. second bar value is always smaller than the first bar value in each x-axis item. I need to show in the second bar label the percentage it is of the first bar value.

example:

600|  550 
500|   H
400|   H 350(63,63%)
300|   H  H
200|   H  H
100|___H__H______________________________ 
         1      2      3      4      5    

the label formatter function(it should do all calculations and return value to the second bar value) :

$.jqplot.LabelFormatter = function(format, val){ return val; };

pointLabels is defined like this:

pointLabels: { show:true, formatString: "%#.3f",  formatter: $.jqplot.LabelFormatter}
Was it helpful?

Solution

Here is the workaround for your problem: JsFiddle link

here is the code which will solve your problem, please make changes to this code or grab the important part and put it in your code.

$(document).ready(function () {
    var s1 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
    var s2 = [5, 15, 15, 30, 45, 60, 35, 49, 75, 95];
    var ticks = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var seriesData = [], seriesIndex = 0;

    function storeSeriesData(){
        seriesData[seriesIndex] = this.data;
        seriesIndex = seriesIndex + 1;
    }

    $.jqplot.LabelFormatter = function(format, val){ 
        var result, pointVal = -1;
        if(seriesIndex > 1){
            for(var i = 0; i < seriesData[seriesIndex - 1].length; i++){
                var tempData = seriesData[seriesIndex - 1][i];
                if(tempData[1] == val){
                    break;
                }
            }
            pointVal = seriesData[seriesIndex - 2][i];
            result = val + "("+ parseFloat(val/pointVal[1] * 100).toFixed(1) + "%)";
        }
        else{
            result = val;
        }
        return result; 
    };

    $.jqplot.preDrawSeriesHooks.push(storeSeriesData);

    var plot1 = $.jqplot('chart1', [s1, s2], {
        seriesDefaults: {
            renderer: $.jqplot.BarRenderer,
            rendererOptions: {
                barPadding: 15,
                barWidth: 25
            },
            pointLabels: { 
                show:true, 
                formatString: "%#.1f",  
                formatter: $.jqplot.LabelFormatter
            }
        },
        grid: {
            drawBorder: true,
            background: '#ffffff'  
        },
        axesDefaults: {
            tickRenderer: $.jqplot.CanvasAxisTickRenderer,
            tickOptions: {
                markSize: 4
            }
        },
        axes: {         
            xaxis: {
                pad: -1.05,
                renderer: $.jqplot.CategoryAxisRenderer,
                ticks: ticks,
                tickOptions: {
                    showGridline: true
                }
            },
            yaxis: {
                pad: 1.05,
                tickOptions: {
                    formatString: '%d',
                    showGridline: true
                }
            }
        }
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top