一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第13天,点击查看活动详情。
title: Hexo -23- 使用 ECharts 插件绘制炫酷图表 author: pxxyyz categories: [Hexo, Website] tags: [Hexo, Website] date: 2021-09-12 23:00:00
excerpt: ECharts 是一个使用 JavaScript 实现的开源可视化库,涵盖各行业图表,满足各种需求,本文介绍如何在 Hexo 博客中使用 ECharts 插件。
ECharts使用
- 安装
hexo-tag-echarts插件
$ npm install hexo-tag-echarts --save
-
注意:ECharts官网教程-[5 分钟上手 ECharts]([echarts.apache.org/zh/tutorial…echarts.apache.org/zh/tutorial… 分钟上手 ECharts))里的
npm install echarts --save并不适合hexo博客,这种安装方式无效,请安装hexo-tag-echarts插件。 -
添加如下js文件
// 通过jsDelivr的CDN引入echarts
<script src="https://cdn.jsdelivr.net/npm/echarts@4.8.0/dist/echarts.min.js"></script>
// 使用GL里的各种组件时需要添加,否则可不需要
<script src="https://cdn.jsdelivr.net/npm/echarts-gl@1.1.1/dist/echarts-gl.min.js"></script>
- 在markdown文件下添加echarts,格式如下
<script>
...
</script>
{% echarts 400 '85%' %}
...
{% endecharts %}
<script>中添加定义的变量和函数,若无设定则可删掉<script></script>{% raw %}{% echarts 400 '85%' %}{% endraw %}和{% raw %}{% endecharts %}{% endraw %}之间添加echarts的option。- 参数400指定图表展示的高度为400px,85%则指定图表展示的宽度为85%,如不写明这两项参数则默认值为高度400px,宽度81%。
- title:标题组件,包含主标题和副标题。
- legend:图例组件。
- tooltip:提示框组件。
- toolbox:工具栏。内置有导出图片,数据视图,动态类型切换,数据区域缩放,重置五个工具。
- xAxis、yAxis:直角坐标系 grid 中的 x 轴、y轴。
- series:系列列表。每个系列通过
type决定自己的图表类型。- series-line:折线/面积图
- series-bar:柱状/条形图
- series-pie:饼图
- series-scatter:散点图
- series-radar:雷达图
- series-tree:树图
- series-boxplot:箱形图
- series-candlestick:K线图
- series-heatmap:热力图
- series-graph:关系图
- 多个图表的数据和函数可能会冲突,请注意!
- 直接在html中直接绘制,然后用
<iframe></iframe>展示效果更佳。关于hexo的html文件渲染问题,可以参考Fluid+自定义html,主要是去掉head部分的说明。 - 在html绘图ECharts的格式如下:
<script src="https://cdn.jsdelivr.net/npm/echarts@4.8.0/dist/echarts.min.js"></script>
<!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
<div id="main" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));
// 指定图表的配置项和数据
var option = {
...
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
// 刷新调整
window.onresize = function () {
myChart.resize();
}
</script>
- 部分echart需要引入其他js,如
bmap、jquery等,请自行添加。 - 使用百度地图的api需要申请密钥(ak),使用格式如下,注意替换
FAKE_AK。
<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=FAKE_AK"></script>
<script type="text/javascript" src="https://api.map.baidu.com/getscript?v=2.0&ak=FAKE_AK"></script>
实例
{% note success %}
下面给出一些echarts官方实例,大多数都可以{% label primary @交互%}。
{% endnote %}
折线图Line
{% echarts 400 '85%' %} option = { 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: '总量', areaStyle: {}, data: [120, 132, 101, 134, 90, 230, 210] }, { name: '联盟广告', type: 'line', stack: '总量', areaStyle: {}, data: [220, 182, 191, 234, 290, 330, 310] }, { name: '视频广告', type: 'line', stack: '总量', areaStyle: {}, data: [150, 232, 201, 154, 190, 330, 410] }, { name: '直接访问', type: 'line', stack: '总量', areaStyle: {}, data: [320, 332, 301, 334, 390, 330, 320] }, { name: '搜索引擎', type: 'line', stack: '总量', label: { normal: { show: true, position: 'top' } }, areaStyle: {}, data: [820, 932, 901, 934, 1290, 1330, 1320] } ] }; {% endecharts %}
{% echarts 400 '85%' %} option = { xAxis: { type: 'category', boundaryGap: false }, yAxis: { type: 'value', boundaryGap: [0, '30%'] }, visualMap: { type: 'piecewise', show: false, dimension: 0, seriesIndex: 0, pieces: [{ gt: 1, lt: 3, color: 'rgba(0, 180, 0, 0.5)' }, { gt: 5, lt: 7, color: 'rgba(0, 180, 0, 0.5)' }] }, series: [ { type: 'line', smooth: 0.6, symbol: 'none', lineStyle: { color: 'green', width: 5 }, markLine: { symbol: ['none', 'none'], label: {show: false}, data: [ {xAxis: 1}, {xAxis: 3}, {xAxis: 5}, {xAxis: 7} ] }, areaStyle: {}, data: [ ['2019-10-10', 200], ['2019-10-11', 400], ['2019-10-12', 650], ['2019-10-13', 500], ['2019-10-14', 250], ['2019-10-15', 300], ['2019-10-16', 450], ['2019-10-17', 300], ['2019-10-18', 100] ] } ] }; {% endecharts %}