react 创建饼状图

  1. npm install react-highcharts –save
  2. 引入页面中 import Highcharts from ‘react-highcharts’
  3. 在render中调用

    代码如下(这是一个饼状图的示例):

注:config中为绘制图必须的属性等内容,可参考highcharts官网示例及api文档。地址为:https://www.hcharts.cn/demo/highcharts

import React from 'react';
import Highcharts from 'react-highcharts'

import './taskMain.less';

class DoughnutChart extends React.Component{
    constructor(props){
        super(props);
    }
    componentWillMount() {
        this.state = {
            config: {
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    spacing:[10,0,40,0],
                },
                title: {
                    text: "运行成功率",
                    style:{
                        "color":"#fff",
                        "fontSize":"16px",
                    },
                    verticalAlign:'middle',
                },
                //这是鼠标悬浮时的提示框
                tooltip: {
                    //point为series中data的数据
                    pointFormat: ' <p style="font-size: 14px">{point.name}:<b>{point.percentage:.1f}%</b></p>',
                    headerFormat:'<span style="font-size: 14px">{series.name}</span><br/>',
                    hideDelay:200
                },
                credits: {
                    enabled: false   // 隐藏highcharts版权
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        //标注
                        dataLabels: {
                            enabled: true, 
                            format: '<span>{point.name} : {point.percentage:.1f}%</span>',
                            distance:12,
                            style: {
                                color: '#108eef',
                                fontWeight: "lighter",
                                fontSize:'1em',
                            }
                        },
                    },
                },
                series: [{
                    type:'pie',
                    name: this.props.name,
                    colorByPoint: true,
                    data: [{
                        name:"成功",
                        color:"#66CDAA",
                        y:80%
                    },{
                        name:"失败",
                        color:"#E9967A",
                        y:20%
                    }],
                }]
            },
        }
    }
    render(){
        return <div className="chart">
            <Highcharts config={this.state.config}></Highcharts>
        </div>
    }
}
export default DoughnutChart;

效果图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值