我有一个 D3 流量可视化,使用户能够在 9 个流域之间切换并查看原始流量数据、“高”和“低”流量事件(红点和黄点)以及高事件的估计阈值(红线):
http://forestecoservices.net/FESTvisualizations/HubbardBrook/streamflow.html
数据源是一个 PostgreSQL 数据库,其数据采用此(相对标准)配置,此处显示为 csv:
日期,ws_1,ws_2,ws_3,ws_4,ws_5,ws_6,ws_7,ws_8,ws_9 1/1/2000,40,0.541,0.57,0.605,0.643,0.594,0.456,0.299,0.206 1/2/2000,0.739, 0.576,0.594,0.654,0.74,0.625,0.44,0.313,0.224
唯一的问题是我不知道如何将变量名输入 D3,.. 我找到了一些示例,但它们都以这种方式设置了数据:
日期、分水岭、价值
2000 年 1 月 1 日,ws_1, 40
2000 年 1 月 1 日,ws_2,0.541
等等
它可能不漂亮,但我知道我可以用 php 伪造它,所以我制作了一个组合框,它调用一个 php 脚本来查询数据库并吐出一个 json,然后 d3 使用它来重绘图形。(我还没有完全弄清楚如何让这些点也移动,但那是另一回事了)。我知道必须有一种(更好的)方法可以在 d3 中本地执行此操作,而无需求助于 php。
非常感谢任何建议!
史蒂夫·西格内尔
这是我的源代码:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body { font: 12px Arial;}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
.grid .tick {
stroke: lightgrey;
opacity: 0.9;
}
.grid path {
stroke-width: 0.1;
}
div.tooltip {
position: absolute;
text-align: center;
width: 80px;
height: 28px;
padding: 2px;
font: 11px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 18px;
pointer-events: none;
}
</style>
<body>
<div id="option">
<select type='select' onchange='updateData(value);' style='color:red;font-size:1.3em;'>
<option value='ws_1' name='ws_1' selected='true' >Watershed 1</option>
<option value='ws_2' name='ws_2'>Watershed 2</option>
<option value='ws_3' name='ws_3'>Watershed 3</option>
<option value='ws_4' name='ws_4'>Watershed 4</option>
<option value='ws_5' name='ws_5'>Watershed 5</option>
<option value='ws_6' name='ws_6'>Watershed 6</option>
<option value='ws_7' name='ws_7'>Watershed 7</option>
<option value='ws_8' name='ws_8'>Watershed 8</option>
<option value='ws_9' name='ws_9'>Watershed 9</option>
</select>
</div>
<script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width =800 - margin.left - margin.right,
height = 420 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d").parse;
var formatTime = d3.time.format("%e %b %Y");
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(10);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
//create line of stream flow values
var valueline = d3.svg.line()
//.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.flow); });
//create line for hi events threshold
var hithreshold = d3.svg.line()
//.interpolate("monotone")
.x(function(d) { return x(d.date); })
.y(function(d) { return y(17.11891); });
var tooltipdiv = d3.select("body") //div for tooltips
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body") //set up svg
.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 + ")");
//Set up Grid Lines
function make_x_axis() {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(10)
}
function make_y_axis() {
return d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
}
// Get the data
d3.json("data/streamflow_json.php?watershed=ws_1", function(error, data)
{
data.forEach(function(d)
{
d.date = parseDate(d.date);
d.flow = +d.flow;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.flow; })]);
svg.append("g") //grid for x axis
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat("")
)
svg.append("g") //grid for y axis
.attr("class", "grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat("")
)
svg.append("path") // Add the valueline path.
.attr("d", valueline(data))
.attr("class", "line");
svg.append("path") // Add the hi threshold path.
.attr("class", "hiline")
.style("stroke", "red")
.attr("d", hithreshold(data));
svg.append("text") // Chart Title
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Hubbard Brook Stream Flow");
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("text") // text label for the x axis
.attr("x", width / 2 )
.attr("y", height + margin.bottom)
.style("text-anchor", "middle")
.text("Date");
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);
svg.append("text") // text for Y axis
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.text("Stream Flow (mm)");
svg.selectAll("circle") // add value dots, sized & colored
.data(data)
.enter().append("circle")
//.attr("class", "dot")
.attr("r", function(d) {
if (d.flow > 17.11891) {return 3}
else if (d.flow < 0.01) { return 3 }
else {return 1}
;})
.style("fill", function(d) {
if (d.flow > 17.11891) {return "red"}
if (d.flow < 0.01) {return "yellow"}
else { return "black" }
;})
//.attr("d", tooltipdot(data))
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.flow); })
.on("mouseover", function(d) {
tooltipdiv.transition()
.duration(200)
.style("opacity", .9);
tooltipdiv.html(formatTime(d.date) + "<br/>" + d.flow)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
tooltipdiv.transition()
.duration(500)
.style("opacity", 0);
});
});
function updateData(value) {
//Get the data again
d3.json("data/streamflow_json.php?watershed="+value, function(error, data)
{
data.forEach(function(d)
{
d.date = parseDate(d.date);
d.flow = +d.flow;
});
// Scale the range of the data again
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.flow; })]);
// Select the section we want to apply our changes to
var svg = d3.select("body").transition();
// Make the changes
svg.select(".line") // change the value line
.duration(1750)
.attr("d", valueline(data));
svg.select(".hiline") // change the hithreshold line
.duration(2750)
.attr("d", hithreshold(data));
/*svg.select(".circle") // change the dot
.duration(750)
.attr("d", tooltipdot(data));*/
svg.select(".x.axis") // change the x axis
.duration(2750)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(2750)
.call(yAxis);
});
};
</script>
</body>