I am using react and typescript. I get an error with type. How can I solve it?
position is of type string. http://www.chartjs.org/docs/latest/configuration/legend.html
[React + ts + react-chartjs-2]
import React from 'react';
import {Pie} from 'react-chartjs-2';
export default class Chart extends React.Component{
constructor(props) {
super(props);
this.state = { };
}
render() {
let options = {
legend: {
position:'bottom',
}
};
return (
<Pie options={options} />
);
}
}
[Error.] Types of property 'options' are incompatible.
Type '{ legend: { position: string; }; }' is not assignable to type 'ChartOptions'.
Types of property 'legend' are incompatible.
Type '{ position: string; }' is not assignable to type 'ChartLegendOptions'.
Types of property 'position' are incompatible.
Type 'string' is not assignable to type 'PositionType'.
Firstly you are missing a required data attribute i.e.
<Pie data={data} />
You can then change your code to:
import React from 'react';
import * as ReactDOM from "react-dom"
import { Pie } from 'react-chartjs-2';
import { ChartOptions } from 'chart.js'
export default class Chart extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
const data = {
labels: [
'Red',
'Green',
'Yellow'
],
datasets: [{
data: [300, 50, 100],
backgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
],
hoverBackgroundColor: [
'#FF6384',
'#36A2EB',
'#FFCE56'
]
}]
};
const options: ChartOptions = {
legend: {
position: 'bottom',
}
};
return (
<Pie data={data} options={options} />
);
}
}
ReactDOM.render(<Chart />, document.getElementById("root"))
You can see this example working here.