Tran Tram
Tran Tram

Reputation: 121

Highcharts - How to display legend symbol inside the tooltip

I have a problem with Highcharts that I would like to ask.

I have a Highcharts, and I want to copy the preview symbol from legends to the tooltip.

I am doing this in 2 different case:

Extra information:

Thanks in advance

Upvotes: 10

Views: 3618

Answers (2)

Aravinda Meewalaarachchi
Aravinda Meewalaarachchi

Reputation: 2639

This goal can be achieve by using pointFormat or pointFormatter. There are couple of example using pointFormatter, So i will use pointFormat attribute to achievethis goal.With this approch you dont need to enable useHTML property.

  tooltip: {
    pointFormat:
      '<svg width="10" height="10" style="color:{series.color}">●</svg> 
       <b>{series.name}: {point.percentage:.0f}%</b>
       <br/>',
    shared: true
  },

Highcharts.chart("container", {
  chart: {
    type: "column"
  },
  title: {
    text: null
  },
  credits: {
    enabled: false
  },
  xAxis: {
    categories: ["Apple", "Orange", "Grapes", "Mango", "Pinapple", "Papaya"],
    title: "Fruits"
  },
  yAxis: {
    min: 0,
    floor: 0,
    ceiling: 100,
    title: {
      text: null
    },
    labels: {
      align: "right",
      x: 0,
      y: -2
    }
  },
  tooltip: {
    pointFormat:
      '<svg width="10" height="10" style="color:{series.color}">●</svg> <b>{series.name}: {point.percentage:.0f}%</b><br/>',
    shared: true
  },
  plotOptions: {
    series: {
      stacking: "normal"
    }
  },
  series: [
    {
      name: "Small",
      data: [5, 3, 4, 7, 2, 3],
      color: "#A2CD32"
    },
    {
      name: "Large",
      data: [2, 2, 3, 2, 1, 2],
      color: "#FFF500"
    },
    {
      name: "Medium",
      data: [3, 4, 4, 2, 5, 2],
      color: "#FF220"
    }
  ]
});
<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>

Upvotes: 0

Kamil Kulig
Kamil Kulig

Reputation: 5826

Here's how to display the the SVG from the legend item in the tooltip (works for columns and pattern fill):

  tooltip: {
    useHTML: true,
    pointFormatter: function() {
      var point = this,
        series = point.series,
        legendSymbol = "<svg width='20' height='20'>" + series.legendSymbol.element.outerHTML + "</svg>";

      return legendSymbol + series.name + ": <b>" + point.y + "</b><br/>";
    }
  }

useHTML must be enabled to make it work.

Live demo: https://jsfiddle.net/kkulig/adr9otbg/

Upvotes: 5

Related Questions