mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-18 20:59:39 +02:00
Fix chart scale issues (#2418)
This commit is contained in:
parent
8db95623cf
commit
18148acd69
1 changed files with 19 additions and 3 deletions
|
@ -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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue