score:3
Accepted answer
You have two issues here:
First, when you do this for both groups of circles:
g.selectAll('circle')
Your second group is selecting existing elements, and the "enter" selection of this second group will be empty.
Try to use different selectors for each group, like this:
//for the first group
g.selectAll('.circles1')
//for the second group
g.selectAll('.circles2')
The second issue is that, for the second group of circles, you should use frequency2
:
.attr('cy', function(d) {
return y(d.frequency2);
})
This is a working demo:
var data = [
{"letter": "A","frequency": "5.01", "frequency2":"18.03"},
{"letter": "B","frequency": "7.80", "frequency2":"15.03"},
{"letter": "C","frequency": "15.35","frequency2":"27.03"},
{"letter": "D","frequency": "22.70","frequency2":"12.03"},
{"letter": "E","frequency": "34.25", "frequency2":"21.03"},
{"letter": "F","frequency": "10.21","frequency2":"28.03"},
{"letter": "G","frequency": "7.68","frequency2":"09.03"},
];
//removing prior svg elements ie clean up svg
d3.select('svg').selectAll("*").remove();
//resetting svg height and width in current svg
d3.select("svg").attr("width", 500).attr("height", 300);
//Setting up of our svg with proper calculations
var svg = d3.select("svg");
var margin = {top: 20, right: 20, bottom: 30, left: 40};
var width = svg.attr("width") - margin.left - margin.right;
var height = svg.attr("height") - margin.top - margin.bottom;
//Plotting our base area in svg in which chart will be shown
var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//X and Y scaling
var x = d3.scaleBand().rangeRound([0, width]).padding(0.4);
var y = d3.scaleLinear().rangeRound([height, 0]);
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return +d.frequency; })]);
//Final Plotting
//for x axis
g.append("g")
.call(d3.axisBottom(x))
.attr("transform", "translate(0," + height + ")");
//for y axis
g.append("g")
.call(d3.axisLeft(y))
.append("text").attr("transform", "rotate(-90)").attr("text-anchor", "end");
/**************** First Path ******************/
//the line function for path
var lineFunction = d3.line()
.x(function(d) {return x(d.letter); })
.y(function(d) { return y(d.frequency); })
.curve(d3.curveLinear);
//defining the lines
var path = g.append("path");
//plotting lines
path
.attr("d", lineFunction(data))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none")
.append("circle");
g.selectAll('.circles1')
.data(data)
.enter().append('circle')
.attr('cx', function(d) {
return x(d.letter);
})
.attr('cy', function(d) {
return y(d.frequency);
})
.attr('r', 6)
.style("fill", "blue");
/**************** Second Path *********************/
//the line function for path
var lineFunction = d3.line()
.x(function(d) {return x(d.letter); })
.y(function(d) { return y(d.frequency2); })
.curve(d3.curveLinear);
//defining the lines
var path = g.append("path");
//plotting lines
path
.attr("d", lineFunction(data))
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("fill", "none")
.append("circle");
g.selectAll('.circles2')
.data(data)
.enter().append('circle')
.attr('cx', function(d) {
return x(d.letter);
})
.attr('cy', function(d) {
return y(d.frequency2);
})
.attr('r', 6)
.style("fill", "red");
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>
Source: stackoverflow.com
Related Query
- D3 - Multiple line chart, Second line circles not showing
- Multiple donut chart not showing percentage
- Nested line chart not showing up
- Multiple line chart not working with date format
- Adding color dynamically to a multiple line chart but it is not working in d3.js
- d3.js Line chart tooltip content not showing
- dc.js line chart is not showing anything
- Line - area chart not showing
- DC.js stack in line chart not showing
- d3 line chart using path is not showing anything
- Adding tooltip to line chart not showing
- d3.js - Multiple line chart doesn't update circles
- D3 line chart not showing anything
- The second call to D3 reusable line chart only draws axes but not lines
- d3 line chart not showing data - based of tutorial
- dc-js line chart showing extra filled line at average change
- Trouble using DC.js (crossfilter and d3 convenience library) - bar chart not showing values
- d3 v4 line chart transition not working
- nvd3 line chart not shown properly. (dots and shaded area)
- How to create Stacked Line Chart D3, Multiple Y Axis and common X Axis
- d3 JSON multiple line chart
- D3 bar chart not showing first element in array
- How create legend for bubble chart in d3? Legend not showing up
- How to add circles onto line chart path d3.js
- Line chart not aligned with x axis
- SOLVE: C3 - Labels not showing in C3 pie chart whit narrow arcs
- d3 adding circles on the highest and lowest value on line chart
- NVD3 Line Chart doesn't display when data passed using ajax - data.map is not a function
- Circles on a moving Line chart
- Chart is not showing the data array of objects
More Query from same tag
- Scrolling visualization only syncs up to text when I load the page twice
- django and d3.tsv, not working
- Expose global variable when using Browserify
- Donut chart d3.js labels
- Edges for Collapsible tree D3
- "Restrict" text scaling on zoom chart
- How can i set the tick text of x-axis between bar in a bar chart in c3.js or d3.js?
- Gradient to <td> cell acording to percentage??Percentage is calculated by some array values
- D3js: finding path's bounding box (without getBBox() )?
- Animating wrapped text in d3
- Add drop shadow to a lot of D3 force simulation nodes?
- Adding elements to a D3 circle pack nodes
- Accented Characters don't display properly
- D3js error with json
- nvd3 line chart not shown properly. (dots and shaded area)
- D3js: How to generate X axis labels/ticks if data for one of axis is 0?
- D3 Sankey chart using circle node instead of rectangle node
- D3 mouse events touch events i.e. click -> supported touch event
- how to highlight a bar in stacked bar chart d3.js v4
- Accessing touch properties on Windows Chrome #74
- Why is the hover state behaviour removed?
- D3 Layout : Tree-like Structure, but link length varies
- D3 link pattern
- D3 version 5, how to chain promise to chart build
- D3 scatter plot transform symbols
- How to clear stacked chart without render
- Using D3 Scales to convert colour to number
- How to visualize JSON dictionary on horizontal D3.js bar chart?
- Finding the user selected option(s) from a `multiple` drop down menu using d3?
- d3 image in circle not rendering properly