Echarts 图例未出现使用数据集和维度

Echarts Legend Not Appearing Using Dataset & Dimensions

我正在使用 Echarts 库。出于某种原因,图例没有出现在这个使用数据集和维度的简单图表中。

卡了好久,试了各种例子都没用。请帮忙!

JS fiddle: https://jsfiddle.net/jcgqfns8/

// based on prepared DOM, initialize echarts instance
        var myChart = echarts.init(document.getElementById('main'));

        // specify chart configuration item and data
        var option = {
            dataset: [{
                source: [
                    ['Test1', 10],
                    ['Test2', 20]
                ],
                dimensions: ['Category', 'Value']
            },
            {
                source: [
                    ['Test1', 15],
                    ['Test2', 25]
                ],
                dimensions: ['Category', 'Value2']
            }],
            xAxis: [{ type: 'category' }, { type: 'category' }],
            yAxis: [{ position: 'left' }, { position: 'right' }],
            series: [
                {
                    type: 'line',
                    encode: {
                        x: 'Category',
                        y: 'Value'
                    },
                    datasetIndex: 0,
                    yAxisIndex: 0
                },
                {
                    type: 'line',
                    encode: {
                        x: 'Category',
                        y: 'Value2'
                    },
                    datasetIndex: 1,
                    yAxisIndex: 1
                }
            ],
            color: ["#c22e34", "#e7b603", "#0097da", "#2b821e", "#035daa", "#339ca8"]
        };

        // use configuration item and data specified to show chart
        myChart.setOption(option);
<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0/echarts.min.js"></script>
</head>
<body>
    <!-- prepare a DOM container with width and height -->
    <div id="main" style="width: 600px;height:400px;"></div>
</body>
</html>

您需要为选项对象添加图例。并为系列对象命名。

 var option = {
        dataset: [{
            source: [
                ['Test1', 10],
                ['Test2', 20]
            ],
            dimensions: ['Category', 'Value']
        },
        {
            source: [
                ['Test1', 15],
                ['Test2', 25]
            ],
            dimensions: ['Category', 'Value2']
        }],
        legend: {}, //need this to show legend
        xAxis: [{ type: 'category' }, { type: 'category' }],
        yAxis: [{ position: 'left' }, { position: 'right' }],
        series: [
            {
                    name: 'legend1', //give a name to series
                type: 'line',
                encode: {
                    x: 'Category',
                    y: 'Value'
                },
                datasetIndex: 0,
                yAxisIndex: 0
            },
            {
                type: 'line',
                name: 'legend2',
                encode: {
                    x: 'Category',
                    y: 'Value2'
                },
                datasetIndex: 1,
                yAxisIndex: 1
            }
        ],
        color: ["#c22e34", "#e7b603", "#0097da", "#2b821e", "#035daa", "#339ca8"]
    };

jsfiddle