欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Chart.js功能與使用方法小結(jié)

 更新時(shí)間:2020年04月29日 09:45:36   作者:xiaoping  
這篇文章主要介紹了Chart.js功能與使用方法,結(jié)合實(shí)例形式分析了Chart.js功能、使用方法及操作注意事項(xiàng),需要的朋友可以參考下

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

官方文檔

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

react中的使用

開始使用

npm install chart.js --save

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

import Chart from "chart.js"

先寫一個(gè)小的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);

https://codesandbox.io/embed/aged-meadow-2sc8m?fontsize=14

動(dòng)態(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>
  );
 }
}

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。

更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁(yè)面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論