Created
August 14, 2020 02:12
-
-
Save calderaro/1d3513d9227bd3ee82f3c0d8ee5f034a to your computer and use it in GitHub Desktop.
Chartjs React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Chart, {ChartDataSets} from 'chart.js'; | |
import styles from './styles'; | |
interface Props { | |
labels: string[]; | |
datasets: ChartDataSets[]; | |
} | |
class Graph extends React.Component<Props> { | |
graph: Chart | null = null; | |
constructor(props: Props) { | |
super(props); | |
this.state = { | |
instance: null | |
}; | |
} | |
componentDidMount() { | |
const ctx = document.getElementById('myChart') as HTMLCanvasElement; | |
this.graph = new Chart(ctx, { | |
type: 'line', | |
data: { | |
labels: this.props.labels, | |
datasets: this.props.datasets | |
}, | |
options: { | |
scales: { | |
yAxes: [ | |
{ | |
ticks: { | |
beginAtZero: true | |
} | |
} | |
] | |
} | |
} | |
}); | |
} | |
render() { | |
return <canvas id="myChart" className={styles.graph}></canvas>; | |
} | |
} | |
export default Graph; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {stylesheet} from 'typestyle'; | |
const styles = stylesheet({ | |
graph: { | |
width: '100%' | |
} | |
}); | |
export default styles; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment