掌握echarts,轻松绘制图表:从入门到精通,实战案例教学,让你的数据可视化更专业

2026-11-08 0 阅读

ECharts 是一个使用 JavaScript 实现的开源可视化库,可以轻松地在网页中绘制各种图表。它提供了丰富的图表类型,如折线图、柱状图、饼图、散点图、地图等,能够满足各种数据可视化的需求。本文将从入门到精通,结合实战案例,教你如何使用 ECharts 绘制图表,让你的数据可视化更加专业。

入门篇:ECharts 基础知识

1. ECharts 简介

ECharts 是一个基于 JavaScript 的开源可视化库,它提供了丰富的图表类型和强大的配置项,可以满足各种数据可视化的需求。ECharts 支持多种浏览器,包括 Chrome、Firefox、Safari、Edge 等,且具有良好的兼容性。

2. ECharts 安装与配置

2.1 安装

ECharts 支持多种安装方式,包括直接下载、CDN 链接、npm 包管理等。以下是一个简单的 CDN 链接配置示例:

<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.3.3/echarts.min.js"></script>

2.2 配置

在 ECharts 中,图表的配置主要通过 echarts.init() 方法初始化图表实例,并传入一个配置对象来实现。以下是一个简单的配置示例:

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

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);

进阶篇:ECharts 图表类型

1. 折线图

折线图是一种用于表示数据随时间变化的图表类型。以下是一个简单的折线图示例:

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

var option = {
    title: {
        text: '折线图示例'
    },
    tooltip: {},
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line'
    }]
};

myChart.setOption(option);

2. 柱状图

柱状图是一种用于表示不同类别数据之间比较的图表类型。以下是一个简单的柱状图示例:

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

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

myChart.setOption(option);

3. 饼图

饼图是一种用于表示各部分占整体比例的图表类型。以下是一个简单的饼图示例:

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);

4. 散点图

散点图是一种用于表示两个变量之间关系的图表类型。以下是一个简单的散点图示例:

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

var option = {
    title: {
        text: '散点图示例'
    },
    tooltip: {
        trigger: 'axis'
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        data: [
            [10.0, 8.04],
            [8.09, 6.95],
            [13.0, 7.58],
            [9.05, 8.81],
            [11.0, 8.33],
            [14.0, 9.96],
            [6.93, 7.24],
            [4.26, 2.27],
            [12.74, 7.11],
            [7.67, 3.11],
            [5.84, 4.22],
            [13.86, 9.54],
            [8.74, 6.58],
            [5.47, 4.97],
            [11.33, 7.01],
            [4.96, 4.35],
            [7.24, 4.26],
            [4.26, 3.24],
            [10.04, 7.02],
            [3.98, 2.28],
            [7.01, 3.46],
            [5.42, 2.17],
            [10.58, 6.83],
            [8.48, 5.68]
        ],
        type: 'scatter'
    }]
};

myChart.setOption(option);

5. 地图

地图是一种用于表示地理位置信息的图表类型。以下是一个简单的地图示例:

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

var option = {
    title: {
        text: '地图示例'
    },
    tooltip: {
        trigger: 'item'
    },
    series: [{
        name: '销量',
        type: 'map',
        mapType: 'china',
        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 应用案例

1. 项目概况

假设我们有一个电商平台,需要统计不同商品类别的销量情况。以下是一个使用 ECharts 绘制柱状图的示例:

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

var option = {
    title: {
        text: '商品类别销量统计'
    },
    tooltip: {},
    xAxis: {
        type: 'category',
        data: ['手机', '电脑', '家电', '服装', '鞋子', '箱包']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '销量',
        type: 'bar',
        data: [120, 200, 150, 80, 70, 110]
    }]
};

myChart.setOption(option);

2. 数据处理

在实际应用中,我们可能需要从后端获取数据,并对其进行处理后再进行可视化。以下是一个简单的数据处理示例:

// 假设从后端获取的数据如下
var data = {
    "phone": 120,
    "computer": 200,
    "appliance": 150,
    "clothing": 80,
    "shoes": 70,
    "bag": 110
};

// 处理数据
var categories = [];
var values = [];
for (var key in data) {
    categories.push(key);
    values.push(data[key]);
}

// 绘制图表
var myChart = echarts.init(document.getElementById('main'));

var option = {
    title: {
        text: '商品类别销量统计'
    },
    tooltip: {},
    xAxis: {
        type: 'category',
        data: categories
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '销量',
        type: 'bar',
        data: values
    }]
};

myChart.setOption(option);

总结

通过本文的学习,相信你已经掌握了 ECharts 的基本知识和使用方法。在实际应用中,你可以根据需求选择合适的图表类型,并对其进行个性化配置,以实现更专业的数据可视化效果。希望本文能对你有所帮助。

分享到: