原文链接:http://www.bootcss.com/p/chart.js/docs/

引入Chart.js文件

首先我们需要在页面中引入Chart.js文件。此工具库在全局命名空间中定义了Chart变量。

<script src="Chart.js"></script>

创建图表

为了创建图表,我们要实例化一个Chart对象。为了完成前面的步骤,首先需要需要传入一个绘制图表的2d context。以下是案例。

<canvas id="myChart" width="400" height="400"></canvas>
//Get the context of the canvas element we want to select
var ctx = document.getElementById("myChart").getContext("2d");
var myNewChart = new Chart(ctx).PolarArea(data);

我们还可以用jQuery获取canvas的context。首先从jQuery集合中获取我们需要的DOM节点,然后在这个DOM节点上调用 getContext("2d") 方法。

//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)。

new Chart(ctx).PolarArea(data,options);

We call a method of the name of the chart we want to create. We pass in the data for that chart type, and the options for that chart as parameters. Chart.js will merge the options you pass in with the default options for that chart type.

曲线图(Line chart)

简介

曲线图就是将数据标于曲线上的一种图表。

一般用于展示趋势数据,和比较两组数据集。

使用案例

new Chart(ctx).Line(data,options);

数据结构

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]
}
]
}

The line chart requires an array of labels for each of the data points. This is show on the X axis.

The data for line charts is broken up into an array of datasets. Each dataset has a colour for the fill, a colour for the line and colours for the points and strokes of the points. These colours are strings just like CSS. You can use RGBA, RGB, HEX or HSL notation.

图表参数

Line.defaults = {

	//Boolean - If we show the scale above the chart data
scaleOverlay : false, //Boolean - If we want to override with a hard coded scale
scaleOverride : false, //** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : null,
//Number - The value jump in the hard coded scale
scaleStepWidth : null,
//Number - The scale starting value
scaleStartValue : null, //String - Colour of the scale line
scaleLineColor : "rgba(0,0,0,.1)", //Number - Pixel width of the scale line
scaleLineWidth : 1, //Boolean - Whether to show labels on the scale
scaleShowLabels : false, //Interpolated JS string - can access value
scaleLabel : "<%=value%>", //String - Scale label font declaration for the scale label
scaleFontFamily : "'Arial'", //Number - Scale label font size in pixels
scaleFontSize : 12, //String - Scale label font weight style
scaleFontStyle : "normal", //String - Scale label font colour
scaleFontColor : "#666", ///Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true, //String - Colour of the grid lines
scaleGridLineColor : "rgba(0,0,0,.05)", //Number - Width of the grid lines
scaleGridLineWidth : 1, //Boolean - Whether the line is curved between points
bezierCurve : true, //Boolean - Whether to show a dot for each point
pointDot : true, //Number - Radius of each point dot in pixels
pointDotRadius : 3, //Number - Pixel width of point dot stroke
pointDotStrokeWidth : 1, //Boolean - Whether to show a stroke for datasets
datasetStroke : true, //Number - Pixel width of dataset stroke
datasetStrokeWidth : 2, //Boolean - Whether to fill the dataset with a colour
datasetFill : true, //Boolean - Whether to animate the chart
animation : true, //Number - Number of animation steps
animationSteps : 60, //String - Animation easing effect
animationEasing : "easeOutQuart", //Function - Fires when the animation is complete
onAnimationComplete : null }

柱状图(Bar chart)

简介

A bar chart is a way of showing data as bars.

It is sometimes used to show trend data, and the comparison of multiple data sets side by side.

使用案例

new Chart(ctx).Bar(data,options);

数据结构

var data = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
data : [65,59,90,81,56,55,40]
},
{
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
data : [28,48,40,19,96,27,100]
}
]
}

The bar chart has the a very similar data structure to the line chart, and has an array of datasets, each with colours and an array of data. Again, colours are in CSS format.

We have an array of labels too for display. In the example, we are showing the same data as the previous line chart example.

图表参数

Bar.defaults = {

	//Boolean - If we show the scale above the chart data
scaleOverlay : false, //Boolean - If we want to override with a hard coded scale
scaleOverride : false, //** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : null,
//Number - The value jump in the hard coded scale
scaleStepWidth : null,
//Number - The scale starting value
scaleStartValue : null, //String - Colour of the scale line
scaleLineColor : "rgba(0,0,0,.1)", //Number - Pixel width of the scale line
scaleLineWidth : 1, //Boolean - Whether to show labels on the scale
scaleShowLabels : false, //Interpolated JS string - can access value
scaleLabel : "<%=value%>", //String - Scale label font declaration for the scale label
scaleFontFamily : "'Arial'", //Number - Scale label font size in pixels
scaleFontSize : 12, //String - Scale label font weight style
scaleFontStyle : "normal", //String - Scale label font colour
scaleFontColor : "#666", ///Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true, //String - Colour of the grid lines
scaleGridLineColor : "rgba(0,0,0,.05)", //Number - Width of the grid lines
scaleGridLineWidth : 1, //Boolean - If there is a stroke on each bar
barShowStroke : true, //Number - Pixel width of the bar stroke
barStrokeWidth : 2, //Number - Spacing between each of the X value sets
barValueSpacing : 5, //Number - Spacing between data sets within X values
barDatasetSpacing : 1, //Boolean - Whether to animate the chart
animation : true, //Number - Number of animation steps
animationSteps : 60, //String - Animation easing effect
animationEasing : "easeOutQuart", //Function - Fires when the animation is complete
onAnimationComplete : null }

雷达图或蛛网图(Radar chart)

简介

A radar chart is a way of showing multiple data points and the variation between them.

They are often useful for comparing the points of two or more different data sets

使用案例

new Chart(ctx).Radar(data,options);

数据结构

var data = {
labels : ["Eating","Drinking","Sleeping","Designing","Coding","Partying","Running"],
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]
}
]
}

For a radar chart, usually you will want to show a label on each point of the chart, so we include an array of strings that we show around each point in the chart. If you do not want this, you can either not include the array of labels, or choose to hide them in the chart options.

For the radar chart data, we have an array of datasets. Each of these is an object, with a fill colour, a stroke colour, a colour for the fill of each point, and a colour for the stroke of each point. We also have an array of data values.

图表

Radar.defaults = {

	//Boolean - If we show the scale above the chart data
scaleOverlay : false, //Boolean - If we want to override with a hard coded scale
scaleOverride : false, //** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : null,
//Number - The value jump in the hard coded scale
scaleStepWidth : null,
//Number - The centre starting value
scaleStartValue : null, //Boolean - Whether to show lines for each scale point
scaleShowLine : true, //String - Colour of the scale line
scaleLineColor : "rgba(0,0,0,.1)", //Number - Pixel width of the scale line
scaleLineWidth : 1, //Boolean - Whether to show labels on the scale
scaleShowLabels : false, //Interpolated JS string - can access value
scaleLabel : "<%=value%>", //String - Scale label font declaration for the scale label
scaleFontFamily : "'Arial'", //Number - Scale label font size in pixels
scaleFontSize : 12, //String - Scale label font weight style
scaleFontStyle : "normal", //String - Scale label font colour
scaleFontColor : "#666", //Boolean - Show a backdrop to the scale label
scaleShowLabelBackdrop : true, //String - The colour of the label backdrop
scaleBackdropColor : "rgba(255,255,255,0.75)", //Number - The backdrop padding above & below the label in pixels
scaleBackdropPaddingY : 2, //Number - The backdrop padding to the side of the label in pixels
scaleBackdropPaddingX : 2, //Boolean - Whether we show the angle lines out of the radar
angleShowLineOut : true, //String - Colour of the angle line
angleLineColor : "rgba(0,0,0,.1)", //Number - Pixel width of the angle line
angleLineWidth : 1, //String - Point label font declaration
pointLabelFontFamily : "'Arial'", //String - Point label font weight
pointLabelFontStyle : "normal", //Number - Point label font size in pixels
pointLabelFontSize : 12, //String - Point label font colour
pointLabelFontColor : "#666", //Boolean - Whether to show a dot for each point
pointDot : true, //Number - Radius of each point dot in pixels
pointDotRadius : 3, //Number - Pixel width of point dot stroke
pointDotStrokeWidth : 1, //Boolean - Whether to show a stroke for datasets
datasetStroke : true, //Number - Pixel width of dataset stroke
datasetStrokeWidth : 2, //Boolean - Whether to fill the dataset with a colour
datasetFill : true, //Boolean - Whether to animate the chart
animation : true, //Number - Number of animation steps
animationSteps : 60, //String - Animation easing effect
animationEasing : "easeOutQuart", //Function - Fires when the animation is complete
onAnimationComplete : null }

极地区域图(Polar area chart)

简介

Polar area charts are similar to pie charts, but each segment has the same angle - the radius of the segment differs depending on the value.

This type of chart is often useful when we want to show a comparison data similar to a pie chart, but also show a scale of values for context.

使用案例

new Chart(ctx).PolarArea(data,options);

数据结构

var data = [
{
value : 30,
color: "#D97041"
},
{
value : 90,
color: "#C7604C"
},
{
value : 24,
color: "#21323D"
},
{
value : 58,
color: "#9D9B7F"
},
{
value : 82,
color: "#7D4F6D"
},
{
value : 8,
color: "#584A5E"
}
]

As you can see, for the chart data you pass in an array of objects, with a value and a colour. The value attribute should be a number, while the color attribute should be a string. Similar to CSS, for this string you can use HEX notation, RGB, RGBA or HSL.

图表

These are the default chart options. By passing in an object with any of these attributes, Chart.js will merge these objects and the graph accordingly. Explanations of each option are commented in the code below.

PolarArea.defaults = {

	//Boolean - Whether we show the scale above or below the chart segments
scaleOverlay : true, //Boolean - If we want to override with a hard coded scale
scaleOverride : false, //** Required if scaleOverride is true **
//Number - The number of steps in a hard coded scale
scaleSteps : null,
//Number - The value jump in the hard coded scale
scaleStepWidth : null,
//Number - The centre starting value
scaleStartValue : null, //Boolean - Show line for each value in the scale
scaleShowLine : true, //String - The colour of the scale line
scaleLineColor : "rgba(0,0,0,.1)", //Number - The width of the line - in pixels
scaleLineWidth : 1, //Boolean - whether we should show text labels
scaleShowLabels : true, //Interpolated JS string - can access value
scaleLabel : "<%=value%>", //String - Scale label font declaration for the scale label
scaleFontFamily : "'Arial'", //Number - Scale label font size in pixels
scaleFontSize : 12, //String - Scale label font weight style
scaleFontStyle : "normal", //String - Scale label font colour
scaleFontColor : "#666", //Boolean - Show a backdrop to the scale label
scaleShowLabelBackdrop : true, //String - The colour of the label backdrop
scaleBackdropColor : "rgba(255,255,255,0.75)", //Number - The backdrop padding above & below the label in pixels
scaleBackdropPaddingY : 2, //Number - The backdrop padding to the side of the label in pixels
scaleBackdropPaddingX : 2, //Boolean - Stroke a line around each segment in the chart
segmentShowStroke : true, //String - The colour of the stroke on each segement.
segmentStrokeColor : "#fff", //Number - The width of the stroke value in pixels
segmentStrokeWidth : 2, //Boolean - Whether to animate the chart or not
animation : true, //Number - Amount of animation steps
animationSteps : 100, //String - Animation easing effect.
animationEasing : "easeOutBounce", //Boolean - Whether to animate the rotation of the chart
animateRotate : true, //Boolean - Whether to animate scaling the chart from the centre
animateScale : false, //Function - This will fire when the animation of the chart is complete.
onAnimationComplete : null
}

饼图(Pie chart)

简介

Pie charts are probably the most commonly used chart there are. They are divided into segments, the arc of each segment shows a the proportional value of each piece of data.

They are excellent at showing the relational proportions between data.

使用案例

new Chart(ctx).Pie(data,options);

数据结构

var data = [
{
value: 30,
color:"#F38630"
},
{
value : 50,
color : "#E0E4CC"
},
{
value : 100,
color : "#69D2E7"
}
]

For a pie chart, you must pass in an array of objects with a value and a color property. The value attribute should be a number, Chart.js will total all of the numbers and calculate the relative proportion of each. The color attribute should be a string. Similar to CSS, for this string you can use HEX notation, RGB, RGBA or HSL.

图表

These are the default options for the Pie chart. Pass in an object with any of these attributes to override them.

Pie.defaults = {
//Boolean - Whether we should show a stroke on each segment
segmentShowStroke : true, //String - The colour of each segment stroke
segmentStrokeColor : "#fff", //Number - The width of each segment stroke
segmentStrokeWidth : 2, //Boolean - Whether we should animate the chart
animation : true, //Number - Amount of animation steps
animationSteps : 100, //String - Animation easing effect
animationEasing : "easeOutBounce", //Boolean - Whether we animate the rotation of the Pie
animateRotate : true, //Boolean - Whether we animate scaling the Pie from the centre
animateScale : false, //Function - Will fire on animation completion.
onAnimationComplete : null
}

环形图(Doughnut chart)

简介

Doughnut charts are similar to pie charts, however they have the centre cut out, and are therefore shaped more like a doughnut than a pie!

They are aso excellent at showing the relational proportions between data.

使用案例

new Chart(ctx).Doughnut(data,options);

数据结构

var data = [
{
value: 30,
color:"#F7464A"
},
{
value : 50,
color : "#E2EAE9"
},
{
value : 100,
color : "#D4CCC5"
},
{
value : 40,
color : "#949FB1"
},
{
value : 120,
color : "#4D5360"
} ]

For a doughnut chart, you must pass in an array of objects with a value and a color property. The value attribute should be a number, Chart.js will total all of the numbers and calculate the relative proportion of each. The color attribute should be a string. Similar to CSS, for this string you can use HEX notation, RGB, RGBA or HSL.

图表

These are the default options for the doughnut chart. Pass in an object with any of these attributes to override them.

Doughnut.defaults = {
//Boolean - Whether we should show a stroke on each segment
segmentShowStroke : true, //String - The colour of each segment stroke
segmentStrokeColor : "#fff", //Number - The width of each segment stroke
segmentStrokeWidth : 2, //The percentage of the chart that we cut out of the middle.
percentageInnerCutout : 50, //Boolean - Whether we should animate the chart
animation : true, //Number - Amount of animation steps
animationSteps : 100, //String - Animation easing effect
animationEasing : "easeOutBounce", //Boolean - Whether we animate the rotation of the Doughnut
animateRotate : true, //Boolean - Whether we animate scaling the Doughnut from the centre
animateScale : false, //Function - Will fire on animation completion.
onAnimationComplete : null
}

Chart.js docs的更多相关文章

  1. JS组件系列——开源免费图表组件:Chart.js

    前言:最近被开源免费得有点上火了,各种组件首先想到的就是是开源否.是否免费.是否和bootstrap风格一致.想着以后做报表肯定要用到图表组件的,于是在Bootstrap中文网上面找到了Chart.j ...

  2. chart.js图表库案例赏析,饼图添加文字

    chart.js图表库案例赏析,饼图添加文字 Chart.js 是一个令人印象深刻的 JavaScript 图表库,建立在 HTML5 Canvas 基础上.目前,它支持6种图表类型(折线图,条形图, ...

  3. chart.js在html中画曲线图

    http://www.bootcss.com/p/chart.js/docs/ http://www.chartjs.org/docs/   中有详细讲解 一.简介 Chart.js是一个基于HTML ...

  4. vue.js 图表chart.js使用

    在使用这个chart.js之前,自己写过一个饼图,总之碰到的问题不少,所以能用现成的插件就用,能节省不少时间 这里不打算介绍chart.js里面详细的参数意义和各个参数的用法,只作为首次使用chart ...

  5. chart.js使用常见问题

    Chart.js是一个简单.面向对象.为设计者和开发者准备的图表绘制工具库. 在使用过程中新手可能会遇到很多问题导致图标无法显示.下面我们来看一下在使用过程中可能会遇到的问题. 刚开始用chart.j ...

  6. Chart.js 與 ASP.NET MVC 整合應用

    Chart.js 是一套開放原始碼的「圖表」繪製函式庫,和其他第三方的圖表工具相比,Chart.js 的特色如下: 支援 HTML 5.響應式網頁 (RWD, Responsive Web Desig ...

  7. Chart.js Y轴数据以百分比展示

    新手一枚,解决的问题喜欢记录,也许正好有人在网上迷茫的百度着.-0- 最近使用Chart.js做折线图的报表展示,直接显示整数啥的很好弄毕竟例子直接在哪里可以用,百分比就没办法了.百度慢慢汲取营养,虽 ...

  8. angulajs中引用chart.js做报表,修改线条样式

    目前还有个问题,在手机上看,当折线y轴值超过1000,会有点问题 1.下载chart js,可以用bower 命令下载 http://www.chartjs.org/docs/#line-chart- ...

  9. Chart.js & CPU 性能监控

    Chart.js 可视化动态 CPU 性能监控 https://github.com/gildata/RAIO/issues/337 https://github.com/chartjs/Chart. ...

随机推荐

  1. Python 双向队列Deque、单向队列Queue 模块使用详解

    Python 双向队列Deque 模块使用详解 创建双向队列Deque序列 双向队列Deque提供了类似list的操作方法: #!/usr/bin/python3 import collections ...

  2. CLR Via CSharp读书笔记(26) - 计算限制的异步操作

    执行上下文: 执行上下文包括安全设置(压缩栈.Thread的Principal属性和Windows身份),宿主设置(System.Threading.HostExecutionContextManag ...

  3. CF651B-Beautiful Paintings

    B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. 什么是Service Mesh?

    转至大佬宋净明的博客:https://jimmysong.io/posts/what-is-a-service-mesh/ Service mesh 又译作 “服务网格”,作为服务间通信的基础设施层. ...

  5. SpringMVC(8) - 处理器映射

    在以前的Spring版本中,用户需要在Web应用程序上下文中定义一个或多个HandlerMapping bean,以将传入的Web请求映射到适当的处理器.通过引入带注解的控制器,就不需要像之前那样定义 ...

  6. [luoguP1360] [USACO07MAR]黄金阵容均衡Gold Balanced L…

    传送门 真的骚的一个题,看了半天只会个前缀和+暴力.. 纯考思维.. 良心题解 #include <cstdio> #include <cstring> #include &l ...

  7. hdu 2579

    #include<stdio.h> #include<queue> #include<iostream> #include<string.h> #inc ...

  8. 洛谷P2814 家谱(gen)

    题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. 输入输出格式 输入格式: 输入由多行组成,首先是一系列有关父子关系的描述,其中每一组 ...

  9. msp430项目编程30

    msp430中项目---电压检测系统 1.SVS工作原理 2.电路工作原理 3.代码(显示部分) 4.代码(功能实现) 5.项目总结

  10. 2017多校Round5(hdu6085~hdu6095)

    补题进度:7/11 1001(模意义下的卷积) 题意: 给出长度<=50000的两个数组A[] B[],保证数组中的值<=50000且A[]中数字两两不同,B[]中数字两两不同 有5000 ...