溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Chart.js

發(fā)布時間:2020-07-29 13:45:07 來源:億速云 閱讀:135 作者:小豬 欄目:web開發(fā)

這篇文章主要講解了如何使用Chart.js,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

本文實例講述了Chart.js功能與使用方法。分享給大家供大家參考,具體如下:

官方文檔

英文文檔 https://www.chartjs.org/docs/2.8.0/
中文文檔 https://chartjs-doc.abingoal.com

react中的使用

開始使用

npm install chart.js --save

在相應(yīng)的頁面中引入 chartjs

import Chart from "chart.js"

先寫一個小的demo

import React from "react";
import ReactDOM from "react-dom";

import Chart from "chart.js";

class App extends React.Component {
 constructor(props) {
  super(props);
  this.state = {};
 }
 componentDidMount() {
 this.renderCanvas()
 }

 // 作圖
 renderCanvas = () => {
  const myChartRef = this.chartRef.getContext("2d");
  new Chart(myChartRef, {
   type: "line",
   data: {
    labels: [1,2,3,4,5],
    datasets: [
     {
      data: [10, 20, 50, 80, 100],
      backgroundColor: "rgba(71, 157, 255, 0.08)",
      borderColor: "rgba(0, 119, 255, 1)",
      pointBackgroundColor: "rgba(56, 96, 244, 1)",
      pointBorderColor: "rgba(255, 255, 255, 1)",
      pointRadius: 4
     }
    ]
   },
   
   options: {
    responsive: true,
    legend: {
     display: false
    },
    maintainAspectRatio: false
   }
  });
 };

 render() {
  return (
   <div style={{ height: 288 }}>
    <canvas id="myChart" ref={ref => (this.chartRef = ref)} />
   </div>
  );
 }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

如何使用Chart.js

https://codesandbox.io/embed/aged-meadow-2sc8m&#63;fontsize=14

動態(tài)更新的數(shù)據(jù)

import React from "react";
import ReactDOM from "react-dom";

import Chart from "chart.js";
let currentChart;

class App extends React.Component {
 constructor(props) {
  super(props);
  this.state = {
   data: [30, 60, 90, 120, 100]
  };
 }
 componentDidMount() {
  this.renderCanvas();
  this.renderCurrent();
 }

 // 作圖
 renderCanvas = () => {
  const myChartRef = this.chartRef.getContext("2d");
  new Chart(myChartRef, {
   type: "line",
   data: {
    labels: [1, 2, 3, 4, 5],
    datasets: [
     {
      data: [10, 20, 50, 80, 100],
      backgroundColor: "rgba(71, 157, 255, 0.08)",
      borderColor: "rgba(0, 119, 255, 1)",
      pointBackgroundColor: "rgba(56, 96, 244, 1)",
      pointBorderColor: "rgba(255, 255, 255, 1)",
      pointRadius: 4
     }
    ]
   },

   options: {
    responsive: true,
    legend: {
     display: false
    },
    maintainAspectRatio: false
   }
  });
 };

 renderCurrent = () => {
  const { data } = this.state;
  const currentCharttemp = this.currentChart.getContext("2d");
  if (typeof currentChart !== "undefined") {
   currentChart.destroy();
  }
  currentChart = new Chart(currentCharttemp, {
   type: "line",
   data: {
    labels: [1, 2, 3, 4, 5],
    datasets: [
     {
      data: data,
      backgroundColor: "rgba(71, 157, 255, 0.08)",
      borderColor: "rgba(0, 119, 255, 1)",
      pointBackgroundColor: "rgba(56, 96, 244, 1)",
      pointBorderColor: "rgba(255, 255, 255, 1)",
      pointRadius: 4
     }
    ]
   },

   options: {
    responsive: true,
    legend: {
     display: false
    },
    maintainAspectRatio: false
   }
  });
 };

 render() {
  return (
   <div>
    <canvas id="myChart" ref={ref => (this.chartRef = ref)} />
    <br />

    <button
     onClick={()=>
      this.setState({ data: [200, 500, 20, 50, 100] }, this.renderCurrent)
     }
    >
     更新數(shù)據(jù)
    </button>
    <canvas id="currentChart7" ref={ref => (this.currentChart = ref)} />
   </div>
  );
 }
}

看完上述內(nèi)容,是不是對如何使用Chart.js有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI