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>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
  </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 =1000;
    var height=600;
   
    var dataset=[];
   
    var svg = d3.select("body").append("svg")
    .attr("width".width)
    .attr("height",height);
   
  for(var i=0;i<5;i++)
  {
  dataset[i]=Math.floor(Math.random()*100);
  }
   
  console.log(dataset);
  //外邊框
  var padding={top:20,right:20,bottom:20,left:20};
  //矩形寬度 包括空白
  var rectStep=35;
  //矩形寬度 不包括空白
  var rectWidth=30; 
 
  //繪製矩形
  function draw()
  {
  //1-----------------------------------
  //擷取矩形updata部分
  var updateRect = svg.selectAll("rect")
  .data(dataset);
 
  //擷取矩形的enter部分
  var enterRect =updateRect.enter();
 
  //擷取矩形的exit部分
  var exitRect =updateRect.exit();
 
  //擷取矩形update方法的處理
  updateRect.attr("fill","steelblue")
  //矩形座標
   .attr("x",function(d,i)
   {
    return padding.left+i*rectStep;
   })
   .attr("y",function(d,i)
   {
    return height-padding.bottom-d;
   })
   //矩形的高寬
   .attr("width",rectWidth)
   .attr("height",function(d,i)
   {
    return d;
   });
  //擷取矩形 enter方法的處理
  enterRect.append("rect")
  .attr("fill","steelblue")
  //矩形座標
   .attr("x",function(d,i)
   {
    return padding.left+i*rectStep;
   })
   .attr("y",function(d,i)
   {
    return height-padding.bottom-d;
   })
   //矩形的高寬
   .attr("width",rectWidth)
   .attr("height",function(d,i)
   {
    return d;
   });
   
  //擷取矩形exit方法的處理
  exitRect.remove();
 
  //2--------------------------------------
 
  //擷取文字update的處理
  var updateText = svg.selectAll("text")
  .data(dataset);
 
  var enterText = updateText.enter();
 
  var exitText = updateText.exit();
 
 
  updateText.attr("fill","white")
  .attr("font-size","14px")
  .attr("text-anchor","middle")
  .attr("x",function(d,i)
  {
  return padding.left+i*rectStep;
  })
  .attr("y",function(d,i)
  {
  return height-padding.bottom-d;
  })
  .attr("dx",rectWidth/2)
  .attr("dy","1em")
  .text(function(d,i)
  {
  return d;
  });
  enterText.append("text")
  .attr("fill","white")
  .attr("font-size","14px")
  .attr("text-anchor","middle")
  .attr("x",function(d,i)
  {
  return padding.left+i*rectStep;
  })
  .attr("y",function(d,i)
  {
  return height-padding.bottom-d;
  })
  .attr("dx",rectWidth/2)
  .attr("dy","1em")
  .text(function(d,i)
  {
  return d;
  });
 
  exitText.remove();
  }
 
 
  //調用繪圖函數
  draw();
 
  //排序
  function sortData()
  {
  dataset.sort(d3.ascending);
  draw();
  }
 
  //增加
  function addData()
  {
  dataset.push(Math.floor(Math.random()*100));
  draw();
  }
  //刪除
  function deleteData()
  {
  //選中所有條
  dataset.shift();
  var bars = svg.selectAll("rect")
  .data(dataset);
 
  bars.exit()  
  .remove();
  draw();
  }
   </script>
    
    <br/>
    <div>
<button type="button" onclick="sortData()">排序</button>
<button type="button" onclick="addData()">增加</button>
<button type="button" onclick="deleteData()">刪除</button>

</div>
  </body>

</html>


參考 網站:http://www.ourd3js.com/wordpress/?p=841

http://blog.csdn.net/tianxuzhang/article/details/14086627


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.