@@ -27,17 +27,39 @@ export interface ChartAnnotations {
2727
2828const BAND_OPACITY = 0.08
2929
30- function firstAxis ( axis : unknown ) : Record < string , unknown > {
31- return toRecord ( Array . isArray ( axis ) ? axis [ 0 ] : axis )
30+ function firstSeries ( option : Record < string , unknown > ) : Record < string , unknown > {
31+ return toRecord ( Array . isArray ( option . series ) ? option . series [ 0 ] : option . series )
3232}
3333
34- /** The axis a threshold is measured on; ECharts defaults an unspecified yAxis to a value axis. */
34+ /** The axis the first series is plotted on, resolved through its `*AxisId` or `*AxisIndex`. */
35+ function seriesAxis (
36+ option : Record < string , unknown > ,
37+ key : 'xAxis' | 'yAxis'
38+ ) : Record < string , unknown > {
39+ const axes = Array . isArray ( option [ key ] ) ? ( option [ key ] as unknown [ ] ) : [ option [ key ] ]
40+ const series = firstSeries ( option )
41+ const id = series [ `${ key } Id` ]
42+ if ( id !== undefined ) {
43+ const axis = axes . find ( ( candidate ) => toRecord ( candidate ) . id === id )
44+ if ( axis === undefined ) throw new Error ( `The first series references a missing ${ key } "${ id } "` )
45+ return toRecord ( axis )
46+ }
47+ const index = series [ `${ key } Index` ] ?? 0
48+ if ( typeof index !== 'number' || ( index > 0 && axes [ index ] === undefined ) )
49+ throw new Error ( `The first series references a missing ${ key } at index ${ String ( index ) } ` )
50+ return toRecord ( axes [ index ] )
51+ }
52+
53+ /**
54+ * The axis a threshold is measured on, among the axes the first series is plotted on; ECharts
55+ * defaults an unspecified yAxis to a value axis.
56+ */
3557export function valueAxisKey ( option : Record < string , unknown > ) : 'xAxis' | 'yAxis' {
3658 if ( option . xAxis === undefined && option . yAxis === undefined )
3759 throw new Error ( 'Thresholds require a value axis' )
38- const y = firstAxis ( option . yAxis )
60+ const y = seriesAxis ( option , ' yAxis' )
3961 if ( y . type === undefined || y . type === 'value' || y . type === 'log' ) return 'yAxis'
40- const x = firstAxis ( option . xAxis )
62+ const x = seriesAxis ( option , ' xAxis' )
4163 if ( x . type === 'value' || x . type === 'log' ) return 'xAxis'
4264 throw new Error ( 'Thresholds require a value axis' )
4365}
@@ -47,7 +69,7 @@ export function valueAxisKey(option: Record<string, unknown>): 'xAxis' | 'yAxis'
4769 * y axis is inverted (as horizontal bar charts usually are).
4870 */
4971function verticalTop ( option : Record < string , unknown > ) : 'start' | 'end' {
50- return firstAxis ( option . yAxis ) . inverse === true ? 'start' : 'end'
72+ return seriesAxis ( option , ' yAxis' ) . inverse === true ? 'start' : 'end'
5173}
5274
5375/** A chart annotations can draw on: a first series with no hand-written marks to collide with. */
0 commit comments