代码之家  ›  专栏  ›  技术社区  ›  GalAbra

访问react chartjs饼图的切片

  •  0
  • GalAbra  · 技术社区  · 6 年前

    我试图创建一个静态(不可点击)饼图使用 react-chartjs-2
    但是,我希望其中一个切片“弹出”,或者看起来比其他切片更大:

    Visual example of the pie chart I'm trying to create

    outerRadius 财产。


    Stack Overflow Github ,这让我想到:

    import { Pie } from 'react-chartjs-2';
    
    <Pie
        data={data}
        options={options}
        getElementsAtEvent={(elems) => {
            // Modify the size of the clicked slice
            elems[0]['_model'].outerRadius = 100;
        }}
    />
    

    但是,我没有找到任何关于在用户没有点击的情况下弹出一个切片的方法。

    0 回复  |  直到 6 年前
        1
  •  1
  •   GalAbra    6 年前

    看了下面之后 Pie 我找到了答案。
    componentDidMount() :

    import React, { Component } from 'react';
    import { Pie } from 'react-chartjs-2';
    
    class PieChart extends Component {
        componentDidMount() {
            const change = {
                sliceIndex: 0,
                newOuterRadius: 100
            }
            const meta = this.pie.props.data.datasets[0]._meta;
            meta[Object.keys(meta)[0]]
                .data[change.sliceIndex]
                ._model
                .outerRadius = change.newOuterRadius;
        }
    
        render() {
            const data = {
                type: 'pie',
                datasets: [ { data: [10, 20, 30] } ],
                labels: ['a', 'b', 'c'],
            };
            const options = {};
    
            return <Pie
                ref={(self) => this.pie = self}
                data={data}
                options={options}
            />
        }
    }
    
    export default PieChart;
    
    推荐文章