score:2
Accepted answer
Your major problems were these:
- You used
d3.max(data
, but data was not an array. I tookdata.rows
in a previous.then(
, which fixed this; - You used
selectAll
, creating onepath
per data point. For an area chart, you actually need only onepath
, or at least one per area. I changed that from.data().enter()
to.datum()
; - You used the values
d.y0
andd.y
, which do not exist on the data object; - You didn't provide the area with a
stroke
, only with afill
. If the area has no filling, that doesn't work.
I'm not saying that this is better, because the data is not ordered, but at least you now see something.
// Area chart width and height
const width = 500,
height = 100;
//Define x and y scale for area chart
const xScale = d3.scaleLinear().range([0, width]);
const yScale = d3.scaleLinear().range([height, 0]);
// Add SVG to #areachart
const svg = d3
.select('#areachart')
.append('svg')
.attr('width', width)
.attr('height', height);
const g = svg.append('g');
// Fetch data
d3.json('https://data.london.gov.uk/api/table/s8c9t_j4fs2?$offset=7600')
.then(response => response.rows)
.then(function(data) {
//Define xScale & yScale domain after data loaded
yScale.domain([
0,
d3.max(data, function(d) {
return +d.total_cases;
}),
]);
xScale.domain(
d3.extent(data, function(d) {
return +d.new_cases;
})
);
// Area generator
const area = d3
.area()
.curve(d3.curveBasis)
.x((d) => xScale(+d.new_cases))
.y((d) => yScale(+d.total_cases));
g.append('g')
.datum(data)
.append('path')
.classed('line', true)
.attr('d', area)
.style('stroke', 'red')
.style('fill', 'red');
})
// if there's an error, log it
.catch((error) => console.log(error));
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.9.2/d3.min.js"></script>
<section id="map">
<div id="visualisation-container">
<div id="visualisation"></div>
<div id="areachart"></div>
</div>
</section>
Source: stackoverflow.com
Related Query
- d3 area chart not being displayed
- D3 v5 area chart not being generated
- D3 - tooltip not being displayed for line chart
- d3 pie chart all black fill, d3.schemeCategory20c not being called
- Label names are not displayed from input data in pie transition chart of d3.js
- area chart with d3 is not rendering on jsp page but working fine with html
- D3.js bar chart colors not shown in print out but displayed on browser
- D3 dots do not match curved area chart
- DC.js Line Chart - no line being displayed
- d3js chart dots and area not updating
- d3js multi-line chart is being drawn off the plot area - is update pattern to the issue?
- C3.js make area chart not opaque
- Pie Chart not rendering in dashboard area using D3 v5
- D3 -- Arcs for chord diagram not being displayed
- D3 - y axis not being appended to chart
- d3.js data imported but bar chart not displayed
- d3.js Stacked Area chart - not appearing
- d3.hexbin data points not being displayed correctly
- Data not being redrawn after being filtered on d3 bar chart
- d3 - legend for parallel coordinate chart not displayed
- d3 line chart not displayed properly
- D3 Pie Chart new path values not being calculated after update
- Line - area chart not showing
- D3 area chart not rendering end to end
- D3.js Basic Chart Drawing Example not displayed
- X axis label not displayed in bar chart with scroll and zoom feature - d3js, Brushing
- Issue in generating the chart with required color, i.e. specified colors are not displayed
- Last tick not being displayed in d3 charts
- Why <text> inside my svg is NOT being displayed
- Graph line not moving with zoomable chart area
More Query from same tag
- derive array of unique property values from an array of objects
- How can I add a label with value to every point of the chart?
- D3 Choropleth Map: Colors are not appearing
- D3 version 4 AXIS
- How to stop d3 headings from appending again and again everytime chart updates
- Click outside of Div to close it. SVG & D3, Angular
- Jshint: error : d3 is not defined
- D3.js how can I generate a variable width stacked chart (Marimekko chart)
- d3, html in svg, animating opacity takes div out of position, why?
- D3 Implementation: custom topology image behind D3 map
- D3 - How can I put an SVG into a DIV via a function?
- How to debug overridden code of a library function in javascript?
- How to know if topojson is installed and working normally?
- How to extract top 10 data in d3.js?
- X,Y domain data-binding in multiple grouped bar charts in d3.js
- Bar chart: set axis domain from csv without header
- d3 read csv with d3.map and d3 queue() function
- Display non-ASCII characters from CSV file retrieved with d3.csv
- How to sort a grouped bar-chart d3js v4
- How to change negative Y axis domain to be positive
- Selecting null: what is the reason behind selectAll(null) in D3?
- d3 focus on node on click
- D3 tick with background
- D3.js v4 - set padding for bar chart
- Creating chart from multi level Object in D3
- d3 pie chart with link in tooltip
- show additional data properties on mouseover in dc.js
- How to Limit Size of Radius With d3.js
- Merge objects that share a common value in D3
- Getting NaN values in svg path 'd' attr when trying to map geojson data