Search code examples
javascriptchartsamchartsamcharts4

How to change the tool tip and label color on amcharts 4 legend custom description


I am plotting pie charts and on hover of the legend item i want to show the custom description.

The below is my code :

/**
 * ---------------------------------------
 * This demo was created using amCharts 4.
 *
 * For more information visit:
 * https://www.amcharts.com/
 *
 * Documentation is available at:
 * https://www.amcharts.com/docs/v4/
 * ---------------------------------------
 */

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.PieChart);

// Add data
chart.data = [{
  "category": "No Country for Old Men",
  "value": 501.9
}, {
  "category": "All the Pretty Horses",
  "value": 301.9
}, {
  "category": "Blood Meridian or the Evening Redness in the West",
  "value": 201.1
}, {
  "category": "The Orchard Keeper",
  "value": 165.8
}, {
  "category": "The Road",
  "value": 139.9
}];


// Add and configure Series
var pieSeries = chart.series.push(new am4charts.PieSeries());
pieSeries.dataFields.value = "value";
pieSeries.dataFields.category = "category";
pieSeries.ticks.template.disabled = true;
pieSeries.labels.template.disabled = true;

chart.legend = new am4charts.Legend();
chart.legend.position = "right";
chart.legend.labels.template.maxWidth = 120;
chart.legend.labels.template.truncate = true;
chart.legend.itemContainers.template.tooltipText = "{category}";
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

#chartdiv {
  width: 100%;
  height: 300px;
}
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/charts.js"></script>
<div id="chartdiv"></div>

As we can see that on hover the tooltip for custom description appears , and its background color is black and label color is white , I want to change the color of it.


Solution

  • For the color of the text, you can use a formatting string:

    chart.legend.itemContainers.template.tooltipText = "[#00ff00]{category}[/]";
    

    For the background color, I don't know.

    /**
     * ---------------------------------------
     * This demo was created using amCharts 4.
     *
     * For more information visit:
     * https://www.amcharts.com/
     *
     * Documentation is available at:
     * https://www.amcharts.com/docs/v4/
     * ---------------------------------------
     */
    
    // Create chart instance
    var chart = am4core.create("chartdiv", am4charts.PieChart);
    
    // Add data
    chart.data = [{
      "category": "No Country for Old Men",
      "value": 501.9
    }, {
      "category": "All the Pretty Horses",
      "value": 301.9
    }, {
      "category": "Blood Meridian or the Evening Redness in the West",
      "value": 201.1
    }, {
      "category": "The Orchard Keeper",
      "value": 165.8
    }, {
      "category": "The Road",
      "value": 139.9
    }];
    
    
    // Add and configure Series
    var pieSeries = chart.series.push(new am4charts.PieSeries());
    pieSeries.dataFields.value = "value";
    pieSeries.dataFields.category = "category";
    pieSeries.ticks.template.disabled = true;
    pieSeries.labels.template.disabled = true;
    
    chart.legend = new am4charts.Legend();
    chart.legend.position = "right";
    chart.legend.labels.template.maxWidth = 120;
    chart.legend.labels.template.truncate = true;
    chart.legend.itemContainers.template.tooltipText = "[#00ff00]{category}[/]";
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
    }
    
    #chartdiv {
      width: 100%;
      height: 300px;
    }
    <script src="https://www.amcharts.com/lib/4/core.js"></script>
    <script src="https://www.amcharts.com/lib/4/charts.js"></script>
    <div id="chartdiv"></div>