score:1
Accepted answer
You just need to add the data points (years) you want to skip, using a value later verified by line.defined
. You can do this programatically, but here I'm just hardcoding the year 2001 with null
as value.
Then, your line generator can be:
const line = d3.line()
.x(d => scaleX(d.Year))
.y(d => scaleY(d.Value))
.defined(d => d.Value)
Since null
is falsy, I'm just checking if Value
is truthy.
Here's your code with that change:
const src = [{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 1997,
"Value": 15.540540540540499
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 1998,
"Value": 15.540540540540499
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 1999,
"Value": 22.4489795918367
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2000,
"Value": 22.972972972973
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2001,
"Value": null
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2002,
"Value": 25.3333333333333
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2003,
"Value": 25.3333333333333
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2004,
"Value": 24.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2005,
"Value": 24.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2006,
"Value": 24.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2007,
"Value": 26.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2008,
"Value": 26.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2009,
"Value": 27.3333333333333
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2010,
"Value": 24.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2011,
"Value": 24.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2012,
"Value": 24.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2013,
"Value": 26
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2014,
"Value": 26
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2015,
"Value": 26.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2016,
"Value": 28.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2017,
"Value": 28.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2018,
"Value": 28.6666666666667
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2019,
"Value": 30.463576158940398
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2020,
"Value": 30.463576158940398
},
{
"Region": "East Asia & Pacific",
"Name": "Australia",
"Year": 2021,
"Value": 31.125827814569501
}
]
////////////////////////////////////////////////////////////
//////////////////////// 1 CREATE SVG ////////////////////
////////////////////////////////////////////////////////////
const width = 1280;
const height = 720;
const svgns = 'http://www.w3.org/2000/svg'
const svg = d3.select('svg')
svg
//.attr('xmlns', svgns)
.attr('viewBox', `0 0 ${width} ${height}`)
//---create a rect covering viewBox --- to be deleted later
svg.append('rect')
.attr('class', 'vBoxRect')
.attr('width', `${width}`)
.attr('height', `${height}`)
.attr('fill', 'none')
.attr('stroke', 'red')
////////////////////////////////////////////////////////////
//////////////////////// 2 CREATE BOUND ////////////////////
////////////////////////////////////////////////////////////
const padding = {
top: 70,
bottom: 50,
left: 70,
right: 190
}
const boundHeight = height - padding.top - padding.bottom;
const boundWidth = width - padding.right - padding.left;
//create BOUND rect -- to be deleted later
svg.append('rect')
.attr('class', 'boundRect')
.attr('x', `${padding.left}`)
.attr('y', `${padding.top}`)
.attr('width', `${boundWidth}`)
.attr('height', `${boundHeight}`)
.attr('fill', 'none')
.attr('stroke', 'green')
//create bound element
const bound = svg.append('g')
.attr('class', 'bound')
//specify transform, must be .style and not .attr, px needs to be mentioned
.style('transform', `translate(${padding.left}px,${padding.top}px)`)
////////////////////////////////////////////////////////////
//////////////////////// 3 CREATE SCALE ////////////////////
////////////////////////////////////////////////////////////
const scaleX = d3.scaleLinear()
.range([0, boundWidth])
.domain(d3.extent(src, d => d.Year))
const scaleY = d3.scaleLinear()
.range([boundHeight, 0])
.domain(d3.extent(src, d => d.Value))
////////////////////////////////////////////////////////////
//////////////////////// 4 CREATE AXIS ////////////////////
////////////////////////////////////////////////////////////
bound.append('g').attr('class', 'yAxis')
.append('g').attr('class', 'yAxisDetail')
.call(d3.axisLeft(scaleY))
const data2 = src.map(d => d.Year)
const count = [...new Set(data2)].length - 1
const minYear = Math.min([...src])
bound.append('g')
.attr('class', 'xAxis')
.append('g')
.attr('class', 'xAxisBottom')
.style('transform', `translateY(${boundHeight}px)`)
.call(d3.axisBottom(scaleX).ticks(count).tickFormat(d3.format("d")))
////////////////////////////////////////////////////////////
//////////////////////// 5 CREATE PATH ////////////////////
////////////////////////////////////////////////////////////
const line = d3.line()
.x(d => scaleX(d.Year))
.y(d => scaleY(d.Value))
.defined(d => d.Value)
bound.append('g')
.attr('class', 'valLine')
.append('path')
.attr('d', line(src))
.attr('stroke', 'black')
.attr('fill', 'none')
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>
<body>
<div id="container" class="svg-container"></div>
<svg>
</svg>
</body>
<script type="text/javascript">
</script>
</html>
Source: stackoverflow.com
Related Query
- d3 to account for missing data in line chart
- C3js - combination chart with data labels only for line
- how to display data from 1st point on words on y axis for line chart in d3.js
- Time in x-axis and data for the y-axis for line chart in d3.js doesn't match/show with what (data) is on JSON
- Specifying hard coded input data for D3 Multi-Series Line Chart
- How to implement tooltip for D3 line chart with data from 2 arrays?
- In d3js, how to plot line chart from csv file, taking data from seperate columns for each row
- how to create labels for data in donut chart using d3.js
- NVD3 line chart with realtime data
- nvd3.js chart ajax data redraw - missing hovereffect + former yAxis scale
- NVD3 Line Chart X Axis Ticks Are Missing
- line chart of json data in d3
- How to plot animated line chart using d3 while the data table is required to be updated every 1 second?
- Visualize date specific data with a line chart in browser with javascript
- D3: How to use exit().remove() for Multi-Series Line Chart
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
- Complex data object with duplicate 'values' = missing chart bars
- d3 show labels only for ticks with data in a bar chart
- Dictionary data ito d3 line chart - Django Python in server side
- how to display bottom data for row chart using dc.js
- Javascript manipulate data into array of object for d3 stacked chart
- D3.js: plot dots on the existing line in a multiseries line chart with long formatted data
- hack for d3 pie chart with 'zero' data
- D3.js Error: Invalid value for <path> attribute for a line chart
- Missing a step for d3 connecting data in choropleth map
- D3 chart for visualizing data by sector and overall and algo explained
- Loading new data to a C3.js line chart
- NVD3 Line Chart doesn't display when data passed using ajax - data.map is not a function
- Passing data for nvd3 chart from Flask as an argument
- Make a line chart in D3.js loading data from an array
More Query from same tag
- d3 multiline voronoi not working
- Border radius in SVG d3
- External javascript cannot run in D3JS
- Access more data from flare.json for d3.tree
- Strange Gridline Behavior
- Rickshaw RangeSlider - Zoom in by default
- Data keyfunction filter always returning first element
- D3 update values to a new dataset on mouse click
- Using setTimeout to delay d3 transition
- Change orientation of Arrow Marker in D3 oriented force graph
- d3-js › Centroid issue in a pie chart
- Convert utc time string to date object
- D3: Create a grouped bar chart from json objects
- How to use the D3 diagonal function to draw curved lines?
- D3 hide .text element from node
- How to get access to the selected element?
- How can I extend D3?
- Trying to animate a circle along an arc path in D3.js (v3)
- How to add hypertext link to Cluster Force Layout circle
- d3.js: tickformat - adding a % sign without multiplying by 100
- Webpack error: Module has no exported member 'Rgb'
- c3js custom title on hover
- how to get the radial gradient to start from the centre of the pie chart?
- d3js set background color when using d3.zoom()
- Adding labels to a 3D pie chart in D3.js
- How to add location specific data to markers on a map
- Exit() is not working for d3 group bar chart
- Overlapping bars in c3 (v4) bar chart
- Using XML attributes in d3.js
- d3.js TSV to Javascript variable