HTML5之Canvas绘图实例——饼状图

实现饼状分布画图(如下):调试环境:Firefox

页面代码:

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4
  5     <!-- basic styles -->
  6     <link href="assets/css/bootstrap.min.css" rel="stylesheet" />
  7     <link rel="stylesheet" href="assets/css/font-awesome.min.css" />
  8
  9     <!-- ace styles -->
 10     <link rel="stylesheet" href="assets/css/ace.min.css" />
 11
 12 </head>
 13
 14 <body>
 15     <div class="widget-box" style="width:60%;">
 16         <div class="widget-header widget-header-flat widget-header-small">
 17             <h5>
 18                 <i class="icon-signal"></i>
 19                 访问来源
 20             </h5>
 21
 22             <div class="widget-toolbar no-border">
 23                 <button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown">
 24                     本周
 25                     <i class="icon-angle-down icon-on-right bigger-110"></i>
 26                 </button>
 27
 28                 <ul class="dropdown-menu pull-right dropdown-125 dropdown-lighter dropdown-caret">
 29                     <li class="active">
 30                         <a href="#" class="blue">
 31                             <i class="icon-caret-right bigger-110">&nbsp;</i>
 32                             本周
 33                         </a>
 34                     </li>
 35
 36                     <li>
 37                         <a href="#">
 38                             <i class="icon-caret-right bigger-110 invisible">&nbsp;</i>
 39                             上周
 40                         </a>
 41                     </li>
 42
 43                     <li>
 44                         <a href="#">
 45                             <i class="icon-caret-right bigger-110 invisible">&nbsp;</i>
 46                             本月
 47                         </a>
 48                     </li>
 49
 50                     <li>
 51                         <a href="#">
 52                             <i class="icon-caret-right bigger-110 invisible">&nbsp;</i>
 53                             上月
 54                         </a>
 55                     </li>
 56                 </ul>
 57             </div>
 58         </div>
 59
 60         <div class="widget-body">
 61             <div class="widget-main">
 62                 <div id="piechart-placeholder"></div>
 63
 64                 <div class="hr hr8 hr-double"></div>
 65
 66                 <div class="clearfix">
 67                     <div class="grid3">
 68                         <span class="grey">
 69                             <i class="icon-facebook-sign icon-2x blue"></i>
 70                             &nbsp; likes
 71                         </span>
 72                         <h4 class="bigger pull-right">1,255</h4>
 73                     </div>
 74
 75                     <div class="grid3">
 76                         <span class="grey">
 77                             <i class="icon-twitter-sign icon-2x purple"></i>
 78                             &nbsp; tweets
 79                         </span>
 80                         <h4 class="bigger pull-right">941</h4>
 81                     </div>
 82
 83                     <div class="grid3">
 84                         <span class="grey">
 85                             <i class="icon-pinterest-sign icon-2x red"></i>
 86                             &nbsp; pins
 87                         </span>
 88                         <h4 class="bigger pull-right">1,050</h4>
 89                     </div>
 90                 </div>
 91             </div><!-- /widget-main -->
 92         </div><!-- /widget-body -->
 93     </div><!-- /widget-box -->
 94     <!-- /.main-container -->
 95     <!-- basic scripts -->
 96     <!--[if !IE]> -->
 97     <!--[if !IE]> -->
 98
 99     <script type="text/javascript">
100         window.jQuery || document.write("<script src=‘assets/js/jquery-2.0.3.min.js‘>" + "<" + "script>");
101     </script>
102
103     <!-- <![endif]-->
104     <!--[if IE]>
105     <script type="text/javascript">
106      window.jQuery || document.write("<script src=‘assets/js/jquery-1.10.2.min.js‘>"+"<"+"script>");
107     </script>
108     <![endif]-->
109     <script type="text/javascript">
110         if ("ontouchend" in document) document.write("<script src=‘assets/js/jquery.mobile.custom.min.js‘>" + "<" + "script>");
111     </script>
112
113     <script src="assets/js/bootstrap.min.js"></script>
114
115     <!-- page specific plugin scripts -->
116     <script src="assets/js/flot/jquery.flot.min.js"></script>    // 曲线图
117     <script src="assets/js/flot/jquery.flot.pie.min.js"></script>   // 饼状图
118
119     <!-- inline scripts related to this page -->
120
121     <script type="text/javascript">
122         jQuery(function ($) {
123             var placeholder = $(‘#piechart-placeholder‘).css({ ‘width‘: ‘90%‘, ‘min-height‘: ‘150px‘ });
124             var data = [
125               { label: "social networks", data: 38.7, color: "#68BC31" },
126               { label: "search engines", data: 24.5, color: "#2091CF" },
127               { label: "ad campaigns", data: 8.2, color: "#AF4E96" },
128               { label: "direct traffic", data: 18.6, color: "#DA5430" },
129               { label: "other", data: 10, color: "#FEE074" }
130             ]
131             function drawPieChart(placeholder, data, position) {
132                 $.plot(placeholder, data, {
133                     series: {
134                         pie: {
135                             show: true,
136                             tilt: 0.8,
137                             highlight: {
138                                 opacity: 0.25
139                             },
140                             stroke: {
141                                 color: ‘#fff‘,
142                                 width: 2
143                             },
144                             startAngle: 2
145                         }
146                     },
147                     legend: {
148                         show: true,
149                         position: position || "ne",
150                         labelBoxBorderColor: null,
151                         margin: [-30, 15]
152                     }
153                   ,
154                     grid: {
155                         hoverable: true,
156                         clickable: true
157                     }
158                 })
159             }
160             drawPieChart(placeholder, data);
161
162             /**
163             we saved the drawing function and the data to redraw with different position later when switching to RTL mode dynamically
164             so that‘s not needed actually.
165             */
166             placeholder.data(‘chart‘, data);
167             placeholder.data(‘draw‘, drawPieChart);
168
169
170
171             var $tooltip = $("<div class=‘tooltip top in‘><div class=‘tooltip-inner‘></div></div>").hide().appendTo(‘body‘);
172             var previousPoint = null;
173
174             placeholder.on(‘plothover‘, function (event, pos, item) {
175                 if (item) {
176                     if (previousPoint != item.seriesIndex) {
177                         previousPoint = item.seriesIndex;
178                         var tip = item.series[‘label‘] + " : " + item.series[‘percent‘] + ‘%‘;
179                         $tooltip.show().children(0).text(tip);
180                     }
181                     $tooltip.css({ top: pos.pageY + 10, left: pos.pageX + 10 });
182                 } else {
183                     $tooltip.hide();
184                     previousPoint = null;
185                 }
186
187             });
188
189         })
190     </script>
191
192 </body>
193 </html>

所需引用的js文件下载链接:

http://files.cnblogs.com/files/xiaoerlang90/Html5%E9%A5%BC%E7%8A%B6%E5%9B%BE%E5%BC%95%E7%94%A8%E6%96%87%E4%BB%B6.zip

时间: 2024-11-16 06:42:26

HTML5之Canvas绘图实例——饼状图的相关文章

用canvas绘制的饼状图,6等分,颜色随机

都说程序员懒,生活还是不能懒的,比如洗头.洗脸,当然也包括是分享咯.下面分享一下我用H5的新标签canvas绘制的饼状图,颜色是随机生成的,动态生成6等分,我还做了随机生成数据的, 配上文字,每份所占的比例就显而意见.,这点用在分析数据还是挺好的.图片如下; canvas 是 HTML5 提供的一个用于展示绘图效果的标签. canvas 原意画布, 帆布. 在 HTML 页面中用于展示绘图效果. 最早 canvas 是苹果提出的一个方案, 今天已经在大多数浏览器中实现.. 基本使用语法: 使用

canvas+js画饼状图

效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>饼状图</title> </head> <body> <canvas id="canvas"></canvas> <script> (function () {

html5 canvas实现饼状图

实现的效果如下: 通过canvas来实现的,主要是想练习封装插件.只需要在html里面简单的添加代码,就可以实现饼状图的自动添加.对于饼状图的颜色,比例等都可以根据需要进行调节. 具体的代码地址:

Extjs 4 生成饼状图实例

前台: //远程读取设备去向图表数据 var Store1 = new Ext.data.Store({ <span style="white-space:pre"> </span>proxy:{ <span style="white-space:pre"> </span>type:'ajax', <span style="white-space:pre"> </span>u

用FusionChartsFree做饼状图、柱状图、折线图的实例

1.先来看看要进行表现的页面:myChart.jsp <%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/

JFreeChart在Struts2中实现3D饼状图统计

在Struts2中,用JFreeChart实现3D饼状图统计 前段时间学习了一下JFreeChart,现在来整理一下自己所作的实例. 下面分别用两种方式来实现: 一种是以java应用程序的方式,一种是以web项目程序的方式 需要加入的jar包有:  jcommon-1.0.17.jar . jfreechart-1.0.14.jar(前两个是JFreeChart中所带的,在下载的JFreeChart的lib目录下) . struts2-jfreechart-plugin-2.3.16.3.jar

canvas动态绘制饼状图,

当我们使用Echrts很Highcharts的时候,总是觉得各种统计图表是多么神奇,今天我就用现代浏览器支持的canvas来绘制饼状统计图,当然仅仅是画出图并没什么难度,但是统计图一般都有输入,根据不同的输入来绘制,需要发挥你脑力. 1.canvas简单使用 1.1先看我们的html,需要一个绘图的区域 <canvas id="drawing" width="500px" height="500px"></canvas>

Android开发自定义控件实现一个饼状图

实现一个如图所示的控件,包括两部分,左边的饼状图和中间的两个小方块,及右边的两行文字 实现起来比较简单,只是一些绘图API的调用 核心代码在onDraw函数里边,,对静态控件进行绘制即可 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /*饼状图的x坐标*/ float centreX= getWidth()/5; /*饼状图的y坐标*/ float centreY= getHeight()/2; /*

python的matplotlib饼状图

在python的matplotlib画图函数中,饼状图的函数为pie pie函数参数解读 plt.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(