How to use plugins with chartist.js?

前端 未结 2 1996
轻奢々
轻奢々 2021-01-22 19:20

I am using chartist.js for making pie chart component. I want to make use of legend plugin https://codeyellowbv.github.io/chartist-plugin-legend/

I am not getting the le

相关标签:
2条回答
  • 2021-01-22 19:29

    Try folowing as the chartist-plugin-legend return Chartist.plugins.legend function. You can pass options also to add customization here is link where you can read it: Link chartist-plugin-legend

    let plugins = [
        Legend()
    ]
    

    Made this change also as react-chartist doesnot take any props called plugins.

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

    Now add a .css file in your directory and import it in your component file. with following content. The content is same as mentioned on the plugin page.

    .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);
            }
        }
    }
    

    Now you can give styling as you want. legend plugin also provides certains options that you can send. Read about it and accordingly pass in the options

    0 讨论(0)
  • 2021-01-22 19:42

    You can use the plugin by adding it on the options property, but first you need to import the ff. dependencies:

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

    add the plugin:

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

    Render it: <ChartistGraph data={data} options={options} type={type} />

    and because the CSS is not included, you can examine the index file of the plugin here and play with it.

    0 讨论(0)
提交回复
热议问题