chart.js的line使用(react組件)

import React, { Component } from "react";
import Chart from "chart.js";

class LineChart extends Component {

    componentDidMount() {
        const ctx = document.getElementById('canvas').getContext('2d');
        const myLine = new Chart(ctx, {
            type: 'line',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
                datasets: [{
                    label: 'My First dataset',
                    backgroundColor: 'rgb(255, 99, 132)',
                    borderColor: 'rgb(255, 99, 132)',
                    data: [
                        1, 4, 3, 5, 8, 90, 48
                    ],
                    fill: false,
                }, {
                    label: 'My Second dataset',
                    fill: false,
                    backgroundColor: 'rgb(54, 162, 235)',
                    borderColor: 'rgb(54, 162, 235)',
                    data: [
                        1, 8, 6, 10, 16, 10, 45
                    ],
                }]
            },
            options: {
                responsive: true,
                title: {
                    display: true,
                    // text: 'Chart.js Line Chart'
                },
                tooltips: {
                    mode: 'index',
                    intersect: false,
                },
                hover: {
                    mode: 'nearest',
                    intersect: true
                },
                scales: {
                    xAxes: [{
                        display: false,
                        scaleLabel: {
                            display: true,
                            labelString: 'Month'
                        }
                    }],
                    yAxes: [{
                        display: true,
                        scaleLabel: {
                            display: true,
                            // labelString: 'Value'
                        },
                        ticks: {
                            min: 0,
                            max: 100,

                            // forces step size to be 5 units
                            stepSize: 5
                        }
                    }]
                },
                legend: { display: false }
            }
        });
    }
    render() {
        return <div style={{ width: "75%" }}>
            <canvas id="canvas"></canvas>
        </div>
    }
}
export default LineChart

如果需要動態效果,直接操作對應的數組即可!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章