Reputation: 512
I am having trouble creating a simple line chart with an array of data.
I am getting the error: Error: attribute d: Expected number, "MNaN,NaNLNaN,NaNL…"
I brought this data in through c# and have converted to an array that looks like:
[
{date: "2017-08-15 16:00:00", high: "73.41", low: "73.2"},
{date: "2017-08-15 15:45:00", high: "73.38", low: "73.2715"},
{date: "2017-08-15 15:30:00", high: "73.33", low: "73.28"},
{date: "2017-08-15 15:15:00", high: "73.305", low: "73.185"},
{date: "2017-08-15 15:00:00", high: "73.22", low: "73.15"},
{date: "2017-08-15 14:45:00", high: "73.24", low: "73.1816"},
]
I am trying to graph the date on the x axis and the high on the y axis. I have found quite a few questions on this and I have referenced a couple below.
Draw D3 Simple Line chart With an Array
Creating a line graph from array of data objects
With my type() function i convert the 'date' objects to type DateTime and i convert the 'high' to a number. I then use render to show the graph.
@foreach (var i in Model.Data)
{
<div>@i.Value.high</div>
}
<script>
var arrayData = [];
var i in Model.Data)
{
@:arrayData.push({date : '@i.Key', high : '@i.Value.high', low : '@i.Value.low'});
}
var outerWidth = 500;
var outerHeight = 500;
var margin = { left: 30, top: 30, right: 30, bottom: 30 };
var xColumn = "date";
var yColumn = "high";
// parse the date / time
var parseTime = d3.timeParse("%d-%b-%y");
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", outerWidth)
.attr("height", outerHeight);
//create a group
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//append a path element to our group
var path = g.append("path");
var innerWidth = outerWidth - margin.left - margin.right;
var innerHeight = outerHeight - margin.top - margin.bottom;
//set ranges
var xScale = d3.scaleTime().range([0, innerWidth]);
var yScale = d3.scaleLinear().range([innerHeight, 0]);
//changed the below from d[xColumn] to d.date
var line = d3.line()
.x(function (d) { return xScale(d.xColumn); })
.y(function (d) { return yScale(d.yColumn); });
//changed the below from d[xColumn] to d.date
function render(data) {
xScale.domain(d3.extent(data, function (d) { return d.date; }));
yScale.domain(d3.extent(data, function (d) { return d.high; }));
path.attr("d", line(data));
}
function type(d) {
d.date = new Date(d.date);
d.date = parseTime(d.date);
d.high = +d.high;
d.low = + d.low;
return d;
}
//below i have deterrmined that the type conversion is working by calling type() on the arrayData; i checked the arrayData.date before then after and saw it was converted to a object; I then checked
//to see if this was an instance of a date
console.log(arrayData.date);
type(arrayData);
var temp3 = (arrayData.date instanceof Date);
console.log(temp3);
var temp = typeof arrayData.date;
var temp2 = typeof arrayData.high;
console.log(temp);
console.log(temp2);
//try and run graph now
render(arrayData);
</script>
After searching all over, to me it looks like I have the right format. I am new to d3 and can not figure out what I am doing wrong. Can someone help?
Upvotes: 0
Views: 1130
Reputation: 92440
The way you are referencing your object's properties doesn't work in javascript.
You assign:
var xColumn = "date";
var yColumn = "high";
and then try to reference d.date
and d.high
with:
.x(function (d) { return xScale(d.xColumn); })
.y(function (d) { return yScale(d.yColumn); });
d.yColumn
and d.xColumn
will return undefined
. If you want to reference them this way you need to use notation like: d[xColumn]
and d[yColumn]
.
Upvotes: 1