How to create a stacked line chart using Apache ECharts using encode


I see this example in Apache ECharts
https://echarts.apache.org/examples/en/editor.html?c=line-stack

import * as echarts from 'echarts';

var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;

option = {
    title: {
        text: '折线图堆叠'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data: ['邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
        type: 'value'
    },
    series: [
        {
            name: '邮件营销',
            type: 'line',
            stack: '总量',
            data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
            name: '联盟广告',
            type: 'line',
            stack: '总量',
            data: [220, 182, 191, 234, 290, 330, 310]
        },
        {
            name: '视频广告',
            type: 'line',
            stack: '总量',
            data: [150, 232, 201, 154, 190, 330, 410]
        },
        {
            name: '直接访问',
            type: 'line',
            stack: '总量',
            data: [320, 332, 301, 334, 390, 330, 320]
        },
        {
            name: '搜索引擎',
            type: 'line',
            stack: '总量',
            data: [820, 932, 901, 934, 1290, 1330, 1320]
        }
    ]
};

option && myChart.setOption(option);

Now that ECharts v5.x has support to specify encode , is it possible to create the above chart using encode instead of specifying data array for each series?
What i am looking for is something on these lines

option = {
    title: {
        text: 'Some Title'
    },
    tooltip: {
        trigger: 'axis'
    },
    dataset:{
        source:[
            ['XAxis_Column_Name','Series_Column_Name','YAxis_Column_Name'],
            ['x1','s1',10],
            ['x2','s2',20],
            ['x3','s1',40],
            ['x4','s1',70],
            ['x5','s2',90],
            ['x5','s1',100],
            ['x6','s3',120],
        ]
    },
    
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        encode:{
            x: "XAxis_Column_Name"
        }
    },
    yAxis: {
        type: 'value',
        encode:{
            x: "YAxis_Column_Name"
        }
    },
    series: [
         {type: 'line'},
         {type: 'line'},
         {type: 'line'}
    ]
};

The end result should be a line chart with 3 lines (one each for s1, s2 and s3). Any help here is much appreciated. I don’t want to deal with data extraction and segregating them into arrays for each series

Source: JavaSript – Stack Overflow

August 15, 2021
Category : News
Tags: charts | echarts | javascript | linechart

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.