问题
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 legend in my pie chart. See screenshot below
Code:
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;
Screenshot:
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/50733529/how-to-use-plugins-with-chartist-js