echarts 图表引入报错怎么回事

122 阅读1分钟

1 下载 npm install echarts --save

官方推荐使用 import * as echarts from 'echarts';引入

如果报错请使用这种引入

const echarts = require('echarts')

2 在html里定义图表的宽高,这不是常规勿6吗

如下

    <div id="container" style="width: 800px;height:500px;"></div>

3在mounted中写图表逻辑,打开点击去eCharts官网找寻自己需要的图表

如下我复制好的图表

<script>
// import * as echarts from 'echarts';
const echarts = require('echarts')

export default { 
    mounted() {
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('container'));
        // 绘制图表
        myChart.setOption({
            title: {
                text: '学生分数统计图'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {
                data: ['李士乐', '黄金鹏', '王圳森', '刘燕飞', '韩洋']
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: false,
                    data: ['第一次', '第二次', '第三次', '第四次', '第五次', '第六次', '第七次']
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                name: '李士乐',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [120, 132, 101, 134, 90, 230, 210]
                },
                {
                name: '黄金鹏',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [220, 182, 191, 234, 290, 330, 310]
                },
                {
                name: '王圳森',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [150, 232, 201, 154, 190, 330, 410]
                },
                {
                name: '刘燕飞',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [320, 332, 301, 334, 390, 330, 320]
                },
                {
                    name: '韩洋',
                    type: 'line',
                    stack: 'Total',
                    label: {
                        show: true,
                        position: 'top'
                    },
                    areaStyle: {},
                    emphasis: {
                        focus: 'series'
                    },
                    data: [20, 32, 1, 34, 90, 100, 120]
                }
            ]
        });
    }
}
</script>

整体源码如下

<template>
    <div>Welcome</div>
    <div id="container" style="width: 800px;height:500px;"></div>
</template>
<script>
// import * as echarts from 'echarts';
const echarts = require('echarts')

export default { 
    mounted() {
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById('container'));
        // 绘制图表
        myChart.setOption({
            title: {
                text: '学生分数统计图'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {
                data: ['李士乐', '黄金鹏', '王圳森', '刘燕飞', '韩洋']
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: false,
                    data: ['第一次', '第二次', '第三次', '第四次', '第五次', '第六次', '第七次']
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                name: '李士乐',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [120, 132, 101, 134, 90, 230, 210]
                },
                {
                name: '黄金鹏',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [220, 182, 191, 234, 290, 330, 310]
                },
                {
                name: '王圳森',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [150, 232, 201, 154, 190, 330, 410]
                },
                {
                name: '刘燕飞',
                type: 'line',
                stack: 'Total',
                areaStyle: {},
                emphasis: {
                    focus: 'series'
                },
                data: [320, 332, 301, 334, 390, 330, 320]
                },
                {
                    name: '韩洋',
                    type: 'line',
                    stack: 'Total',
                    label: {
                        show: true,
                        position: 'top'
                    },
                    areaStyle: {},
                    emphasis: {
                        focus: 'series'
                    },
                    data: [20, 32, 1, 34, 90, 100, 120]
                }
            ]
        });
    }
}
</script>

效果