程序员

Hexo驱动博客中使用Echarts动态图表

2016-04-08  本文已影响873人  Airing

Echarts介绍

今后要经常做数据分析,总不能一直拿静态图片来充当图表吧。于是想看看有没有什么好的工具。

原来自己是一直在用D3制作交互式图标,流程复杂,效果简单。现在发现了Echarts,感觉发现了新大陆一样。

具体教程去百度的官网(http://echarts.baidu.com/)查看文档,很简单的,这里就不多说了。

echarts官网

下面来简单说一下如何在Hexo驱动的博客中使用echarts图表。

在博客页面中引用js文件

在所用主题目录下layout\_partial中的head.ejs里加入:

<script src="http://echarts.baidu.com/dist/echarts.common.min.js"></script>

安装hexo-tag-echarts插件

在博客站点目录下执行npm install hexo-tag-echarts --save

使用范例

对于动态百度图表范例,将其提供的option部分复制,形成下述代码即可。

{% echarts 400 '81%' %}
{
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器,坐标轴触发有效
            type : 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
        }
    },
    legend: {
        data:['利润', '支出', '收入']
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis : [
        {
            type : 'value'
        }
    ],
    yAxis : [
        {
            type : 'category',
            axisTick : {show: false},
            data : ['周一','周二','周三','周四','周五','周六','周日']
        }
    ],
    series : [
        {
            name:'利润',
            type:'bar',
            itemStyle : {
                normal: {
                    label: {show: true, position: 'inside'}
                }
            },
            data:[200, 170, 240, 244, 200, 220, 210]
        },
        {
            name:'收入',
            type:'bar',
            stack: '总量',
            itemStyle: {
                normal: {
                    label : {show: true}
                }
            },
            data:[320, 302, 341, 374, 390, 450, 420]
        },
        {
            name:'支出',
            type:'bar',
            stack: '总量',
            itemStyle: {normal: {
                label : {show: true, position: 'left'}
            }},
            data:[-120, -132, -101, -134, -190, -230, -210]
        }
    ]
};
{% endecharts %}

其中,参数400指定图表展示的高度为400px,81%则指定图表展示的宽度为81%,如不写明这两项参数则默认值为高度400px,宽度81%。

最终制作的图表效果可以参考我的博客:Hexo驱动博客中使用Echarts动态图表|Airing的博客

参考资料:

上一篇 下一篇

猜你喜欢

热点阅读