score:3
Accepted answer
Change your data structure to something like this:
var data = [{
name: "cat",
value: 10
}, {
name: "dog",
value: 3
}, {
name: "pig",
value: 7
}, {
name: "bird",
value: 7
}];
That way, you can define the line generator (look at the demo to see what those scales are):
var line = d3.line()
.x(function(d) {
return xScale(d.name)
})
.y(function(d) {
return yScale(d.value)
});
Thus, the x
argument is the qualitative value of the name
variable, while the y
is the quantitative value of the value
variable.
The important point here is: if you're using a qualitative variable for the x axis, set a qualitative scale for dealing with that qualitative variable, and pass the return of such scale to the line generator.
Here is the demo:
var data = [{
name: "cat",
value: 10
}, {
name: "dog",
value: 3
}, {
name: "pig",
value: 7
}, {
name: "bird",
value: 7
}];
var width = 500,
height = 200;
var svg = d3.selectAll("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var xScale = d3.scalePoint()
.domain(data.map(function(d) {
return d.name
}))
.range([50, width - 50])
.padding(0.5);
var yScale = d3.scaleLinear()
.domain([0, d3.max(data, function(d) {
return d.value
}) * 1.1])
.range([height - 50, 10]);
var line = d3.line()
.x(function(d){ return xScale(d.name)})
.y(function(d){ return yScale(d.value)});
svg.append("path")
.attr("d", line(data))
.attr("stroke", "teal")
.attr("stroke-width", "2")
.attr("fill", "none");
var xAxis = d3.axisBottom(xScale);
var yAxis = d3.axisLeft(yScale);
svg.append("g").attr("transform", "translate(0,150)")
.attr("class", "xAxis")
.call(xAxis);
svg.append("g")
.attr("transform", "translate(50,0)")
.attr("class", "yAxis")
.call(yAxis);
<script src="https://d3js.org/d3.v4.min.js"></script>
Source: stackoverflow.com
Related Query
- javascript d3 line graph with ordinal x axis
- JavaScript d3 line graph with ordinal axis and json data
- wrong date printing on x axis of line graph in d3 with extra tick
- Multi line graph with ordinal x-axis
- line graph renders wrongly and problem with the X axis (time), cannot brush the graph - time series dc.js
- Getting d3 axis tick marks to line up center with graph points
- Real time line graph with nvd3.js
- javascript charting - nvd3 line chart with two Y-axis
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- How to show a tooltip with value when mouseover a svg line graph using d3.js?
- D3: skip null values in line graph with several lines
- Horizontal pan with ordinal x axis in d3
- Line chart using ordinal x-axis with d3.js and nvd3.js
- D3 line graph with arbitrarily many lines (and a specific data format)
- Visualize date specific data with a line chart in browser with javascript
- Animate lines in a line graph with D3.js
- Multiples with x/y axis and mouseover example - line path shows wrong values
- dc.js bar chart with ordinal x axis scale doesn't render correctly
- d3.js:vertical moving line's intersection point with line graph
- D3.js zoom with Ordinal axis not working - Issue with setup of scale in zoom behavior?
- Simple line graph with dates in D3
- d3js graph retaining old axis data on refreshing with new data
- d3 line graph with smooth update animation
- Creating a D3 Line Graph with Points using 2 Level Nested JSON
- Add dots on a multi line D3.js Graph with nested data
- How to line up x axis on stacked bar chart with a line overlay in d3.js
- d3 v4 TypeScript DefinitelyTyped Angular2 line with ScaleTime on the X axis
- How to remove axis line overflow in c3js line graph
- d3 bar chart with fixed bar width and fixed spacing to align in center axis line
- Line chart not aligned with x axis
More Query from same tag
- D3 Pie chart initial render issue
- How can I prevent new elements overwriting existing elements in d3.js?
- D3.js events firing on load
- Is there a built-in way to have unfilled stroked SVG objects clickable?
- d3v4 checkout chart - making sure the color scheme is correct for the legend and circles
- Updating NVD3 /D3 chart as per user input radio button
- Adding text labels in middle of each bar - stacked bar chart & Mouse Over Events
- rxjs combining BehaviorSubject and Zip Observables
- Getting query string at end of URL and using it to fetch data
- Error parsing D3 d=""repeated two times
- Positioning svg element over div
- How to rearrange nested array by the same key's property
- How to iterate over data in JSON file using D3 javascript
- dimple js measure axis values
- y axis set custom values in D3 Charts v4
- getting error with d3 (V4) Stacked Bar Chart
- D3 geo node plotting on map?
- Ghost line path when zooming and panning a D3 chart
- How to add zoom-in and zoom-out buttons in dc.geoChoroplethChart
- JavaScript refresh file contents
- linking nodes of variable radius with arrows
- D3 - Update bar chart data onclick
- Trouble Shooting Cannot Read Property 'length' of Undefined
- D3 DataMaps: How to stack bubbles based on radius?
- Changing d3.js symbol size based on the attribute of data
- d3js - my line graph not working, and throws error
- Problems with the x and y axis
- Uncaught TypeError: canvas.selectAll(...).data(...).enter is not a function in d3
- d3-transition attrTween, styleTween methods are not getting triggered in d3 version 4
- Not able to update the graph and axes both dynamically in D3.js