@@ -3,11 +3,11 @@ import React, { ComponentPropsWithoutRef } from 'react'
33
44import useGetLatest from '../hooks/useGetLatest'
55import useIsomorphicLayoutEffect from '../hooks/useIsomorphicLayoutEffect'
6- import Area from '../seriesTypes/Area'
7- import Bar from '../seriesTypes/Bar'
6+ import Bar , { getPrimary } from '../seriesTypes/Bar'
87import Line from '../seriesTypes/Line'
98//
109import {
10+ Axis ,
1111 AxisDimensions ,
1212 AxisOptions ,
1313 AxisOptionsWithScaleType ,
@@ -24,7 +24,6 @@ import {
2424 materializeStyles ,
2525 getSeriesStatus ,
2626 getDatumStatus ,
27- sortDatumsBySecondaryPx ,
2827} from '../utils/Utils'
2928import buildAxisLinear from '../utils/buildAxis.linear'
3029import { ChartContextProvider } from '../utils/chartContext'
@@ -234,55 +233,45 @@ function ChartInner<TDatum>({
234233
235234 const primaryAxisOptions = React . useMemo ( ( ) : BuildAxisOptions < TDatum > => {
236235 const firstValue = getFirstDefinedValue ( options . primaryAxis , options . data )
237- const optionsWithScaleType = axisOptionsWithScaleType (
236+ const axisOptions = axisOptionsWithScaleType (
238237 options . primaryAxis ,
239238 firstValue
240239 )
241240
242- return { position : 'bottom' , ...optionsWithScaleType }
241+ return { position : 'bottom' , ...axisOptions }
243242 } , [ options . data , options . primaryAxis ] )
244243
245244 const secondaryAxesOptions = React . useMemo ( ( ) => {
246245 return options . secondaryAxes . map (
247246 ( secondaryAxis , i ) : BuildAxisOptions < TDatum > => {
248247 const firstValue = getFirstDefinedValue ( secondaryAxis , options . data )
249248
250- const optionsWithScaleType = axisOptionsWithScaleType (
251- secondaryAxis ,
252- firstValue
253- )
249+ const axisOptions = axisOptionsWithScaleType ( secondaryAxis , firstValue )
254250
255- if ( ! optionsWithScaleType . elementType ) {
251+ if ( ! axisOptions . elementType ) {
256252 if ( primaryAxisOptions . scaleType === 'band' ) {
257- optionsWithScaleType . elementType = 'bar'
258- } else if ( optionsWithScaleType . stacked ) {
259- optionsWithScaleType . elementType = 'area'
253+ axisOptions . elementType = 'bar'
254+ } else if ( axisOptions . stacked ) {
255+ axisOptions . elementType = 'area'
260256 }
261257 }
262258
263259 if (
264- typeof optionsWithScaleType . stacked === 'undefined' &&
265- optionsWithScaleType . elementType &&
266- [ 'area' ] . includes ( optionsWithScaleType . elementType )
260+ typeof axisOptions . stacked === 'undefined' &&
261+ axisOptions . elementType &&
262+ [ 'area' ] . includes ( axisOptions . elementType )
267263 ) {
268- optionsWithScaleType . stacked = true
264+ axisOptions . stacked = true
269265 }
270266
271267 return {
272268 position : ! i ? 'left' : 'right' ,
273- ...optionsWithScaleType ,
269+ ...axisOptions ,
274270 }
275271 }
276272 )
277273 } , [ options . data , options . secondaryAxes , primaryAxisOptions ] )
278274
279- if (
280- primaryAxisOptions . scaleType === 'band' &&
281- ! secondaryAxesOptions . some ( axisOptions => axisOptions . stacked )
282- ) {
283- primaryAxisOptions . stacked = primaryAxisOptions . stacked ?? true
284- }
285-
286275 // Resolve Tooltip Option
287276 const tooltipOptions = React . useMemo ( ( ) => {
288277 const tooltipOptions = defaultTooltip ( options ?. tooltip )
@@ -467,12 +456,27 @@ function ChartInner<TDatum>({
467456 const datumsByInteractionGroup = new Map < any , Datum < TDatum > [ ] > ( )
468457 const datumsByTooltipGroup = new Map < any , Datum < TDatum > [ ] > ( )
469458
470- let getInteractionKey = ( datum : Datum < TDatum > ) => `${ datum . primaryValue } `
459+ let getInteractionPrimary = ( datum : Datum < TDatum > ) => {
460+ if ( secondaryAxes . every ( d => d . elementType === 'bar' && ! d . stacked ) ) {
461+ const secondaryAxis = secondaryAxes . find (
462+ d => d . id === datum . secondaryAxisId
463+ ) !
464+
465+ if ( secondaryAxis . elementType === 'bar' && ! secondaryAxis . stacked ) {
466+ return getPrimary ( datum , primaryAxis , secondaryAxis )
467+ }
468+ }
469+
470+ return datum . primaryValue
471+ }
472+
473+ let getInteractionKey = ( datum : Datum < TDatum > ) =>
474+ `${ getInteractionPrimary ( datum ) } `
471475 let getTooltipKey = ( datum : Datum < TDatum > ) => `${ datum . primaryValue } `
472476
473477 if ( options . interactionMode === 'closest' ) {
474478 getInteractionKey = datum =>
475- `${ datum . primaryValue } _${ datum . secondaryValue } `
479+ `${ getInteractionPrimary ( datum ) } _${ datum . secondaryValue } `
476480 }
477481
478482 if ( tooltipOptions . groupingMode === 'single' ) {
@@ -610,8 +614,6 @@ function ChartInner<TDatum>({
610614 [ orderedSeries , secondaryAxes ]
611615 )
612616
613- let memoizeSeries = ! options . getDatumStyle && ! options . getSeriesStyle
614-
615617 // eslint-disable-next-line react-hooks/exhaustive-deps
616618 let getSeriesInfo = ( ) => ( {
617619 primaryAxis,
@@ -628,7 +630,7 @@ function ChartInner<TDatum>({
628630 [ primaryAxis , secondaryAxes , seriesByAxisId ]
629631 )
630632
631- if ( memoizeSeries ) {
633+ if ( options . memoizeSeries ) {
632634 getSeriesInfo = getMemoizedSeriesInfo
633635 }
634636
@@ -643,15 +645,16 @@ function ChartInner<TDatum>({
643645
644646 const { elementType } = secondaryAxis
645647 const Component = ( ( ) => {
646- if ( elementType === 'line' ) {
648+ if (
649+ elementType === 'line' ||
650+ elementType === 'bubble' ||
651+ elementType === 'area'
652+ ) {
647653 return Line
648654 }
649655 if ( elementType === 'bar' ) {
650656 return Bar
651657 }
652- if ( elementType === 'area' ) {
653- return Area
654- }
655658 throw new Error ( 'Invalid elementType' )
656659 } ) ( )
657660
@@ -747,3 +750,21 @@ function axisOptionsWithScaleType<TDatum>(
747750
748751 return { ...options , scaleType } as AxisOptionsWithScaleType < TDatum >
749752}
753+
754+ function sortDatumsBySecondaryPx < TDatum > (
755+ datums : Datum < TDatum > [ ] ,
756+ secondaryAxes : Axis < TDatum > [ ]
757+ ) {
758+ return [ ...datums ] . sort ( ( a , b ) => {
759+ const aAxis = secondaryAxes . find ( d => d . id === a . secondaryAxisId )
760+ const bAxis = secondaryAxes . find ( d => d . id === b . secondaryAxisId )
761+
762+ const aPx =
763+ aAxis ?. scale ( aAxis . stacked ? a . stackData ?. [ 1 ] : a . secondaryValue ) ?? NaN
764+
765+ const bPx =
766+ bAxis ?. scale ( bAxis . stacked ? b . stackData ?. [ 1 ] : b . secondaryValue ) ?? NaN
767+
768+ return aPx - bPx
769+ } )
770+ }
0 commit comments