我如何使用D3.js编程线图来可视化从服务器端接收的数据
How can I program a line chart using D3.js to visualise a data which is received from a server side ?
服务器端有这个链接
http://www.tr-l6/STWebService/Service.svc/session/Fetchnodehistorybyname?strnodename=starbucks接收json格式的数据,这是数据
{"Id":466,"Name":"korea",
"Occurrences":
[{"OccurrenceDate":"'/Date(1398207600000+0100)'/","OccurrenceFrequency":27},
{"OccurrenceDate":"'/Date(1398726000000+0100)'/","OccurrenceFrequency":1},
{"OccurrenceDate":"'/Date(1398898800000+0100)'/","OccurrenceFrequency":4},
{"OccurrenceDate":"'/Date(1399071600000+0100)'/","OccurrenceFrequency":303}]}
这是我用来绘制折线图的代码,但这段代码被编程为从同一台pc接收数据。它不会从服务器端检索数据…我的问题是如何使用Jquery和Ajax来转换此代码,通过使用上面提到的链接从服务器端检索数据来绘制折线图。请记住,我的json文件是嵌套的。这是完整的代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
margin: 0;
}
path.line {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
path.area {
fill: #e7e7e7;
}
.axis {
shape-rendering: crispEdges;
}
.x.axis line {
stroke: #fff;
}
.x.axis .minor {
stroke-opacity: .5;
}
.x.axis path {
display: none;
}
.y.axis line, .y.axis path {
fill: none;
stroke: #000;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 80, right: 80, bottom: 80, left: 80},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate1 = d3.time.format.iso.parse;
// Scales and axes. Note the inverted domain for the y-scale: bigger is up!
var x = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
xAxis = d3.svg.axis().scale(x).tickSize(-height).tickSubdivide(true),
yAxis = d3.svg.axis().scale(y).ticks(4).orient("right");
// An area generator, for the light fill.
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(d.Occurrences.OccurrenceDate); })
.y0(height)
.y1(function(d) { return y(d.Occurrences.OccurrenceFrequency); });
// A line generator, for the dark stroke.
var line = d3.svg.line()
.interpolate("monotone")
.x(function(d) { return x(d.OccurrenceDate); })
.y(function(d) { return y(d.OccurrenceFrequency); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("data3.json", function(error, data) {
data.Occurrences.forEach(function(d){
var dc
dc = d.OccurrenceDate.substring(6, 16)
dc = new Date(dc*1000)
d.OccurrenceDate= parseDate1(dc)
d.OccurrenceFrequency = +d.OccurrenceFrequency
return d;
});
x.domain(d3.extent(data, function(d) { return d.OccurrenceDate; }));
y.domain([0, d3.max(data, function(d) { return d.OccurrenceFrequency; })]);
svg.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
});
</script>
您可以使用d3检索JSON数据,如下所示;
d3.json("/path/to/json", function(d) {
//rendering code
}
,然后,在回调函数中(代替呈现代码),您可以使用变量"d"访问JSON数据。
下面总结了检索数据后要遵循的一般过程;
//Create a svg
svg = d3.select("selector_to_some_DOM_element").append("svg");
//Then append text for each data JSON entry
svg.selectAll(".yourClassNameHere")
.data(d) //received data
.enter()
.text(function (d) { //add some text
//using OccurrenceFrequency
return d.OccurrenceFrequency
});
您可以在http://pothibo.com/2013/09/d3-js-how-to-handle-dynamic-json-data/
相关文章:
- 如何在谷歌可视化中组合数字和模式格式化程序
- 加载泰坦尼克号可视化数据时出现问题
- 如何使用图表包装函数有条件地格式化谷歌可视化表单元格
- AngularJS&Google可视化-使用状态更改处理程序(Google API)在HTML页面上查看$sco
- Javascript图形可视化工具包,具有高性能(500-1000个节点)
- Google可视化addListener调用函数引用错误
- 如何在web上动态可视化数据集
- 可视化到选择框中
- 从特定系列的谷歌可视化折线图中删除悬停工具提示
- 谷歌图表可视化仪表板数据源问题
- 在可视化中将CSS从悬停更改为单击
- 使用 d3 可视化快速射击的“点击”
- 为 D3 可视化创建平均 Y 线
- 谷歌可视化数据表间歇性问题
- Google 图表 - 设置选择不会滚动到表格可视化中的选定行
- 是否有从服务器(如 postgres)中提取数据的数据可视化库
- 调用谷歌可视化API服务器端
- 从nodejs服务器直接渲染和提供d3可视化
- 我如何使用D3.js编程线图来可视化从服务器端接收的数据
- 谷歌可视化API在本地工作,但不在外部服务器上工作