-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Party Trend Chart #5
Comments
showAllParties = false;
if showAll === false {
parties = ["A"];
} |
Work in ProgressHow Is the Dataset Represented?1.
|
To do
General
Institutes
|
color.domain(
d3.keys(data[0])
.filter(function(key) {
return key !== dateValue && key !== "Lead" && key !== "Red (A+B+F+Ø)" && key !== "Blue (V+O+I+C+K)" && key !== "Polling Firm";
})); instituteColor.domain(["Voxmeter", "Gallup", "Greens", "Megafon", "Rambøll", "DR"]) |
Ignore filter implemented: color.domain(
d3.keys(data[0])
.filter(function(key) {
if (ignoreFilter.indexOf(key) === -1) return key;
})); Not sure why it had to have |
Two working solutions to the polling firms, via seemant on IRC: instituteColor.domain(
d3.nest().key(function(d) {
return d["Polling Firm"];
})
.map(data, d3.map)
.keys()
); instituteColor.domain(
d3.nest().key(function(d) {
return d["Polling Firm"];
})
.entries(data)
.map(function(d) { return d.key; })
);
|
Confidence Interval
|
|
Linear-regression code: |
Actually, it fails on single for |
Fixed the bug; was missing a |
Reference to related issue on science.js: jasondavies/science.js#12. |
Thanks to @radiodario for sorting it out:
|
Still need to change the code to work for Some of the errors:
|
Hey thanks for the kudos 👍 I think the most important thing to stress here is that values to |
New error emerges, when more data is used: http://bl.ocks.org/ndarville/1875db6aebe05f0037bf. Happens from object |
Fixed the error; it had an errant 2014 instead of 2015 at the very top. (@radiodario.)
Especially when missed data is appended at the bottom—non-chronologically. |
diff --git a/index.html b/index.html
index 8922fb0..68a35db 100644
--- a/index.html
+++ b/index.html
@@ -59,6 +59,7 @@
<body>
<!-- <script src="http://d3js.org/d3.v3.min.js"></script> -->
<script src="d3.min.js?v=3.2.8"></script>
+ <script src="science.v1.min.js?v=1.9.1"></script>
<script type="text/javascript"charset="utf-8">
// Settings
var width = 440,
@@ -82,15 +83,15 @@
electionDate = "", // "09/14/2015", // Breaks when the year is 2015 for some reason
yAxisTitle = "Votes (%)",
dateValue = "Date",
- instituteValue = "Polling Firm"
- showLineChart = false,
+ instituteValue = "Polling Firm",
showDots = true,
showAllParties = false,
recalculateYMax = false;
- if (showAllParties === false) {
- var parties = ["A"];
- }
- var displayInstitutes = (showAllParties === false && parties.length === 1) ? true : false;
+ parties = showAllParties === true ? [] : ["A"];
+
+ // Autoconfig
+ var singleParty = (showAllParties === false && parties.length === 1) ? true : false,
+ displayInstitutes = singleParty;
var ignoreFilter = [
"Lead",
@@ -120,12 +121,6 @@
.orient("left")
.tickFormat(function(d) { return d + "%"; });
- var line = d3.svg.line()
- .interpolate("linear")
- .defined(function(d) { return !isNaN(d.dataValue); })
- .x(function(d) { return x(d.date); })
- .y(function(d) { return y(d.dataValue); });
-
var svg = d3.select("body").append("svg")
.attr({
"width": width + margin.left + margin.right,
@@ -171,7 +166,7 @@
values: data.map(function(d) {
return {
date: d.date,
- dataValue: parseFloat(d[name]),
+ dataValue: +d[name],
institute: d[instituteValue]
};
})
@@ -185,7 +180,7 @@
]);
// Compute y.domain()
- if (recalculateYMax === true && showAllParties === false && parties.length === 1) {
+ if (recalculateYMax === true && singleParty === true) {
y.domain([
0, d3.max(data, function(d) { return d[parties[0]]; })
]);
@@ -251,16 +246,28 @@
});
}
- // Connect the dots with line
- /// No support for null values
- if (showLineChart === true) {
+ // Plot LOESS regression
+ if (singleParty === true) {
+ var line = d3.svg.line()
+ .interpolate("linear")
+ .x(function(d) { return d[0]; })
+ .y(function(d) { return d[1]; });
+
graph.append("path")
- .attr({
- "class": "line",
- "d": function(d) { return line(d.values); }
+ .datum(function() {
+ var loess = science.stats.loess();
+ loess.bandwidth(.2);
+
+ var xVal = data.map(function(d) { return x(d.date); }).reverse(),
+ yVal = data.map(function(d) { return y(+d[parties[0]]); }).reverse(),
+ loessData = loess(xVal, yVal);
+
+ return d3.zip(xVal, loessData);
})
+ .attr("class", "line")
+ .attr("d", line)
.style("stroke", function(d) {
- return (showAllParties === false && parties.length === 1) ? "#777" : color(d.name);
+ return singleParty === true ? "#777" : color(d.name);
});
} |
|
Challenge
Implement LOESS trend line
Refactor with
d3.nest()
Representing data in
data.csv
Dealing with gaps of data (ignore)
Will involve saving the
!NaN
somewhere for connection:I should ideally write something that can be easily be included in a project.
The text was updated successfully, but these errors were encountered: