Created
November 23, 2022 20:39
-
-
Save lejonmanen/a6c73324408892d84f0b0940bf5f30ab to your computer and use it in GitHub Desktop.
chart.js demo med React-komponent
This file contains 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 { Pie, Bar, Line } from 'react-chartjs-2'; | |
import { data1, data2 } from './demoData' | |
const DemoChart = () => ( | |
<div className="demo-chart"> | |
<Bar data={data1} /> | |
<Bar data={data2} /> | |
</div> | |
) | |
export default DemoChart |
This file contains 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
// Alternativ 1: använd objektformen | |
// Observera att objektet måste ha egenskaperna x och y | |
const daysInMonth = [ | |
{ month: 'januari', days: 31 }, | |
{ month: 'februari', days: 28 }, | |
{ month: 'mars', days: 31 }, | |
{ month: 'april', days: 30 }, | |
{ month: 'maj', days: 31 }, | |
].map(({ month, days }) => ({ x: month, y: days })) | |
const data1 = { | |
datasets: [{ | |
label: 'Månader', | |
data: daysInMonth | |
}] | |
} | |
// Alternativ 2: separata listor för varje serie | |
const monthNameSeries = ['januari', 'februari', 'mars', 'april', 'maj'] // x | |
const dayAmountSeries = [31, 28, 31, 30, 31] | |
const data2 = { | |
labels: monthNameSeries, | |
datasets: [{ | |
label: 'Månader', | |
data: dayAmountSeries | |
}] | |
} | |
export { data1, data2 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment