Skip to content

Instantly share code, notes, and snippets.

@calderaro
Created August 14, 2020 02:12
Show Gist options
  • Save calderaro/1d3513d9227bd3ee82f3c0d8ee5f034a to your computer and use it in GitHub Desktop.
Save calderaro/1d3513d9227bd3ee82f3c0d8ee5f034a to your computer and use it in GitHub Desktop.
Chartjs React
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;
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