Skip to content

Commit 1093365

Browse files
committed
Changes to accommodate issue 4632:
1. Unconditionally select default initial axis ticks based on SVG size. 2. Make half-stepSize adjustment to axis ticks only if multiAxis chart. 3. Unconditionally, if not user specified, snap yMin or yMax to zero if already close. 4. Re-order yaxes in one sample chart to prioritize stacked column visual resolution. Add comment to draw attention to this feature of multi axis charts. 5. In Scales.niceScale: Check that maxTicks is always valid (suspect some Unit tests don't set SVG dimensions). 6. Set tickAmount explicitly in two unit tests to compensate for variation in scaling following the above changes.
1 parent cad1d2f commit 1093365

File tree

7 files changed

+75
-41
lines changed

7 files changed

+75
-41
lines changed

samples/react/column/stacked-column-with-line-new.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,19 @@
295295
}
296296
}
297297
},
298+
// In order to align the ticks of multiple yaxes with the charts horizontal grid
299+
// lines, the scaling of the first [unhidden] yaxis determines the number
300+
// of grid lines and hence the number of ticks for all subsequent yaxes, unless
301+
// they specify a tickAmount.
302+
// We want the stacked bars to have the best possible resolution
303+
// visually so they have been listed first.
298304
yaxis: [
299305
{
300-
seriesName: 'Line',
301-
opposite: true
306+
seriesName: ['Bar1','Bar2','Bar3','bar4']
302307
},
303308
{
304-
seriesName: ['Bar1','Bar2','Bar3','bar4']
309+
seriesName: 'Line',
310+
opposite: true
305311
}
306312
],
307313
legend: {

samples/source/column/stacked-column-with-line-new.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,19 @@ xaxis: {
7676
}
7777
}
7878
},
79+
// In order to align the ticks of multiple yaxes with the charts horizontal grid
80+
// lines, the scaling of the first [unhidden] yaxis determines the number
81+
// of grid lines and hence the number of ticks for all subsequent yaxes, unless
82+
// they specify a tickAmount.
83+
// We want the stacked bars to have the best possible resolution
84+
// visually so they have been listed first.
7985
yaxis: [
8086
{
81-
seriesName: 'Line',
82-
opposite: true
87+
seriesName: ['Bar1','Bar2','Bar3','bar4']
8388
},
8489
{
85-
seriesName: ['Bar1','Bar2','Bar3','bar4']
90+
seriesName: 'Line',
91+
opposite: true
8692
}
8793
],
8894
legend: {

samples/vanilla-js/column/stacked-column-with-line-new.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,19 @@
278278
}
279279
}
280280
},
281+
// In order to align the ticks of multiple yaxes with the charts horizontal grid
282+
// lines, the scaling of the first [unhidden] yaxis determines the number
283+
// of grid lines and hence the number of ticks for all subsequent yaxes, unless
284+
// they specify a tickAmount.
285+
// We want the stacked bars to have the best possible resolution
286+
// visually so they have been listed first.
281287
yaxis: [
282288
{
283-
seriesName: 'Line',
284-
opposite: true
289+
seriesName: ['Bar1','Bar2','Bar3','bar4']
285290
},
286291
{
287-
seriesName: ['Bar1','Bar2','Bar3','bar4']
292+
seriesName: 'Line',
293+
opposite: true
288294
}
289295
],
290296
legend: {

samples/vue/column/stacked-column-with-line-new.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,19 @@
298298
}
299299
}
300300
},
301+
// In order to align the ticks of multiple yaxes with the charts horizontal grid
302+
// lines, the scaling of the first [unhidden] yaxis determines the number
303+
// of grid lines and hence the number of ticks for all subsequent yaxes, unless
304+
// they specify a tickAmount.
305+
// We want the stacked bars to have the best possible resolution
306+
// visually so they have been listed first.
301307
yaxis: [
302308
{
303-
seriesName: 'Line',
304-
opposite: true
309+
seriesName: ['Bar1','Bar2','Bar3','bar4']
305310
},
306311
{
307-
seriesName: ['Bar1','Bar2','Bar3','bar4']
312+
seriesName: 'Line',
313+
opposite: true
308314
}
309315
],
310316
legend: {

src/modules/Scales.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export default class Scales {
2929
axisCnf = w.config.yaxis[index]
3030
maxTicks = Math.max((gl.svgHeight - 100) / 15, 2)
3131
}
32+
if (!Utils.isNumber(maxTicks)) {
33+
maxTicks = 10
34+
}
3235
gotMin = axisCnf.min !== undefined && axisCnf.min !== null
3336
gotMax = axisCnf.max !== undefined && axisCnf.min !== null
3437
let gotStepSize =
@@ -37,8 +40,6 @@ export default class Scales {
3740
axisCnf.tickAmount !== undefined && axisCnf.tickAmount !== null
3841
let ticks = gotTickAmount
3942
? axisCnf.tickAmount
40-
: !axisCnf.forceNiceScale
41-
? 10
4243
: gl.niceScaleDefaultTicks[
4344
Math.min(
4445
Math.round(maxTicks / 2),
@@ -100,19 +101,17 @@ export default class Scales {
100101
// Determine Range
101102
let range = Math.abs(yMax - yMin)
102103

103-
if (axisCnf.forceNiceScale) {
104-
// Snap min or max to zero if close
105-
let proximityRatio = 0.15
106-
if (!gotMin && yMin > 0 && yMin / range < proximityRatio) {
107-
yMin = 0
108-
gotMin = true
109-
}
110-
if (!gotMax && yMax < 0 && -yMax / range < proximityRatio) {
111-
yMax = 0
112-
gotMax = true
113-
}
114-
range = Math.abs(yMax - yMin)
104+
// Snap min or max to zero if close
105+
let proximityRatio = 0.15
106+
if (!gotMin && yMin > 0 && yMin / range < proximityRatio) {
107+
yMin = 0
108+
gotMin = true
109+
}
110+
if (!gotMax && yMax < 0 && -yMax / range < proximityRatio) {
111+
yMax = 0
112+
gotMax = true
115113
}
114+
range = Math.abs(yMax - yMin)
116115

117116
// Calculate a pretty step value based on ticks
118117

@@ -231,18 +230,25 @@ export default class Scales {
231230
} else {
232231
// Snap range to ticks
233232
if (!gotMin && !gotMax) {
234-
if (gotTickAmount) {
235-
// Allow a half-stepSize shift if series doesn't cross the X axis
236-
// to ensure graph doesn't clip. Not if it does cross, in order
237-
// to keep the 0 aligned with a grid line in multi axis charts.
238-
let shift = stepSize / (yMax - yMin > yMax ? 1 : 2)
239-
let tMin = shift * Math.floor(yMin / shift)
240-
if (Math.abs(tMin - yMin) <= shift / 2) {
241-
yMin = tMin
242-
yMax = yMin + stepSize * tiks
243-
} else {
244-
yMax = shift * Math.ceil(yMax / shift)
245-
yMin = yMax - stepSize * tiks
233+
if (gl.isMultipleYAxis && gotTickAmount) {
234+
// Ensure graph doesn't clip.
235+
let tMin = stepSize * Math.floor(yMin / stepSize)
236+
let tMax = tMin + stepSize * tiks
237+
if (tMax < yMax) {
238+
stepSize *= 2
239+
}
240+
yMin = tMin
241+
tMax = yMax
242+
yMax = yMin + stepSize * tiks
243+
// Snap min or max to zero if possible
244+
range = Math.abs(yMax - yMin)
245+
if (yMin > 0 && yMin < Math.abs(tMax - yMax)) {
246+
yMin = 0
247+
yMax = stepSize * tiks
248+
}
249+
if (yMax < 0 && -yMax < Math.abs(tMin - yMin)) {
250+
yMax = 0
251+
yMin = -stepSize * tiks
246252
}
247253
} else {
248254
yMin = stepSize * Math.floor(yMin / stepSize)
@@ -297,9 +303,9 @@ export default class Scales {
297303
}
298304

299305
// Prune tiks down to range if series is all integers. Since tiks > range,
300-
// range is very low (< 10 or so). Skip this step if tickAmount is true
301-
// because, either the user set it, or the chart is multiscale and this
302-
// axis is not determining the number of grid lines.
306+
// range is very low (< 10 or so). Skip this step if gotTickAmount is true
307+
// because either the user set tickAmount or the chart is multiscale and
308+
// this axis is not determining the number of grid lines.
303309
if (
304310
!gotTickAmount &&
305311
axisCnf.forceNiceScale &&

tests/unit/exponential-data.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ describe('Exponential values should parse', () => {
7272
]
7373
}
7474
],
75+
yaxis: {
76+
tickAmount: 10
77+
},
7578
xaxis: {
7679
type: 'datetime'
7780
}

tests/unit/small-range.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ describe('yaxis scale to ignore duplication if fractions are present in series',
139139
],
140140
},
141141
yaxis: {
142+
tickAmount: 8,
142143
labels: {
143144
formatter: (val) => {
144145
return val.toFixed(2)

0 commit comments

Comments
 (0)