Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EChart series with different tooltip

I have the following EChart in my Angular application:

var option = {
  xAxis: {
    type: 'category',
    data: ["A", "B", "C", "D", "E"]
  },
  yAxis: {
    type: 'value',
  },
  tooltip: {
      trigger: 'axis',
         axisPointer: {
           type: 'shadow',
         },
        formatter: (params) => {
          return (
            "Text One" +
            '<br/>' +
        
            params[0].name
          );
        },
      },
  series: [
    {
      data: [1, 2, 3, 4, 5],
      name: 'value',
      stack: 'one',
      type: 'bar',
      tooltip: {
        formatter: (params) => {
          return (
            "Text One" +
            '<br/>' +
        
            params[0].name
          );
        },
      },
    },
    {
      data: [0, 1, 2, 3, 4],
      name: 'prediction',
      stack: 'one',
      type: 'bar',
      tooltip: {
        formatter: (params) => {
          return (
            "Text Two" +
            '<br/>' +
            params[0].name
          );
        },
      },
    },
  ],
};

I basically have two series and my question is how I can format the tooltip for each series specifically.

In my example only "Text 1" is shown from the first series.

like image 459
mrks Avatar asked Jun 07 '26 14:06

mrks


1 Answers

If you use tooltip trigger 'axis', 'params' in the formatter function argument would be an array containing series tooltip information.

If you want to format the first series, you can access params[0]. Similarly if you want to format the second series, you can access params[1].

See example below:

var option = {
  xAxis: {
    type: "category",
    data: ["A", "B", "C", "D", "E"],
  },
  yAxis: {
    type: "value",
  },
  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "shadow",
    },
    formatter: (params) => {
      return `
                Tooltip: <br />
                ${params[0].seriesName}: ${params[0].value}<br />
                ${params[1].seriesName}: ${params[1].value}
                `;
    },
  },
  series: [
    {
      data: [1, 2, 3, 4, 5],
      name: "model",
      stack: "one",
      type: "bar",
    },
    {
      data: [0, 1, 2, 3, 4],
      name: "prediction",
      stack: "one",
      type: "bar",
    },
  ],
};
like image 163
Indra Rudianto Avatar answered Jun 10 '26 03:06

Indra Rudianto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!