使用Chart.js在网页上画图的学习

提供网站:http://www.bootcss.com/p/chart.js/docs/

HTML练习代码,对于IE,只支持IE8及以上版本:

需要自己在网上下载Chart.min.js,excanvas.js  ,jquery-1.8.2.min.js

<!DOCTYPE html>

<html>

<head>

<meta name="viewport" content="width=device-width" />

<title>Index</title>

<script src="~/Scripts/jquery-1.8.2.min.js"></script>

<script src="~/Scripts/Chart.min.js"></script>

<!--[if lte IE 8]>

<script src="~/Scripts/excanvas.js"></script>

<![endif]-->

<script type="text/javascript">

var randomScalingFactor = function () { return Math.round(Math.random() * 100) };//生成随机数字

var lineChartData = {

labels: ["January", "February", "March", "April", "May", "June", "July"],//图上的各点(X坐标)

datasets: [

{

label: "My First dataset",

fillColor: "rgba(220,220,220,0.2)",

strokeColor: "rgba(220,220,220,1)",

pointColor: "rgba(220,220,220,1)",

pointStrokeColor: "#fff",

pointHighlightFill: "#fff",

pointHighlightStroke: "rgba(220,220,220,1)",

data: [40, 45, 50, 30, 20, 10, 0]//趋势图,线上各点的值(Y坐标)

},

{

label: "My Second dataset",

fillColor: "rgba(151,187,205,0.2)",

strokeColor: "rgba(151,187,205,1)",

pointColor: "rgba(151,187,205,1)",

pointStrokeColor: "#fff",

pointHighlightFill: "#fff",

pointHighlightStroke: "rgba(151,187,205,1)",

data: [25, 30, 35, 50, 65, 68, 80]//趋势图,线上各点的值(Y坐标)

}

]

}

//画趋势图

var chart1 = function () {

var ctx = document.getElementById("canvas").getContext("2d");

window.myLine = new Chart(ctx).Line(lineChartData, {

responsive: true

});

}

//百分比分布(环形)图数据

var doughnutData = [

{

value: 300,

color: "#F7464A",

highlight: "#FF5A5E",

label: "Red"

},

{

value: 50,

color: "#46BFBD",

highlight: "#5AD3D1",

label: "Green"

},

{

value: 100,

color: "#FDB45C",

highlight: "#FFC870",

label: "Yellow"

},

{

value: 40,

color: "#949FB1",

highlight: "#A8B3C5",

label: "Grey"

},

{

value: 120,

color: "#4D5360",

highlight: "#616774",

label: "Dark Grey"

}

];

//画百分比分布(环形)图

var chart2 = function () {

var ctx = document.getElementById("canvas").getContext("2d");

window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, { responsive: true });

};

//柱状图数据

var barChartData = {

labels: ["January", "February", "March", "April", "May", "June", "July"],

datasets: [

{

fillColor: "rgba(220,220,220,0.5)",

strokeColor: "rgba(220,220,220,0.8)",

highlightFill: "rgba(220,220,220,0.75)",

highlightStroke: "rgba(220,220,220,1)",

data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]

},

{

fillColor: "rgba(151,187,205,0.5)",

strokeColor: "rgba(151,187,205,0.8)",

highlightFill: "rgba(151,187,205,0.75)",

highlightStroke: "rgba(151,187,205,1)",

data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]

}

]

}

//画柱状图

var chart3 = function () {

var ctx = document.getElementById("canvas").getContext("2d");

window.myBar = new Chart(ctx).Bar(barChartData, {

responsive: true

});

};

//百分比分布图数据(饼状)

var pieData = [

{

value: 300,

color: "#F7464A",

highlight: "#FF5A5E",

label: "Red"

},

{

value: 50,

color: "#46BFBD",

highlight: "#5AD3D1",

label: "Green"

},

{

value: 100,

color: "#FDB45C",

highlight: "#FFC870",

label: "Yellow"

},

{

value: 40,

color: "#949FB1",

highlight: "#A8B3C5",

label: "Grey"

},

{

value: 120,

color: "#4D5360",

highlight: "#616774",

label: "Dark Grey"

}

];

//画百分比分布图(饼状)

var chart4 = function () {

var ctx = document.getElementById("canvas").getContext("2d");

window.myPie = new Chart(ctx).Pie(pieData);

};

//扇形图数据

var polarData = [

{

value: 300,

color: "#F7464A",

highlight: "#FF5A5E",

label: "Red"

},

{

value: 50,

color: "#46BFBD",

highlight: "#5AD3D1",

label: "Green"

},

{

value: 100,

color: "#FDB45C",

highlight: "#FFC870",

label: "Yellow"

},

{

value: 40,

color: "#949FB1",

highlight: "#A8B3C5",

label: "Grey"

},

{

value: 120,

color: "#4D5360",

highlight: "#616774",

label: "Dark Grey"

}

];

//画扇形图

var chart5 = function () {

var ctx = document.getElementById("canvas").getContext("2d");

window.myPolarArea = new Chart(ctx).PolarArea(polarData, {

responsive: true

});

};

//多边形数据对比图数据

var radarChartData = {

labels: ["Eating", "Drinking", "Sleeping", "Designing", "Coding", "Cycling", "Running"],

datasets: [

{

label: "My First dataset",

fillColor: "rgba(220,220,220,0.2)",

strokeColor: "rgba(220,220,220,1)",

pointColor: "rgba(220,220,220,1)",

pointStrokeColor: "#fff",

pointHighlightFill: "#fff",

pointHighlightStroke: "rgba(220,220,220,1)",

data: [65, 59, 90, 81, 56, 55, 40]

},

{

label: "My Second dataset",

fillColor: "rgba(151,187,205,0.2)",

strokeColor: "rgba(151,187,205,1)",

pointColor: "rgba(151,187,205,1)",

pointStrokeColor: "#fff",

pointHighlightFill: "#fff",

pointHighlightStroke: "rgba(151,187,205,1)",

data: [28, 48, 40, 19, 96, 27, 100]

}

]

};

//画多边形数据对比图

var chart6 = function () {

window.myRadar = new Chart(document.getElementById("canvas").getContext("2d")).Radar(radarChartData, {

responsive: true

});

};

$(function () {

chart4();

})

</script>

</head>

<body>

<div style="width:30%">

<div>

<canvas id="canvas" height="450" width="600"></canvas>

</div>

</div>

</body>

</html>

饼状图显示效果:

时间: 2025-01-14 01:10:10

使用Chart.js在网页上画图的学习的相关文章

用js在网页上完成倒计时3秒后自动跳转到另一个页面

<body> <div id="time"></div> <a href="#" onclick="stop()">停止</a> <script type="text/javascript"> var i=3; function changeTime(){ document.getElementById("time").innerHTM

JS让网页上文字出现键盘打字的打字效果

一个挺简单的网页特效:JS让网页上文字出现键盘打字的打字效果实现 演示地址:http://codepen.io/guihailiuli/pen/jPOYMZ 以代码形式实现过程分析: <html> <head> <title>打字效果</title> <meta http-equiv="Content-Type" Content="text/html;charset=gb2312" /> <style

原生JS在网页上复制的所有文字后面自动加上一段版权声明

不少技术博客有这样的处理,当我们复制代码的时候,会自动加上一段本信息版权为XXXX,这是怎么实现的呢? 其实实现的方式很简单,可以在我的网站页面上绑定一个copy事件,当你复制文章内容的时候,自动在剪切板文字后面加上一段版权声明. 我这边是没有加这些的,代码什么的想复制就复制好了.大家都是靠代码技能吃饭的,可以体会写代码的不易,相信一定会尊重知识劳动成果——保留出处等版权信息的. 效果示例 : 代码示例: <body> <p>是的你没有看错, 我还是在路边吃快餐, 在北京市的三里屯

利用JS 在网页上获取并显示当前日期 星期

下边的HTML代码,可以取出日期与星期 <html><body><h1><script language=JavaScript>var d, s = "";var x = new Array("星期日", "星期一", "星期二","星期三","星期四", "星期五","星期六");d = new D

chart.js轻量级图表插件使用

安装下载 下载地址:https://github.com/chartjs/Chart.js/releases/latest cdn LINK:https://cdnjs.com/libraries/Chart.js 文档:http://chartjs.cn/docs/ Chart.js有两种不同的版本.常规版本的Chart.js和 Chart.min.js,附带chart.js库的同时还带着颜色解析器.如果你想使用这个版本并且在你的图表中使用时间轴,你需要在安装Chart.js之前安装上Mome

Chart.js: 一个简单的 JS Chart Library

Chart.js 是一个 Open Source 的 JavaScript Chart Library.它一共有 6 中 Chart,全都是 HTML5 based. 底下是 Chart.js 所提供的 6 中 Charts的实例图形,包括了折线图.柱形图.圆饼图等,这样很容易在html网页中实现动态图的效果: Line Chart Bar Chart Radar Chart Pie Chart Polar Area Chart Doughnut Chart 快速上手 你可以到 github下载

Html5之高级-7 HTML5 Chart.js(概述、入门、使用)

一.Chart.js 概述 Chart.js 简介 - Chart.js 是一个简单.面向对象.为设计者和开发者准备的图表绘制工具库 - 官方地址: http://www.chartjs.org/ Chart.js 特点 - 基于 HTML 5 - Chart.js 基于 HTML5 canvas技术,支持所有现代浏览器,并且针对IE7/8提供了降级替代方案 - 简单.灵活 - Chart.js 不依赖任何外部工具库,轻量级(压缩之后仅有4.5k),并且提供了加载外部参数的方法 Chart.js

CSS3-基于浮动的布局,响应式WEB设计,定位网页上的元素,设计打印页面的css技术

基于浮动的布局: 1.除非图片设置了宽度,否则始终应该要对浮动的图片设置一个宽度,这样可以让浏览器给其他内容腾出环绕的空间 2.当侧边栏的高度与主内容区的高度不一致的时候,可以用个margin进行调整 3.在一个非浮动元素<div>中存在一个浮动元素,当浮动元素高于元素本身的时候,内容就会溢出<div>中,解决方案如下: A:在</div>介绍之前添加一个换行,并且添加一个类<br class=”clear”/>,之后创建一个样式br.class{clear

js获取网页的各种高度

网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWidth (包括边线的宽)网页可见区域高: document.body.offsetHeight (包括边线的高)网页正文全文宽: document.body.scrollWidth网页正文全文高: document.body.scrollHeight网页被卷去的高: document.body.sc