I work through an introductory project in d3. From a list separated by commas, I read the form data. I can get the data to show initially without any problems. However, when I zoom in or out using the mouse wheel, two things happen:
- The signs of the x axis, which should be at times, turn into numbers, the basis of which I do not quite understand.
- Y axis labels, which must be between 0 and 25,000 for the dataset I'm working with, turn into numbers similar to the ones displayed on the x axis.
- Bars that are initially displayed disappear.
I have included all of my source code at the bottom of this post, but I want to highlight the elements that I think are important. Keep in mind that I'm not entirely sure what matters, as I'm a complete newbie to d3.
When a mousewheel event is received, this function is called
function redraw() {
drawAxes();
redrawBars();
}
drawAxes () is as follows:
function drawAxes() {
chart.select(".x.axis").call(xAxis);
chart.select(".y.axis").call(yAxis);
}
redrawBars () is as follows:
function redrawBars() {
chart.selectAll("g.data_bar")
.attr("height", function(d) { return yAxisScale(maxPval - d.pval); })
.attr("transform", function(d) { return "translate(0," + yAxisScale(d.pval) + ")"; } );
}
The data is as follows: timestamp, power_val
2015-04-14 18:49:17, 14388
2015-04-14 18:49:18, 14388
2015-04-14 18:49:19, 14456
2015-04-14 18:49:20, 14456
2015-04-14 18:49:21, 14289
2015-04-14 18:49:22, 14289
2015-04-14 18:49:36, 16106
2015-04-14 18:53:36, 6463
2015-04-14 18:53:37, 6463
2015-04-14 18:53:38, 6425
2015-04-14 18:53:39, 6425
2015-04-14 18:53:40, 6498
2015-04-14 18:53:41, 3848
2015-04-14 18:53:42, 3848
2015-04-14 18:53:43, 3848
2015-04-14 18:53:44, 3713
2015-04-14 18:53:45, 3713
2015-04-14 18:53:46, 3677
2015-04-14 18:53:47, 3677
Let me know if there is anything else I need to include or describe. I understand that my code is pretty ugly at the moment.
var barWidth = 1,
dateFormatter = d3.time.format("%Y-%m-%d %H:%M:%S"),
margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var maxPval = -1;
var xAxisScale = d3.time.scale().range([0, width]),
yAxisScale = d3.scale.linear().range([height, 0]),
xAxis = d3.svg.axis()
.scale(xAxisScale)
.orient("bottom"),
yAxis = d3.svg.axis()
.scale(yAxisScale)
.orient("left");
var zoom = d3.behavior.zoom()
.x(xAxisScale)
.y(yAxisScale)
.scaleExtent([0.1,1000])
.on("zoom", zoomed);
function zoomed() {
console.log("mousewheel event received.");
redraw();
}
var chart = d3.select(".chart")
.attr("width", (width + margin.left + margin.right))
.attr("height", (height + margin.top + margin.bottom))
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + margin.left + ", " + height + ")")
.call(xAxis);
chart.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + margin.left + ", " + 0 + ")")
.call(yAxis);
function initAxes(data) {
var xAxisExtent = d3.extent(data, function(d) { return +d.date; } );
xAxisScale.domain([new Date(xAxisExtent[0]), new Date(xAxisExtent[1])]);
yAxisScale.domain([0, d3.max(data, function(d) { return d.pval; })]);
drawAxes();
}
function drawAxes() {
chart.select(".x.axis").call(xAxis);
chart.select(".y.axis").call(yAxis);
}
function initBars(data) {
chart.selectAll("g.data_bar")
.data(data)
.enter()
.append("g")
.attr("transform",
function(d) {
console.log(d.date);
return "translate(" + (xAxisScale(d.date) + margin.left) + ",0)";
})
.attr("class", "data_bar")
.append("rect")
.attr("width", barWidth - 0.1)
.attr("height", function(d) { return yAxisScale(maxPval - d.pval); })
.attr("transform", function(d) { return "translate(0," + yAxisScale(d.pval) + ")"; } );
}
function redraw() {
drawAxes();
redrawBars();
}
function redrawBars() {
chart.selectAll("g.data_bar")
.attr("height", function(d) { return yAxisScale(maxPval - d.pval); })
.attr("transform", function(d) { return "translate(0," + yAxisScale(d.pval) + ")"; } );
}
function selector(d) {
d.pval = +d["power_val"];
d.date = dateFormatter.parse(d["timestamp"]);
return d;
}
d3.csv("out_file", selector, function(error, data) {
if (error) {
console.log("error reading csv file.");
return;
}
maxPval = d3.max(data, function(d) { return d.pval; });
console.log(maxPval);
initAxes(data);
initBars(data);
});
body {
background-color: darkseagreen;
}
.chart rect {
fill: black;
}
.chart text {
fill: black;
font: 3px sans-serif;
text-anchor: start;
}
.axis text {
font: 6px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: white;
shape-rendering: crispEdges;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Bar Chart</title>
<link rel="stylesheet" type="text/css" href="zoom_bar_chart.css">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<svg class="chart"></svg>
<script src="zoom_bar_chart.js" charset="utf-8"></script>
</body>
</html>
Run codeHide result