apache / echarts

Apache ECharts is a powerful, interactive charting and data visualization library for browser
https://echarts.apache.org
Apache License 2.0
60.48k stars 19.62k forks source link

Legend Values Must be Series Names #9518

Closed jfarrin1 closed 5 years ago

jfarrin1 commented 5 years ago

General Questions

PLEASE MAKE SURE OF ALL THE FOLLOWING OPTIONS IN REQUIRED FIELDS ARE TICKED (with x)! Otherwise, the issue will not be answered. And think before you tick. :) 请确保以下每项都打上勾了!并且确保都做了这些事哦~ 这将大量简化我们的工作流程,使你的问题更快速得到解答。

In this issue, I have provided information with: 在这个 issue 中我提供了以下信息:

Issue Type

Issue Details

I would like to define a custom legend, but setOptions.legend.data can only contain series.name values (most charts) or series.data[i].name (pie/funnel charts).

Expected Behavior

I have a multi-series graph where I want a legend that includes all data.name values from each series. In the demo example, I want a legend that shows: Germany, England, and France with the corresponding color.

image

Current Behavior

Currently, the legend values of a non-pie like chart can only come from the series.name and cannot be set to the series.data.name values. If the values in legend.data do not mach a series.name value, then no legend is displayed (as shown in demo).

I assume this is an issue because of how the legend interacts with the series - like how clicking on a legend name can toggle displaying of that series.

Online Example

THIS IS REQUIRED FOR ALL BUG REPORTS AND QUESTIONS!!! 如果是提问或报 bug 一定要提供这一项!

http://gallery.echartsjs.com/editor.html?c=xLFLb0Uw7O&v=3

option = {
    title: {
        text: 'Top 2 Countries by Category (fake data)'
    },
    legend: {
        data: ['Germany', 'England', 'France']
    },
    color: ['green', 'pink', 'blue'],
    xAxis: {
        data: ['Healthcare', 'Crime', 'GDP']
    },
    yAxis: {},
    series: [{
        name: "Countries1",
        type: 'bar',
        label:{
          show: true,
          formatter: '{b}'
        },
        data:[{value: 220, name: 'Germany', itemStyle:{color: 'green'}}, {value: 258, name: 'England', itemStyle:{color: 'pink'}}, {value: 211, name: 'Germany', itemStyle:{color: 'green'}}]
    },
    {
        name: "Countries2",
        type: 'bar',
        label:{
          show: true,
          formatter: '{b}'
        },
        data:[{value: 120, name: 'England', itemStyle:{color: 'pink'}}, {value: 158, name: 'France', itemStyle:{color: 'blue'}}, {value: 111, name: 'France', itemStyle:{color: 'blue'}}]
    }]
};

Topics

Anything Else We Need to Know

N/A

Environment

pissang commented 5 years ago

You can try legend.formatter to display any text you want. But the data in legend must be same with series.name so they can be connected properly

jfarrin1 commented 5 years ago

You can try legend.formatter to display any text you want. But the data in legend must be same with series.name so they can be connected properly

First, thank you for the quick response! Much appreciated :).

Using the formatter still only passes the series.name value, there is no way to access the data[i].name values. There must also only be 1 legend label per series - of course that makes sense for the default functionality, but then there is no way to have more labels than series. I don't need the legend labels to be connected toggle a series on/off or have any functionality on click. I just need a custom legend that can be whatever array of names I want. As I understand, this is not possible at the moment.

Ovilia commented 5 years ago

You may add a series that doesn't contain data, but only to show legend.

atealxt commented 3 years ago

You may add a series that doesn't contain data, but only to show legend.

This is interesting and tricky, should mention in docs or support natively.