(二)联动的饼图与柱形图

1.  前言

2.  联动的饼图与柱形图

图:

本地链接:http://127.0.0.1/Example/case4/pie-bar.html

知识点:

  1 <!DOCTYPE html>
  2 <meta charset="utf-8">
  3 <style>
  4 body{
  5     width:1060px;
  6     margin:50px auto;
  7 }
  8 path {  stroke: #fff; }
  9 path:hover {  opacity:0.9; }
 10 rect:hover {  fill:blue; }
 11 .axis {  font: 10px sans-serif; }
 12 .legend tr{    border-bottom:1px solid grey; }
 13 .legend tr:first-child{    border-top:1px solid grey; }
 14
 15 .axis path,
 16 .axis line {
 17   fill: none;
 18   stroke: #000;
 19   shape-rendering: crispEdges;
 20 }
 21
 22 .x.axis path {  display: none; }
 23 .legend{
 24     margin-bottom:76px;
 25     display:inline-block;
 26     border-collapse: collapse;
 27     border-spacing: 0px;
 28 }
 29 .legend td{
 30     padding:4px 5px;
 31     vertical-align:bottom;
 32 }
 33 .legendFreq, .legendPerc{
 34     align:right;
 35     width:50px;
 36 }
 37
 38 </style>
 39 <body>
 40 <div id=‘dashboard‘>
 41 </div>
 42 <script src="http://d3js.org/d3.v3.min.js"></script>
 43 <script>
 44 function dashboard(id, fData){
 45     var barColor = ‘steelblue‘;
 46     function segColor(c){ return {low:"#807dba", mid:"#e08214",high:"#41ab5d"}[c]; }
 47
 48     // compute total for each state.
 49     fData.forEach(function(d){d.total=d.freq.low+d.freq.mid+d.freq.high;});
 50
 51     // function to handle histogram.
 52     function histoGram(fD){
 53         var hG={},    hGDim = {t: 60, r: 0, b: 30, l: 0};
 54         hGDim.w = 500 - hGDim.l - hGDim.r,
 55         hGDim.h = 300 - hGDim.t - hGDim.b;
 56
 57         //create svg for histogram.
 58         var hGsvg = d3.select(id).append("svg")
 59             .attr("width", hGDim.w + hGDim.l + hGDim.r)
 60             .attr("height", hGDim.h + hGDim.t + hGDim.b).append("g")
 61             .attr("transform", "translate(" + hGDim.l + "," + hGDim.t + ")");
 62
 63         // create function for x-axis mapping.
 64         var x = d3.scale.ordinal().rangeRoundBands([0, hGDim.w], 0.1)
 65                 .domain(fD.map(function(d) { return d[0]; }));
 66
 67         // Add x-axis to the histogram svg.
 68         hGsvg.append("g").attr("class", "x axis")
 69             .attr("transform", "translate(0," + hGDim.h + ")")
 70             .call(d3.svg.axis().scale(x).orient("bottom"));
 71
 72         // Create function for y-axis map.
 73         var y = d3.scale.linear().range([hGDim.h, 0])
 74                 .domain([0, d3.max(fD, function(d) { return d[1]; })]);
 75
 76         // Create bars for histogram to contain rectangles and freq labels.
 77         var bars = hGsvg.selectAll(".bar").data(fD).enter()
 78                 .append("g").attr("class", "bar");
 79
 80         //create the rectangles.
 81         bars.append("rect")
 82             .attr("x", function(d) { return x(d[0]); })
 83             .attr("y", function(d) { return y(d[1]); })
 84             .attr("width", x.rangeBand())
 85             .attr("height", function(d) { return hGDim.h - y(d[1]); })
 86             .attr(‘fill‘,barColor)
 87             .on("mouseover",mouseover)// mouseover is defined below.
 88             .on("mouseout",mouseout);// mouseout is defined below.
 89
 90         //Create the frequency labels above the rectangles.
 91         bars.append("text").text(function(d){ return d3.format(",")(d[1])})
 92             .attr("x", function(d) { return x(d[0])+x.rangeBand()/2; })
 93             .attr("y", function(d) { return y(d[1])-5; })
 94             .attr("text-anchor", "middle");
 95
 96         function mouseover(d){  // utility function to be called on mouseover.
 97             // filter for selected state.
 98             var st = fData.filter(function(s){ return s.State == d[0];})[0],
 99                 nD = d3.keys(st.freq).map(function(s){ return {type:s, freq:st.freq[s]};});
100
101             // call update functions of pie-chart and legend.
102             pC.update(nD);
103             leg.update(nD);
104         }
105
106         function mouseout(d){    // utility function to be called on mouseout.
107             // reset the pie-chart and legend.
108             pC.update(tF);
109             leg.update(tF);
110         }
111
112         // create function to update the bars. This will be used by pie-chart.
113         hG.update = function(nD, color){
114             // update the domain of the y-axis map to reflect change in frequencies.
115             y.domain([0, d3.max(nD, function(d) { return d[1]; })]);
116
117             // Attach the new data to the bars.
118             var bars = hGsvg.selectAll(".bar").data(nD);
119
120             // transition the height and color of rectangles.
121             bars.select("rect").transition().duration(500)
122                 .attr("y", function(d) {return y(d[1]); })
123                 .attr("height", function(d) { return hGDim.h - y(d[1]); })
124                 .attr("fill", color);
125
126             // transition the frequency labels location and change value.
127             bars.select("text").transition().duration(500)
128                 .text(function(d){ return d3.format(",")(d[1])})
129                 .attr("y", function(d) {return y(d[1])-5; });
130         }
131         return hG;
132     }
133
134     // function to handle pieChart.
135     function pieChart(pD){
136         var pC ={},    pieDim ={w:250, h: 250};
137         pieDim.r = Math.min(pieDim.w, pieDim.h) / 2;
138
139         // create svg for pie chart.
140         var piesvg = d3.select(id).append("svg")
141             .attr("width", pieDim.w).attr("height", pieDim.h).append("g")
142             .attr("transform", "translate("+pieDim.w/2+","+pieDim.h/2+")");
143
144         // create function to draw the arcs of the pie slices.
145         var arc = d3.svg.arc().outerRadius(pieDim.r - 10).innerRadius(0);
146
147         // create a function to compute the pie slice angles.
148         var pie = d3.layout.pie().sort(null).value(function(d) { return d.freq; });
149
150         // Draw the pie slices.
151         piesvg.selectAll("path").data(pie(pD)).enter().append("path").attr("d", arc)
152             .each(function(d) { this._current = d; })
153             .style("fill", function(d) { return segColor(d.data.type); })
154             .on("mouseover",mouseover).on("mouseout",mouseout);
155
156         // create function to update pie-chart. This will be used by histogram.
157         pC.update = function(nD){
158             piesvg.selectAll("path").data(pie(nD)).transition().duration(500)
159                 .attrTween("d", arcTween);
160         }
161         // Utility function to be called on mouseover a pie slice.
162         function mouseover(d){
163             // call the update function of histogram with new data.
164             hG.update(fData.map(function(v){
165                 return [v.State,v.freq[d.data.type]];}),segColor(d.data.type));
166         }
167         //Utility function to be called on mouseout a pie slice.
168         function mouseout(d){
169             // call the update function of histogram with all data.
170             hG.update(fData.map(function(v){
171                 return [v.State,v.total];}), barColor);
172         }
173         // Animating the pie-slice requiring a custom function which specifies
174         // how the intermediate paths should be drawn.
175         function arcTween(a) {
176             var i = d3.interpolate(this._current, a);
177             this._current = i(0);
178             return function(t) { return arc(i(t));    };
179         }
180         return pC;
181     }
182
183     // function to handle legend.
184     function legend(lD){
185         var leg = {};
186
187         // create table for legend.
188         var legend = d3.select(id).append("table").attr(‘class‘,‘legend‘);
189
190         // create one row per segment.
191         var tr = legend.append("tbody").selectAll("tr").data(lD).enter().append("tr");
192
193         // create the first column for each segment.
194         tr.append("td").append("svg").attr("width", ‘16‘).attr("height", ‘16‘).append("rect")
195             .attr("width", ‘16‘).attr("height", ‘16‘)
196             .attr("fill",function(d){ return segColor(d.type); });
197
198         // create the second column for each segment.
199         tr.append("td").text(function(d){ return d.type;});
200
201         // create the third column for each segment.
202         tr.append("td").attr("class",‘legendFreq‘)
203             .text(function(d){ return d3.format(",")(d.freq);});
204
205         // create the fourth column for each segment.
206         tr.append("td").attr("class",‘legendPerc‘)
207             .text(function(d){ return getLegend(d,lD);});
208
209         // Utility function to be used to update the legend.
210         leg.update = function(nD){
211             // update the data attached to the row elements.
212             var l = legend.select("tbody").selectAll("tr").data(nD);
213
214             // update the frequencies.
215             l.select(".legendFreq").text(function(d){ return d3.format(",")(d.freq);});
216
217             // update the percentage column.
218             l.select(".legendPerc").text(function(d){ return getLegend(d,nD);});
219         }
220
221         function getLegend(d,aD){ // Utility function to compute percentage.
222             return d3.format("%")(d.freq/d3.sum(aD.map(function(v){ return v.freq; })));
223         }
224
225         return leg;
226     }
227
228     // calculate total frequency by segment for all state.
229     var tF = [‘low‘,‘mid‘,‘high‘].map(function(d){
230         return {type:d, freq: d3.sum(fData.map(function(t){ return t.freq[d];}))};
231     });
232
233     // calculate total frequency by state for all segment.
234     var sF = fData.map(function(d){return [d.State,d.total];});
235
236     var hG = histoGram(sF), // create the histogram.
237         pC = pieChart(tF), // create the pie-chart.
238         leg= legend(tF);  // create the legend.
239 }
240 </script>
241
242 <script>
243 var freqData=[
244 {State:‘AL‘,freq:{low:4786, mid:1319, high:249}}
245 ,{State:‘AZ‘,freq:{low:1101, mid:412, high:674}}
246 ,{State:‘CT‘,freq:{low:932, mid:2149, high:418}}
247 ,{State:‘DE‘,freq:{low:832, mid:1152, high:1862}}
248 ,{State:‘FL‘,freq:{low:4481, mid:3304, high:948}}
249 ,{State:‘GA‘,freq:{low:1619, mid:167, high:1063}}
250 ,{State:‘IA‘,freq:{low:1819, mid:247, high:1203}}
251 ,{State:‘IL‘,freq:{low:4498, mid:3852, high:942}}
252 ,{State:‘IN‘,freq:{low:797, mid:1849, high:1534}}
253 ,{State:‘KS‘,freq:{low:162, mid:379, high:471}}
254 ];
255
256 dashboard(‘#dashboard‘,freqData);
257 </script>

时间: 2024-08-11 07:38:40

(二)联动的饼图与柱形图的相关文章

High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件

原文地址:https://www.codeproject.com/articles/14075/high-speed-charting-control 本文翻译在CodeProject上的介绍(主要还是谷歌翻译,看不太明白的地方,请对比原文,敬请原谅),方便自己和后面人的学习(花费了两天时间,希望是值得的).推荐一个前辈写的东西:TeeChart替代品,MFC下好用的高速绘图控件-(Hight-Speed Charting),自己也转载了这篇文章,在转载的文章中根据自己的实验修改了一些东西,修改

Echarts 使用asp.net +ashx+ajax 实现 饼图、柱形图后台交互

向上效果图 前端code /* * ------------------------------------------------------------------ * module-information: * ------------------------------------------------------------------ * create * @date 2017-02-09 * @author vicm<[email protected]> * ---------

柱形图,饼图等

1.<概率论与数理统计>浙大版,书P148 10 ,加上画茎叶图 频率直方图: 高度为:频率.组距 宽度:组距 箱线图: 茎叶图: 2.求下列数据的均值,中位数,众数,极差,方差与标准差 93 62 51 93 75 82 93 62 65 51 如上,用R语言,表示,均值为72.7 中位数:70 众数:93 极差:42 方差:284.2333 标准差:16.85922 3,某家庭月支出如下: 饼图: 柱形图:

highcharts 柱形图 饼图 加URL或Click事件

我们在做图表的时候,有时候需要在单个数据上加链接或点击事件,是在plotOptions里的events里设置的 plotOptions: { pie: { cursor: 'pointer', events: { click: function(e) { location.href = e.point.url; //上面是当前页跳转,如果是要跳出新页面,那就用 //window.open(e.point.url); //这里的url要后面的data里给出 } }, } }, series: [{

奥威Power-BI连锁商超BI报表分析——“品牌销售分析”

作为连锁商超BI软件的销售分析类报表,“品牌销售分析”需要将各门店的销售情况及趋势等关键指标数据进行直观展示,下面小编将简单的介绍一下奥威连锁商超BI软件Power-BI的品牌销售分析报表:       (1)左上方通过饼图展示公司当前月份各门店销售占比情况,各门店的销售业绩情况一目了然:       (2)左下方则通过三个条形比例图展示商品类型.消费人群的占比情况,可快速了解商品大品类对应的消费人群:       (3)右侧则通过上下两个柱形图,展示各个门店的销售情况及毛利情况以及门店近12个

vue项目的一些最佳实践提炼和经验总结

项目组织结构 ajax数据请求的封装和api接口的模块化管理 第三方库按需加载 利用less的深度选择器优雅覆盖当前页面UI库组件的样式 webpack实时打包进度 vue组件中选项的顺序 路由的懒加载 路由模块拆分化管理 项目组织结构 清晰的项目结构能让别人开发进来更容易理解,当然,每个人都有一定的代码风格习惯.但基于vue开发框架的项目,vue-cli脚手架搭建的项目组织结构大同小异.同时,预想到后面的需求变更及功能增加进展得更有效率,下面截图是我觉得比较好的项目组织结构: 这个截图只是针对

推荐12个最好的 JavaScript 图形绘制库

众多周知,图形和图表要比文本更具表现力和说服力.图表是数据图形化的表示,通过形象的图表来展示数据,比如条形图,折线图,饼图等等.可视化图表可以帮助开发者更容易理解复杂的数据,提高生产的效率和 Web 应用和项目的可靠性. 在这篇文章中,我们收集了12款值得网站开发者收藏的 JavaScript 图形图表库,可以帮助你实现各种功能的图表. D3.js D3 是最流行的可视化库之一,它被很多其他的表格插件所使用.它允许绑定任意数据到 DOM,然后将数据驱动转换应用到文档中.你可以使用它用一个数组创建

5分钟入门数据分析

对于刚刚入门数据分析的同学来说,非常有必要对大数据分析流程有一个整体的认识,明白整个分析链都有哪些环节.当您清楚数据的分析过程之后,你自然也就找到了通向高阶分析的钥匙.除了具备解决异常问题的处理能力之外,更能轻松优化分析模型,甚至是通过已有的分析结果倒推出数据发展变化的经过. 从大体上来讲,数据分析主要包括确定分析目标.收集数据.数据探索.构建分析模型.模型发布.可视化展示这几个流程. 1.确定分析目标 "凡事预则立,不预则废",确立大数据分析目标同样适用.在分析数据之前,必须要明确分

专家教你使用MaxCompute玩转大数据分析

摘要: 摘要传统的数据分析经常使用的工具是Hadoop或Spark在使用之前环境是需要用户自己去搭建的.随着业务逐渐向云迁移如何在云上进行大数据分析是需要解决的问题.为此阿里云提供了一项很重要的服务--大数据计算服务MaxCompute. 本次的分享主要分为三部分: 一.企业云上搭建的数据分析平台:该部分主要介绍阿里云搭建的数据分析平台整体架构和分析流程. 二.大数据计算服务MaxCompute:该部分主要介绍了大数据计算服务MaxCompute的具体情况,包括特点.使用场景.功能组成以及使用过