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

How to get the color/color code of the legend icon which is assigned to charts.

Viewing 3 posts - 1 through 3 (of 3 total)
  • #34562

    Want to know if we can get the color of the legend icon which is assigned to that part in the graph or chart.

    specially in pie charts.

    We haven’t set any color to the chart in data points. it is generating from the default color set.

    please help

    thank you
    manoj

    • This topic was modified 3 years, 5 months ago by koro.manoj. Reason: spelling mistake
    #34564

    Here is the jsfiddle links to the issue.

    //want to get the legend color value/code of PlayStation 3. i.e BLUE

    console.log(e.dataPoint);
    inside indexLabelFormatter

    thank you
    manoj kumar

    #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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.