9

In a stacked bar chart we can show total of each series in every stack, like this current However I want value of each series to be shown, not total like this(please ignore the fact that the two samples have different number of series) desired Additionally I would like to show the total of the stack at the top. What i mean is that, f you look at the first graph, in the first bar, values are 5,15(5+10),24(15+9). My desired result should be like the second graph, where values for the first bar are like, 10,9 and finally a total at the top 19
Is it possible with this library?

Anupam
  • 7,966
  • 3
  • 41
  • 63

1 Answers1

8

A bit of a hack here. Since you want one more label for each series, I've introduced an "empty" series. It replicates what you want pretty well, though. Fiddle here.

$(document).ready(function(){
  var s1 = [5, 6];
  var s2 = [7, 5];
  var s3 = [14, 9];
  var s4 = [0, 0]; //empty series just for total labels

  var pLabels1 = []; // arrays for each inner label
  var pLabels2 = [];
  var pLabels3 = [];
  var pLabelsTotal = []; // array of totals above each column
  for (var i = 0; i < s1.length; i++){
      pLabels1.push('<div style="border:1px solid gray">'+s1[i]+'</div>');
      pLabels2.push('<div style="border:1px solid gray">'+s2[i]+'</div>');
      pLabels3.push('<div style="border:1px solid gray">'+s3[i]+'</div>');
      pLabelsTotal.push(s1[i]+s2[i]+s3[i]);      
  }   

  plot3 = $.jqplot('chart2', [s1, s2, s3, s4], {
    // Tell the plot to stack the bars.
    stackSeries: true,
    captureRightClick: true,
    seriesDefaults:{
      renderer:$.jqplot.BarRenderer,
      rendererOptions: {
          // Put a 30 pixel margin between bars.
          barMargin: 30,
          // Highlight bars when mouse button pressed.
          // Disables default highlighting on mouse over.
          highlightMouseDown: true   
      },
    },
    series:[
        {
            pointLabels:{
                show:true,
                labels:pLabels1,
                ypadding: -25,
                escapeHTML:false
            }
        },
        {
            pointLabels:{
                show:true,
                labels:pLabels2,
                ypadding: -25,
                escapeHTML:false
            }
        },
                {
            pointLabels:{
                show:true,
                labels:pLabels3,
                ypadding: -25,
                escapeHTML:false
            }
        },
              {
            pointLabels:{
                show:true,
                labels:pLabelsTotal,
                ypadding: 7,
                escapeHTML:false
            }
        }
    ],
    axes: {
      xaxis: {
          renderer: $.jqplot.CategoryAxisRenderer
      },
      yaxis: {
        padMin: 0,
        min: 0
      }
    },
    legend: {
      show: false,
    }      
  });
});

Produces:

enter image description here

Mark
  • 106,305
  • 20
  • 172
  • 230
  • hmm great tnx. For some reason the fiddle is not working, iguess the link to resources are wrong. Tnx anyway – Anupam Jan 29 '12 at 17:42
  • @anu, sorry should have mentioned about the fiddle. jqplot does not allow hotlinking their js files. You have to navigate to the files and cache them in your browser for the fiddle to work. – Mark Jan 29 '12 at 22:13
  • 2
    I made a jsfiddle sample for this code which imports the raw jqPlot scripts, so if interested you can see the code running without a hassle http://jsfiddle.net/Boro/Ymca3/135/ I just had to add a String concatenation to `pLabelsTotal.push(""+(s1[i]+s2[i]+s3[i]));` as for some weird reason it was displaying `.00` after each total. Also a while ago I made a similar solution, though in my sample totals are hard-coded. If interested, you can check it out here http://stackoverflow.com/a/10296988/613495 – Boro May 24 '12 at 10:53
  • @Boro thats very helpful tnx. I couldnt find that post while i was searching on this issue – Anupam May 25 '12 at 03:47
  • @Boro :I am facing problems in updating the pointLables while re plotting. Can you tell me what is wrong [in this script](http://jsfiddle.net/Ymca3/185/). Bars are getting updated properly but not the labels. – Anupam Feb 06 '13 at 08:22
  • @anu I think that your approach didn't work was maybe because the labels are div's or maybe the replot ignores the labels? Anyway you could either completely draw a new plot with new data or after replot use `jQuery` to correct the values yourself as shown here http://jsfiddle.net/Boro/Ymca3/221/ – Boro Feb 06 '13 at 11:58
  • @Boro Hmm I am doing it the way you suggested. Thanks – Anupam Feb 08 '13 at 11:48