score:1
The main problem here is that you are accessing your data incorrectly. Your data is an array with one element. That one element contains an object with name
, code
, and children
. So when you just pass the whole object to valueline()
it's not going to work because valueline
is then going to look for values that don't exist.
Also it looks like you are trying to access a date
attribute that doesn't exist with d.name = +d.date
The data you really want to plot is in data[0].children
. I've set the x-axis domain from the data, but if you want to control the order of an ordinal scale, it's often better to explicitly set the domain with something like x.domain(['SEASONAL_CYQ3', 'SEASONAL_CYQ4',...])
Here's a working example. It need a little stylizing, but I've started some text styling on the axes to get it started.
Also, it's worth upgrading to use d3 v4 if your just getting started with it.
var margin = {
top: 30,
right: 20,
bottom: 80,
left: 50
};
var width = 600 - margin.left - margin.right;
var height = 270 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
var valueline = d3.svg.line()
.x(function (d) {
return x(d.name);
})
.y(function (d) {
return y(d.count);
});
var svg = d3.select("body")
.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 + ")");
// Get the data
var data = [{
"name": "Seasonal Pop",
"code": "SEASONAL_POP",
"children": [{
"name": "SEASONAL_LYQ1",
"code": "SEASONAL_LYQ1",
"count": 1200
}, {
"name": "SEASONAL_LYQ2",
"code": "SEASONAL_LYQ2",
"count": 2000
}, {
"name": "SEASONAL_LYQ3",
"code": "SEASONAL_LYQ3",
"count": 1060
}, {
"name": "SEASONAL_LYQ4",
"code": "SEASONAL_LYQ4",
"count": 2300
}, {
"name": "SEASONAL_CYQ1",
"code": "SEASONAL_CYQ1",
"count": 1300
}, {
"name": "SEASONAL_CYQ2",
"code": "SEASONAL_CYQ2",
"count": 3400
}, {
"name": "SEASONAL_CYQ3",
"code": "SEASONAL_CYQ3",
"count": 4500
}, {
"name": "SEASONAL_CYQ4",
"code": "SEASONAL_CYQ4",
"count": 5500
}]
}];
x.domain(data[0].children.map(d => d.name))
y.domain([0, d3.max(data[0].children, function (d) {
return d.count;
})]);
console.log("xed", x(data[0].children[0].name))
svg.append("path") // Add the valueline path.
.attr("d", valueline(data[0].children))
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("y", 0)
.attr("x", 9)
.attr('font-size', '10px')
.attr("dy", ".35em")
.attr("transform", "rotate(90)")
.style("text-anchor", "start");
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis)
.selectAll('text')
.attr('font-size', '10px')
body {
font: 12px Arial;
}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path, .axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<body></body>
Source: stackoverflow.com
Related Query
- D3 - attribute d: Expected number Error in creating curved chart
- Error: <path> attribute d: expected number - when trying to build a line chart with D3
- Drawing line in d3js results in d3.v5.min.js:2 Error: <path> attribute d: Expected number
- D3js line chart error "attribute d: Expected number, "M49,NaNZ"."
- D3 line chart - Error: <path> attribute d: Expected number
- Error: D3 V4 Multi-series line chart with Angular-cli - <path> attribute d: Expected number
- <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…". Error while creating a line chart
- d3js line chart error - drawing weird area
- Prob reading bar chart with d3js v7 Error: <rect> attribute height: Expected length, "NaN"
- d3, line chart, Error: <path> attribute d: Expected number
- SVG viewBox Attribute Makes D3Js Line chart disappear
- Line chart of D3.js won't show due to <path> attribute d: Expected number, "M0,NaNL38.695652173…"
- D3.js Line Graph - Error: <path> attribute d: Expected number
- D3js line chart not transitioning, Tooltip error on new bars
- D3 Simple Line Chart: Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "[object Object]"
- Fisheye effect D3js image slideshow works on rects/objects but not image files, Error: <image> attribute x: Expected length, "NaN"
- D3 Persistent Path d expected number error during transition
- "Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…". " Error with D3
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
- d3js line chart -- how to extend last step to end of range?
- D3 line chart, Expected number shows number and NaN
- D3js - Getting a line chart drawn, from "JSON data" input using d3.json
- D3.js geoPath map returning Error: <path> attribute d: Expected number
- 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.js Error: Invalid value for <path> attribute for a line chart
- Error when transitioning an arc: <path> attribute d: Expected arc flag ('0' or '1')
- D3 multi-line chart - Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNC…"
- d3js - line chart `circle` placement to the lines are not properly sets
- d3 Error: <path> attribute d: Expected number date
More Query from same tag
- I'm trying to build D3.js by using CodeMirror editor, but anything I input in there can't be properly visualized by D3.js. What am I missing?
- How to make d3.js generate an svg without drawing it?
- How show jsPDF data on image
- How to Create Blank Lower 48 Map in Django JS
- create visualization by word count frequency in D3.js
- d3.brush is selecting surrounding divs with radial chart; only occurs d3 3.3+
- Is the order of the ".on" functions important in D3?
- Combine Nodes & Links in D3 Force Graph After CSV is Parsed to make one Array (Angular & D3.JS)
- Drawing line outside of axis in d3js line chart
- Positioning externally loaded svg in D3
- How to set background color for svg text tspan without foreign object?
- Draw line between rectangle from border to border
- dc charts look different when building Angular App in production mode
- d3.js - Bar chart won't display with scale
- Translating EmberJS D3-Charts to AngularJS
- How do I change XTick format in NVD3?
- d3.next.rollup returns undefined
- Adding labels to both ends of <rect> in a bar chart
- D3 assigning nodes labels
- How to use d3.line().curve when using x1, y1, and x2, y2?
- d3js - How to use the `this` keyword or what is the alternate?
- D3 nodes floating out of the frame
- NVD3 : Display different values on X axis as compared to the data
- D3.js Mouseover text inside donut chart
- d3 draw horizontal bipartite graph for word alignment visualization
- D3 taking an index string and classifying a circle as hidden or not
- How to make all the nodes circle the center node?
- How to generate a treemap using only squares
- how to create multiline chart using dc.js
- dc.js: Bar chart on subset of data