Home Forums Chart Support How to get the color/color code of the legend icon which is assigned to charts. Reply To: How to get the color/color code of the legend icon which is assigned to charts.

#34580

manoj kumar,

You can get the color of legend by using selectedColorset property of chart and index of dataPoint as shown in the code snippet below

indexLabelFormatter: function (e) {
  var dpIndex = getDataPointIndex(e.dataSeries, e.dataPoint);
  var selectedColorSet = chart.get("selectedColorSet");
  var legendColor = e.dataPoint.legendMarkerColor ? e.dataPoint.legendMarkerColor : (e.dataPoint.color ? e.dataPoint.color : selectedColorSet[dpIndex % (selectedColorSet.length-1)]);
  console.log(legendColor);
}
.
.
.
function getDataPointIndex(dataSeries, dataPoint) {
  return dataSeries.dataPoints.findIndex(dp => dp === dataPoint);
}

Also, please take at this updated JSFiddle for complete working code.

Get legend color in pie chart

—-
Manoj Mohan
Team CanvasJS