代码之家  ›  专栏  ›  技术社区  ›  stone rock

如何在charist.js中使用插件?

  •  2
  • stone rock  · 技术社区  · 7 年前

    我正在使用charist.js制作饼图组件。我想利用传奇插件 https://codeyellowbv.github.io/chartist-plugin-legend/

    我没有在我的饼图上看到这个传说。见下面的截图

    代码:

    import React, { Component } from 'react';
    import ChartistGraph from "react-chartist";
    import Legend from "chartist-plugin-legend";
    
    import './piechart.css';
    
    let options = {
      width:400,
      height:500,
      labelInterpolationFnc: function(value) {
        return value[0]
      }
    };
    
    
    let plugin = {
        plugin:'legend'
    }
    
    
    class Chart extends Component {
    
      render(){
        return(
          <div>
              <div className="center">
              <ChartistGraph data={data} options={options} plugins={plugin} type="Pie"/>
              </div>
          </div>
    
        )}
    
    }
    
    export default Chart;
    

    截图:

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  1
  •   simbathesailor    7 年前

    请尝试以下操作,因为charist plugin legend返回charist.plugins.legend函数。您还可以传递选项以添加自定义。以下是链接,您可以在其中阅读: Link chartist-plugin-legend

    let plugins = [
        Legend()
    ]
    

    做了这个改动,因为react charist不使用任何称为插件的道具。

    <ChartistGraph data={data} options={{...options, plugins}} type="Pie"/>
    

    现在,在目录中添加一个.css文件并将其导入到组件文件中。内容如下。内容与插件页面上提到的相同。

    .ct-legend {
        position: relative;
        z-index: 10;
    
        li {
            position: relative;
            padding-left: 23px;
            margin-bottom: 3px;
        }
    
        li:before {
            width: 12px;
            height: 12px;
            position: absolute;
            left: 0;
            content: '';
            border: 3px solid transparent;
            border-radius: 2px;
        }
    
        li.inactive:before {
            background: transparent;
        }
    
        &.ct-legend-inside {
            position: absolute;
            top: 0;
            right: 0;
        }
    
        @for $i from 0 to length($ct-series-colors) {
            .ct-series-#{$i}:before {
                background-color: nth($ct-series-colors, $i + 1);
                border-color: nth($ct-series-colors, $i + 1);
            }
        }
    }
    

    现在你可以随心所欲地做造型了。图例插件还提供了可以发送的certains选项。阅读并相应地传递选项

        2
  •  4
  •   Sachi Tekina    7 年前

    您可以通过将插件添加到 options 属性,但首先需要导入FF。依赖项:

    import React, { Component } from 'react';
    import ChartistGraph from "react-chartist";
    import Legend from "chartist-plugin-legend";
    

    添加插件:

    let options = {
        width:400,
        height:500,
        plugins: [
            Legend()
        ]
    };
    

    渲染: <ChartistGraph data={data} options={options} type={type} />

    由于不包括css,您可以检查 plugin here 玩它。