@@ -49,6 +49,7 @@ import { ILegend, ILegendContainer, Legends } from '../Legends/index';
49
49
import { DirectionalHint } from '@fluentui/react/lib/Callout' ;
50
50
import { IChart , IImageExportOptions } from '../../types/index' ;
51
51
import { toImage } from '../../utilities/image-export-utils' ;
52
+ import { ScaleLinear } from 'd3-scale' ;
52
53
53
54
const getClassNames = classNamesFunction < IAreaChartStyleProps , IAreaChartStyles > ( ) ;
54
55
@@ -133,6 +134,7 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
133
134
private _emptyChartId : string ;
134
135
private _cartesianChartRef : React . RefObject < IChart > ;
135
136
private _legendsRef : React . RefObject < ILegendContainer > ;
137
+ private _containsSecondaryYAxis = false ;
136
138
137
139
public constructor ( props : IAreaChartProps ) {
138
140
super ( props ) ;
@@ -189,6 +191,8 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
189
191
if ( ! this . _isChartEmpty ( ) ) {
190
192
const { lineChartData } = this . props . data ;
191
193
const points = this . _addDefaultColors ( lineChartData ) ;
194
+ this . _containsSecondaryYAxis =
195
+ ! ! this . props . secondaryYScaleOptions && points . some ( point => point . useSecondaryYScale ) ;
192
196
const { colors, opacity, data, calloutPoints } = this . _createSet ( points ) ;
193
197
this . _calloutPoints = calloutPoints ;
194
198
const isXAxisDateType = getXAxisType ( points ) ;
@@ -443,7 +447,7 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
443
447
const renderPoints : Array < IAreaChartDataSetPoint [ ] > = [ ] ;
444
448
let maxOfYVal = 0 ;
445
449
446
- if ( this . props . mode === 'tozeroy' ) {
450
+ if ( this . _shouldFillToZeroY ( ) ) {
447
451
keys . forEach ( ( key , index ) => {
448
452
const currentLayer : IAreaChartDataSetPoint [ ] = [ ] ;
449
453
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -480,7 +484,12 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
480
484
: renderPoints ?. length > 1 ) ;
481
485
return {
482
486
renderData : renderPoints ,
483
- maxOfYVal,
487
+ // The maxOfYVal prop is only required for the primary y-axis. When the data includes
488
+ // a secondary y-axis, the mode defaults to tozeroy, so maxOfYVal should be calculated using
489
+ // only the data points associated with the primary y-axis.
490
+ maxOfYVal : this . _containsSecondaryYAxis
491
+ ? findNumericMinMaxOfY ( this . props . data . lineChartData ! ) . endValue
492
+ : maxOfYVal ,
484
493
} ;
485
494
} ;
486
495
@@ -613,8 +622,10 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
613
622
containerHeight : number ,
614
623
containerWidth : number ,
615
624
xElement : SVGElement | null ,
625
+ yAxisElement ?: SVGElement | null ,
626
+ yScaleSecondary ?: ScaleLinear < number , number > ,
616
627
) => {
617
- this . _chart = this . _drawGraph ( containerHeight , xAxis , yAxis , xElement ! ) ;
628
+ this . _chart = this . _drawGraph ( containerHeight , xAxis , yAxis , yScaleSecondary , xElement ! ) ;
618
629
} ;
619
630
620
631
private _onLegendHover ( legend : string ) : void {
@@ -727,15 +738,22 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
727
738
return fillColor ;
728
739
} ;
729
740
730
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
731
- private _drawGraph = ( containerHeight : number , xScale : any , yScale : any , xElement : SVGElement ) : JSX . Element [ ] => {
741
+ private _drawGraph = (
742
+ containerHeight : number ,
743
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
744
+ xScale : any ,
745
+ yScalePrimary : ScaleLinear < number , number > ,
746
+ yScaleSecondary : ScaleLinear < number , number > | undefined ,
747
+ xElement : SVGElement ,
748
+ ) : JSX . Element [ ] => {
732
749
const points = this . _addDefaultColors ( this . props . data . lineChartData ) ;
733
750
const { pointOptions, pointLineOptions } = this . props . data ;
734
751
735
752
const graph : JSX . Element [ ] = [ ] ;
736
753
let lineColor : string ;
737
754
// eslint-disable-next-line @typescript-eslint/no-explicit-any
738
755
this . _data . forEach ( ( singleStackedData : Array < any > , index : number ) => {
756
+ const yScale = points [ index ] . useSecondaryYScale && yScaleSecondary ? yScaleSecondary : yScalePrimary ;
739
757
const curveFactory = getCurveFactory ( points [ index ] . lineOptions ?. curve , d3CurveBasis ) ;
740
758
const area = d3Area ( )
741
759
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -751,7 +769,7 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
751
769
// eslint-disable-next-line @typescript-eslint/no-explicit-any
752
770
. y ( ( d : any ) => yScale ( d . values [ 1 ] ) )
753
771
. curve ( curveFactory ) ;
754
- const layerOpacity = this . props . mode === 'tozeroy' ? 0.8 : this . _opacity [ index ] ;
772
+ const layerOpacity = this . _shouldFillToZeroY ( ) ? 0.8 : this . _opacity [ index ] ;
755
773
graph . push (
756
774
< React . Fragment key = { `${ index } -graph-${ this . _uniqueIdForGraph } ` } >
757
775
{ this . props . enableGradient && (
@@ -821,6 +839,8 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
821
839
return ;
822
840
}
823
841
842
+ const yScale = points [ index ] . useSecondaryYScale && yScaleSecondary ? yScaleSecondary : yScalePrimary ;
843
+
824
844
if ( ! this . props . optimizeLargeData || singleStackedData . length === 1 ) {
825
845
// Render circles for all data points
826
846
graph . push (
@@ -1064,4 +1084,8 @@ export class AreaChartBase extends React.Component<IAreaChartProps, IAreaChartSt
1064
1084
const { chartTitle, lineChartData } = this . props . data ;
1065
1085
return ( chartTitle ? `${ chartTitle } . ` : '' ) + `Area chart with ${ lineChartData ?. length || 0 } data series. ` ;
1066
1086
} ;
1087
+
1088
+ private _shouldFillToZeroY ( ) {
1089
+ return this . props . mode === 'tozeroy' || this . _containsSecondaryYAxis ;
1090
+ }
1067
1091
}
0 commit comments