html中统计图怎么做,数据统计图用react怎么做

08a61158ae4db53de086ea9c4e233bb9.png

数据统计图用react怎么做

Echarts图表功能很强大,使用起来也很方便,下面我们就说说怎么在react中使用Echarts。用的前端开发工具是webStorm

1.添加依赖

在webStorm的命令行输入以下语句:npm install --save echarts-for-react

依赖添加完成后项目的node_modules目录下出现echarts的依赖目录和echarts-for-react的依赖目录

94265246cde308dc5cb5b40dc0be89eb.png

在项目的package.json中出现echarts-for-react的依赖,如图所示:

b49bcaf32524fb43e8c7bbc930734908.png

启动本地服务后会报错,说找不到echarts的依赖,所以如果只执行npm install --save echarts-for-react会报错,可以执行下面的命令npm install --save echarts-for-react

npm install echarts --save

此时项目的package.json会出现两个依赖,如下图所示:

3719f6beb64ffc474a57e9d2edca9763.png

2.在图表页面引入Echartsimport React from 'react';

import ReactEcharts from 'echarts-for-react';

3.echarts的官方图表示例

官方网站:http://echarts.baidu.com/examples.html,提供了很多图表

我们以折线图为例,下图是官方的图表和对应的代码示例:

439bc1d105aa802276587ca7f9f212d1.png

在react中使用echarts很简单,只需要将option中的代码放到react的getOtion方法中就就OK了,import React from 'react';

import ReactEcharts from 'echarts-for-react';

const LineMarkerEcharts = React.createClass({

propTypes: {

},

getOtion: function() {

const option = {

title: {

text: '未来一周气温变化',

subtext: '纯属虚构'

},

tooltip: {

trigger: 'axis'

},

legend: {

data:['最高气温','最低气温']

},

toolbox: {

show: true,

feature: {

dataZoom: {

yAxisIndex: 'none'

},

dataView: {readOnly: false},

magicType: {type: ['line', 'bar']},

restore: {},

saveAsImage: {}

}

},

xAxis: {

type: 'category',

boundaryGap: false,

data: ['周一','周二','周三','周四','周五','周六','周日']

},

yAxis: {

type: 'value',

axisLabel: {

formatter: '{value} °C'

}

},

series: [

{

name:'最高气温',

type:'line',

data:[11, 11, 15, 13, 12, 13, 10],

markPoint: {

data: [

{type: 'max', name: '最大值'},

{type: 'min', name: '最小值'}

]

},

markLine: {

data: [

{type: 'average', name: '平均值'}

]

}

},

{

name:'最低气温',

type:'line',

data:[1, -2, 2, 5, 3, 2, 0],

markPoint: {

data: [

{name: '周最低', value: -2, xAxis: 1, yAxis: -1.5}

]

},

markLine: {

data: [

{type: 'average', name: '平均值'},

[{

symbol: 'none',

x: '90%',

yAxis: 'max'

}, {

symbol: 'circle',

label: {

normal: {

position: 'start',

formatter: '最大值'

}

},

type: 'max',

name: '最高点'

}]

]

}

}

]

};

return option;

},

render: function() {

let code = "

" option={this.getOtion()} " +

" style={{height: '350px', width: '1000px'}} " +

" className='react_for_echarts' />";

return (

option={this.getOtion()}

style={{height: '350px', width: '1000px'}}

className='react_for_echarts' />

);

}

});

export default LineMarkerEcharts;

此时,这个图表就成为了我们react中的组件,在项目中需要使用图表的时候直接引入组件就可以了。

更多React相关技术文章,请访问React答疑栏目进行学习!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值