从入门到精通:echarts图表制作详细教程与实战案例解析

2026-10-20 0 阅读

ECharts 是一个使用 JavaScript 实现的开源可视化库,它提供了一系列交互式图表,能够帮助开发者轻松地展示数据。从简单的柱状图、折线图到复杂的地图、仪表盘,ECharts 都能胜任。本文将带你从入门到精通,一步步学习如何使用 ECharts 制作图表,并通过实战案例解析,让你更好地理解和应用。

第一章:ECharts 简介

1.1 ECharts 的特点

  • 丰富的图表类型:支持多种图表类型,如折线图、柱状图、饼图、散点图、地图、雷达图等。
  • 高度可定制:提供丰富的配置项,可以自由调整图表的样式、颜色、大小等。
  • 交互性强:支持鼠标悬停、点击等交互事件,提供丰富的交互效果。
  • 跨平台:支持主流浏览器,包括 Chrome、Firefox、Safari、Edge 等。

1.2 ECharts 的安装

ECharts 是一个纯 JavaScript 库,可以通过以下几种方式安装:

  • CDN 链接:直接通过 CDN 链接引入 ECharts 的 JavaScript 文件。
  • npm 包管理器:使用 npm 安装 ECharts。
  • 下载源码:从 ECharts 的官网下载源码。

第二章:ECharts 基础语法

2.1 初始化 ECharts 实例

在 HTML 页面中,首先需要引入 ECharts 的 JavaScript 文件,然后创建一个用于渲染图表的 DOM 元素,最后通过 echarts.init() 方法初始化 ECharts 实例。

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入柱状图
require('echarts/lib/chart/bar');
// 引入提示框和标题组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');

// 初始化 ECharts 实例
var myChart = echarts.init(document.getElementById('main'));

2.2 配置图表

ECharts 的配置项分为两个部分:option 和 series。option 用于设置图表的全局配置,而 series 用于定义图表中的系列数据。

// 指定图表的配置项和数据
var option = {
    title: {
        text: 'ECharts 入门示例'
    },
    tooltip: {},
    legend: {
        data:['销量']
    },
    xAxis: {
        data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
    },
    yAxis: {},
    series: [{
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

2.3 动态更新图表

在实际应用中,我们可能需要根据用户操作或其他因素动态更新图表。可以通过 setOption() 方法更新图表的配置项和数据。

// 动态更新数据
myChart.setOption({
    series: [{
        data: [15, 30, 45, 20, 20, 30]
    }]
});

第三章:ECharts 实战案例解析

3.1 柱状图

柱状图常用于展示不同类别数据的对比。以下是一个简单的柱状图示例:

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

var option = {
    title: {
        text: '柱状图示例'
    },
    tooltip: {},
    legend: {
        data:['销量']
    },
    xAxis: {
        data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
    },
    yAxis: {},
    series: [{
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
    }]
};

myChart.setOption(option);

3.2 饼图

饼图常用于展示不同类别数据的占比。以下是一个简单的饼图示例:

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

var option = {
    title: {
        text: '饼图示例'
    },
    tooltip: {
        trigger: 'item'
    },
    legend: {
        orient: 'vertical',
        left: 'left',
        data: ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
    },
    series: [{
        name: '销量',
        type: 'pie',
        radius: '50%',
        data: [
            {value: 5, name: '衬衫'},
            {value: 20, name: '羊毛衫'},
            {value: 36, name: '雪纺衫'},
            {value: 10, name: '裤子'},
            {value: 10, name: '高跟鞋'},
            {value: 20, name: '袜子'}
        ],
        emphasis: {
            itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
        }
    }]
};

myChart.setOption(option);

3.3 地图

地图常用于展示地理位置分布的数据。以下是一个简单的中国地图示例:

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

var option = {
    title: {
        text: '中国地图示例'
    },
    tooltip: {
        trigger: 'item'
    },
    visualMap: {
        min: 0,
        max: 100,
        left: 'left',
        top: 'bottom',
        text: ['高','低'],           // 文本,默认为数值文本
        calculable: true
    },
    series: [{
        name: '销量',
        type: 'map',
        mapType: 'china',
        roam: true,
        label: {
            show: false
        },
        data: [
            {name: '北京',value: Math.round(Math.random() * 1000)},
            {name: '天津',value: Math.round(Math.random() * 1000)},
            {name: '上海',value: Math.round(Math.random() * 1000)},
            {name: '重庆',value: Math.round(Math.random() * 1000)},
            {name: '河北',value: Math.round(Math.random() * 1000)},
            {name: '山西',value: Math.round(Math.random() * 1000)},
            {name: '辽宁',value: Math.round(Math.random() * 1000)},
            {name: '吉林',value: Math.round(Math.random() * 1000)},
            {name: '黑龙江',value: Math.round(Math.random() * 1000)},
            {name: '江苏',value: Math.round(Math.random() * 1000)},
            {name: '浙江',value: Math.round(Math.random() * 1000)},
            {name: '安徽',value: Math.round(Math.random() * 1000)},
            {name: '福建',value: Math.round(Math.random() * 1000)},
            {name: '江西',value: Math.round(Math.random() * 1000)},
            {name: '山东',value: Math.round(Math.random() * 1000)},
            {name: '河南',value: Math.round(Math.random() * 1000)},
            {name: '湖北',value: Math.round(Math.random() * 1000)},
            {name: '湖南',value: Math.round(Math.random() * 1000)},
            {name: '广东',value: Math.round(Math.random() * 1000)},
            {name: '海南',value: Math.round(Math.random() * 1000)},
            {name: '四川',value: Math.round(Math.random() * 1000)},
            {name: '贵州',value: Math.round(Math.random() * 1000)},
            {name: '云南',value: Math.round(Math.random() * 1000)},
            {name: '陕西',value: Math.round(Math.random() * 1000)},
            {name: '甘肃',value: Math.round(Math.random() * 1000)},
            {name: '青海',value: Math.round(Math.random() * 1000)},
            {name: '台湾',value: Math.round(Math.random() * 1000)},
            {name: '内蒙古',value: Math.round(Math.random() * 1000)},
            {name: '广西',value: Math.round(Math.random() * 1000)},
            {name: '西藏',value: Math.round(Math.random() * 1000)},
            {name: '宁夏',value: Math.round(Math.random() * 1000)},
            {name: '新疆',value: Math.round(Math.random() * 1000)},
            {name: '香港',value: Math.round(Math.random() * 1000)},
            {name: '澳门',value: Math.round(Math.random() * 1000)}
        ]
    }]
};

myChart.setOption(option);

第四章:ECharts 高级应用

4.1 数据可视化

ECharts 支持多种数据可视化方式,如散点图、雷达图、K线图等。以下是一个散点图示例:

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

var option = {
    title: {
        text: '散点图示例'
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            label: {
                backgroundColor: '#6a7985'
            }
        }
    },
    legend: {
        data:['销量']
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '销量',
        type: 'scatter',
        data: [
            [10, 20],
            [20, 10],
            [30, 30],
            [40, 20],
            [50, 10]
        ],
        markPoint: {
            data: [
                {type: 'max', name: '最大值'},
                {type: 'min', name: '最小值'}
            ]
        },
        markLine: {
            data: [
                {type: 'average', name: '平均值'}
            ]
        }
    }]
};

myChart.setOption(option);

4.2 仪表盘

仪表盘常用于展示实时数据。以下是一个简单的仪表盘示例:

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

var option = {
    title: {
        text: '仪表盘示例'
    },
    tooltip: {
        formatter: '{a} <br/>{b}: {c}%'
    },
    series: [
        {
            name: '业务指标',
            type: 'gauge',
            detail: {valueAnimation: true},
            data: [{value: 50, name: '完成度'}]
        }
    ]
};

myChart.setOption(option);

第五章:总结

通过本文的学习,相信你已经对 ECharts 有了一定的了解。ECharts 是一个功能强大的可视化库,可以帮助你轻松地制作各种图表。在实际应用中,你可以根据自己的需求选择合适的图表类型,并通过配置项进行个性化定制。希望本文能帮助你更好地掌握 ECharts,为你的数据可视化之路提供帮助。

分享到: