score:1
- The
timeParse
assigned to the variableparseTimeUtc
is the correct one considering the format of the dates in your data. Once you parse the dates in the
forEach
loop, you don't need to parse them again while setting the xAxis or within theline
generator function.temp_data.forEach(function (d) { d.date = parseTime_utc(d.date) d.temperature = +d.temperature; });
All the dates are the same and so I've made slight changes (months) to make the line visible.
Added the following
style
to the line:fill:none; stroke: steelblue;
to make the line visible.You had a SVG appended within a SVG (copy/paste error I suppose). Anyway, changed
#watertemp_graph
to a<div></div>
.
Snippet:
var temp_data = [
{"temperature": "11.0", "date": "2018-08-22T14:53:37.267Z" },
{"temperature": "11.2", "date": "2018-07-22T14:53:37.267Z" },
{"temperature": "10.9", "date": "2018-08-22T14:53:37.267Z" },
{"temperature": "11.3", "date": "2018-05-22T14:53:37.267Z" },
{"temperature": "11.0", "date": "2018-08-22T14:53:37.267Z" }]
var parseDate = d3.timeParse("%Y-%m-%d %X"); //27-May-12 16:00:00. This is used for D3JS parsing
var formatTime = d3.timeFormat("%Y-%m-%d %X");
var parseTime_utc = d3.timeParse("%Y-%m-%dT%H:%M:%S.%LZ");
var formatDate_utc = d3.timeFormat("%b %d, %Y %H:%M:%S");
// var parse = parseDate(moment.utc(d.date).format("YYYY-MM-DD HH:mm:ss"))
temp_data.forEach(function (d) {
d.date = parseTime_utc(d.date)
d.temperature = +d.temperature;
});
var margin = { top: 30, right: 10, bottom: 50, left: 70 },
width = 600 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
// Parse the date / time
// Set the ranges
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
// Define the axes
var xAxis = d3.axisBottom(x)
.ticks(5);
var yAxis = d3.axisLeft(y)
.ticks(5);
// Define the line
var valueline = d3.line()
.x(function (d) { return x(d['date']); })
.y(function (d) { return y(d['temperature']); });
// Adds the svg canvas
var svg = d3.select("#" + "watertemp_graph")
.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 + ")");
// Scale the range of the data
x.domain(d3.extent(temp_data, function (d) { return d.date; }));
y.domain(d3.extent(temp_data, function (d) { return d['temperature']; }));
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(temp_data)).style('fill', 'none').style('stroke', 'steelblue');
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
<script src="https://d3js.org/d3.v4.min.js"></script>
<div id="watertemp_graph">
</div>
Updated codepen: https://codepen.io/anon/pen/bmmbzw
Also, if you could let us know why you have the other timeParse
rs and timeFormat
s, maybe I can help you with using those as well.
Hope this helps.
UPDATE: Need of a flexible parser?
As stated in the docs by Mike: If a more flexible parser is desired, try multiple formats sequentially until one returns non-null and my suggestion would be to be to further validate the non-null value (date) by following this answer here.
Source: stackoverflow.com
Related Query
- D3 Persistent Path d expected number error during transition
- "Error: <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…". " Error with D3
- D3 - attribute d: Expected number Error in creating curved chart
- SVG path error: <path> attribute d: Expected number
- Error: <path> attribute d: expected number - when trying to build a line chart with D3
- Angular with d3: Error: <path> attribute d: Expected number, "MNaN,25.384615384…"
- Error: D3 V4 Multi-series line chart with Angular-cli - <path> attribute d: Expected number
- D3 Simple Line Chart: Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "[object Object]"
- With D3, how can I set attribute ("fill", "none") of axis path and line, but not text (without editing stylesheet)
- d3.selection.prototype throwing an error with typescript and angular 2
- D3.js geoPath map returning Error: <path> attribute d: Expected number
- Error when transitioning an arc: <path> attribute d: Expected arc flag ('0' or '1')
- d3 Error: <path> attribute d: Expected number date
- Stumped by d3 error I'm receiving - Error: <path> attribute d: Expected number, "M0,NaNL21.923076923…"
- Prob reading bar chart with d3js v7 Error: <rect> attribute height: Expected length, "NaN"
- 'undefined is not a function' error at runtime with Angular 2/Typescript function
- d3.js/Typescript: Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "function line(da…"
- d3.js - attribute d: Expected number
- Drawing line in d3js results in d3.v5.min.js:2 Error: <path> attribute d: Expected number
- d3, line chart, Error: <path> attribute d: Expected number
- Attribute cy returning expected length error in D3 V4
- d3.v5.min.js:2 Error: <path> attribute d: Expected moveto path command ('M' or 'm'), "function a(a){va…"
- D3 scaleBand() resulting in path attribute error
- D3 line chart - Error: <path> attribute d: Expected number
- Error with D3 line graph path
- D3 fill for a horizontal stacked bar chart in Angular with typeScript: .attr("fill", ..) gets error "No overload matches this call" in VSC
- Facing problem with : Error: <rect> attribute height: Expected length, "NaN" and Error: <rect> attribute y: Expected length, "NaN"
- Error: <path> attribute d: Expected number D3
- <path> attribute d: Expected number, "MNaN,NaNLNaN,NaNL…". Error while creating a line chart
- Angular 7 integration with D3: adding click listener throws error
More Query from same tag
- How to implement Javascript within Salesforce Visual Force?
- Having both mouse events and bar transitions
- d3 SVG text element background-color with getBBox() wrong order
- How to show the barlength in px of a SVG above the mouse pointer
- javascript return map with key as empty string
- Getting d3.js to work with raphael.js
- How to run a D3 example
- D3.js - Drawing points on map fails due to wrong projection
- How can I run a javascript as a node.js script without the DOM?
- How do I create a new scale() function in d3.js? I would like to create a cumulative distribution function
- Draw vertical line after n data points in d3
- invalid date at d3.scaleTime()
- Creating Column Chart with DC.js
- D3JS TopoJSON map of Australia: projection accurate but no country rendered
- How to remove gaps in C3 timeseries chart?
- Keep D3 objects size constant on Map while zoom in/out
- JS Callback (passing data to D3)
- Round images in D3.js Tree graph
- d3js call animations on an object, enter/append is not a function
- D3 Tree Layout: I want to use jQuery to toggle all children
- Time jump in Dimple Line Graphic
- CSV Data Fail to Load in IE but loads in Chrome, Firefox, and Edge in my SharePoint Site Using D3
- D3 - Polylines from label to arc in Donut or Pie Chart
- Can't run d3js to a website (jupyter notebook)
- dc.js reset does not affect mouseZoomable
- Why can't I place a div over a SVG using z-index
- Integrate data in html for Parallel Coordinates visualization in D3
- how to zoom d3.js chart and reload data with greater granularity (in AJAX)
- How to set y tick value in c3.js?
- d3.js Sankey diagram: rectangles fill color