react结合echarts画折线图并实现数据分离

在react项目中使用echarts绘制折线图示例练习,在SingleLine3Common.js做逻辑处理,设置绘制折线图的配置项,在App3.js中定义数据格式并给出具体数据,在SingleLine3.js初始化echarts界面,然后获取数据完成图表的绘制。

  • ecahrts画图逻辑实现:SingleLine3Common.js
import React,{Component} from 'react';

//引入提示框
require ('echarts/lib/component/tooltip');
//引入图例
require ('echarts/lib/component/legend');
//引入直角坐标系
require ('echarts/lib/component/grid');
//引入标题
require ('echarts/lib/component/title');
//引入线段
require ('echarts/lib/chart/line');
   
export const renderLineChart=function(data,title,xAxis,series,seriesValue){
    let option = {
        
        tooltip: {
            trigger: 'axis'
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },       
        legend: {
            data:[]
        },
        yAxis: {
            // show:false,  //设置是否显示坐标轴
            type: 'value'
        },
        xAxis: {
            // show:false,  //设置是否显示坐标轴
            type: 'category',
            boundaryGap: false,
            data: []
        },
        series: []
    };
    //绘制图形
    data.map(function (item){
        //设置图例
        option.legend.data.push(item[title]);
        //给series赋值
        option.series.push({
            name:item[title],
            type:'line',
            data:renderSeries(item[series],seriesValue),
             /**设置直线的颜色,粗细 */                       
             itemStyle: {
                normal: {
                    color: 'rgb(0,255, 0)',
                    //线条样式
                    lineStyle:{
                         type: 'solid',
                         width: 1  //设置线条粗细
                    }
                }
            },
            showSymbol:false,  //设置折线上的圆点
        })
    });
    //给x轴赋值
    data.map(function (item){          
        item.data.map(function (item){
            option.xAxis.data.push(item[xAxis]);
        })           
    });
    //过滤,将x轴不存在的加进去,存在的不要加进去
    option.xAxis.data=Array.from(new Set(option.xAxis.data));
    console.log(option);  
    return option;
};
//再次遍历,给y轴赋值
export const renderSeries=function(data,value){
    let result=[];
    data.map(function (item){
        result.push(item[value]);
    });
    return result;
};
  • echarts初始化:SingleLine3.js
import React,{Component} from 'react';

//引入echarts主模块
import echarts from 'echarts/lib/echarts';
class SingleLine3 extends Component{
    /**设置渲染状态id */
    state={
        id:Math.random().toString()
     };
    componentDidMount() { 
        /**设置状态id
         * 1.this.props.id
         * 2.this.state.id
         */
        let myCharts=echarts.init(document.getElementById(this.state.id));  
        myCharts.setOption(this.props.option);
    }
    render() {
        return (
            <div id={this.state.id} style={{width:'100%',height:600}}></div>
        );
    }
}
export default SingleLine3;
  • 数据格式定义:App3.js
import React,{Component} from 'react';
import SingleLine3 from './SingleLine3';
import {renderLineChart} from "./SingleLine3Common";

class App3 extends Component{
    state={
        data:[
            {
                title:"第一条折线",
                data:[
                    {
                        name:'星期一',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期二',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期三',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期四',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期五',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期六',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期天',
                        total:Math.random().toFixed(2),
                    },
                ]
            },
            {
                title:"第二条折线",
                data:[
                    {
                        name:'星期一',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期二',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期三',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期四',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期五',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期六',
                        total:Math.random().toFixed(2),
                    },
                    {
                        name:'星期天',
                        total:Math.random().toFixed(2),
                    },
                ]
            },
        ]
    };   
    render() {
        const {data}=this.state;
        return (           
            <div className="App" style={{padding:20}}>
                <SingleLine3  option={renderLineChart(data,'title','name','data','total')}/>
            </div>
        );
    }
}
export default App3;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值