Chart.js功能與使用方法小結(jié)
本文實(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ì)有所幫助。
- 使用Chart.js圖表庫(kù)制作漂亮的響應(yīng)式表單
- Chart.js 輕量級(jí)HTML5圖表繪制工具庫(kù)(知識(shí)整理)
- 詳解Chart.js輕量級(jí)圖表庫(kù)的使用經(jīng)驗(yàn)
- 在 Angular 中使用Chart.js 和 ng2-charts的示例代碼
- 使用Vue.js 和Chart.js制作絢麗多彩的圖表
- ichart.js繪制虛線、平均分虛線效果的實(shí)現(xiàn)代碼
- Chart.js在Laravel項(xiàng)目中的應(yīng)用示例
- vue集成chart.js的實(shí)現(xiàn)方法
- 利用ECharts.js畫K線圖的方法示例
- JavaScript Chart 插件整理
- 詳解vue文件中使用echarts.js的兩種方式
- vue.js+Echarts開發(fā)圖表放大縮小功能實(shí)例
相關(guān)文章
JavaScript實(shí)現(xiàn)移動(dòng)端帶transition動(dòng)畫的輪播效果
這篇文章主要介紹了JavaScript原生實(shí)現(xiàn)帶transition動(dòng)畫的自動(dòng)+手動(dòng)輪播效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03JavaScript類型轉(zhuǎn)換方法及需要注意的問題小結(jié)(挺全面)
JavaScript類型轉(zhuǎn)換方法及需要注意的問題,在js中經(jīng)常需要對(duì)數(shù)據(jù)類型的轉(zhuǎn)換操作,需要的朋友可以參考下。2010-11-11JS頁(yè)面刷新與重新加載功能實(shí)現(xiàn)(關(guān)閉當(dāng)前窗口)
在計(jì)算機(jī)網(wǎng)頁(yè)中如果我們想獲取當(dāng)前頁(yè)面最新的內(nèi)容,可以刷新當(dāng)前頁(yè)面重新獲取數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于JS頁(yè)面刷新與重新加載功能實(shí)現(xiàn)(關(guān)閉當(dāng)前窗口)的相關(guān)資料,需要的朋友可以參考下2023-10-10網(wǎng)上應(yīng)用的一個(gè)不錯(cuò)common.js腳本
網(wǎng)上應(yīng)用的一個(gè)不錯(cuò)common.js腳本...2007-08-08微信小程序 動(dòng)態(tài)修改頁(yè)面數(shù)據(jù)及參數(shù)傳遞過程詳解
這篇文章主要介紹了微信小程序 動(dòng)態(tài)修改頁(yè)面數(shù)據(jù)及參數(shù)傳遞過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09JavaScript中Object.values()的用法舉例
這篇文章主要給大家介紹了關(guān)于JavaScript中Object.values()的用法舉例,Object.values()是JavaScript中一個(gè)內(nèi)置的靜態(tài)函數(shù),用于返回一個(gè)對(duì)象中所有屬性值的數(shù)組,需要的朋友可以參考下2023-09-09js實(shí)現(xiàn)帶圓角的兩級(jí)導(dǎo)航菜單效果代碼
這篇文章主要介紹了js實(shí)現(xiàn)帶圓角的兩級(jí)導(dǎo)航菜單效果代碼,涉及javascript鼠標(biāo)事件及頁(yè)面元素樣式動(dòng)態(tài)變換技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-08-08