I'm new to D3 and working on a modified version of Chained Transitions: http://bl.ocks.org/mbostock/3903818.
However one problem I'm running into is forcing the y-axis to start at 0. Normally this would be simple fix with changing the y domain and range properties when declaring your other variables but in the context of the transition function, I'm running into issues. Here is the code: http://jsfiddle.net/maureenlinke/DCc6g/
I believe the issue is here:
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(homes, function(c) { return d3.min(c.values, function(v) { return v.price; }); }),
d3.max(homes, function(c) { return d3.max(c.values, function(v) { return v.price; }); })
]);
Thanks,
Maureen
You just need to fix the domain:
var maxPrice = d3.max(homes, function(c) {
return d3.max(c.values, function(v) { return v.price; });
});
y.domain([0, maxPrice]);
The d3.extent function returns the min/max range of the data in the form of an array [min, max].
Normally, you would code the following:
y.domain( d3.extent(homes, function(d) { return d.price; }) );
So the following should work:
var yRange = d3.extent(homes, function(d) { return d.price; });
y.domain( [0, yRange[1] ); // Force the min to 0
Related
I'm quite new to D3 and coding in general. I'm trying to set up a bar chart which includes/excludes data depending on a checkbox. I have a set of product groups and countries which I want to toggle in/out of the total represented by the bar. The output should be one bar per product.
My full data set has many more products, product groups and countries so it is not viable to create a key-value pair for each potential combination of checkboxes. Instead I would like to create a function that re-evaluates the checkboxes and re-filters the data and updates the rollup when a checkbox is changed.
I'm not sure where this function should sit in my code or what it should look like... This is what I'm working with at the moment:
var data = data.filter(function(d) {
if (document.getElementById("nz_button").checked) {
return d.country == 'NZ'
}
if (document.getElementById("au_button").checked) {
return d.country == 'AU'
}
if (document.getElementById("us_button").checked) {
return d.country == 'US'
}
})
// to see how many distinct groups there are and sum volume
var products = d3.nest()
.key(function(d) {
return d.product
})
.rollup(function(leaves) {
var sum = 0;
leaves.forEach(function(d) {
sum += d.volume;
})
return sum
})
.entries(data);
Full code: http://plnkr.co/edit/qezdwMLt48RPc8KH17hS?p=preview
Maybe I should be working with selections and re-running the nest/rollup when required?
Any help appreciated. Thanks :)
You can move the full code which makes the graph in a new function like this:
function makeDataGraph(data) {//function to make the graph.
//
// FILTER
//
var data = data.filter(function(d) {
if (document.getElementById("au_button").checked) {
return d.country == 'AU'
}
if (document.getElementById("us_button").checked) {
return d.country == 'US'
}
if (document.getElementById("nz_button").checked) {
return d.country == 'NZ'
}
})
// to see how many distinct groups there are and sum volume
var products = d3.nest()
.key(function(d) {
return d.product
})
.rollup(function(leaves) {
var sum = 0;
leaves.forEach(function(d) {
sum += d.volume;
})
return sum
})
.entries(data);
// sorting on descending total
console.log(products);
products.sort(function(a, b) {
return b.values - a.values
})
var max = d3.max(products, function(d) {
return d.values;
});
var xscale = d3.scale.linear()
.domain([0, max])
.range([0, 600])
var svg = d3.select("svg");
//
// Still needs to be cleaned up \/ \/
//
var rects = svg.selectAll("rect.product")
.data(products)
rects.exit().remove();
rects.enter().append("rect").classed("product", true)
rects.attr({
x: 200,
y: function(d, i) {
return 100 + i * 50
},
width: function(d, i) {
return xscale(d.values)
},
height: 50
}).on("click", function(d, i) {
console.log(i, d);
})
var labels = svg.selectAll("text.label")
.data(products)
labels.exit().remove();
labels.enter().append("text").classed("label", true)
labels.attr({
x: 195,
y: function(d, i) {
return 128 + i * 50
},
"text-anchor": "end",
"alignment-baseline": "middle"
}).text(function(d) {
return d.key || "N/A"
})
var volume = svg.selectAll("text.volume")
.data(products);
volume.exit().remove();
volume.enter().append("text").classed("volume", true)
volume.attr({
x: function(d, i) {
return 205 + xscale(d.values)
},
y: function(d, i) {
return 128 + i * 50
},
"text-anchor": "start",
"alignment-baseline": "middle"
}).text(function(d) {
return d.values || "N/A"
})
}
Remember to do rects.exit().remove(); so that when the data is changed on click of the checkbox, rectangles related to old data is removed.
Now you can call this function from the click event and also afterloading the tsv like this:
d3.tsv("data.tsv", function(err, udata) {
var udata = udata.map(process);
console.log("udata", udata);
var data = udata // making new var to preserve unfiltered data
makeDataGraph(data);//call the function to make graph
function handleClick() { // event handler...
makeDataGraph(data)
}
//add listener to all check boxes.
d3.selectAll(".filter_button").on("click", handleClick);
});
working code here
I would like to load a dc dataTable with dimensional data that has been grouped and filtered. Is this possible? I am assuming it is but the examples I have seen so far have simple groups coded at the time of table construction and I can't get it to work any other way!
var facts = crossfilter(data);
var testTable = dc.dataTable('#testtable');
var providerDim = facts.dimension(function (d) {
if (d.report_by_3 !== null) {
return d.report_by_3;
}
});
var MH003Group = providerDim.group().reduceSum(function (d) {
if (d.indicator_code === 'MH003') {
return (d.observed / d.expected);
} else {
return null;
}
});
var MH003Group2 = {
all: function () {
return MH003Group.all().filter(function (d) {
return d.value !== null;
});
}
};
dataTable.width(960)
.height(8000)
.dimension(providerDim)
// .group(MH003Group2)
.group(function (d) {
return 'data';
})
.size(10)
.columns([function (d) {return d.indicator_code;},
function (d) {return d.report_by_1;},
function (d) {return d.report_by_3;},
function (d) {return (d.observed / d.expected);}
]);
.sortBy(function (d) {return d.report_by_1;})
.order(d3.ascending);
In the code above I would like to display filtered rows in the dimension using the pre-defined grouping rather than all the rows of data in the dimension which is what is currently happening.
Here is the important part of my code. For some reasons, there is a problem with the "errorBars2". It always returns this error:
Invalid value for <path> attribute d="M40.5,NaNL40.5,NaNZ"
I spent now hours on searching for the mistake but I can't find it! Could somebody explain me what's the problem with the "errorBars2"?
In errorBarArea2 you have written for
.y0(function (d) {
return y0(+d.top_after + +d.moe3);
})
but in the JSON structure it is not available change it to +d.moe3_after
and you have written
.y1(function (d) {
return y0(+d.low_after - +d.moe4);
})
but in the JSON structure it is not available change it to +d.moe4_after
So the final code is
var errorBarArea2 = d3.svg.area()
.x(function (d) {
return x3(d.name) +x3.rangeBand()/2;
})
.y0(function (d) {
return y0(+d.top_after + +d.moe3_after);
})
.y1(function (d) {
return y0(+d.low_after - +d.moe4_after);
})
Tell me if this works.
var errorBarArea2 = d3.svg.area()
.x(function (d) {
return x3(d.name) +x3.rangeBand()/2;
})
.y0(function (d) {
return y2(d.top_after + +d.moe3);
})
.y1(function (d) {
return y2(d.low_after - +d.moe4);
})
I'm having some trouble with the exit().remove() function in a stacked area chart I am creating.
JSFiddle here: Link
I have functionality where the user can enable/disable the data in the chart by clicking on the legend rectangle/color. I know that the data is being updated based on console messages and the Y axis changing scale, but the data in the chart does not change. For instance if the user deselects the Failed category the orange layer should disappear and the Failed and Passed layers should re-adjust.
The issue appears to be in lines 214 to 234 in the fiddle, specifically where I am calling exit().remove():
// filter the data
var updatedData = dataSeries.filter(function(d) {
if(d.vis === "1"){
return d;
}
else {
return null;
}
});
stack.values(function(d) { return d.values; });
layer = stack(updatedData);
main_layer.selectAll(".layer")
.data(layer);
main_layer
.attr("d", function(d) { return main_area(d.values); });
main_layer.exit().remove();
The error I am getting is Object [object Array] has no method exit I have tried changing the selectAll to just a select, but that also produces the same error. Thanks in advance.
I finally got this working. The code below updates the layers correctly:
// filter the data
var updatedData = dataSeries.filter(function(d) {
if(d.vis === "1"){
return d;
}
else {
return null;
}
});
stack(updatedData);
var sel = main_layer.select(".layer");
sel
.attr("class", function(d) { return d.key + " layer"; })
.style("fill", function(d, i) {
if(d.vis === "1") {
return z(i);
}
else return null;
})
.attr("d", function(d) {
if(d.vis === "1") {
return main_area(d.values);
}
else return null;
});
Using D3, how can I find the maximum value in an object where all the values are arrays of floats?
var data = {
"point1": {
"level1": [...],
"level2": [...]
},
...
};
I read Mike's answer on finding the max of a nested array, but I'm not sure what to do here since this is an object of arrays.
Currently I'm doing this, which is awful:
var c1Max = d3.max(data.point1.level1, function(d) { return d.value; });
var c2Max = d3.max(data.point1.level2, function(d) { return d.value; });
var c3Max = d3.max(data.point2.level1, function(d) { return d.value; });
var c4Max = d3.max(data.point2.level2, function(d) { return d.value; });
var c5Max = d3.max(data.point3.level1, function(d) { return d.value; });
var c6Max = d3.max(data.point3.level2, function(d) { return d.value; });
var max = d3.max([c1Max, c2Max, c3Max, c4Max, c5Max, c6Max]);
Maybe I should iterate over all the keys? Or maybe I could flatten the data object somehow? Or maybe there's a nicer way.
You can use d3.entries to convert the object into something you can iterate over:
var max = d3.max(d3.entries(data), function(d) {
return d3.max(d3.entries(d.value), function(e) {
return d3.max(e.value);
});
});
In this particular case (keys not required) you can of course also use d3.values, which makes the above code a bit shorter.
To make it work with the structure in your jsfiddle, you need this code:
var max1 = d3.max(d3.entries(data), function(d) {
return d3.max(d3.entries(d.value), function(e) {
return d3.max(e.value, function(f) { return f.value; });
});
});