Skip to content

Commit

Permalink
Merge pull request #125 from ONSvisual/120-line-with-area-y-domain-ca…
Browse files Browse the repository at this point in the history
…lculation

Fixing y-domain calculations
  • Loading branch information
MikeLister authored Sep 1, 2023
2 parents d9470a5 + 3952766 commit 5928387
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion line-chart-with-area/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ config = {
// "lineCurveType": "curveCatmullRom" // Catmull-Rom spline curve
// "lineCurveType": "curveMonotoneX" // Monotone spline curve

"xDomain": "auto",
"yDomain": [0,7],
// either "auto" or an array for the x domain e.g. [0,2000]
"xAxisTickFormat": {
"sm": "%b %y",
Expand Down
16 changes: 10 additions & 6 deletions line-chart-with-area/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ function drawGraphic() {

const y = d3
.scaleLinear()
.domain([
0,
d3.max(graphic_data, (d) => Math.max(...fulldataKeys.map((c) => d[c])))
])
// .nice()
.range([height, 0]);

if (config.essential.yDomain == "auto") {
y.domain(
[d3.min(graphic_data, (d) => Math.min(...fulldataKeys.map((c) => d[c]))),
d3.max(graphic_data, (d) => Math.max(...fulldataKeys.map((c) => d[c])))]
)
} else {
y.domain(config.essential.yDomain)
}
//console.log(`yAxis defined`);


Expand Down Expand Up @@ -249,7 +253,7 @@ function drawGraphic() {
.attr('transform', `translate(0, 0)`)
.append('text')
.attr('x', -margin.left + 10)
.attr('y', 0)
.attr('y', -10)
.attr('class', 'axis--label')
.text(config.essential.yAxisLabel)
.attr('text-anchor', 'start');
Expand Down

0 comments on commit 5928387

Please sign in to comment.