Commits

randyv2zhurosoft  committed 5f594b2

Add hacky support for displaying value, percent, and label as pie chart labels

  • Participants
  • Parent commits b5d9ab6

Comments (0)

Files changed (1)

File src/plugins/jqplot.pieRenderer.js

         this.highlightColors = [];
         // prop: dataLabels
         // Either 'label', 'value', 'percent' or an array of labels to place on the pie slices.
+        // If the array of labels consists *only* of 'label', 'value', and/or 'percent' then each of these will be displayed,
+        // e.g. this.dataLabels = ['label', 'value'] <-- shows both the label and the value of the slice.
+        // e.g. this.dataLabels = ['label', 'something_else'] <-- placed directly on the pie slices.
         // Defaults to percentage of each pie slice.
         this.dataLabels = 'percent';
         // prop: showDataLabels
             }
             
         }
+
+        // Check for a special array of 'label', 'value', and/or 'percent'
+        var specialArray = true;
+        if (this.dataLabels.constructor == Array) {
+          for (i = 0; i < this.dataLabels.length; i++) {
+            var label = this.dataLabels[i];
+            if (label != 'value' && label != 'percent' && label != 'label') {
+              specialArray = false;
+            }
+          }
+        }
+
         for (var i=0; i<gd.length; i++) {
             var ang1 = (i == 0) ? sa : gd[i-1][1] + sa;
             // Adjust ang1 and ang2 for sliceMargin
                     label = $.jqplot.sprintf(fstr, gd[i][2]*100);
                 }
                 else if (this.dataLabels.constructor == Array) {
+                  if (specialArray) {
+                    labels = []
+                    for (j = 0; j < this.dataLabels.length; j++) {
+                      if (this.dataLabels[j] == 'label') {
+                        fstr = this.dataLabelFormatString || '%s';
+                        labels.push($.jqplot.sprintf(fstr, gd[i][0]));
+                      }
+                      else if (this.dataLabels[j] == 'value') {
+                        fstr = this.dataLabelFormatString || '%d';
+                        labels.push($.jqplot.sprintf(fstr, this.data[i][1]));
+                      }
+                      else if (this.dataLabels[j] == 'percent') {
+                        fstr = this.dataLabelFormatString || '%d%%';
+                        labels.push($.jqplot.sprintf(fstr, gd[i][2]*100));
+                      }
+                    }
+                    label = labels.join(" - ");
+                  } else {
                     fstr = this.dataLabelFormatString || '%s';
                     label = $.jqplot.sprintf(fstr, this.dataLabels[i]);
+                  }
                 }
             
                 var fact = (this._radius ) * this.dataLabelPositionFactor + this.sliceMargin + this.dataLabelNudge;
     
 })(jQuery);
     
-    
+