score:3
Accepted answer
You are correct, the problem is d3.curveBasis
indeed.
As you can see, d3.curveBasis
is an interpolator that generates a path which will not pass exactly over the control points:
My suggestion is using an interpolator which generates a path that passes over the control points, such as d3.curveCatmullRom
:
Here is your code with that change:
data = [
{
"login_date": "2017-09-24",
"unique_user_count": 2,
"total_login_count": 2
},
{
"login_date": "2017-09-25",
"unique_user_count": 25,
"total_login_count": 46
},
{
"login_date": "2017-09-26",
"unique_user_count": 31,
"total_login_count": 74
},
{
"login_date": "2017-09-27",
"unique_user_count": 29,
"total_login_count": 58
},
{
"login_date": "2017-09-28",
"unique_user_count": 29,
"total_login_count": 60
},
{
"login_date": "2017-09-29",
"unique_user_count": 31,
"total_login_count": 71
},
{
"login_date": "2017-09-30",
"unique_user_count": 1,
"total_login_count": 1
},
{
"login_date": "2017-10-01",
"unique_user_count": 1,
"total_login_count": 1
},
{
"login_date": "2017-10-02",
"unique_user_count": 41,
"total_login_count": 71
},
{
"login_date": "2017-10-03",
"unique_user_count": 30,
"total_login_count": 67
},
{
"login_date": "2017-10-04",
"unique_user_count": 28,
"total_login_count": 45
},
{
"login_date": "2017-10-05",
"unique_user_count": 32,
"total_login_count": 48
},
{
"login_date": "2017-10-06",
"unique_user_count": 30,
"total_login_count": 50
},
{
"login_date": "2017-10-07",
"unique_user_count": 1,
"total_login_count": 1
},
{
"login_date": "2017-10-08",
"unique_user_count": 1,
"total_login_count": 1
},
{
"login_date": "2017-10-09",
"unique_user_count": 35,
"total_login_count": 76
},
{
"login_date": "2017-10-10",
"unique_user_count": 37,
"total_login_count": 63
},
{
"login_date": "2017-10-11",
"unique_user_count": 41,
"total_login_count": 76
},
{
"login_date": "2017-10-12",
"unique_user_count": 42,
"total_login_count": 83
},
{
"login_date": "2017-10-13",
"unique_user_count": 41,
"total_login_count": 68
},
{
"login_date": "2017-10-15",
"unique_user_count": 1,
"total_login_count": 1
},
{
"login_date": "2017-10-16",
"unique_user_count": 48,
"total_login_count": 84
},
{
"login_date": "2017-10-17",
"unique_user_count": 50,
"total_login_count": 98
},
{
"login_date": "2017-10-18",
"unique_user_count": 38,
"total_login_count": 56
},
{
"login_date": "2017-10-19",
"unique_user_count": 38,
"total_login_count": 98
},
{
"login_date": "2017-10-20",
"unique_user_count": 40,
"total_login_count": 71
},
{
"login_date": "2017-10-22",
"unique_user_count": 2,
"total_login_count": 2
}];
//Define SVG container full width and height
const fullWidth = 600;
const fullHeight = 200;
//Define bar chart area widht and height
const margin = {
top: 10,
bottom: 10,
left: 20,
right: 20
}
const chartWidth = fullWidth - margin.left - margin.right;
const chartHeight = fullHeight - margin.top - margin.bottom;
//Draw SVG container
let svg = d3.select('body')
.append('svg')
.attr('width', fullWidth)
.attr('height', fullHeight);
//Define xand y scale range of the bar chart
const xScale = d3.scaleBand()
.range([0, chartWidth]);
const yScale = d3.scaleLinear()
.range([chartHeight, 0]);
const yScale2 = d3.scaleLinear()
.range([chartHeight, 0]);
console.log('Data received from an API:', data)
//defiene x and y scale domain
yScale
.domain([0, d3.max(data, d => +d.total_login_count)]);
yScale2
.domain([0, d3.max(data, d => +d.unique_user_count)]);
xScale
.domain(data.map(d => d.login_date));
//Generate total login area chart
let area = d3.area()
.curve(d3.curveCatmullRom)
.x(function (d) {
return xScale(d.login_date);
})
.y0(fullHeight)
.y1(function (d) {
return yScale(+d.total_login_count)
});
//Generate unique user count area chart
let area2 = d3.area()
.curve(d3.curveCatmullRom)
.x(function (d) {
return xScale(d.login_date);
})
.y0(fullHeight)
.y1(function (d) {
return yScale2(+d.unique_user_count)
});
//Draw bar chart
let group = svg.selectAll('g')
.data([data])
.enter()
.append('g');
//Draw area for total login count
group
.append('path')
.attr('class', 'area')
.attr('d', area);
//Draw area for unique user count
group
.append('path')
.attr('class', 'area2')
.attr('d', area2);
//Dot points
let points = group.selectAll('circle')
.data(data)
.enter()
.append('circle');
//Dot points
let points2 = group.select('circle')
.data(data)
.enter()
.append('circle');
points.attrs({
"cx": d => xScale(d.login_date),
"cy": d => yScale(+d.total_login_count),
"r": 5
})
.style("opacity", 1)
.style('fill', '#F9A2CB');
points2.attrs({
"cx": d => xScale(d.login_date),
"cy": d => yScale2(+d.unique_user_count),
"r": 5
})
.style("opacity", 1)
.style('fill', '#8BDBCE');
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bar chart</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-fetch.v1.min.js"></script>
<script src='https://d3js.org/d3-selection-multi.v0.4.min.js'></script>
</head>
<style>
.line {
fill: none;
stroke: orange;
stroke-width: 1px;
}
.area {
fill: #F9A2CB;
stroke: none;
opacity: 0.6;
}
.area2 {
fill: #8BDBCE;
stroke: none;
opacity: 0.6;
}
</style>
<body>
<script type="text/javascript" src="main.js"></script>
</body>
</html>
Source: stackoverflow.com
Related Query
- Circle points are not aligned with area shape
- Data points and ticks in the scaleBand axis are not aligned
- Multiline chart x-axis ticks not aligned with data points
- Markers are not properly aligned with path in d3 with leaflet
- d3 lat/long points are not lining up with map
- Insert padding so that points do not overlap with y or x-axis
- Angular 4 + D3v4 : dragging circle not working correctly in conjunction with zooming
- Why are programmatically inserted SVG <tspan> elements not drawn, except with d3.js?
- Elements appended to SVG with D3 are not appearing
- D3 graph with area fill not working
- area chart with d3 is not rendering on jsp page but working fine with html
- Generate random points within an svg circle area in d3 or javascript
- d3 line and area charts not updating with new data array
- d3.js - draw ellipse shape with 4 points by svg path
- Line chart not aligned with x axis
- 10 points within unit circle - finding optimal distribution with D3 force layout?
- responsive d3 area graph stretches circle interaction points
- Circle not appearing on graph made with D3
- Fill not working with areaRadial() generated shape
- Not all d3 points are showing in correct position on leaflet map
- d3.js - dragmove circle with v6 not work as expected
- How can I apply a clipPath in SVG with multiple paths and NOT clip out the area between the paths?
- Values are not reflecting Dynamically in D3 Bar Chart using. Overlapping occur with the existing Graph.
- DC.js bar chart x-axis labels not aligned with bars
- x-axis text labels not aligned with bars in D3.js
- nvd3 multibar chart with stacked option the bars are not appearing properly
- Values on the top of the bars are not moving along with the bars
- Starting point of x-axis in d3.js bar chart is not aligned with the y-intercept
- D3 Bar chart. Values not aligned with Y axis
- the lines' points x1, x2, y1, y2 do not coincide with nodes in a radial tree
More Query from same tag
- How can I add a "%" to all values in d3.js?
- Understanding D3 Force-Layout: Why previous node values x,y NOT EQUAL px,py after 1 timestep?
- jquery tipsy tooltip not working with d3.js circles
- possible to tween d3.geo.circle().angle()
- Creating Map Custom Visual in Power BI
- How to select and deselect a svg line and change its color on selection and deselection using d3.js and javascript , jquery?
- Flask+d3.js: Passing multiple datasets to html
- how to handle the padding when brush moving
- Drawing a graph with images and arrows with d3 issues
- Draw/update circles in rect - nested selections confusion
- "export 'mouse' (imported as 'd3') was not found in 'd3'
- Javascript for loop Uncaught TypeError from data attributes
- javascript and d3.js variable scope: variable resets outside of function call
- d3js v5 + Topojson v3 Map with border rendering
- Highlight path in d3 tree is not working in IE
- d3js diagonal paths in tree layout don't look right
- Error parsing data when drawing d3 line chart
- Basic Help for Geojson with d3
- Putting HTML Table into ToolTip in D3.js
- Visual Studio 2015 Import d3 Cannot Find Module 'd3'
- D3 time axis - why do tick marks seem to be invisible by default?
- Change bar chart into pie chart
- How to make d3 path stroke to curve inside? Like the reverse of "cardinal-closed" interpolation
- Linked hover between elements
- D3 js selecting rectangles in
- Remove overlapping dots from chart
- Render tick at zero y-value with d3 series plot
- D3 exit().remove() on stack area chart
- In D3 v4, how do I keep the width of a bar centered around a tick mark?
- D3.js color the node circles with a function using spectrum.js