Skip to content

Commit e1a3f5e

Browse files
committed
Multiple-yaxis-scales, 3 series with 2 scales demo was broken by 3.47.0 after the introduction of the new yaxis:seriesName as an array feature. Refactored some of that code. Miscellaneous: Remove yaxis.min: 0 from the bar axes in sample as not required.
2 parents e5bf029 + f7bedbc commit e1a3f5e

File tree

6 files changed

+58
-31
lines changed

6 files changed

+58
-31
lines changed

samples/react/mixed/multiple-yaxes.html

-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
},
115115
yaxis: [
116116
{
117-
min: 0,
118117
seriesName: 'Income',
119118
axisTicks: {
120119
show: true,
@@ -139,7 +138,6 @@
139138
}
140139
},
141140
{
142-
min: 0,
143141
seriesName: 'Cashflow',
144142
opposite: true,
145143
axisTicks: {

samples/source/mixed/multiple-yaxes.xml

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ xaxis: {
3838
},
3939
yaxis: [
4040
{
41-
min: 0,
4241
seriesName: 'Income',
4342
axisTicks: {
4443
show: true,
@@ -63,7 +62,6 @@ yaxis: [
6362
}
6463
},
6564
{
66-
min: 0,
6765
seriesName: 'Cashflow',
6866
opposite: true,
6967
axisTicks: {

samples/vanilla-js/mixed/multiple-yaxes.html

-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
},
9898
yaxis: [
9999
{
100-
min: 0,
101100
seriesName: 'Income',
102101
axisTicks: {
103102
show: true,
@@ -122,7 +121,6 @@
122121
}
123122
},
124123
{
125-
min: 0,
126124
seriesName: 'Cashflow',
127125
opposite: true,
128126
axisTicks: {

samples/vue/mixed/multiple-yaxes.html

-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@
117117
},
118118
yaxis: [
119119
{
120-
min: 0,
121120
seriesName: 'Income',
122121
axisTicks: {
123122
show: true,
@@ -142,7 +141,6 @@
142141
}
143142
},
144143
{
145-
min: 0,
146144
seriesName: 'Cashflow',
147145
opposite: true,
148146
axisTicks: {

src/modules/Scales.js

+57-22
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,14 @@ export default class Scales {
578578
unassignedSeriesIndices.push(i)
579579
seriesYAxisReverseMap.push(null)
580580
})
581+
cnf.yaxis.forEach((yaxe, yi) => {
582+
axisSeriesMap[yi] = []
583+
})
584+
581585
let unassignedYAxisIndices = []
582586
// here, we loop through the yaxis array and find the item which has "seriesName" property
583587
cnf.yaxis.forEach((yaxe, yi) => {
588+
let assigned = false
584589
// Allow seriesName to be either a string (for backward compatibility),
585590
// in which case, handle multiple yaxes referencing the same series.
586591
// or an array of strings so that a yaxis can reference multiple series.
@@ -592,19 +597,27 @@ export default class Scales {
592597
} else {
593598
seriesNames.push(yaxe.seriesName)
594599
}
595-
axisSeriesMap[yi] = []
596600
seriesNames.forEach((name) => {
597601
cnf.series.forEach((s, si) => {
598-
// if seriesName matches we use that scale.
599602
if (s.name === name) {
603+
assigned = true
600604
axisSeriesMap[yi].push(si)
601-
seriesYAxisReverseMap[si] = yi
602-
let remove = unassignedSeriesIndices.indexOf(si)
603-
unassignedSeriesIndices.splice(remove, 1)
605+
let remove
606+
if (yi === si) {
607+
seriesYAxisReverseMap[si] = yi
608+
remove = unassignedSeriesIndices.indexOf(si)
609+
} else {
610+
seriesYAxisReverseMap[yi] = yi
611+
remove = unassignedSeriesIndices.indexOf(yi)
612+
}
613+
if (remove !== -1) {
614+
unassignedSeriesIndices.splice(remove, 1)
615+
}
604616
}
605617
})
606618
})
607-
} else {
619+
}
620+
if (!assigned) {
608621
unassignedYAxisIndices.push(yi)
609622
}
610623
})
@@ -667,26 +680,48 @@ export default class Scales {
667680
// 1: [1,2,3,4]
668681
// If the chart is stacked, it can be assumed that any axis with multiple
669682
// series is stacked.
683+
//
684+
// If this is an old chart and we are being backward compatible, it will be
685+
// expected that each series is associated with it's corresponding yaxis
686+
// through their indices, one-to-one.
687+
// If yaxis.seriesName matches series.name, we have indices yi and si.
688+
// A name match where yi != si is interpretted as yaxis[yi] and yaxis[si]
689+
// will both be scaled to fit the combined series[si] and series[yi].
690+
// Consider series named: S0,S1,S2 and yaxes A0,A1,A2.
691+
//
692+
// Example 1: A0 and A1 scaled the same.
693+
// A0.seriesName: S0
694+
// A1.seriesName: S0
695+
// A2.seriesName: S2
696+
// Then A1 <-> A0
697+
//
698+
// Example 2: A0, A1 and A2 all scaled the same.
699+
// A0.seriesName: S2
700+
// A1.seriesName: S0
701+
// A2.seriesName: S1
702+
// A0 <-> A2, A1 <-> A0, A2 <-> A1 --->>> A0 <-> A1 <-> A2
670703

671704
// First things first, convert the old to the new.
672-
let emptyAxes = []
673-
axisSeriesMap.forEach((axisSeries, ai) => {
674-
for (let si = ai + 1; si < axisSeriesMap.length; si++) {
675-
let iter = axisSeries.values()
676-
for (const val of iter) {
677-
let i = axisSeriesMap[si].indexOf(val)
678-
if (i !== -1) {
679-
axisSeries.push(si) // add series index to current yaxis
680-
axisSeriesMap[si].splice(i, 1) // remove it from its old yaxis
681-
}
705+
706+
// Consolidate series indices into axes
707+
if (axisSeriesMap.length === cnf.series.length) {
708+
axisSeriesMap.forEach((axisSeries, ai) => {
709+
let si = axisSeries[0]
710+
if (si !== ai) {
711+
axisSeriesMap[si].push(ai)
712+
axisSeriesMap[ai].splice(0,1)
682713
}
683-
if (axisSeriesMap[si].length < 1 && emptyAxes.indexOf(si) === -1) {
684-
emptyAxes.push(si)
714+
})
715+
// Now duplicate axisSeriesMap elements that need to be the same.
716+
axisSeriesMap.forEach((axisSeries, ai) => {
717+
if (!axisSeries.length) {
718+
axisSeriesMap.forEach((as, i) => {
719+
if (as.indexOf(ai) > -1) {
720+
axisSeriesMap[ai] = axisSeriesMap[i].map((x) => x)
721+
}
722+
})
685723
}
686-
}
687-
})
688-
for (let i = emptyAxes.length - 1; i >= 0; i--) {
689-
axisSeriesMap.splice(emptyAxes[i], 1)
724+
})
690725
}
691726

692727
// Compute min..max for each yaxis

src/modules/axes/Grid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ class Grid {
377377

378378
let xAxis = new XAxis(this.ctx)
379379
xAxis.drawXaxisTicks(x1, 0, w.globals.dom.elGraphical)
380-
x1 = x1 + w.globals.gridWidth / xCount + 0.3
380+
x1 = x1 + w.globals.gridWidth / xCount
381381
x2 = x1
382382
}
383383
}

0 commit comments

Comments
 (0)