score:0
Accepted answer
Fyi
you are not using your own created function
parseDate
function and type
function
when i read that function, i think somebody manage to rearange the d.Date
on the data
result
your axis domain is [ invalid date, invalid date ]
solution
i convert d.Date to proper format
you sending data on each item not in array
result
your chart not drawing nor having error
solution
i make it double array on jsonData
Line_chart.data([jsonData])
context.data([jsonData])
take a look
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.zoom {
cursor: move;
fill: none;
pointer-events: all;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script type="text/javascript">
//Example: https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f
let jsonData1 =
[
{"Date": "1/1/2014 01:00", "Air_Temp": 3.1},
{"Date": "1/1/2014 02:00", "Air_Temp": 3.2},
{"Date": "1/1/2014 03:00", "Air_Temp": 1.6},
{"Date": "1/1/2014 04:00", "Air_Temp": 1.0},
{"Date": "1/1/2014 05:00", "Air_Temp": 2.3}
];
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 110, left: 40},
margin2 = {top: 430, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
height2 = +svg.attr("height") - margin2.top - margin2.bottom;
var parseDate = d3.timeParse("%m/%d/%Y %H:%M");
let jsonData = jsonData1.map(function(d,i){ d.Date = parseDate(d.Date); return d })
var x = d3.scaleTime().range([0, width]),
x2 = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
y2 = d3.scaleLinear().range([height2, 0]);
var xAxis = d3.axisBottom(x),
xAxis2 = d3.axisBottom(x2),
yAxis = d3.axisLeft(y);
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush end", brushed);
var zoom = d3.zoom()
.scaleExtent([1, Infinity])
.translateExtent([[0, 0], [width, height]])
.extent([[0, 0], [width, height]])
.on("zoom", zoomed);
var line = d3.line()
.x(function (d) { return x(d.Date); })
.y(function (d) { return y(d.Air_Temp); });
var line2 = d3.line()
.x(function (d) { return x2(d.Date); })
.y(function (d) { return y2(d.Air_Temp); });
var clip = svg.append("defs").append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("width", width)
.attr("height", height)
.attr("x", 0)
.attr("y", 0);
var Line_chart = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.attr("clip-path", "url(#clip)");
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");
x.domain(d3.extent(jsonData, function(d) { return d.Date; }));
y.domain([0, d3.max(jsonData, function (d) { return d.Air_Temp; })]);
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
Line_chart.selectAll('path')
.data([jsonData])
.enter()
.append("path")
.attr("class", "line")
.attr("d", function (d){ line });
context.append("path")
.data([jsonData])
.attr("class", "line")
.attr("d", line2);
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x.range());
svg.append("rect")
.attr("class", "zoom")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
var s = d3.event.selection || x2.range();
x.domain(s.map(x2.invert, x2));
Line_chart.select(".line").attr("d", line);
focus.select(".axis--x").call(xAxis);
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
.scale(width / (s[1] - s[0]))
.translate(-s[0], 0));
}
function zoomed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
var t = d3.event.transform;
x.domain(t.rescaleX(x2).domain());
Line_chart.select(".line").attr("d", line);
focus.select(".axis--x").call(xAxis);
context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}
function type(d) {
d.Date = parseDate(d.Date);
d.Air_Temp = +d.Air_Temp;
return d;
}
</script>
</body>
</html>
score:0
You need to convert the dates
var parseDate = d3.timeParse("%m/%d/%Y %H:%M");
jsonData.forEach( d => { d.Date = parseDate(d.Date); });
And pass the dataset as one to the path
elements
Line_chart.append("path")
.data([jsonData])
.attr("class", "line")
.attr("d", line);
context.append("path")
.data([jsonData])
.attr("class", "line")
.attr("d", line2);
Source: stackoverflow.com
Related Query
- D3 graph not displaying JSON information from variable
- Line chart not displaying from JSON Data
- Updating links on a force directed graph from dynamic json data
- Render D3 graph from a string of JSON instead of a JSON file
- d3 piechart from local json variable
- Convert a d3 chart to load data from json inside a variable
- d3js pie graph from jquery ajax - correlating json keys and values on the chart
- Breaking text from json into several lines for displaying labels in a D3 force layout
- d3js Force Directed Graph - Click on node to popup infobox which read from JSON
- How to fetch data from json file to draw a dynamic graph by using d3.js
- D3.js doesn´t draw graph after changing source from tsv to JSON
- queue.js pass data from local variable not from external file
- very simple d3.js line graph not displaying in browser
- Getting graph attributes from JSON file with d3.js
- Passing JS variable consisting of content from a JSON into d3.json function
- Accessing JSON information from Rails in d3.js
- Displaying coordinates in D3 from JSON array
- Replacing fixed json variable with a json value returned from a php file
- JSON data from Django to D3 graph
- Displaying values from JSON along an axis
- Focus not displaying the graph properly D3 javascript
- d3.js tool tip on a force directed graph not displaying complete data while hovering over links
- d3.js line graph axis not displaying data correctly
- d3.request is not a function. get JSON from API Rest
- Line Graph using D3 is not starting from the date i want it to
- d3js - force: nodes and links from json file not loaded in my svg during redrawing
- Specifying Values in D3 Graph from JSON
- d3.js graph not displaying on asp.net page
- data not bind from json for d3 chart
- Extract array from one json variable
More Query from same tag
- d3.js using functions within d3.js-specific functions
- Slicing data in d3.js
- d3 js enter method not working
- Filter for multiple discrete values in crossfilter
- Dashed line transforms the null values into zeros on the chart
- d3.js sankey diagram: where does ".dy" and .dx" get set?
- nextAll selector, transform elements after clicked element
- IE11 does not accept SVG height and width when using D3
- node attributes in D3
- Filtering data using Checkboxes - D3
- How to set default value appearing in dropdown list with D3?
- Zoom to Box examples on https://bl.ocks.org do not work properly with Internet Explorer
- How to change the height of d3 observable notebook embed
- Sankey diagram using d3.js - Add information to link title
- Change Color of Links for Sankey Diagram in D3 / Javascript
- Creating interactive floor plan using d3.js (or similar)
- d3 grouped bar chart inconsistent items alignment
- Reusable D3 chart with common code separated out
- D3 version 5, how to chain promise to chart build
- Multi-series in dimplejs
- Javascript heatmap.js plugin to show data step by step in D3.js
- Is there a way to override c3_chart_internal_fn.generateResize on c3js?
- Unable to color Japan and North Korea in a d3 world map
- consume nested data in d3/javascript
- Two layers d3 map using on click event
- How To Build d3.js From Source?
- Closure with D3.Transition.Each Callback
- How to enable text selection in a D3.js-SVG that has zoom enabled?
- Issue with updating d3 interactive matrix scatterplot to v4
- How to link D3 zoom and pan controls