做网站|网站建设,就上易助科技网

前端技术文档及相关资料下载

关于前端html、css、js等技术上的各种疑难棘手问题的解决方案探讨及相关资料下载!

JS图表工具插件C H A R T
来源:易助科技网浏览量:3收藏

简介

Chart.js 是是一个基于 HTML5 技术的、简单的、灵活的 JavaScript 图表工具。   能够很简单便捷的在 Web 网站或是 Web 应用程序添加有交互性的图表。


使用

1.  引入 Chart.js 文件


2.  HTML

HTML Canvas 代码,图表显示位置:

<canvas id="myChart" width="400" height="400"></canvas>


3. 调用

使用 Chart.js 创建图表,需要初始化 Chart 类,并传入图表节点,通过不同方式获取节点:  

//以下方式都可以
const ctx = document.getElementById('myChart'); // 获取节点
const ctx = document.getElementById('myChart').getContext('2d'); // getContext() 方法返回 canvas 的上下文
const ctx = $('#myChart'); // jQuery 获取
const ctx = 'myChart'; // 传入节点 ID


通过以上代码获得元素节点后,我们就可以创建自己的图表类型了。以下实例我们创建一个简单的折线图:

const ctx = document.getElementById('myChart');
const labels = ['一月份', '二月份', '三月份','四月份', '五月份', '六月份', '七月份']; // 设置 X 轴上对应的标签
const data = {
labels: labels,
datasets: [{
label: '我的第一个折线图',
data: [65, 59, 80, 81, 56, 55, 40],
fill: false,
borderColor: 'rgb(75, 192, 192)', // 设置线的颜色
tension: 0.1
}]
};
const config = {
type: 'line', // 设置图表类型
data: data,
};
const myChart = new Chart(ctx, config);


相关链接

官网地址 : https://www.chartjs.org/

Github 地址:https://github.com/chartjs/Chart.js

在线演示地址 :https://www.runoob.com/try/try.php?filename=trychartjs_line