D3js-堆棧圖,d3js-堆棧

來源:互聯網
上載者:User

D3js-堆棧圖,d3js-堆棧

:


原始碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>D3 -堆棧圖</title><style type="text/css">.axis path,.axis line{fill: none;stroke: black; shape-rendering: crispEdges; }.axis text {font-family: sans-serif;font-size: 14px;font-weight: bold;}</style>  </head>    <body>  <script type="text/javascript" src="js/d3/d3.js"></script><script type="text/javascript" src="js/d3/d3.min.js"></script>   <script type="text/javascript">      var width = 750;   var height = 500;   var padding ={left:80 ,top:50,right:100,bottom:30};      var svg = d3.select("body").append("svg")      .attr("width",width)      .attr("height",height);   //資料   var dataset = [{ name: "PC" ,   sales: [{ year:2005, profit: 3000 },{ year:2006, profit: 1300 },{ year:2007, profit: 3700 },{ year:2008, profit: 4900 },{ year:2009, profit: 700 },{ year:2010, profit: 700 }] },{ name: "SmartPhone" ,   sales: [{ year:2005, profit: 2000 },{ year:2006, profit: 4000 },{ year:2007, profit: 1810 },{ year:2008, profit: 6540 },{ year:2009, profit: 2820 },{ year:2010, profit: 1000 }] },{ name: "Software" ,   sales: [{ year:2005, profit: 1100 },{ year:2006, profit: 1700 },{ year:2007, profit: 1680 },{ year:2008, profit: 4000 },{ year:2009, profit: 4900 },{ year:2010, profit: 700 }] } ];    //layout轉換資料,轉換成適合堆棧圖的資料   var stack = d3.layout.stack()   .values(function(d){//擷取或設定每個系列值的訪問器函數   return d.sales;   })    .x(function(d){//擷取或設定x軸訪問器函數   return d.year;   })   .y(function(d)//擷取或設定y軸訪問器函數   {   return d.profit;   });   var data =stack(dataset);      console.log(data);//輸出資料,可以查看到y0和y        //x軸比例尺   var xRange = width-padding.left-padding.right;   //序列比例尺 (設定x軸上每個節點(年份)所顯示的位置)   var xScale =d3.scale.ordinal()     .domain(data[0].sales.map(function(d){ //設定比例尺的定義域  (在x軸要顯示的資料)     return d.year;     }))     .rangeBands([0,xRange],0.3);//為離散的塊劃分範圍,(設定圖表適合頁面的寬度,顯示位置)      //Y軸比例尺//獲得定義域最大值  (data[data.length-1]是最上面那個矩形,位於最高層,所以他的sales中的y0+y是最大的)var bigProfit = d3.max(data[data.length-1].sales,function(d){return d.y+d.y0; //y0即該層起始座標,y是高度});//獲得範圍最大值var yRange =height-padding.top-padding.bottom;   //線性比例尺   var yScale = d3.scale.linear()   .domain([0,bigProfit])   //定義域   .range([0,yRange]); //範圍      //顏色比例尺   var color = d3.scale.category20();    //添加分組g標籤 並設定顏色 var groupRect = svg.selectAll("g") .data(data) .enter() .append("g") .attr("fill",function(d,i) { return color(i); });  //添加矩形 var rects = groupRect.selectAll("rect") .data(function(d) { return d.sales; }) .enter() .append("rect") .attr("x",function(d,i){ return xScale(d.year); //x軸上座標的位置 }) .attr("y",function(d,i){ return yRange-(yScale(d.y0+d.y));//Y軸上座標的高度 }) .attr("width",function(d,i) { return xScale.rangeBand(); //rangeBand()取得離散塊的寬度,即x軸上各個矩形的寬度 }) .attr("height",function(d,i) { return yScale(d.y); //y為矩形的高度 })  //堆棧圖位移位置,即具體頁面左邊和頂部的位置 .attr("transform","translate("+padding.left+","+padding.top+")");   //添加 x軸 var xAxis = d3.svg.axis() .scale(xScale)//取得比例尺 .orient("bottom");//設定顯示的方位  svg.append("g") .attr("class","axis") .attr("transform",function(d,i) //座標位置 { return "translate("+padding.left+","+(height-padding.bottom)+")"; }) .call(xAxis) //x軸座標說明 .append("text") .attr("x",function(d) { return width-padding.left-padding.right; }) .text("year");  //添加y軸 yScale.range([yRange,0]); //y軸上資料是從上到下遞減  var yAxis = d3.svg.axis() .scale(yScale) .orient("left");  svg.append("g") .attr("class","axis") .attr("transform",function(d,i) //座標位置 { return "translate("+padding.left+","+(height-padding.bottom-yRange)+")"; }) .call(yAxis) //y軸座標說明 .append("text") .text("profit") .attr("x",function(d) { return -20; });  //分組標籤 var labHeight=50; var labRadius=10; //圓形標識 var labelCircle = groupRect.append("circle") .attr("cx",function(d) { return width-padding.right*0.98; }) .attr("cy",function(d,i) { return padding.top*2+labHeight*i*0.5; }) .attr("r",labRadius); //文本文字 var labelText = groupRect.append("text") .attr("x",function(d) { return width-padding.right*0.8; }).attr("y",function(d,i){return padding.top*2+labHeight*i*0.5;})//dy使 文字顯示和圓形的圓心在同一行 .attr("dy",function(d){return labRadius/2;}) .text(function(d){return d.name;});   </script>      </body></html>

其中y和y0的介紹:y表示矩形(柱狀體)的高度,y0表示這個柱狀體的起始高度(即這個矩形從哪裡開始繪製)

解釋一下:(以繪製的第一排柱狀體為例)

繪製出來是下面這個效果


第一個矩形(最下面那個藍色矩形)起始位置y0是0,它的高度y(也就是資料中profit的值)是3000;中間那個矩形它在第一個矩形上面所以它的起始高度y0是3000,而它的高度y是2000;最上面那層矩形它的起始高度是下面兩個矩形的高度和就是5000,它的高度y是1100。這就是y和y0所表示的意思。


來源網站:http://www.ourd3js.com/wordpress/?p=1007

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.