D3js-对柱状图的增,删,排序
2015-07-01 17:25
633 查看
<%@ 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
<%
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
相关文章推荐
- js 没有注册类(ie网页打不开就是这个问题搞得鬼)
- D3js-对柱状图的增,删,排序
- js中Array数组的操作
- JSTL中if...else...逻辑判断的简单用法
- jsp引入文件时候经常遇到的${ctx}
- fastjson,gson的性能比较
- Python中使用PhantomJS抓取Javascript网页数据
- 原生JS 表单提交验证器
- j2ee,jsp,servlet文件下载服务器端
- 原生JS 表单提交验证器
- Js 怎么遍历json对象所有key及根据动态key获取值
- JSP Note
- js 原型链
- 201507010852_《Javascript权威指南(第六版)——判断值类型的type函数 、鸭式辩型》(P210-217)
- JavaScript开发过程中遇到的问题及解决
- 什么是原型语言
- JS选中checkbox后获取table内一行TD所有数据的方法
- json判断null的方法
- ArcGIS for js API 3.9
- 惰性函数——JS高级