1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-18 20:59:39 +02:00

Fix chart scale issues (#2418)
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions

This commit is contained in:
Zach Gollwitzer 2025-06-26 18:59:11 -04:00 committed by GitHub
parent 8db95623cf
commit 18148acd69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -508,15 +508,31 @@ export default class extends Controller {
}
get _d3YScale() {
const reductionPercent = this.useLabelsValue ? 0.3 : 0.05;
const dataMin = d3.min(this._normalDataPoints, this._getDatumValue);
const dataMax = d3.max(this._normalDataPoints, this._getDatumValue);
const padding = (dataMax - dataMin) * reductionPercent;
// Use 0 as baseline, but allow negative values if they exist
const yMin = Math.min(0, dataMin);
// Handle edge case where all values are the same (including all zeros)
const range = dataMax - yMin;
if (range === 0) {
// If all values are 0, show 0-100 scale. Otherwise center the value with padding.
const padding = dataMax === 0 ? 100 : Math.abs(dataMax) * 0.5;
return d3
.scaleLinear()
.rangeRound([this._d3ContainerHeight, 0])
.domain([yMin - padding, dataMax + padding]);
}
// Add padding to prevent overlapping with labels and for visual breathing room
const topPadding = range * 0.1;
const bottomPadding = range * (this.useLabelsValue ? 0.15 : 0.05);
return d3
.scaleLinear()
.rangeRound([this._d3ContainerHeight, 0])
.domain([dataMin - padding, dataMax + padding]);
.domain([yMin - bottomPadding, dataMax + topPadding]);
}
_setupResizeObserver() {