score:3
Accepted answer
you are adding new axes every time and appending new path and text elements every time.
ok, here's a full solution...
var margin = {
top: 6,
right: 80,
bottom: 30,
left: 30
},
width = 600 - 20 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
var parsedate = d3.time.format("%d-%m-%y").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xaxis = d3.svg.axis()
.scale(x)
.ticksize(-height)
.tickpadding(10)
.ticksubdivide(true)
.orient("bottom");
var yaxis = d3.svg.axis()
.scale(y)
.tickpadding(10)
.ticksize(-width)
.ticksubdivide(true)
.ticks(5)
.orient("left")
.tickformat(d3.format(".0f"));
var line = d3.svg.line()
.interpolate("cardinal")
.x(function (d) {
return x(d.timestamp);
})
.y(function (d) {
return y(d.temperature);
});
var svg = d3.select("#progresschart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
svg.append("g")
.attr("class", "y axis")
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("niveau");
var render = function (newdata, t) {
var data = fetchdata(newdata);
color.domain(d3.keys(data[0]).filter(function (key) {
return key !== "timestamp";
}));
data.foreach(function (d) {
d.timestamp = parsedate(d.timestamp);
});
var cities = color.domain().map(function (name) {
return {
name: name,
values: data.map(function (d) {
return {
timestamp: d.timestamp,
temperature: +d[name]
};
})
};
});
x.domain(d3.extent(data, function (d) {
return d.timestamp;
}));
y.domain([
d3.min(cities, function (c) {
return d3.min(c.values, function (v) {
return v.temperature;
});
}),
d3.max(cities, function (c) {
return d3.max(c.values, function (v) {
return v.temperature;
});
})]);
svg.selectall(".x.axis")
.call(xaxis);
svg.selectall(".y.axis")
.transition().duration(t)
.call(yaxis)
var city = svg.selectall(".city")
.data(cities),
cityenter = city.enter().append("g")
.attr("class", "city");
cityenter
.append("path")
.attr("class", "line");
city.select(".line")
.transition().duration(t)
.attr("d", function (d) {
return line(d.values);
})
.style("stroke", function (d) {
return color(d.name);
});
cityenter.append("text")
.attr("x", 3)
.attr("dy", ".35em");
city.select("text")
.text(function (d) {
return d.name;
})
.transition().duration(t)
.attr("transform", function (d) {
var final = d.values[d.values.length - 1];
return "translate(" + x(final.timestamp) + "," + y(final.temperature) + ")";
});
city.exit().remove();
};
var fetchdata = function (newdata) {
if (!newdata) {
return [{
forventet: 8,
nuværende: 1,
timestamp: "12-4-2015"
}, {
forventet: 8,
nuværende: 2,
timestamp: "12-5-2015"
}, {
forventet: 8,
nuværende: 7,
timestamp: "12-6-2015"
}]
} else {
return [{
forventet: 2,
nuværende: 3,
timestamp: "12-4-2015"
}, {
forventet: 6,
nuværende: 5,
timestamp: "12-5-2015"
}, {
forventet: 4,
nuværende: 7,
timestamp: "12-6-2015"
}]
}
};
render(false, 0);
settimeout(function () {
render(true, 2000)
}, 2000)
.grid .tick {
stroke: lightgrey;
opacity: 0.7;
shape-rendering: crispedges;
}
.grid path {
stroke-width: 0;
}
.axis path {
fill: none;
stroke: #bbb;
shape-rendering: crispedges;
}
.axis text {
fill: #555;
}
.axis line {
stroke: #e7e7e7;
shape-rendering: crispedges;
}
.axis, .axis-label {
font-size: 12px;
}
.line {
fill: none;
stroke-width: 1.5px;
}
.dot {
/* consider the stroke-with the mouse detect radius? */
stroke: transparent;
stroke-width: 10px;
cursor: pointer;
}
.dot:hover {
stroke: rgba(68, 127, 255, 0.3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="progresschart"></div>
score:2
your variable city
contains the enter selection computed by the data join.
var city = svg.selectall(".city")
.data(cities)
.enter()
this enter selection will, of course, have no function .exit()
to return the elements to remove. the documentation of selection.enter()
has a good example on how to combine the enter, update and exit selection:
var update_sel = svg.selectall("circle").data(data)
update_sel.attr(/* operate on old elements only */)
update_sel.enter().append("circle").attr(/* operate on new elements only */)
update_sel.attr(/* operate on old and new elements */)
update_sel.exit().remove() /* complete the enter-update-exit pattern */
for your code the following should work:
var city = svg.selectall(".city")
.data(cities);
city.enter().append("g")
.attr("class", "city");
Source: stackoverflow.com
Related Query
- d3 re rendering chart exit is not a function
- area chart with d3 is not rendering on jsp page but working fine with html
- NVD3 Line Chart doesn't display when data passed using ajax - data.map is not a function
- Horizontal bar chart exit method not working
- Pie is not a function in Ember D3 Pie Chart
- d3 exit function not removing 1st element of group
- Pie Chart not rendering in dashboard area using D3 v5
- this.set() not a function when clicking D3 Pie Chart Segments
- D3 chart angular directive not rendering
- Colour not rendering properly Pie Chart d3
- d3JS Chart Not Rendering Properly
- D3.horizon Chart not rendering
- dc.js - Chart not rendering (single row bar chart - horizontal row), with the exception of legend, axes and tick marks/values (which do render)
- D3 v5 bubble chart using pack not rendering
- D3 area chart not rendering end to end
- d3.js line chart not rendering stocks line chart
- d3 Stacked Bar Chart - Bars not rendering
- Uncaught TypeError: n.getMonth is not a function in d3 bar chart
- on mouseover function not executing for d3 line chart
- Bar Chart Not Rendering As Intended in d3.JS
- D3js not rendering chart properly: Everything shows up as a line
- D3 Bar chart is not rendering in angular 5
- Chart not rendering
- D3 not rendering simple bar chart in jade
- D3 area chart not rendering right
- D3, VueJS, axes of chart not rendering
- I dont understand why my d3js chart is not updating though it passes through update function
- Desired label in center of pie chart not rendering DC/D3.js
- Nivo Pie Chart Not Rendering
- How to make exit set on d3 stacked bar chart function properly?
More Query from same tag
- d3.mouse(this) throws TypeError: t is null
- D3 force directed graph: nodes not staying at mouse position while dragging
- D3 Tree Diagram Curved Line Start Position
- Create the appropriate space for variable axis labels
- Circles not appending to coordinates
- duplicating the whole svg element using d3.js
- Given two arbitrary interpolated curves, color them based on relative values
- Tooltip always displaying the same value
- Append Image In Tiled Pieces to D3.js Grid
- Add a legend to a hierchical bar chart in D3
- D3 transformation not applied to multiple <g> elements
- What does input parameter 'd' mean in D3.js function?
- How to display interactively percentages in d3?
- Make Gist from Github repo for display on bl.ocks.org
- Double click timer is always null when mousedown/mouseup registered
- Double counting in bar charts?
- how do i format the value shown in a d3 graph
- javascript take datetime input and filter list based on input
- Get Extent of Complicated Data Structure in D3
- D3- Drawing line on leaflet map through mouse events
- D3 circle packing diameter calculation
- ReferenceError: d3 is not defined using web-maker
- d3.js: incremental transition translation
- Log scale occasionally generating hundreds of ticks
- D3 bring a circle to top?
- How to update D3 contours with setInterval?
- Constrain enter/exit events to only background div in d3.js
- D3 canvas force directed layout bottom issue
- Calling externally-located csv data in javascript
- The column names of my CSV files are years in yyyy format. How do I refer to these columns using "." operator in d.columnName?