使用数据集和维度未显示Echarts图例

时间:2018-09-29 13:58:53

标签: javascript echarts

我正在使用Echarts库。由于某些原因,图例没有使用数据集和维度显示在此简单图表中。

我被困了很长时间,尝试过各种例子,但无济于事。请帮忙!

JS小提琴: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>

1 个答案:

答案 0 :(得分:1)

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

struct.pack_into

jsfiddle