React学习系列Echarts pie

先安装插件 

npm install echarts --save

代码

import React, { Component } from 'react';
// 引入 ECharts 主模块
import echarts from 'echarts/lib/echarts';
// 引入饼状图
import  'echarts/lib/chart/pie';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
// 引入图例组件
import 'echarts/lib/component/legend';

class Pie extends Component {
    constructor(props){
        super(props);

        this.state = {
            data1:8,
            data2:12
        }
    }
    componentDidMount(){
        let myChart = echarts.init(document.getElementById('pie'));
        // 绘制图表
        myChart.setOption({
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b}: {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                x: 'right',
                top: 'middle',
                data:['已签到','未签到']
            },
            series: [
                {
                    name:'考勤统计',
                    type:'pie',
                    radius: ['50%', '70%'],
                    avoidLabelOverlap: false,
                    label: {
                        normal: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            show: true,
                            textStyle: {
                                fontSize: '30',
                                fontWeight: 'bold'
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data:[
                        {
                            value: this.state.data1,
                            name:'已签到',
                            itemStyle: {
                                normal: {
                                    color: '#3AA0FF'
                                }
                            }},
                        {
                            value: this.state.data2,
                            name:'未签到',
                            itemStyle: {
                                normal: {
                                    color: '#E56363'
                                }
                            }
                        }
                    ]
                }
            ]
        });
    };
    render() {
        return (
            <div>
                <div id="pie" style={{width: '450px', height:'400px'} }></div>
            </div>
        )
    }
}
export default Pie;

 截图

在使用 React 和 TypeScript 引入 ECharts 饼图时,你需要按照以下步骤进行操作: 1. 首先,确保你已经安装了 ECharts 的相关依赖。你可以使用 npm 或 yarn 来进行安装: ```shell npm install echarts ``` ```shell yarn add echarts ``` 2. 创建一个新的 React 组件,用于显示 ECharts 饼图。例如,你可以创建一个名为 `PieChart` 的组件: ```tsx import React, { useEffect, useRef } from 'react'; import echarts from 'echarts'; interface PieChartProps { data: any[]; // 饼图的数据 } const PieChart: React.FC<PieChartProps> = ({ data }) => { const chartRef = useRef<HTMLDivElement>(null); useEffect(() => { if (chartRef.current) { const chart = echarts.init(chartRef.current); const option = { series: [ { type: 'pie', data, }, ], }; chart.setOption(option); } }, [data]); return <div ref={chartRef} style={{ width: '100%', height: '300px' }} />; }; export default PieChart; ``` 3. 在你的页面或其他组件中使用 `PieChart` 组件,并传入饼图所需的数据。例如: ```tsx import React from 'react'; import PieChart from './PieChart'; const App: React.FC = () => { const data = [ { value: 335, name: 'Category 1' }, { value: 310, name: 'Category 2' }, { value: 234, name: 'Category 3' }, { value: 135, name: 'Category 4' }, { value: 1548, name: 'Category 5' }, ]; return ( <div> <h1>ECharts Pie Chart</h1> <PieChart data={data} /> </div> ); }; export default App; ``` 这样,你就可以在 React 应用中引入并使用 ECharts 饼图了。记得根据你的实际需求修改数据和样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值