如何使用Chart.js来创建漂亮图表的所有知识

2022-11-17 0 226

如何使用Chart.js来创建漂亮图表的所有知识

Chart.js创建漂亮图表的所有知识

Chart.js 中文文档地址:http://www.bootcss.com/p/chart.js/docs/

 

如何使用Chart.js来创建漂亮图表的所有知识

引入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.

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!cheeksyu@vip.qq.com
2. 本站不保证所提供下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有积分奖励和额外收入!
5.严禁将资源用于任何违法犯罪行为,不得违反国家法律,否则责任自负,一切法律责任与本站无关

源码下载

发表评论
暂无评论