All right, I have found a solution that works, even though I don't fully understand it yet. The solution was mostly copied from this fiddle. I will point out differences between the example code and this code that have to be made (if someone can please explain why, that would be great) for it to work.
I will post my entire code for any fellow newbies that find D3 confusing.
HTML: (I'm working off a pyramid application, hence the script imports in the head)
<head>
<title>
Graph
</title>
<link rel="stylesheet" type="text/css" href="${request.static_url('monitor:static/css/graph.css')}"/>
<script type="text/javascript" src="${request.static_url('monitor:static/jquery-ui-1.10.3/jquery-1.9.1.js')}"></script>
<script type="text/javascript" src="${request.static_url('monitor:static/jquery-ui-1.10.3/ui/jquery-ui.js')}"></script>
<script type="text/javascript" src="${request.static_url('monitor:static/D3/d3.min.js')}"></script>
<script type="text/javascript" src="${request.static_url('monitor:static/js/graph.js')}"></script>
</head>
<body>
<body onload="graphplot()">
<h4 id="Heading">Streamgraph</h4>
<div id="graph_area"></div>
</body>
And then here is my graph.js:
function graphplot(){
test_data0 = [{"0": 0.2, "1": 0.0, "-1": 0.0}, {"0": 0.0, "1": 0.6, "-1": 0.0}, {"0": 0.0, "1": 0.3, "-1": 0.0}, {"0": 0.0, "1": 0.0, "-1": 0.6}, {"0": 0.3, "1": 0.0, "-1": 0.1}, {"0": 0.0, "1": 0.2, "-1": 0.3}, {"0": 0.3, "1": 0.5, "-1": 0.0}, {"0": 0.3, "1": 0.0, "-1": 0.0}, {"0": 0.0, "1": 0.0, "-1": 0.0}]
test_data1 = [{"0": 0.0, "1": 0.0, "-1": 0.0}, {"0": 0.0, "1": 0.6, "-1": 0.0}, {"0": 0.0, "1": 0.3, "-1": 0.0}, {"0": 0.0, "1": 0.0, "-1": 0.6}, {"0": 0.3, "1": 0.3, "-1": 0.0}, {"0": 0.0, "1": 0.3, "-1": 0.3}, {"0": 0.3, "1": 0.1, "-1": 0.6}, {"0": 0.3, "1": 0.0, "-1": 0.0}, {"0": 0.0, "1": 0.0, "-1": 0.0}]
var width = $("#graph_area").width(),
height = 500;
var toggle = 0; //just so that the animation toggles between these two data
$("#graph_area").click(function(){
console.log('test')
if (toggle == 0){
streamed_history(test_data1)
toggle = 1;
}else {
streamed_history(test_data0)
toggle=0;
}
});
var colors = {'0': '#6ff500', '1': '#ffad0a', '-1': '#f90035'}, //colours for the three layers; no longer random
feedbacks = [-1, 0, 1],
stack = d3.layout.stack().offset("wiggle"); //the plot sits on its base without wiggle
//.values(function(d) { return d.values; }); --->removed. Throws: TypeError: t is undefined
var svg = d3.select("#graph_area").append("svg")
.attr("width", width)
.attr("height", height);
streamed_history(test_data0)
function streamed_history(data) {
data_array = feedbacks.map(function (f) {
return data.map(function(element, i) { return {x: i, y: element[f]}; })
}),
layers = stack(data_array) //--->No nest.entries. And the below code for only one layers variable
layers = feedbacks.map(function (f, i) {
return {layer: layers[i], feedback: f, color: colors[f]}
})
var x = d3.scale.linear()
.domain([0, data.length - 1])
.range([0, width]);
var y = d3.scale.linear()
.domain([0,1]) //--->coplicated thing removed. It just needs to be:([0, max of data])
.range([height, 0]);
var area = d3.svg.area().interpolate("basis")
.x(function(d) { return x(d.x); })
.y0(function(d) { return y(d.y0); })
.y1(function(d) { return y(d.y0 + d.y); });
//enter
svg.selectAll("path")
.data(layers)
.enter().append("path")
.attr("d", function (d) {return area(d.layer);})
.style("fill", function(d) { return d.color; });
//update
d3.selectAll("path") //this effectively replaces transition(), since now each dataset is plotted independently with the duration of transition defined here.
.data(layers)
.transition()
.duration(500)
.attr("d", function (d) {return area(d.layer);});
}
}//end graphplot
graphplot()
Hope this helps anyone in the future!