Skip to content

Commit

Permalink
Working on using the series style attribute to influence axis scaling
Browse files Browse the repository at this point in the history
for bars and columns, specifically referencing a zero baseline.
Generally, only the chart type is used.

Fix crash in label formatter in
samples/source/stacked-column-with-line*.xml. Tried to format an
undefined value when all series are collapsed.
  • Loading branch information
rosco54 committed Mar 8, 2024
1 parent 0b95446 commit fec4d6c
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 39 deletions.
2 changes: 1 addition & 1 deletion samples/react/column/stacked-column-with-line-new.html
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion samples/react/column/stacked-column-with-line.html
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions samples/react/misc/annotations-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
show: false,
},
labels: series.monthDataSeries1.dates,
yaxis: {
min: 8000
},
xaxis: {
type: 'datetime',
}
Expand Down
3 changes: 1 addition & 2 deletions samples/react/mixed/line-column-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@
yaxis: {
title: {
text: 'Points',
},
min: 0
}
},
tooltip: {
shared: true,
Expand Down
2 changes: 1 addition & 1 deletion samples/react/mixed/line-column.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Line, Column &amp; Area</title>
<title>Line &amp; Column</title>

<link href="../../assets/styles.css" rel="stylesheet" />

Expand Down
2 changes: 1 addition & 1 deletion samples/source/column/stacked-column-with-line-new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ xaxis: {
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion samples/source/column/stacked-column-with-line.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ xaxis: {
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions samples/source/misc/annotations-example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ legend: {
show: false,
},
labels: series.monthDataSeries1.dates,
yaxis: {
min: 8000
},
xaxis: {
type: 'datetime',
}
Expand Down
3 changes: 1 addition & 2 deletions samples/source/mixed/line-column-area.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ xaxis: {
yaxis: {
title: {
text: 'Points',
},
min: 0
}
},
tooltip: {
shared: true,
Expand Down
2 changes: 1 addition & 1 deletion samples/source/mixed/line-column.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<title>Line, Column &amp; Area</title>
<title>Line &amp; Column</title>

<chart>
<options>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion samples/vanilla-js/column/stacked-column-with-line.html
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions samples/vanilla-js/misc/annotations-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
show: false,
},
labels: series.monthDataSeries1.dates,
yaxis: {
min: 8000
},
xaxis: {
type: 'datetime',
}
Expand Down
3 changes: 1 addition & 2 deletions samples/vanilla-js/mixed/line-column-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@
yaxis: {
title: {
text: 'Points',
},
min: 0
}
},
tooltip: {
shared: true,
Expand Down
2 changes: 1 addition & 1 deletion samples/vanilla-js/mixed/line-column.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Line, Column &amp; Area</title>
<title>Line &amp; Column</title>

<link href="../../assets/styles.css" rel="stylesheet" />

Expand Down
2 changes: 1 addition & 1 deletion samples/vue/column/stacked-column-with-line-new.html
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion samples/vue/column/stacked-column-with-line.html
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
labels: {
showDuplicates: false,
formatter: function(value) {
return value.toFixed(0)
return value?.toFixed(0) // Value is undefined if all series are collapsed
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions samples/vue/misc/annotations-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@
show: false,
},
labels: series.monthDataSeries1.dates,
yaxis: {
min: 8000
},
xaxis: {
type: 'datetime',
}
Expand Down
3 changes: 1 addition & 2 deletions samples/vue/mixed/line-column-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@
yaxis: {
title: {
text: 'Points',
},
min: 0
}
},
tooltip: {
shared: true,
Expand Down
2 changes: 1 addition & 1 deletion samples/vue/mixed/line-column.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Line, Column &amp; Area</title>
<title>Line &amp; Column</title>

<link href="../../assets/styles.css" rel="stylesheet" />

Expand Down
53 changes: 34 additions & 19 deletions src/modules/Range.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ class Range {
}

getMinYMaxY(
startingIndex,
startingSeriesIndex,
lowestY = Number.MAX_VALUE,
highestY = -Number.MAX_VALUE,
endingIndex = null
endingSeriesIndex = null
) {
const cnf = this.w.config
const gl = this.w.globals
let maxY = -Number.MAX_VALUE
let minY = Number.MIN_VALUE

if (endingIndex === null) {
endingIndex = startingIndex + 1
if (endingSeriesIndex === null) {
endingSeriesIndex = startingSeriesIndex + 1
}

let firstXIndex = 0
let lastXIndex = 0
let seriesX = undefined
if (gl.seriesX.length >= endingIndex) {
seriesX = [...new Set([].concat(...gl.seriesX.slice(startingIndex, endingIndex)))]
if (gl.seriesX.length >= endingSeriesIndex) {
seriesX = [...new Set([].concat(...gl.seriesX.slice(startingSeriesIndex, endingSeriesIndex)))]
firstXIndex = 0
lastXIndex = seriesX.length - 1
// Eventually brushSource will be set if the current chart is a target.
Expand Down Expand Up @@ -83,9 +83,11 @@ class Range {
seriesMax = gl.seriesRangeEnd
}

for (let i = startingIndex; i < endingIndex; i++) {
for (let i = startingSeriesIndex; i < endingSeriesIndex; i++) {
gl.dataPoints = Math.max(gl.dataPoints, series[i].length)

const seriesType = cnf.series[i].type

if (gl.categoryLabels.length) {
gl.dataPoints = gl.categoryLabels.filter(
(label) => typeof label !== 'undefined'
Expand Down Expand Up @@ -120,7 +122,7 @@ class Range {
// Array : CandleO, CandleH, CandleM, CandleL, CandleC
// Candlestick: O H L C
// Boxplot : Min Q1 Median Q3 Max
switch (cnf.series[i].type) {
switch (seriesType) {
case 'candlestick': {
if (typeof gl.seriesCandleC[i][j] !== 'undefined') {
maxY = Math.max(maxY, gl.seriesCandleH[i][j])
Expand All @@ -135,13 +137,14 @@ class Range {
}
}

// there is a combo chart and the specified series in not either candlestick, boxplot, or rangeArea/rangeBar; find the max there
// there is a combo chart and the specified series in not either
// candlestick, boxplot, or rangeArea/rangeBar; find the max there.
if (
cnf.series[i].type &&
(cnf.series[i].type !== 'candlestick' &&
cnf.series[i].type !== 'boxPlot' &&
cnf.series[i].type !== 'rangeArea' &&
cnf.series[i].type !== 'rangeBar')
seriesType &&
(seriesType !== 'candlestick' &&
seriesType !== 'boxPlot' &&
seriesType !== 'rangeArea' &&
seriesType !== 'rangeBar')
) {
maxY = Math.max(maxY, gl.series[i][j])
lowestY = Math.min(lowestY, gl.series[i][j])
Expand Down Expand Up @@ -177,6 +180,17 @@ class Range {
gl.hasNullValues = true
}
}
if (seriesType === 'bar' || seriesType === 'column') {
if (minY < 0 && maxY < 0) {
// all negative values in a bar series, hence make the max to 0
maxY = 0
highestY = Math.max(highestY, 0)
}
if (minY === Number.MIN_VALUE) {
minY = 0
lowestY = Math.min(lowestY, 0)
}
}
}

if (
Expand Down Expand Up @@ -212,20 +226,21 @@ class Range {
gl.minY = Number.MIN_VALUE

let lowestYInAllSeries = Number.MAX_VALUE
let minYMaxY

if (gl.isMultipleYAxis) {
// we need to get minY and maxY for multiple y axis
lowestYInAllSeries = Number.MAX_VALUE
for (let i = 0; i < gl.series.length; i++) {
const minYMaxYArr = this.getMinYMaxY(i)
gl.minYArr[i] = minYMaxYArr.lowestY
gl.maxYArr[i] = minYMaxYArr.highestY
lowestYInAllSeries = Math.min(lowestYInAllSeries, minYMaxYArr.lowestY)
minYMaxY = this.getMinYMaxY(i)
gl.minYArr[i] = minYMaxY.lowestY
gl.maxYArr[i] = minYMaxY.highestY
lowestYInAllSeries = Math.min(lowestYInAllSeries, minYMaxY.lowestY)
}
}

// and then, get the minY and maxY from all series
const minYMaxY = this.getMinYMaxY(
minYMaxY = this.getMinYMaxY(
0,
lowestYInAllSeries,
null,
Expand Down

0 comments on commit fec4d6c

Please sign in to comment.