I'm trying to add points to a line graph using d3 in this example: http://bl.ocks.org/mbostock/3884955
I was also trying to follow this post
How do you get the points to look like this picture from the documentation? http://github.com/mbostock/d3/wiki/line.png
The stroke of the circle should match the line color.
var color = d3.scale.category10();
d3.csv("data.csv", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; }));
data.forEach(function(d) {
d.date = parseDate(d.date);
});
var ranks = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {date: d.date, ranking: +d[name]};
})
};
});
var rank = svg.selectAll(".rank")
.data(ranks)
.enter().append("g")
.attr("class", "rank");
rank.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); });
var point = rank.append("g")
.attr("class", "line-point");
point.selectAll('circle')
.data(function(d){ return d.values})
.enter().append('circle')
.attr("cx", function(d) { return x(d.date) })
.attr("cy", function(d) { return y(d.ranking) })
.attr("r", 3.5)
.style("fill", "white")
.style("stroke", function(d) { return color(d.name); });
First of all, it is important to understand how to build a basic line chart with d3.js. Here, the data is in long (or tidy) format: one numerical columns provides the information of each group. Thus, the first step is to use the d3.nest function to group the variable. Read more about it here.
Line 8–30: Style section to style different elements. Line 34: onload= “lineChart ()” means we are telling the system to load the linechart () function immediately to show D3 graphs after the page has been loaded. Line 36–37: Create a SVG in the size of 1200px by 750px for us to put graphic elements in later.
Navigate to MultiLineChart_D3 and browse index.html, and you should have a multi-line graph based on the sample data. In the previous tutorial, when we created xScale and yScale using Range and Domain, we hard-coded the minimum and maximum for the domain.
D3 Library Tutorials Opinion About Posted on Nov 1, 2019 in d3data sciencetutorial Making an Interactive Line Chart in D3.js v.5 Static graphs are a big improvement over no graphs but we can all agree that static information is not particularly engaging. On the web there is no presenter to talk over a picture.
.style("stroke", function(d) { return color(this.parentNode.__data__.name); })
See JSBin code
Found answer here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With