@@ -41,6 +41,10 @@ import type GlobalModel from 'echarts/types/src/model/Global';
4141import type ComponentModel from 'echarts/types/src/model/Component' ;
4242import { EchartsHandler , EventHandlers } from '../types' ;
4343import Echart from '../components/Echart' ;
44+ import {
45+ getViableTimeseriesEchartOptions ,
46+ resolveTimeseriesGridOffset ,
47+ } from './transformers' ;
4448import {
4549 rebaseSeriesData ,
4650 snapToNearestX ,
@@ -61,19 +65,6 @@ const TIMER_DURATION = 300;
6165const MAX_CUSTOM_LEGEND_HEIGHT = 160 ;
6266const MAX_CUSTOM_LEGEND_HEIGHT_RATIO = 0.3 ;
6367const 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- }
7768
7869// Bound the legend after accounting for the fixed ECharts grid reservations,
7970// leaving enough coordinate space for the plot itself to remain usable.
@@ -87,8 +78,8 @@ export const getTimeseriesLegendMaxHeight = (
8778 Math . max (
8879 Math . floor (
8980 chartBodyHeight -
90- resolveGridOffset ( grid . top , chartBodyHeight ) -
91- resolveGridOffset ( grid . bottom , chartBodyHeight ) -
81+ resolveTimeseriesGridOffset ( grid . top , chartBodyHeight ) -
82+ resolveTimeseriesGridOffset ( grid . bottom , chartBodyHeight ) -
9283 MIN_TIMESERIES_PLOT_HEIGHT ,
9384 ) ,
9485 0 ,
@@ -834,59 +825,15 @@ export default function EchartsTimeseries({
834825 customLegend !== undefined &&
835826 chartBodyHeight > TIMESERIES_CONSTANTS . compactChartHeight &&
836827 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- ] ) ;
828+ const chartEchartOptions = useMemo (
829+ ( ) =>
830+ getViableTimeseriesEchartOptions (
831+ echartOptions ,
832+ chartBodyHeight ,
833+ formData . zoomable ,
834+ ) ,
835+ [ chartBodyHeight , echartOptions , formData . zoomable ] ,
836+ ) ;
890837 const renderEchart = ( {
891838 chartHeight,
892839 chartWidth,
0 commit comments