I have an area chart with a focus and context section. The context section allows the user to change the time period shown in the focus section.
The data includes information on a population over time, and includes gender. I want to be able to toggle between Men, Women, and All. I have the start of this working, in that I'm grabbing the data I need and feeding it to the chart. However, the area charts don't exit when I click another option, so the graphs for Men/Women/All get stacked on top of each other.
Can anyone help me untangle this?
Code is below, and here is a fiddle: https://jsfiddle.net/3wtrsegp/
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>
.brush rect.selection {
fill: none;
opacity: 1;
}
rect.handle{
fill: #666;
}
</style>
</head>
<body>
<div id="buttons">
<button id="All" class="button selected">All</button>
<button id="Men" class="button">Men</button>
<button id="Women" class="button">Women</button>
</div>
<div id='ptchart'></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var json = [
{
"date": "2020-01-02",
"gender": "Men",
"value": 4320
},
{
"date": "2020-01-02",
"gender": "Women",
"value": 984
},
{
"date": "2020-01-15",
"gender": "Men",
"value": 4624
},
{
"date": "2020-01-15",
"gender": "Women",
"value": 1005
},
{
"date": "2020-02-03",
"gender": "Men",
"value": 5488
},
{
"date": "2020-02-03",
"gender": "Women",
"value": 978
},
{
"date": "2020-02-18",
"gender": "Men",
"value": 5842
},
{
"date": "2020-02-18",
"gender": "Women",
"value": 1006
},
{
"date": "2020-03-02",
"gender": "Men",
"value": 6925
},
{
"date": "2020-03-02",
"gender": "Women",
"value": 1004
},
{
"date": "2020-03-16",
"gender": "Men",
"value": 6132
},
{
"date": "2020-03-16",
"gender": "Women",
"value": 948
},
{
"date": "2020-04-01",
"gender": "Men",
"value": 5852
},
{
"date": "2020-04-01",
"gender": "Women",
"value": 685
},
{
"date": "2020-04-15",
"gender": "Men",
"value": 8697
},
{
"date": "2020-04-15",
"gender": "Women",
"value": 497
},
{
"date": "2020-05-01",
"gender": "Men",
"value": 4547
},
{
"date": "2020-05-01",
"gender": "Women",
"value": 468
}
]
var margin = { top: 30, right: 210, bottom: 110, left: 50 },
margin2 = { top: 380, right: 200, bottom: 70, left: 50 },
width = 1000 - margin.left - margin.right,
height = 440 - margin.top - margin.bottom,
height2 = 490 - margin2.top - margin2.bottom;
var x = d3.scaleTime().range([0, width]),
x2 = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
y2 = d3.scaleLinear().range([height2, 0]);
var xAxis = d3.axisBottom(x).tickFormat(d3.timeFormat("%b %Y")).ticks(5),
xAxis2 = d3.axisBottom(x2).ticks(0).tickSize(0),
yAxis = d3.axisLeft(y);
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush", brushed);
var svg = d3.select("#ptchart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
const parseDate = d3.timeParse("%Y-%m-%d");
let data = json
updateChart(data)
d3.selectAll(".button").on("click", function () {
var section = d3.select(this).attr("id");
if (section == 'All') { data = json } else {
data = json.filter(d => d.gender == section)
}
updateChart(data);
})
updateChart(json);
function updateChart(selectedData) {
const data = d3.nest()
.key(d => d.date)
.rollup(v => d3.sum(v, d => d.value))
.entries(selectedData);
data.forEach((d) => {
d.date = parseDate(d.key);
d.value = +d.value;
})
x.domain(d3.extent(data, d => d.date));
y.domain([0, d3.max(data, d => d.value) + 500]);
x2.domain(x.domain());
y2.domain(y.domain());
var area = d3.area()
.x(function (d) { return x(d.date); })
.y0(height)
.y1(function (d) { return y(d.value); })
var line = d3.line()
.x(d => x(d.date))
.y(d => y(d.value))
// This is what the area looked like before I started to try and get the enter/update/exit pattern working:
// var area = focus.append("g");
// area.attr("clip-path", "url(#clip)");
// area.selectAll('path')
// .data([data])
// .enter().append("path")
// .attr("class", "area")
// .attr("d", area)
// .attr('fill', '#eeeeee')
var area = focus.append("g").selectAll('path')
.data([data])
area.enter().append("path")
.attr("class", "area")
.attr('fill', '#eeeeee')
area.transition()
.attr("d", area)
.attr("clip-path", "url(#clip)");
area.exit().remove()
var arealine = focus.append("g");
arealine.attr("clip-path", "url(#clip)");
arealine.selectAll('path')
.data([data])
.enter().append("path")
.attr("class", "line")
.attr("d", line)
.attr('fill', 'none')
.attr('stroke', '#333')
var capacityline = focus.append('g')
capacityline.selectAll('line')
.data(data)
.enter().append('line')
.attr('class', 'capacityline')
.attr('x1', 0)
.attr('y1', d => y(12500))
.attr('x2', width)
.attr('y2', d => y(12500))
.attr('stroke-width', '1px')
.attr('stroke', '#4D4E56')
.attr('stroke-dasharray', 4)
var stooltip = d3.select("body").append("div")
.attr("class", "tooltip");
svg.selectAll(".axis").remove();
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
var area2 = d3.area()
.x(d => x2(d.date))
.y0(height2)
.y1(d => y2(d.value))
var line2 = d3.line()
.x(d => x2(d.date))
.y(d => y2(d.value))
var area = context.append("g");
area.attr("clip-path", "url(#clip)");
area.selectAll('path')
.data([data])
.enter().append("path")
.attr("class", "area")
.attr("d", area2)
.attr('fill', '#eeeeee')
var arealine = context.append("g");
arealine.attr("clip-path", "url(#clip)");
arealine.selectAll('path')
.data([data])
.enter().append("path")
.attr("class", "line")
.attr("d", line2)
.attr('fill', 'none')
.attr('stroke', '#333')
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x.range());
}
function brushed() {
var area = d3.area()
.x(function (d) { return x(d.date); })
.y0(height)
.y1(function (d) { return y(d.value); })
var line = d3.line()
.x(function (d) { return x(d.date); })
.y(function (d) { return y(d.value); })
var selection = d3.event.selection;
x.domain(selection.map(x2.invert, x2));
focus.selectAll(".area")
.attr("d", area)
focus.selectAll(".line")
.attr("d", line)
focus.select(".axis--x").call(xAxis);
}
</script>
</body>
</html>
from How do I implement d3's enter update exit pattern for an area chart with focus and context?
No comments:
Post a Comment