@@ -578,9 +578,14 @@ export default class Scales {
578
578
unassignedSeriesIndices . push ( i )
579
579
seriesYAxisReverseMap . push ( null )
580
580
} )
581
+ cnf . yaxis . forEach ( ( yaxe , yi ) => {
582
+ axisSeriesMap [ yi ] = [ ]
583
+ } )
584
+
581
585
let unassignedYAxisIndices = [ ]
582
586
// here, we loop through the yaxis array and find the item which has "seriesName" property
583
587
cnf . yaxis . forEach ( ( yaxe , yi ) => {
588
+ let assigned = false
584
589
// Allow seriesName to be either a string (for backward compatibility),
585
590
// in which case, handle multiple yaxes referencing the same series.
586
591
// or an array of strings so that a yaxis can reference multiple series.
@@ -592,19 +597,27 @@ export default class Scales {
592
597
} else {
593
598
seriesNames . push ( yaxe . seriesName )
594
599
}
595
- axisSeriesMap [ yi ] = [ ]
596
600
seriesNames . forEach ( ( name ) => {
597
601
cnf . series . forEach ( ( s , si ) => {
598
- // if seriesName matches we use that scale.
599
602
if ( s . name === name ) {
603
+ assigned = true
600
604
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
+ }
604
616
}
605
617
} )
606
618
} )
607
- } else {
619
+ }
620
+ if ( ! assigned ) {
608
621
unassignedYAxisIndices . push ( yi )
609
622
}
610
623
} )
@@ -667,26 +680,48 @@ export default class Scales {
667
680
// 1: [1,2,3,4]
668
681
// If the chart is stacked, it can be assumed that any axis with multiple
669
682
// 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
670
703
671
704
// 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 )
682
713
}
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
+ } )
685
723
}
686
- }
687
- } )
688
- for ( let i = emptyAxes . length - 1 ; i >= 0 ; i -- ) {
689
- axisSeriesMap . splice ( emptyAxes [ i ] , 1 )
724
+ } )
690
725
}
691
726
692
727
// Compute min..max for each yaxis
0 commit comments