Search code examples
javascriptjquerychartsjqplotdonut-chart

Add Legend to Donut Chart - jqPlot


OK, this should be relatively simple :

enter image description here

  • I'm adding a donut chart, which does work
  • However, the 'legend' (like Head (+) along with the corresponding colour) does NOT show up.

Code :

$(document).ready(function(){
  var s1 = [['Head (+)',<?php echo $headScore; ?>], ['Head (-)',<?php echo 6-$headScore; ?>]];
  var s2 = [['Body (+)',<?php echo $totalScore-$headScore; ?>], ['Body (-)',<?php echo 7-$totalScore+$headScore; ?>]];

  var plot3 = $.jqplot('linkchart', [s1,s2], {
      title:"Score Profile",
    seriesDefaults: {
      // make this a donut chart.
      renderer:$.jqplot.DonutRenderer,
      rendererOptions:{
        // Donut's can be cut into slices like pies.
        sliceMargin: 3,
        // Pies and donuts can start at any arbitrary angle.
        startAngle: -90,
        showDataLabels: false
      },
      legend: { show:true, location: 'e' }
    }
  });
});

What am I doing wrong?


Solution

  • Kameleon,

    It looks like you have done a silly mistake. : )

    First end the seriesDefaults property and then define the legend.

    You have placed the legend inside the seriesDefaults.

    var plot3 = $.jqplot('linkchart', [s1,s2], {
        title:"Score Profile",
            seriesDefaults: {
                // make this a donut chart.
                renderer:$.jqplot.DonutRenderer,
                rendererOptions:{
                    // Donut's can be cut into slices like pies.
                    sliceMargin: 3,
                    // Pies and donuts can start at any arbitrary angle.
                    startAngle: -90,
                    showDataLabels: false
                } // Not here...
            },
            //Place the legend here....
            legend: { show:true, location: 'e' }
        });
    });
    

    I have not tested it. But I think it should work.

    Thank you.