Skip to content

Commit

Permalink
When chart is type 'bar', ensure reference to zero is maintained
Browse files Browse the repository at this point in the history
when zoomed and auto scaling the Y axis.

When forceNiceScale = true, set default ticks based on SVG size.

Collapsed series handling:
1) The series YAxis map now represents a single axis as well
as multiple axes so that collapsed series correctly manage axis
visibility there also in both types of charts.

2) Axis hiding now works for mixed charts that include stacked series.

3) Fix a detail that was prematurely hiding axes if series were not
available to assign to an axis until loaded via chart.updateSeries.

4) Use the first visible axis to render the grid ticks.

5) Fix for:
RangeArea Chart: hide all series results in a crash #3984
Deselecting all series in a rangeArea graph results in an error #4297

Tweaked some sample charts for improved presentation.
  • Loading branch information
rosco54 committed Mar 7, 2024
1 parent fe6901c commit d41387d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/modules/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ export default class Data {
range = this.handleRangeDataFormat('xy', ser, i)
}

gl.seriesRangeStart.push(range.start)
gl.seriesRangeEnd.push(range.end)
// Fix: RangeArea Chart: hide all series results in a crash #3984
gl.seriesRangeStart.push(range.start === undefined ? [] : range.start)
gl.seriesRangeEnd.push(range.end === undefined ? [] : range.end)

gl.seriesRange.push(range.rangeUniques)

Expand Down
1 change: 1 addition & 0 deletions src/modules/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class Range {
gl.minYArr[0] = gl.yAxisScale[0].niceMin
gl.maxYArr[0] = gl.yAxisScale[0].niceMax
gl.seriesYAxisMap = [gl.series.map((x, i) => i)]
gl.seriesYAxisReverseMap = gl.series.map((x, i) => 0)
}

return {
Expand Down
32 changes: 19 additions & 13 deletions src/modules/Scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export default class Scales {
if (gl.isBarHorizontal) {
axisCnf = w.config.xaxis
// The most ticks we can fit into the svg chart dimensions
maxTicks = (gl.svgWidth - 100) / 25 // Guestimate
maxTicks = Math.max((gl.svgWidth - 100) / 25, 2) // Guestimate
} else {
axisCnf = w.config.yaxis[index]
maxTicks = (gl.svgHeight - 100) / 15
maxTicks = Math.max((gl.svgHeight - 100) / 15, 2)
}
gotMin = axisCnf.min !== undefined && axisCnf.min !== null
gotMax = axisCnf.max !== undefined && axisCnf.min !== null
Expand Down Expand Up @@ -267,11 +267,6 @@ export default class Scales {
} else {
let yMaxPrev = yMax
yMax = stepSize * Math.ceil(yMax / stepSize)
if (Math.abs(yMax - yMin) / Utils.getGCD(range, stepSize) > maxTicks) {
// Use default ticks to compute yMax then shrinkwrap
yMax = yMin + stepSize * ticks
yMax -= stepSize * Math.floor((yMax - yMaxPrev) / stepSize)
}
}
}
range = Math.abs(yMax - yMin)
Expand Down Expand Up @@ -564,8 +559,12 @@ export default class Scales {
const maxYArr = gl.maxYArr

let axisSeriesMap = []
let seriesYAxisReverseMap = []
let unassignedSeriesIndices = []
cnf.series.forEach((s, i) => {unassignedSeriesIndices.push(i)})
cnf.series.forEach((s, i) => {
unassignedSeriesIndices.push(i)
seriesYAxisReverseMap.push(null)
})
let unassignedYAxisIndices = []
// here, we loop through the yaxis array and find the item which has "seriesName" property
cnf.yaxis.forEach((yaxe, yi) => {
Expand All @@ -586,6 +585,7 @@ export default class Scales {
// if seriesName matches we use that scale.
if (s.name === name) {
axisSeriesMap[yi].push(si)
seriesYAxisReverseMap[si] = yi
let remove = unassignedSeriesIndices.indexOf(si)
unassignedSeriesIndices.splice(remove, 1)
}
Expand All @@ -602,22 +602,28 @@ export default class Scales {
// default single and multiaxis config options which simply includes zero,
// one or as many yaxes as there are series but do not reference them by name.
let lastUnassignedYAxis
unassignedYAxisIndices.forEach((yi) => {
lastUnassignedYAxis = yi
axisSeriesMap[yi] = []
for (let i = 0; i < unassignedYAxisIndices.length; i++) {
lastUnassignedYAxis = unassignedYAxisIndices[i]
axisSeriesMap[lastUnassignedYAxis] = []
if (unassignedSeriesIndices) {
axisSeriesMap[yi].push([unassignedSeriesIndices[0]])
let si = unassignedSeriesIndices[0]
unassignedSeriesIndices.shift()
axisSeriesMap[lastUnassignedYAxis].push([si])
seriesYAxisReverseMap[si] = lastUnassignedYAxis
} else {
break
}
})
}

if (lastUnassignedYAxis) {
unassignedSeriesIndices.forEach((i) => {
axisSeriesMap[lastUnassignedYAxis].push(i)
seriesYAxisReverseMap[i] = lastUnassignedYAxis
})
}

gl.seriesYAxisMap = axisSeriesMap.map((x) => x)
gl.seriesYAxisReverseMap = seriesYAxisReverseMap.map((x) => x)
this.sameScaleInMultipleAxes(minYArr, maxYArr, axisSeriesMap)
}

Expand Down
6 changes: 5 additions & 1 deletion src/modules/axes/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,17 @@ class Grid {
this.elGridBorders.hide()
}

// Draw the grid using ticks from the first unhidden Yaxis.
// Draw the grid using ticks from the first unhidden Yaxis,
// or yaxis[0] is all hidden.
let gridAxisIndex = 0
while (gridAxisIndex < w.globals.seriesYAxisMap.length &&
w.globals.ignoreYAxisIndexes.indexOf(gridAxisIndex) !== -1
) {
gridAxisIndex++
}
if (gridAxisIndex === w.globals.seriesYAxisMap.length) {
gridAxisIndex = 0
}

let yTickAmount = w.globals.yAxisScale[gridAxisIndex].result.length - 1

Expand Down
32 changes: 15 additions & 17 deletions src/modules/legend/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,13 @@ export default class Helpers {
let series = Utils.clone(w.config.series)

if (w.globals.axisCharts) {
let shouldNotHideYAxis = false
let yaxis = w.config.yaxis[w.globals.seriesYAxisReverseMap[realIndex]]

if (
w.config.yaxis[realIndex] &&
w.config.yaxis[realIndex].show &&
w.config.yaxis[realIndex].showAlways
yaxis &&
yaxis.show &&
yaxis.showAlways
) {
shouldNotHideYAxis = true
if (w.globals.ancillaryCollapsedSeriesIndices.indexOf(realIndex) < 0) {
w.globals.ancillaryCollapsedSeries.push({
index: realIndex,
Expand All @@ -187,19 +186,18 @@ export default class Helpers {
})
w.globals.ancillaryCollapsedSeriesIndices.push(realIndex)
}
}

if (!shouldNotHideYAxis) {
w.globals.collapsedSeries.push({
index: realIndex,
data: series[realIndex].data.slice(),
type: seriesEl.parentNode.className.baseVal.split('-')[1]
})
w.globals.collapsedSeriesIndices.push(realIndex)

let removeIndexOfRising = w.globals.risingSeries.indexOf(realIndex)
} else {
if (w.globals.collapsedSeriesIndices.indexOf(realIndex) < 0) {
w.globals.collapsedSeries.push({
index: realIndex,
data: series[realIndex].data.slice(),
type: seriesEl.parentNode.className.baseVal.split('-')[1]
})
w.globals.collapsedSeriesIndices.push(realIndex)

w.globals.risingSeries.splice(removeIndexOfRising, 1)
let removeIndexOfRising = w.globals.risingSeries.indexOf(realIndex)
w.globals.risingSeries.splice(removeIndexOfRising, 1)
}
}
} else {
w.globals.collapsedSeries.push({
Expand Down
3 changes: 2 additions & 1 deletion src/modules/settings/Globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ export default class Globals {
// of divisors. The array is indexed using a calculated maxTicks value
// divided by 2 simply to halve the array size. See Scales.niceScale().
niceScaleDefaultTicks: [1,2,4,4,6,6,6,6,6,6,6,6,6,6,6,6,6,6,12,12,12,12,12,12,12,12,12,24],
seriesYAxisMap: [] // Indices of Series to Indices of yAxis map. Multiple series can be referenced to each yAxis.
seriesYAxisMap: [], // Given yAxis index, return all series indices belonging to it. Multiple series can be referenced to each yAxis.
seriesYAxisReverseMap: [] // Given a Series index, return its yAxis index.
}
}

Expand Down

0 comments on commit d41387d

Please sign in to comment.