0

I need echart legend like:

enter image description here

I have tried using this code:

legend: {
    icon: "circle",
    left: 10,
    data: ['Very High', 'Hight', 'Moderate', 'Low'],
    orient: 'vertical',
    formatter: '{name} {icon} {value}',
    textStyle: {
        color: "rgba(255, 255, 255, 0.7)",
    }
}

But it shows like this:

enter image description here

Can any tell me how can I place the left icon center of the text?

Code can be tested here

Arif
  • 6,094
  • 4
  • 49
  • 81

1 Answers1

1

By the conventional methods, probably, it is impossible.

The legend formatter support only {name}. If I understand correctly, its role just only to show measure units. To my regret the Legend not the most convenient component for customize but Echarts in general allows you to do almost anything within reason.

Usually you have minimum two ways to get what you want:

  1. We can disable default component and draw the new from scratch with graphic (example) or create own extension by examples. Alas, it's difficult way if you want to spent time for only the single chart.

  2. Another way to stylize visual part of the most suitable component to the desired result. In fact its just imitation by improvised means without implementation. However its fast, easy and it can cover a large number of cases despite some limitations. I sketched a draft that looks like you picture (without design), try to use and customize it.

enter image description here

  1. Also we can go beyond the Echarts as a main tool and use HTML + CSS. In this case Echarts acts only as a source of data and events. This option may be the most convenient because build UI with HTML a bit simple and we can use ready-made templates.
Sergey Fedorov
  • 3,696
  • 2
  • 17
  • 21
  • Thanks a million. It helps me a lot. Can I disable full legend and make it using html/css? If you have any example reference that would be great. – Arif May 20 '20 at 15:31
  • 1
    For disable legend you need to set `legend: { show: false }`. Legend template for example you can found on github (https://github.com/search?l=JavaScript&q=legend&type=Repositories). Also looks on (https://github.com/susielu/d3-legend), it's great plugin for d3.js, you can get knowledge about anatomy of legends. – Sergey Fedorov May 22 '20 at 05:32
  • Can help me to answer this question https://stackoverflow.com/q/62058901/3835843 – Arif May 28 '20 at 09:07
  • 1
    Yes, see answer – Sergey Fedorov May 28 '20 at 10:49