Reputation: 23
I'm trying to use d3.js to display a graph using a json object (string) that is being pulled form a server on the code behind of an aspx webpage. I've been troubleshooting the past couple of days, and have hit a brick wall.
I am getting:
d3.v5.min.js:2 Error: <path> attribute d: Expected number, "MNaN,189.61165048…".
And when I log the dttm and xScale dttm, I get this (this is just one example, not the entire result):
dttm: 2019-07-29 23:59:53 xScale: undefined
So it's clear to me that when i'm calling xScale(d.dttm) in the line function, it is returning null which could explain the problem i'm having.
Any assistance on how I can go about getting this corrected would be greatly appreciated!
var jsonObject = <%=json%>;
var parseTime = d3.timeParse("%Y-%m-%dT%H:%M:%SZ");
var formatTime = d3.timeFormat("%Y-%m-%d %H:%M:%S");
jsonObject.forEach(function (d) {
d.dttm = formatTime(parseTime(d.dttm));
d.perc = +d.perc;
});
var dataNest = d3.nest()
.key(function (d) { return d.server_name; })
.entries(jsonObject);
var margin = { top: 30, right: 20, bottom: 30, left: 50 },
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
var xScale = d3.scaleTime()
.domain(d3.extent(jsonObject, function (d) { return d.dttm; }))
.range([0, width]);
var yScale = d3.scaleLinear()
.domain([0, 103])
.range([height, 0]);
var colorScale = d3.scaleOrdinal(d3.schemeCategory10);
var xAxis = d3.axisBottom().scale(xScale).ticks(5);
var yAxis = d3.axisLeft().scale(yScale).ticks(5);
var line = d3.line()
.x(function (d) {
console.log("dttm: " + d.dttm + " xScale: " + xScale(d.dttm));
return xScale(d.dttm);
})
.y(function (d) { return yScale(d.perc); });
var svg = d3.select("#dataviz")
.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 + ")");
d3.data = jsonObject;
dataNest.forEach(function (d, i) {
console.log("index: " + i + " server: " + d.key + " color: " + colorScale(i) + " the object is below...");
console.log(d);
svg.append("path")
.attr("class", "line")
.attr("d", line(d.values))
.style("stroke", colorScale(i))
});
EDIT: Here are some useful results from Console.log()
jsonObject.forEach(function (d) {
console.log(parseTime(d.dttm));
d.dttm = formatTime(parseTime(d.dttm));
d.perc = +d.perc;
});
returns (just a couple of examples, the data set is quite large:
Mon Jul 29 2019 23:58:43 GMT-0600 (Mountain Daylight Time)
Mon Jul 29 2019 23:58:46 GMT-0600 (Mountain Daylight Time)
Mon Jul 29 2019 23:58:48 GMT-0600 (Mountain Daylight Time)
Mon Jul 29 2019 23:59:08 GMT-0600 (Mountain Daylight Time)
jsonObject.forEach(function (d) {
d.dttm = formatTime(parseTime(d.dttm));
d.perc = +d.perc;
});
console.log(jsonObject);
returns (just a sample, the dataset is large): https://i.sstatic.net/xIZit.jpg
Here is the JSON.stringify for jsonObject
[{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:03","perc":3},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:04","perc":3},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:06","perc":18},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:09","perc":10},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:13","perc":5},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:14","perc":5},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:16","perc":5},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:19","perc":9},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:23","perc":13},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:24","perc":4},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:26","perc":2},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:29","perc":6},{"server_name":"XXXXXXX","dttm":"2019-07-29 00:00:33","perc":14},
Upvotes: 2
Views: 559
Reputation: 1677
Since I don't have access to your dataset, I can't really test this out but - I think the issue is in the datetime parsing. formatTime returns a string, which is not what the scaleTime function needs to create an x-scale (it needs date-time format to convert to x-scale). You need to take that part out of your code.
jsonObject.forEach(function (d) {
d.dttm = parseTime(d.dttm);
d.perc = +d.perc;
});
Upvotes: 1