react+echarts柱状图demo

1.npm下载Echarts并引入项目(Echarts官网Apache ECharts

2.项目中新建myechart.js

import React, { Component } from 'react';
import * as echarts from 'echarts';
import { renderBarCharts } from '../utils';


class MyEcharts extends Component {

    componentDidMount() {
        // 基于准备好的dom,初始化echarts实例
        const MyEcharts = echarts.init(document.getElementById("charts"));
        // console.log(MyEcharts);

        // 绘制图表
        MyEcharts.setOption(renderBarCharts('小马的echarts柱状图'))

    }

    render() {
        return (
            <div>
                <div id='charts' style={{ height: 700 }}>

                </div>
            </div>
        )
    }

}

export default MyEcharts;

3.表格数据



export function renderBarCharts(title) {
    return {
        // 图表背景颜色
        // backgroundColor: '#ccc',
        title: {
            text: title,
            left: "center",
            textStyle: {
                color: 'pink',
                fontSize: 24,

            }
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['Rainfall', 'Evaporation'],
            textStyle: {
                color: 'black',
            },
            orient: 'vertical',
            left: 'center',
            bottom:'bottom',
        },
        toolbox: {
            show: true,
            feature: {
                dataView: { show: true, readOnly: false },
                magicType: { show: true, type: ['line', 'bar'] },
                restore: { show: true },
                saveAsImage: { show: true }
            }
        },
        calculable: true,
        xAxis: [
            {
                name: '月份',
                nameTextStyle: {
                    padding: [20, 0, 0, 0],    // 四个数字分别为上右下左与原位置距离
                    color:'green',
                    fontSize: 18,
                },
                type: 'category',
                // prettier-ignore
                data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
                axisLabel: {
                    margin: 20,
                    interval: 0,
                    rotate: 20,
                    textStyle: {
                        //改变x轴上文字的颜色
                        color: "green",
                        fontSize: 18,
                    }
                }
            }
        ],
        yAxis: [
            {
                name:'降雨量',
                nameTextStyle: {
                    padding: [20, 0, 0, 0],    // 四个数字分别为上右下左与原位置距离
                    color:'red',
                    fontSize: 18,
                },

                type: 'value',
                axisLabel: {
                    textStyle: {
                        //改变y轴上文字的颜色
                        color: "red",
                        fontSize: 18,
                    }
                },
                axisLine: {
                    show: true,//是否显示轴线
                    lineStyle: {
                        color: 'black',//刻度线的颜色
                    }
                }
            }
        ],
        series: [
            {
                name: 'Rainfall',
                type: 'bar',
                data: [
                    2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
                ],
                itemStyle: {
                    normal: {
                        color: 'pink'
                    },
                },
                markPoint: {
                    data: [
                        { type: 'max', name: 'Max' },
                        { type: 'min', name: 'Min' }
                    ]
                },
                markLine: {
                    data: [{ type: 'average', name: 'Avg' }]
                }
            },
            {
                name: 'Evaporation',
                type: 'bar',
                data: [
                    2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
                ],
                markPoint: {
                    data: [
                        { name: 'Max', value: 182.2, xAxis: 7, yAxis: 183 },
                        { name: 'Min', value: 2.3, xAxis: 11, yAxis: 3 }
                    ],
                    label: {
                        offset: [0, 0],
                        textStyle: {
                            color: "red",
                            fontSize: 12,
                        },
                    },
                },
                markLine: {
                    data: [{ type: 'average', name: 'Avg' }],
                    label: {
                        offset: [0, 0],
                        textStyle: {
                            color: "red",
                            fontSize: 12,
                        },
                    },
                }
            }
        ]
    }
}

运行图:

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值