最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Echart configuration for Pie legends in a particular way - Stack Overflow

programmeradmin0浏览0评论

I want to display pie legends along with its value in a different format. I am attaching the image. I am looking for it but did not find how to do it till now. If you see the image, legends and the value corresponding to it are shown that i am unable to replicate.

I want to display pie legends along with its value in a different format. I am attaching the image. I am looking for it but did not find how to do it till now. If you see the image, legends and the value corresponding to it are shown that i am unable to replicate.

Share Improve this question asked Apr 23, 2020 at 11:35 user12893845user12893845 1764 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You want to display value instead percentages? If yes then this is a little work that need to do because by default legend ponent don't know about another dimensions of you data. Try to start with this code:

var option = {
  //...
  legend: {
    formatter: name => {
      var series = myChart.getOption().series[0];
      var value = series.data.filter(row => row.name === name)[0].value
      return name + '    ' + value;
    },
  }
}

  var myChart = echarts.init(document.getElementById('main'));
  var option = {
    tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
        orient: 'vertical',
        left: 10,
        data: ['Category1', 'Category2', 'Category3', 'Category4', 'Category5'],
        formatter: (name) => {
	        var series = myChart.getOption().series[0];
      		var value = series.data.filter(row => row.name === name)[0].value;
      		return name + '    ' + value;
        },
    },
    series: [
        {
            name: 'Series',
            type: 'pie',
            label: { show: false },
            labelLine: { show: false },
            data: [
                {value: 335,  name: 'Category1'},
                {value: 310,  name: 'Category2'},
                {value: 234,  name: 'Category3'},
                {value: 135,  name: 'Category4'},
                {value: 1548, name: 'Category5'}
            ]
        }
    ]
};


myChart.setOption(option);
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/echarts.min.js"></script>
<div id="main" style="width: 600px;height:400px;"></div>

发布评论

评论列表(0)

  1. 暂无评论