Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECharts in React #3

Open
wusb opened this issue Feb 10, 2018 · 0 comments
Open

ECharts in React #3

wusb opened this issue Feb 10, 2018 · 0 comments
Labels

Comments

@wusb
Copy link
Owner

wusb commented Feb 10, 2018

ECharts是众所周知的一个百度出品的数据可视化图形框架,前两周在跟阿里健康合作的项目体重曲线正好有用到,借助ECharts的定制化开发,可高度还原产品及设计要求。

Calendar Preview

跟直接引用ECharts不同的是,我们可以NPM安装好后,按需引入:

1.安装ECharts

yarn add echarts --dev

2.引入需要的模块

import echarts from 'echarts/lib/echarts';
import 'echarts/lib/chart/line';

​ 这个模块只需要绘制曲线图,所以只引入折线图,这样打包的时候可以节省空间。

3.定义好绘制体重曲线的函数

drawWeightTrend(data){
    let xAxisDate = data.map((item)=>{
      return item.record_on;
    });
    let yAxisDate = data.map((item)=>{
      return item.weight;
    });

    xAxisDate = xAxisDate.reverse().slice(-7);
    yAxisDate = yAxisDate.reverse().slice(-7);

    let weightTrend = echarts.init(document.getElementById('weightTrend'));
    weightTrend.setOption({
      grid:{
        left: 30,
        top: 20,
        right: 30,
        bottom: 20
      },
      xAxis: [{
        type: 'category',
        boundaryGap: false,
        data: xAxisDate,
        axisLine: {
          show: false
        },
        axisTick:{
          length: 0
        },
        axisLabel: {
          color: '#999'
        },
        splitLine: {
          lineStyle:{
            color: '#eee'
          }
        }
      }],
      yAxis: [{
        type: 'value',
        axisLine: {
          show: false
        },
        axisTick:{
          length: 0
        },
        axisLabel: {
          color: '#999'
        },
        splitLine: {
          lineStyle:{
            color: '#eee'
          }
        },
        min:25
      }],
      series: [{
        type: 'line',
        label: {
          normal:{
            show: true,
            color: '#3acfb9',
            borderColor: '#3acfb9'
          }
        },
        itemStyle: {
          normal:{
            color: '#3acfb9',
            borderWidth: 2,
          }
        },
        lineStyle: {
          normal: {
            color: '#3acfb9'
          }
        },
        areaStyle: {
          normal: {
            color: '#3acfb9',
            opacity: 0.4
          }
        },
        showAllSymbol: true,
        symbol: 'emptyCircle',
        symbolSize: 6,
        data: yAxisDate
      }]
    })
  }

函数的setOption配置跟其他使用方式一致,可参考ECharts官方文档

@wusb wusb added the React label Feb 10, 2018
@wusb wusb closed this as completed Feb 10, 2018
@wusb wusb reopened this Mar 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant