第一步:引入chart.js文件。此工具库在全局命名空间中定义了chart变量。
chart.js文件下载Release Version 2.4.0 · chartjs/Chart.js · GitHub
<script src = " chart.js "></script>
第二步:创建图表实现chart对象,首先传入一个绘图表2d context。我们用H5中的cavcas标签实现
<canvas id = "chart" width ="400" height ="400"></canvas>
第三步:创建图表
//首先通过ID获取标签对象,并告诉他绘制的是二维图像
var chart = document.getElementById('chart').getContext('2d');
//创建Chart对象,传入参数
var ctx=new Chart(ctx,{data,options});
//这里的data和options直接传入
其中data数据结构:
labels表示X轴的参数
xLabels表示类别轴,自定义X轴参数
yLabels表示类别轴,自定义Y轴参数
datasets数据集中:[
backgroundColor:填充色,
borderColor:曲线边框色,
piontColor:数据点的颜色,
pointStrokeColor:数据点边框色,
data:数据
]
控制选项options:
title:{ //图表标题
display:true, //是否显示
position:'left', //标题位置
text:'', //标题名
fontColor:'' , //标题颜色
fontFamily:'', //标题字体格式
fontSize:10 //字体大小
},
elements: { //元素配置
arc:{ //弧线配置
backgroundColor:'',
borderColor:'',
borderwidth:
},
line:{
tension:0-1, //线的张力
borderColor:'',
stepped:true //承阶梯显示
},
point:{
radius:3 //点的半径
},
rectangle:{
backgroundColor:'',
borderWidth:,
borderColor:'',
borderSkipped:'bottom' //隐藏哪个边
}
},
legend:{ //图例
display:true,
position:'left',
labels:{ //图例文字控制
fontColor:'' ,
fontSize:10
}
},
scales:{ //坐标轴
xAxes:[{ //X轴信息
display:true, //是否显示
position:'top', //轴的位置
ticks:{ //轴文字控制
beginAtZero:true, //是否从零开始
fontSize:15,
min:0, //从最小值开始
max:100 //到最大值结束
},
}],
yAxes:[{
//同上
}]
},
animation:{ //动画配置
duration:1000 //动画播放时间
}
以上页面代码:
<!DOCTYPE HTML>
<html>
<body>
<div><canvas id="myChart1" width="400" height="400"></canvas></div>
<div><canvas id="myChart2" width="400" height="400"></canvas></div>
<div><canvas id="myChart3" width="400" height="400"></canvas></div>
<div><canvas id="myChart4" width="400" height="400"></canvas></div>
<div><canvas id="myChart5" width="400" height="400"></canvas></div>
<div><canvas id="myChart6" width="400" height="400"></canvas></div>
<script>
var ctx = document.getElementById("myChart1");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: '第1月',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: ['rgba(255, 0, 0, 0.2)'],
borderColor: ['rgba(255, 0, 0,0.7)'],
borderWidth: 1
},
{
label: '第2月',
data: [15, 12, 3, 50, 12, 43],
backgroundColor: ['rgba(0, 255, 0, 0.2)'],
borderColor: [ 'rgba(0, 255, 0,0.7)'],
borderWidth: 1
},
{
label: '第3月',
data: [45, 10, 13, 20, 12, 23],
backgroundColor: ['rgba(0, 0, 255, 0.2)'],
borderColor: [ 'rgba(0, 0, 255,0.7)'],
borderWidth: 1
}
]
},
options: {
title:{
display:true,
text:'折线图',
fontSize:20,
fontColor:'red'
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
var ctx = document.getElementById("myChart2");
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 0, 0, 0.2)',
'rgba(255, 100, 200, 0.2)',
'rgba(255, 200, 0, 0.2)',
'rgba(255,250, 0, 0.2)',
'rgba(255, 150, 0, 0.2)',
'rgba(255, 0, 200, 0.2)',
],
borderColor: [
'rgba(255, 0, 0,0.7)',
'rgba(255, 100, 200, 0.7)',
'rgba(255, 200, 0, 0.7)',
'rgba(255,250, 0, 0.7)',
'rgba(255, 150, 0, 0.7)',
'rgba(255, 0, 200, 0.7)',
],
borderWidth: 1
}
]
},
options: {
title:{
display:true,
text:'饼状图',
fontSize:20,
fontColor:'red'
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
var ctx = document.getElementById("myChart3");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: '第1月',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 0, 0, 0.2)',
'rgba(255, 0, 0, 0.2)',
'rgba(255, 0, 0, 0.2)',
'rgba(255, 0, 0, 0.2)',
'rgba(255, 0, 0, 0.2)',
'rgba(255, 0, 0, 0.2)',
],
borderColor: [
'rgba(255, 0, 0,0.7)',
'rgba(255, 0, 0,0.7)',
'rgba(255, 0, 0,0.7)',
'rgba(255, 0, 0,0.7)',
'rgba(255, 0, 0,0.7)',
'rgba(255, 0, 0,0.7)',
],
borderWidth: 1
},
{
label: '第2月',
data: [15, 12, 3, 50, 12, 43],
backgroundColor: [
'rgba(0, 255, 0, 0.2)',
'rgba(0, 255, 0, 0.2)',
'rgba(0, 255, 0, 0.2)',
'rgba(0, 255, 0, 0.2)',
'rgba(0, 255, 0, 0.2)',
'rgba(0, 255, 0, 0.2)',
],
borderColor: [
'rgba(0, 255, 0,0.7)',
'rgba(0, 255, 0,0.7)',
'rgba(0, 255, 0,0.7)',
'rgba(0, 255, 0,0.7)',
'rgba(0, 255, 0,0.7)',
'rgba(0, 255, 0,0.7)',
'rgba(0, 255, 0,0.7)',
],
borderWidth: 1
},
{
label: '第2月',
data: [60, 80, 20, 12, 25, 60],
backgroundColor: [
'rgba(0, 0, 255, 0.2)',
'rgba(0, 0, 255, 0.2)',
'rgba(0, 0, 255, 0.2)',
'rgba(0, 0, 255, 0.2)',
'rgba(0, 0, 255, 0.2)',
'rgba(0, 0, 255, 0.2)',
],
borderColor: [
'rgba(0, 0, 255,0.7)',
'rgba(0, 0, 255,0.7)',
'rgba(0, 0, 255,0.7)',
'rgba(0, 0, 255,0.7)',
'rgba(0, 0, 255,0.7)',
'rgba(0, 0, 255,0.7)',
],
borderWidth: 1
}
]
},
options: {
title:{
display:true,
text:'柱状图',
fontSize:20,
fontColor:'red'
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
var ctx = document.getElementById("myChart4");
var myChart = new Chart(ctx, {
type: 'radar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
//一个{}表示一组数据的数据结构
{
fill:false, //是否充满
pointStyle:'triangle', //点的风格 'circle', 'triangle', 'rect', 'rectRot', 'cross', 'crossRot', 'star', 'line'
label: '第1月', //标签
data: [12, 19, 3, 5, 2, 3], //数据
backgroundColor: ['rgba(255, 0, 0, 0.5)'], //曲线与X轴之间的颜色
borderColor: ['rgba(255, 0, 0,0.7)'], //曲线的颜色
piontColor:['rgba(0,255,0,1)'], //点的颜色
pointBackgroundColor: "rgba(255,0,0,1)", //填充点的颜色
borderWidth:2, //曲线的宽度
showLine:true //曲线是显示与否
},
{
label: '第2月',
pointStyle:'rect',
data: [15, 12, 3, 50, 12, 43],
backgroundColor: ['rgba(0, 255, 0, 0.2)'],
borderColor: [ 'rgba(0, 255, 0,0.7)'],
borderWidth: 1
},
{
label: '第3月',
pointStyle:'star',
data: [45, 10, 13, 20, 12, 23],
backgroundColor: ['rgba(0, 0, 255, 0.2)'],
borderColor: [ 'rgba(0, 0, 255,0.7)'],
borderWidth: 1
}
]
},
options: {
title:{
display:true,
text:'雷达图',
fontSize:20,
fontColor:'red'
},
scales: {
xAxes:[{ //X轴属性
display:true //坐标轴是否显示
}],
yAxes: [{ //Y轴属性
display:true,
stacked:false, //是否堆叠
ticks: {
beginAtZero:true
}
}]
}
}
});
var ctx = document.getElementById("myChart5");
var myChart = new Chart(ctx, {
type: 'polarArea',
data: {
datasets: [{
data: [11,16,7,3,14],
backgroundColor: [
"#FF6384",
"#4BC0C0",
"#FFCE56",
"#E7E9ED",
"#36A2EB"
],
label: 'My dataset' // for legend
}],
labels: [
"Red",
"Green",
"Yellow",
"Grey",
"Blue"
]
},
options: {
title:{
display:true,
text:'极地图',
fontSize:20,
fontColor:'red'
},
elements: {
points: {
borderWidth: 1,
borderColor: 'rgb(0, 0, 0)'
}
}
}
});
var ctx = document.getElementById("myChart6");
var myChart = new Chart(ctx, {
type: 'pie',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
data: [12, 19, 3, 5, 2, 3],
hoverBorderWidth:5,
hoverBackgroundColor:
[
'rgba(255, 0, 0, 0.8)',
'rgba(255, 100, 200, 0.8)',
'rgba(255, 200, 0, 0.8)',
'rgba(255,250, 0, 0.8)',
'rgba(255, 150, 0, 0.8)',
'rgba(255, 0, 200, 0.8)',
],
backgroundColor: [
'rgba(255, 0, 0, 0.2)',
'rgba(255, 100, 200, 0.2)',
'rgba(255, 200, 0, 0.2)',
'rgba(255,250, 0, 0.2)',
'rgba(255, 150, 0, 0.2)',
'rgba(255, 0, 200, 0.2)',
],
borderColor: [
'rgba(255, 0, 0,0.7)',
'rgba(255, 100, 200, 0.7)',
'rgba(255, 200, 0, 0.7)',
'rgba(255,250, 0, 0.7)',
'rgba(255, 150, 0, 0.7)',
'rgba(255, 0, 200, 0.7)',
],
borderWidth: 1
}
]
},
options: {
title:{
display:true,
text:'环状图',
fontSize:20,
fontColor:'red'
},
position:'right',
cutoutPercentage:30, //圆心半径
rotation:0,
animation:{
animateScale:true, //是否缩放
animateRotate:true //是否旋转
},
scales: {
yAxes: [{
ticks: { //标记
beginAtZero:true, //从那边开始
}
}]
}
}
});
</script>
</body>
</html>
有啥发现记得留言告诉我哦!^_^