Chart.js功能與使用方法小結(jié)
本文實例講述了Chart.js功能與使用方法。分享給大家供大家參考,具體如下:
官方文檔
英文文檔 https://www.chartjs.org/docs/2.8.0/
中文文檔 https://chartjs-doc.abingoal.com
在react中的使用
開始使用
npm install chart.js --save
在相應的頁面中引入 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);
https://codesandbox.io/embed/aged-meadow-2sc8m?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> ); } }
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
- 使用Chart.js圖表庫制作漂亮的響應式表單
- Chart.js 輕量級HTML5圖表繪制工具庫(知識整理)
- 詳解Chart.js輕量級圖表庫的使用經(jīng)驗
- 在 Angular 中使用Chart.js 和 ng2-charts的示例代碼
- 使用Vue.js 和Chart.js制作絢麗多彩的圖表
- ichart.js繪制虛線、平均分虛線效果的實現(xiàn)代碼
- Chart.js在Laravel項目中的應用示例
- vue集成chart.js的實現(xiàn)方法
- 利用ECharts.js畫K線圖的方法示例
- JavaScript Chart 插件整理
- 詳解vue文件中使用echarts.js的兩種方式
- vue.js+Echarts開發(fā)圖表放大縮小功能實例
相關(guān)文章
JavaScript實現(xiàn)移動端帶transition動畫的輪播效果
這篇文章主要介紹了JavaScript原生實現(xiàn)帶transition動畫的自動+手動輪播效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03JavaScript類型轉(zhuǎn)換方法及需要注意的問題小結(jié)(挺全面)
JavaScript類型轉(zhuǎn)換方法及需要注意的問題,在js中經(jīng)常需要對數(shù)據(jù)類型的轉(zhuǎn)換操作,需要的朋友可以參考下。2010-11-11JS頁面刷新與重新加載功能實現(xiàn)(關(guān)閉當前窗口)
在計算機網(wǎng)頁中如果我們想獲取當前頁面最新的內(nèi)容,可以刷新當前頁面重新獲取數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于JS頁面刷新與重新加載功能實現(xiàn)(關(guān)閉當前窗口)的相關(guān)資料,需要的朋友可以參考下2023-10-10微信小程序 動態(tài)修改頁面數(shù)據(jù)及參數(shù)傳遞過程詳解
這篇文章主要介紹了微信小程序 動態(tài)修改頁面數(shù)據(jù)及參數(shù)傳遞過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09JavaScript中Object.values()的用法舉例
這篇文章主要給大家介紹了關(guān)于JavaScript中Object.values()的用法舉例,Object.values()是JavaScript中一個內(nèi)置的靜態(tài)函數(shù),用于返回一個對象中所有屬性值的數(shù)組,需要的朋友可以參考下2023-09-09