score:2
Accepted answer
the code is adding the circles elements as children of the svg element but the rest of the graph elements are children of a g element which is child of svg element. the g element has a translate transform. need to add circle elements as children of the g element (i.e. change svg.selectall("circle.line") to graph.selectall("circle.line")).
var m = [80, 80, 80, 80]; // margins
var w = 1000 - m[1] - m[3]; // width
var h = 400 - m[0] - m[2]; // height
var data = [
{"name" : "city1", value:200},
{"name" : "city2", value:32},
{"name" : "city3", value:566},
{"name" : "city4", value:124},
{"name" : "city5", value:22},
{"name" : "city6", value:154}
];
var max_value = d3.max(data, function(d) { return d.value });
var x = d3.scale.linear().domain([0, data.length - 1]).range([0, w]);
var y = d3.scale.linear().domain([0, max_value]).range([h, 0]);
var svg = d3.select("#graph").append("svg:svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2]);
var graph = svg.append("svg:g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
var xaxis = d3.svg.axis().scale(x)
.ticksize(-h).ticksubdivide(true)
.tickformat(function(e){
if(math.floor(e) != e) {
return;
}
return data[e].name;
});
graph.append("svg:g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xaxis);
var yaxisleft = d3.svg.axis().scale(y).ticksize( -w ).ticksubdivide(true).orient("left");
graph.append("svg:g")
.attr("class", "y axis")
.call(yaxisleft);
var line = d3.svg.line()
.x(function (d, i) {return x(i);})
.y(function (d) { return y(d.value);});
graph.append("path").attr("d", line(data)).style("stroke", "black").style("stroke-width", 2);
graph.selectall("circle.line")
.data(data)
.enter().append("svg:circle")
.attr("class", "line")
.style("fill", "green")
.attr("cx", line.x())
.attr("cy", line.y())
.attr("r", 3.5);
body {
margin: 0;
padding: 0;
}
path {
stroke: steelblue;
stroke-width: 1;
fill: none;
}
.axis {
shape-rendering: crispedges;
}
.x.axis line {
stroke: lightgrey;
}
.x.axis .minor {
stroke-opacity: .5;
}
.x.axis path {
display: none;
}
.y.axis line, .y.axis path {
fill: none;
stroke: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="graph"></div>
Source: stackoverflow.com
Related Query
- d3js - line chart `circle` placement to the lines are not properly sets
- How do you add multiple lines to a D3JS line chart where the depedent variable data sources start at different positions along the range?
- nvd3 multibar chart with stacked option the bars are not appearing properly
- d3 chart is going out of the box, last circle is not coming properly
- D3.js Line chart not updating properly when applying the general update pattern
- The second call to D3 reusable line chart only draws axes but not lines
- add circle in a spiral chart with d3js with line connecting to center
- d3js gantt chart with date/time scale at the top and current day blue line
- D3 line chart does not show tool tip and data points properly
- Why the line in this D3 chart isn't properly displayed when updated?
- D3JS chart: Hovering on some transitioning elements while others are still transitioning too stops the execution of the chart animation
- Line chart not hitting the right value on chart and has a smooth line
- D3.js how to make the lines not go inside the arc of a donut chart
- d3js - my line chart is not working
- D3 Multi Line Chart Not Displaying Properly With X Axis Value
- Values are not reflecting Dynamically in D3 Bar Chart using. Overlapping occur with the existing Graph.
- Drawing a line using d3 is not visible when all data items are the same
- d3JS Chart Not Rendering Properly
- Line Chart path in D3 JS not displaying Properly
- change the color of the lines in the line chart vegalite
- Why are the first two text labels of my bar chart not shown?
- D3JS Line Chart Transition between lines
- d3 line chart not displayed properly
- dc Line chart not highlighting properly in Firefox on legend hover
- Using D3, I am trying to add and remove lines from a multi line chart when the legend is clicked
- D3.js horizontal line chart does not show tool tip properly
- d3.js: The xaxis labels are showing bold for a line chart
- Issue in generating the chart with required color, i.e. specified colors are not displayed
- My chart lines are not in order
- D3js canvas and line are not visible
More Query from same tag
- How to animate the radius and the arc length of a pie chart in d3 (at the same time)?
- Adding new data point entries to a line chart
- Change starting point of alongPath animation
- D3 collapsible tree with attributes
- Transition path length with marker in D3.js makes markers vanish
- D3 (with angular) Failing to insert a span inside a div
- Progress bar: how to make color transition according to the progress?
- d3: how to focus separate tooltips for stacked area chart?
- unable to retrieve mutated data from getter
- d3 transitioning barchart with variable length
- How to show only specific node level in d3 tree layout
- d3 Cumulative Line Chart
- Why isn't this line chart drawing? (d3js)
- D3.js Chart Random Data or Weather API
- Set opacity of circles based on the occurrences in the parent element (Group) - D3.js
- Why are the first two text labels of my bar chart not shown?
- Making pie charts using d3.js?
- Is this JSON I made using jsonify with flask and python 3 formatted correctly for making a D3 graph? And if not, how should I format it?
- How to force d3js to take hours into account when x axis is created?
- Typings Error when using D3 V4 on Angular 2 and Typescript V2
- Inversion with ordinal scale
- arrows on links in d3js force layout
- How to force a specific amount of y axis ticks in d3 charts?
- Starting d3 force-layout collapsed, text labels duplicating
- D3 Selecting all elements having a certain class or combination of classes
- D3 bar chart transition confusion
- How can I update text-label in d3 donut according with my array's data
- Using .filter() on a horizontal brush
- Textarea do not show in nested svg
- Uncaught ReferenceError: d3 is not defined