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

Chart.js 是一个令人印象深刻的 JavaScript 图表库,建立在 HTML5 Canvas 基础上。目前,它支持6种图表类型(折线图,条形图,雷达图,饼图,柱状图和极地区域区)。而且,这是一个独立的包,不依赖第三方 JavaScript 库,小于 5KB。

前天用了一下,由于以前也稍微用过,今天总结了一下(水平有限,如果问题,请不吝赐教):

开发中文文档:http://www.bootcss.com/p/chart.js/docs/

chart.js下载:

把所有的图标全都写了一遍,并把所有的可控属性(颜色等)均已随机数出现,大小自己控制;

这是所有的代码,只有一个Chart.js外部文件,引来即用:

<!doctype html>
<html>
<head>
<title>Chart</title>
<mate charset="utf-8"></mate>
<script src="Chart.js"></script>
</head>
<body> <div style="width:50%;margin: 0px auto;"> <!--柱状图开始-->
<div>
<h1>柱状图(Bar chart)</h1>
<canvas id="canvas-bar" height="450" width="600"></canvas>
</div> <script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var randomScalingFactor_255 = function(){ return Math.round(Math.random()*255)};
var radom_color = function(){
return '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6);
}
var barChartData = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
fillColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.5)",
strokeColor : "rgba(" + randomScalingFactor_255()+","+ randomScalingFactor_255()+"," + randomScalingFactor_255() + ",0.8)",
highlightFill: "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.75)",
highlightStroke: "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
fillColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.5)",
strokeColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.8)",
highlightFill : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.75)",
highlightStroke : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
] }
var ctx = document.getElementById("canvas-bar").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
responsive : true
});
</script>
<!--柱状图结束--> <!--曲线图(Line chart)开始-->
<div>
<h1>曲线图(Line chart)</h1>
<canvas id="canvas-line" height="450" width="600"></canvas>
</div>
<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var lineChartData = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
label: "My First dataset",
fillColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.2)",
strokeColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointStrokeColor : radom_color(),
pointHighlightFill : radom_color(),
pointHighlightStroke : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
label: "My Second dataset",
fillColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.2)",
strokeColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointStrokeColor : radom_color(),
pointHighlightFill : radom_color(),
pointHighlightStroke : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
label: "My First dataset",
fillColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.3)",
strokeColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointStrokeColor : '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6),
pointHighlightFill : '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6),
pointHighlightStroke : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
] }
var ctx = document.getElementById("canvas-line").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true
})
</script> <!--曲线图(Line chart)结束--> <!--雷达图或蛛网图(Radar chart)开始-->
<div>
<h1>雷达图或蛛网图(Radar chart)</h1>
<canvas id="canvas-radar" height="450" width="600"></canvas>
</div>
<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var radarData = {
labels : ["January","February","March","April","May","June","July"],
datasets : [
{
label: "My First dataset",
fillColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.2)",
strokeColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointStrokeColor : '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6),
pointHighlightFill : '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6),
pointHighlightStroke : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
},
{
label: "My Second dataset",
fillColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",0.2)",
strokeColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointColor : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
pointStrokeColor : '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6),
pointHighlightFill : '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6),
pointHighlightStroke : "rgba(" + randomScalingFactor_255() + "," + randomScalingFactor_255() + "," + randomScalingFactor_255() + ",1)",
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}, ] }
var ctx = document.getElementById("canvas-radar").getContext("2d");
window.myLine = new Chart(ctx).Radar(radarData, {
responsive: true
})
</script>
<!--雷达图或蛛网图(Radar chart)结束--> <!--极地区域图(Polar area chart)开始-->
<div>
<h1>极地区域图(Polar area chart)</h1>
<canvas id="canvas-polar" height="450" width="600"></canvas>
</div>
<script>
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
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 ctx = document.getElementById("canvas-polar").getContext("2d");
window.myLine = new Chart(ctx).PolarArea(polarData, {
responsive: true
})
</script>
<!--极地区域图(Polar area chart)结束--> <!--饼图(Pie chart)开始-->
<div>
<h1>饼图(Pie chart)</h1>
<canvas id="chart-pie" width="450" height="450"/>
</div>
<script> var pieData = [
{
value: 300,
color: radom_color(),
highlight: radom_color(),
label: "Red" },
{
value: 200,
color: radom_color(),
highlight: radom_color(),
label: "Green"
},
{
value: 100,
color: radom_color(),
highlight: radom_color(),
label: "Yellow"
},
{
value: 400,
color: radom_color(),
highlight: radom_color(),
label: "Grey"
},
{
value: 120,
color: radom_color(),
highlight: radom_color(),
label: "Dark Grey"
}
];
var ctx = document.getElementById("chart-pie").getContext("2d");
window.myPie = new Chart(ctx).Pie(pieData);
</script> <!--饼图(Pie chart)结束-->
<!--环形图(Doughnut chart)开始-->
<div style="width: 600px;height:600px;">
<h1>环形图(Doughnut chart)</h1>
<canvas id="chart-Doughnut" width="300" height="300"/>
</div>
<script>
var doughnutData = [
{
value: 1,
label: "One",
color:radom_color()
},
{
value: 2,
label: "Two",
color:radom_color()
},
{
value: 3,
label: "Three",
color:radom_color()
},
{
value: 4,
label: "Four",
color:radom_color()
},
{
value: 5,
label: "Five",
color:radom_color()
} ]; var ctx = document.getElementById("chart-Doughnut").getContext("2d");
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {responsive : true});
</script>
<!--环形图(Doughnut chart)结束--> </div> </body>
</html>

项目需要在饼图上写字(最后没用),找到了一个高手改过源码之后的文件:http://download.csdn.net/detail/renfufei/7102809

具体做法:

饼状图,添加文字
1. 修改的是 Chart.js,大致在 772行 编码,也可以搜索 renfufei@qq.com 
2. 示例是 samples/pie.html,使用的是 UTF-8编码

--
var pieData = [
{
value: 30,
color:"#F38630"
,text: "男生"
},
{
value : 50,
color : "#E0E4CC"
,text: "女生"
},
{
value : 100,
color : "#69D2E7"
,text: "男人"
} ];
--

chart.js可以完成功能,但是对于项目中的特殊需求并不能很好的实现~

 

chart.js图表库案例赏析,饼图添加文字的更多相关文章

  1. 手绘风格的 JS 图表库:Chart.xkcd

    本文作者:HelloGitHub-kalifun 图表库千万个今天 HelloGitHub 给大家推荐个很有"特色"的图表库:一个手绘风格的 JS 图表库 -- Chart.xkc ...

  2. Highcharts纯js图表库,以后可以跟客户说,你跟阿里云ECS用的图表库是同款

    Highcharts是一款纯javascript编写的图表库,能够很简便的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图.柱状图.饼图.散点图等多达18 ...

  3. 让IE8支持HTML5及canvas功能!chart.js图表绘制工具库IE8上兼容方案

    第一步,我们加上对html5的支持. <!--[if IE]> <script src="/public/html5.js" type="text/ja ...

  4. php 使用GD库压缩图片,添加文字图片水印

    先上一个工具类,提供了压缩,添加文字.图片水印等方法: image.class.php <?php class Image { private $info; private $image; pu ...

  5. 可能是史上最强大的js图表库——ECharts带你入门

    PS:之前的那篇博客Highcharts——让你的网页上图表画的飞起 ,评论中,花儿笑弯了腰 和 StanZhai 两位仁兄让我试试 ECharts ,去主页看到<Why ECharts ?&g ...

  6. 史上最强大的js图表库——ECharts带你入门(转)

    出处:http://www.cnblogs.com/zrtqsk/p/4019412.html PS:之前的那篇博客Highcharts——让你的网页上图表画的飞起 ,评论中,花儿笑弯了腰 和 Sta ...

  7. 关于highcharts(功能强大、开源、美观、图表丰富、兼容绝大多数浏览器的纯js图表库)

    官网http://www.hcharts.cn/ 引入下列文件 <script type="text/javascript" src="http://cdn.hch ...

  8. Highcharts 功能强大、开源、美观、图表丰富、兼容绝大多数浏览器的纯js图表库

    http://www.hcharts.cn/index.php 暂无介绍,等待后续补充

  9. hcharts中文网 一个js图表库

    http://www.bossidc.com/info/gongju/2013/0717/2133.html   hcharts源码包下载 http://www.hcharts.cn/demo/hig ...

随机推荐

  1. asp.net身份认证

    在网上看到几篇比较好的文章很详细讲解了Form.Membership.以及Identity身份认证 Form身份认证: http://www.cnblogs.com/fish-li/archive/2 ...

  2. CentOS 6破解Mysql5.5的办法

    首先,你必须要有操作系统的root权限了.要是连系统的root权限都没有的话,先考虑root系统再走下面的步骤.类似于安全模式登录系统,有人建议说是pkill mysql,但是我不建议哈.因为当你执行 ...

  3. 数据包判断是否丢包 ping+tracert+mtr

    1.用咱们最常用的Ping命令来查看是不是真的丢包了 这里可以看到数据包发送了4个,返回了4个,丢失=0  证明没有丢失 也有可能中间路由做了策略不给ICMP的回应 这样就ping没法判断了  正常情 ...

  4. PHP中数据库的连接

    <?php //1.链接MySQL服务器 $conn = mysql_connect("localhost", "root" , 199452); //2 ...

  5. try 返回前执行fianlly

    try catch  finally 语句中 如果try中有返回语句,如果在fianlly代码块中有对这个值修改的话,并不影响其放回值 public class Test { public stati ...

  6. 16)JAVA实现回调(Android,Swing中各类listener的实现)

           熟悉MS-Windows和X Windows事件驱动设计模式的开发人员,通常是把一个方法的指针传递给事件源,当某一事件发生时来调用这个方法(也称为"回调").Java ...

  7. 【原】RDD专题

    RDD是什么东西?在Spark中有什么作用?如何使用?  1.RDD是什么 (1)为什么会产生RDD? 传统的MapReduce虽然具有自动容错.平衡负载和可拓展性的优点,但是其最大缺点是采用非循环式 ...

  8. Map和HashMap

    通过查询JDK帮助文档,我们可以得知Map的说明.方法等 import java.util.Map; import java.util.HashMap; class Test{ public stat ...

  9. hdu 4324 Triangle LOVE

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4324 Triangle LOVE Description Recently, scientists f ...

  10. 基于opencv 的图片模糊判断代码

    #include"cv.h"  #include"highgui.h"  #include<iostream>  using namespace s ...