Chart.js是一个简单、面向对象、为设计者和开发者准备的图表绘制工具库。它非常简单帮你展现各种数据的图表展现形式,我们今天看看如何引入和基本的操作,并用它绘制一个制曲线图(Line chart)。
Chart.js特性
1、6种图表类型
Chart.js帮你用不同的方式让你的数据变得可视化。每种类型的图表都有动画效果,并且看上去非常棒,即便是在retina屏幕上。
- ◆ 曲线图(Line charts)
- ◆ 柱状图(Bar charts)
- ◆ 雷达图或蛛网图(Radar charts)
- ◆ 饼图(Pie charts)
- ◆ 极地区域图(Polar area charts)
- ◆ 环形图(Doughnut charts)
2、基于HTML5
Chart.js基于HTML5 canvas技术,支持所有现代浏览器,并且针对IE7/8提供了降级替代方案。
3、简单、灵活
Chart.js不依赖任何外部工具库,轻量级(压缩之后仅有4.5k),并且提供了加载外部参数的方法。
引入Chart.js文件
首先我们需要在页面中引入Chart.js文件。此工具库在全局命名空间中定义了Chart变量。
<script src="Chart.js"></script>
创建图表
为了创建图表,我们要实例化一个Chart对象。为了完成前面的步骤,首先需要需要传入一个绘制图表的2d context。以下是案例。
Html:
<canvas id="myChart" width="400" height="400"></canvas>
Javascript:
//Get the context of the canvas element we want to selectvar ctx = document.getElementById("myChart").getContext("2d");
var myNewChart = new Chart(ctx).PolarArea(data);
我们还可以用jQuery获取canvas的context。首先从jQuery集合中获取我们需要的DOM节点,然后在这个DOM节点上调用 getContext("2d") 方法。
Javascript:
//Get context with jQuery - using jQuery's .get() method.var ctx = $("#myChart").get(0).getContext("2d");
//This will get the first returned node in the jQuery collection.var myNewChart = new Chart(ctx);
当我们完成了在指定的canvas上实例化Chart对象之后,Chart.js会自动针对retina屏幕做缩放。
Chart对象设置完成后,我们就可以继续创建Chart.js中提供的具体类型的图表了。下面这个案例中,我们将展示如何绘制一幅极地区域图(Polar area chart)。
Javascript:
new Chart(ctx).PolarArea(data,options);
曲线图(Line chart)
简介
曲线图就是将数据标于曲线上的一种图表。
一般用于展示趋势数据,和比较两组数据集。
使用案例

Javascript:
new Chart(ctx).Line(data,options);
数据结构
Javascript:
var data = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
data : [65,59,90,81,56,55,40]
},
{fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
data : [28,48,40,19,96,27,100]
}]}
图表参数
Javascript:
Line.defaults = {
//Boolean - If we show the scale above the chart datascaleOverlay : false,
//Boolean - If we want to override with a hard coded scalescaleOverride : false,
//** Required if scaleOverride is true **//Number - The number of steps in a hard coded scalescaleSteps : null,
//Number - The value jump in the hard coded scalescaleStepWidth : null,
//Number - The scale starting valuescaleStartValue : null,
//String - Colour of the scale linescaleLineColor : "rgba(0,0,0,.1)",
//Number - Pixel width of the scale linescaleLineWidth : 1,
//Boolean - Whether to show labels on the scalescaleShowLabels : false,
//Interpolated JS string - can access valuescaleLabel : "<%=value%>",
//String - Scale label font declaration for the scale labelscaleFontFamily : "'Arial'",
//Number - Scale label font size in pixelsscaleFontSize : 12,
//String - Scale label font weight stylescaleFontStyle : "normal",
//String - Scale label font colourscaleFontColor : "#666",
///Boolean - Whether grid lines are shown across the chartscaleShowGridLines : true,
//String - Colour of the grid linesscaleGridLineColor : "rgba(0,0,0,.05)",
//Number - Width of the grid linesscaleGridLineWidth : 1,
//Boolean - Whether the line is curved between pointsbezierCurve : true,
//Boolean - Whether to show a dot for each pointpointDot : true,
//Number - Radius of each point dot in pixelspointDotRadius : 3,
//Number - Pixel width of point dot strokepointDotStrokeWidth : 1,
//Boolean - Whether to show a stroke for datasetsdatasetStroke : true,
//Number - Pixel width of dataset strokedatasetStrokeWidth : 2,
//Boolean - Whether to fill the dataset with a colourdatasetFill : true,
//Boolean - Whether to animate the chartanimation : true,
//Number - Number of animation stepsanimationSteps : 60,
//String - Animation easing effectanimationEasing : "easeOutQuart",
//Function - Fires when the animation is completeonAnimationComplete : null
}
所属分类: JavaScript/jquery
相关文章:
▪ 编写规范简洁高效优美JavaScript代码2016-07-26
▪ Htlm5本地存储和连线时段储存应用举例2016-07-21
▪ Underscore.js去弥补jQuery 没有实现的功能2016-07-23
▪ Unslider最棒的jQuery轮播插件|511遇见强烈推荐2016-01-21
▪ Jquery+Bootstrap实现Collapse菜单源码(手风琴折叠效果)2016-10-07
▪ wordpress侧边jQuery返回顶部代码2016-01-01
▪ wordpress添加响应旋转木马flexisel.js效果2017-02-09
▪ WordPress 3D旋转彩色标签云2015-12-15
▪ jQuery.Pin 固定页面元素的jQuery插件2016-02-28
▪ Headroom.js固定页头(导航条)利器|511遇见强烈推荐2016-02-22
