Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] accessing legend unselected series in tooltip formatter function #20731

Open
revintec opened this issue Feb 3, 2025 · 4 comments
Open
Labels
en This issue is in English new-feature topic: legend

Comments

@revintec
Copy link

revintec commented Feb 3, 2025

What problem does this feature solve?

https://echarts.apache.org/examples/en/editor.html?c=line-simple&code=PYBwLglsB2AEC8sDeAoWsAeBBDEDOAXMmurGAJ4gCmRA5AMYCGYVA5sAE7m0A0J6AE2aMiAbVoBZGL1i0AKgFcqM2gHUqAlXIAWClQDEOEFQGVmphdFoBdEgF8-6cjnxFUpMpRqyAbowA2SrT2jrD-bFTQAgTupABGwGBgwAC2BABEAEwApOmh6HhU4fQs0cQe6OkAjOlEAGYBhfywdiEkycD-kCAxzWBGrKxUHBmMuHh5zXWcKcwsIwAUjBwcAJTwAHyxHgJUcQqDw83o9DB4nVQAdP7ArEsrq82t6A4khUZUhLCizdukQmARN8qgBWAAMPFgmQAzBCoZkACyQzJVAAckKq0JBGIRAHZkQA2MHWfKkCjUOj-CDQZSQ6CMFLearpJ6kv6CYRiGFwzKI5FojFYnH4qFEknHTwU2RUmkyemMohZFkeZ6wWx2ADcQA

Image

in the tooltip formatter callback, arr contains only legend selected series, making it almost impossible to access unselected ones. I wanna achieve something like this, notice the series unselected by legend are still shown in the tooltip

Image

accessing the unselected data through getOption is not viable here, as one can unselect all series, making it impossible to get data index through arr[0].dataIndex. and different series may have different data layout, we can't simply reuse dataIndex


I did a lot of code reading but can't find where the dataByCoordSys is actually populated(checked filterSeries and related code but no luck

  • can you point me the code path?
Image

What does the proposed API look like?

modify the formatter callback signature like this, it should be mostly backward compatible
if concerned, we could add toString and toJSON to the new object, or just append a new parameter instead

Image

or we we could do the following, it works according to ECMA standards although it may look unconventional

Image
@echarts-bot echarts-bot bot added en This issue is in English pending We are not sure about whether this is a bug/new feature. labels Feb 3, 2025
@helgasoft
Copy link

helgasoft commented Feb 4, 2025

one possible solution
And here is the answer from DeepSeek. Hmm, better than mine.

  tooltip: {
    trigger: "axis",
    formatter: (arr) => {
      const dataIndex = arr[0].dataIndex; // Current x-axis index
      const xCategory = option.xAxis.data[dataIndex]; // x-axis label (e.g., 'Mon')
      let content = `${xCategory}<br/>`; // Start building the tooltip content
  
      // Loop through all series in the option
      option.series.forEach((series) => {
        const value = series.data[dataIndex] ?? 'N/A'; // Use 'N/A' if data is missing
        content += `${series.name}: ${value}<br/>`; // Add series name and value
      });
  
      return content;
    }
  }

@Ovilia Ovilia added topic: legend and removed pending We are not sure about whether this is a bug/new feature. labels Feb 5, 2025
@MatthiasMert
Copy link

MatthiasMert commented Feb 5, 2025

Here is another possible workaround which takes advantage of echarts standard formatters by only modifying the visuals of legend items and series. The basic idea is to keep track of the state of selected series yourself and set the echarts legends items always to selected state after a change event. Based on your own state, series color is set to transparent and legend color to grey.

@revintec
Copy link
Author

revintec commented Feb 8, 2025

one possible solution And here is the answer from DeepSeek. Hmm, better than mine.

  tooltip: {
    trigger: "axis",
    formatter: (arr) => {
      const dataIndex = arr[0].dataIndex; // Current x-axis index
      const xCategory = option.xAxis.data[dataIndex]; // x-axis label (e.g., 'Mon')
      let content = `${xCategory}<br/>`; // Start building the tooltip content
  
      // Loop through all series in the option
      option.series.forEach((series) => {
        const value = series.data[dataIndex] ?? 'N/A'; // Use 'N/A' if data is missing
        content += `${series.name}: ${value}<br/>`; // Add series name and value
      });
  
      return content;
    }
  }

thanks for the reply. I did come up with that solution, but it has some drawbacks(mentioned in the original post

Image

@revintec
Copy link
Author

revintec commented Feb 8, 2025

Here is another possible workaround which takes advantage of echarts standard formatters by only modifying the visuals of legend items and series. The basic idea is to keep track of the state of selected series yourself and set the echarts legends items always to selected state after a change event. Based on your own state, series color is set to transparent and legend color to grey.

thanks for that, this is a great workaround, though hacky, it solves all the problems, and no data layout issues
still, hope echarts will someday have these features builtin, so we could write less code :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
en This issue is in English new-feature topic: legend
Projects
None yet
Development

No branches or pull requests

4 participants