@@ -46,22 +46,53 @@ import {
4646 snapToNearestX ,
4747 SeriesDataPoint ,
4848} from './percentChange' ;
49- import { OrientationType , TimeseriesChartTransformedProps } from './types' ;
49+ import {
50+ OrientationType ,
51+ TimeseriesChartTransformedProps ,
52+ TimeseriesCustomLegend ,
53+ } from './types' ;
5054import { formatSeriesName } from '../utils/series' ;
5155import { getTemporalXAxisDrillByFilter } from '../utils/xAxisDrillByFilter' ;
5256import { ExtraControls } from '../components/ExtraControls' ;
5357import TimeseriesLegend from './TimeseriesLegend' ;
58+ import { TIMESERIES_CONSTANTS } from '../constants' ;
5459
5560const TIMER_DURATION = 300 ;
5661const MAX_CUSTOM_LEGEND_HEIGHT = 160 ;
5762const MAX_CUSTOM_LEGEND_HEIGHT_RATIO = 0.3 ;
63+ const MIN_TIMESERIES_PLOT_HEIGHT = 80 ;
64+ const MIN_ECHARTS_GRID_HEIGHT = 1 ;
65+
66+ function resolveGridOffset ( offset : number | string , chartBodyHeight : number ) {
67+ if ( typeof offset === 'number' ) {
68+ return Number . isFinite ( offset ) ? Math . max ( offset , 0 ) : 0 ;
69+ }
70+
71+ const percentage = offset . match ( / ^ \s * ( - ? \d + (?: \. \d + ) ? ) % \s * $ / ) ;
72+ const pixels = percentage
73+ ? ( Number ( percentage [ 1 ] ) / 100 ) * chartBodyHeight
74+ : Number ( offset ) ;
75+ return Number . isFinite ( pixels ) ? Math . max ( pixels , 0 ) : 0 ;
76+ }
5877
59- // Bound the legend to a minority of the allocated chart body so the plot stays
60- // visible while the legend's own viewport scrolls independently.
61- export const getTimeseriesLegendMaxHeight = ( chartBodyHeight : number ) =>
78+ // Bound the legend after accounting for the fixed ECharts grid reservations,
79+ // leaving enough coordinate space for the plot itself to remain usable.
80+ export const getTimeseriesLegendMaxHeight = (
81+ chartBodyHeight : number ,
82+ grid : TimeseriesCustomLegend [ 'grid' ] ,
83+ ) =>
6284 Math . min (
6385 MAX_CUSTOM_LEGEND_HEIGHT ,
6486 Math . floor ( Math . max ( chartBodyHeight , 0 ) * MAX_CUSTOM_LEGEND_HEIGHT_RATIO ) ,
87+ Math . max (
88+ Math . floor (
89+ chartBodyHeight -
90+ resolveGridOffset ( grid . top , chartBodyHeight ) -
91+ resolveGridOffset ( grid . bottom , chartBodyHeight ) -
92+ MIN_TIMESERIES_PLOT_HEIGHT ,
93+ ) ,
94+ 0 ,
95+ ) ,
6596 ) ;
6697const getTimestampFromTimeAxisValue = ( value : string | number ) => {
6798 if ( typeof value === 'number' ) {
@@ -796,6 +827,66 @@ export default function EchartsTimeseries({
796827 ) ;
797828
798829 const chartBodyHeight = Math . max ( height - extraControlHeight , 0 ) ;
830+ const customLegendMaxHeight = customLegend
831+ ? getTimeseriesLegendMaxHeight ( chartBodyHeight , customLegend . grid )
832+ : 0 ;
833+ const shouldRenderCustomLegend =
834+ customLegend !== undefined &&
835+ chartBodyHeight > TIMESERIES_CONSTANTS . compactChartHeight &&
836+ customLegendMaxHeight > 0 ;
837+ const chartEchartOptions = useMemo ( ( ) => {
838+ if ( ! customLegend || shouldRenderCustomLegend ) {
839+ return echartOptions ;
840+ }
841+
842+ const gridOption = Array . isArray ( echartOptions . grid )
843+ ? echartOptions . grid [ 0 ]
844+ : echartOptions . grid ;
845+ if ( ! gridOption || typeof gridOption !== 'object' ) {
846+ return echartOptions ;
847+ }
848+
849+ const gridTop = resolveGridOffset ( customLegend . grid . top , chartBodyHeight ) ;
850+ const gridBottom = resolveGridOffset (
851+ customLegend . grid . bottom ,
852+ chartBodyHeight ,
853+ ) ;
854+ const isCompact =
855+ chartBodyHeight <= TIMESERIES_CONSTANTS . compactChartHeight ;
856+ if (
857+ ! isCompact &&
858+ chartBodyHeight - gridTop - gridBottom >= MIN_ECHARTS_GRID_HEIGHT
859+ ) {
860+ return echartOptions ;
861+ }
862+
863+ const top = isCompact ? Math . min ( gridTop , 12 ) : gridTop ;
864+ const requestedBottom =
865+ isCompact && ! formData . zoomable ? Math . min ( gridBottom , 5 ) : gridBottom ;
866+ const bottom = Math . min (
867+ requestedBottom ,
868+ Math . max ( chartBodyHeight - top - MIN_ECHARTS_GRID_HEIGHT , 0 ) ,
869+ ) ;
870+ const compactGrid = {
871+ ...gridOption ,
872+ bottom,
873+ containLabel : false ,
874+ top,
875+ } ;
876+
877+ return {
878+ ...echartOptions ,
879+ grid : Array . isArray ( echartOptions . grid )
880+ ? [ compactGrid , ...echartOptions . grid . slice ( 1 ) ]
881+ : compactGrid ,
882+ } ;
883+ } , [
884+ chartBodyHeight ,
885+ customLegend ,
886+ echartOptions ,
887+ formData . zoomable ,
888+ shouldRenderCustomLegend ,
889+ ] ) ;
799890 const renderEchart = ( {
800891 chartHeight,
801892 chartWidth,
@@ -808,7 +899,7 @@ export default function EchartsTimeseries({
808899 refs = { refs }
809900 height = { chartHeight }
810901 width = { chartWidth }
811- echartOptions = { echartOptions }
902+ echartOptions = { chartEchartOptions }
812903 eventHandlers = { eventHandlers }
813904 queryEventHandlers = { queryEventHandlers }
814905 zrEventHandlers = { zrEventHandlers }
@@ -822,15 +913,15 @@ export default function EchartsTimeseries({
822913 < div ref = { extraControlRef } >
823914 < ExtraControls formData = { formData } setControlValue = { setControlValue } />
824915 </ div >
825- { customLegend ? (
916+ { customLegend && shouldRenderCustomLegend ? (
826917 < WithLegend
827918 height = { chartBodyHeight }
828919 position = { customLegend . orientation }
829920 width = { width }
830921 renderLegend = { ( ) => (
831922 < TimeseriesLegend
832923 { ...customLegend }
833- maxHeight = { getTimeseriesLegendMaxHeight ( chartBodyHeight ) }
924+ maxHeight = { customLegendMaxHeight }
834925 onAll = { ( ) => dispatchLegendAction ( { type : 'legendAllSelect' } ) }
835926 onInverse = { ( ) =>
836927 dispatchLegendAction ( { type : 'legendInverseSelect' } )
0 commit comments