@@ -26,15 +26,17 @@ import {
2626} from '@superset-ui/core' ;
2727import { t } from '@apache-superset/core/translation' ;
2828import type { EChartsCoreOption } from 'echarts/core' ;
29+ import type { CustomSeriesRenderItem } from 'echarts' ;
2930import { Refs } from '../types' ;
30- import { sanitizeHtml as escapeHtml } from '../utils/series' ;
3131import { EchartsBulletChartProps , BulletChartTransformedProps } from './types' ;
32+ import { sanitizeHtml as escapeHtml } from '../utils/series' ;
3233import {
3334 getColorLuminance ,
3435 tokenizeToNumericArray ,
3536 tokenizeToStringArray ,
3637} from './utils' ;
3738import { estimateWrappedLegendRowCount } from '../utils/legendLayout' ;
39+ import { getMetricValue } from '../utils/metricBounds' ;
3840
3941// Foregrounds for band labels drawn over a custom fill, chosen from the fill's
4042// own luminance so they stay readable in both light and dark themes.
@@ -58,6 +60,29 @@ const wcagContrast = (luminanceA: number, luminanceB: number): number => {
5860 return ( lighter + 0.05 ) / ( darker + 0.05 ) ;
5961} ;
6062const MEASURE_BAR_FRACTION = 0.28 ;
63+ // Per-row range bands (relative ranges) fill this share of their row.
64+ const ROW_BAND_FRACTION = 0.7 ;
65+ // The target tick stands taller than the measure bar, like d3-bullet.
66+ const TARGET_TICK_FRACTION = MEASURE_BAR_FRACTION * 1.8 ;
67+
68+ // Draws one range band from data [start, end, rowIndex] within its row.
69+ const renderRowBand : CustomSeriesRenderItem = ( _params , api ) => {
70+ const row = Number ( api . value ( 2 ) ) ;
71+ const start = api . coord ( [ api . value ( 0 ) , row ] ) ;
72+ const end = api . coord ( [ api . value ( 1 ) , row ] ) ;
73+ const rowSize = api . size ? ( api . size ( [ 0 , 1 ] ) as number [ ] ) [ 1 ] : 0 ;
74+ const height = rowSize * ROW_BAND_FRACTION ;
75+ return {
76+ type : 'rect' ,
77+ shape : {
78+ x : start [ 0 ] ,
79+ y : start [ 1 ] - height / 2 ,
80+ width : Math . max ( end [ 0 ] - start [ 0 ] , 0 ) ,
81+ height,
82+ } ,
83+ style : api . style ( ) ,
84+ } ;
85+ } ;
6186// Small pixel gap below the measure bar so the target/marker symbol doesn't
6287// touch its edge.
6388const MARKER_GAP_BELOW_BAR_PX = 12 ;
@@ -76,6 +101,8 @@ export default function transformProps(
76101 markerLabels : rawMarkerLabels ,
77102 markerLines : rawMarkerLines ,
78103 markerLineLabels : rawMarkerLineLabels ,
104+ targetMetric,
105+ rangesRelative,
79106 yAxisFormat,
80107 showLabels,
81108 showLegend,
@@ -97,9 +124,30 @@ export default function transformProps(
97124 // the largest row drives the default band and the axis extent
98125 const measure = Math . max ( ...measures ) ;
99126
127+ // Each row's own target, read from the same grouped query as the measure.
128+ const targetLabel = targetMetric ? getMetricLabel ( targetMetric ) : '' ;
129+ const targets = categories . map ( ( _ , i ) =>
130+ getMetricValue ( rows [ i ] , targetMetric ) ,
131+ ) ;
132+ const definedTargets = targets . filter (
133+ ( value ) : value is number => value !== undefined ,
134+ ) ;
135+
100136 // Match the legacy default: a single band up to 110% of the measure.
101- const explicitRanges = tokenizeToNumericArray ( rawRanges ) ;
102- const ranges = explicitRanges ?? [ 0 , measure * 1.1 ] ;
137+ const typedRanges = tokenizeToNumericArray ( rawRanges ) ;
138+ const ranges = typedRanges ?? [ 0 , measure * 1.1 ] ;
139+ // Relative ranges are percentages of each row's target, so every row
140+ // gets its own thresholds; otherwise all rows share the typed ones.
141+ const relativeRanges = Boolean (
142+ rangesRelative && typedRanges && definedTargets . length ,
143+ ) ;
144+ const rowRanges = categories . map ( ( _ , i ) => {
145+ if ( ! relativeRanges ) return ranges ;
146+ const target = targets [ i ] ;
147+ return target === undefined
148+ ? [ ]
149+ : ranges . map ( percent => ( target * percent ) / 100 ) ;
150+ } ) ;
103151 const rangeLabels = tokenizeToStringArray ( rawRangeLabels ) ?? [ ] ;
104152 const markers = tokenizeToNumericArray ( rawMarkers ) ?? [ ] ;
105153 const markerLabels = tokenizeToStringArray ( rawMarkerLabels ) ?? [ ] ;
@@ -108,14 +156,22 @@ export default function transformProps(
108156
109157 const formatter = getNumberFormatter ( yAxisFormat ) ;
110158
159+ const allRangeValues = rowRanges . flat ( ) ;
111160 const axisMin = Math . min (
112161 0 ,
113162 ...measures ,
114- ...ranges ,
163+ ...allRangeValues ,
164+ ...definedTargets ,
165+ ...markers ,
166+ ...markerLines ,
167+ ) ;
168+ let axisMax = Math . max (
169+ ...measures ,
170+ ...allRangeValues ,
171+ ...definedTargets ,
115172 ...markers ,
116173 ...markerLines ,
117174 ) ;
118- let axisMax = Math . max ( ...measures , ...ranges , ...markers , ...markerLines ) ;
119175 if ( axisMax === axisMin ) {
120176 // All values identical (e.g. an empty result measuring 0) would produce a
121177 // zero-width axis domain; expand it so the chart still renders.
@@ -139,9 +195,10 @@ export default function transformProps(
139195 label : rangeLabels [ i ] ,
140196 // Colors only apply to user-entered ranges; the automatic fallback band
141197 // must not pick up a stale `range_colors` entry kept from earlier edits.
142- color : explicitRanges ? rangeColors ?. [ i ] || undefined : undefined ,
198+ color : typedRanges ? rangeColors ?. [ i ] || undefined : undefined ,
199+ index : i ,
143200 } ) )
144- . filter ( ( { value } ) => value > axisMin )
201+ . filter ( ( { value } ) => relativeRanges || value > axisMin )
145202 . sort ( ( a , b ) => b . value - a . value ) ;
146203 // Each band's label sits inside its own top-right corner: the visible strip
147204 // of a nested band ends at its threshold, so the label lands inside the
@@ -197,10 +254,13 @@ export default function transformProps(
197254 // Which labeled range a measure falls within, for the bar tooltip. Range
198255 // labels render at band edges, so naming the containing band on hover
199256 // disambiguates which side of a boundary a value sits on.
200- const ascendingRanges = [ ...ranges ]
201- . map ( ( value , i ) => ( { value, label : rangeLabels [ i ] } ) )
202- . sort ( ( a , b ) => a . value - b . value ) ;
203- const containingRangeLabel = ( value : number ) : string | undefined => {
257+ const containingRangeLabel = (
258+ value : number ,
259+ row : number ,
260+ ) : string | undefined => {
261+ const ascendingRanges = rowRanges [ row ]
262+ . map ( ( threshold , i ) => ( { value : threshold , label : rangeLabels [ i ] } ) )
263+ . sort ( ( a , b ) => a . value - b . value ) ;
204264 const hit = ascendingRanges . find ( range => value <= range . value ) ;
205265 if ( hit ?. label ) return hit . label ;
206266 const last = ascendingRanges [ ascendingRanges . length - 1 ] ;
@@ -214,10 +274,12 @@ export default function transformProps(
214274 const markerLabelAt = ( index : number , labels : string [ ] , values : number [ ] ) =>
215275 labels [ index ] ? labels [ index ] : formatter ( values [ index ] ) ;
216276
277+ const formatThreshold = ( value : number ) =>
278+ relativeRanges ? `${ value } % ${ t ( 'of target' ) } ` : formatter ( value ) ;
217279 const rangeName = ( value : number , i : number ) =>
218280 rangeLabels [ i ]
219- ? `${ rangeLabels [ i ] } : \u2264 ${ formatter ( value ) } `
220- : `\u2264 ${ formatter ( value ) } ` ;
281+ ? `${ rangeLabels [ i ] } : \u2264 ${ formatThreshold ( value ) } `
282+ : `\u2264 ${ formatThreshold ( value ) } ` ;
221283 const markerName = ( value : number , i : number , labels : string [ ] ) =>
222284 labels [ i ] ? `${ labels [ i ] } : ${ formatter ( value ) } ` : String ( formatter ( value ) ) ;
223285
@@ -226,6 +288,7 @@ export default function transformProps(
226288 // widths and reserve grid space for it.
227289 const legendNames = [
228290 metricLabel ,
291+ ...( definedTargets . length ? [ targetLabel ] : [ ] ) ,
229292 ...ranges . map ( rangeName ) ,
230293 ...markers . map ( ( value , i ) => markerName ( value , i , markerLabels ) ) ,
231294 ...markerLines . map ( ( value , i ) => markerName ( value , i , markerLineLabels ) ) ,
@@ -250,6 +313,56 @@ export default function transformProps(
250313 ( MEASURE_BAR_FRACTION / 2 ) * rowHeight + MARKER_GAP_BELOW_BAR_PX ,
251314 ) ;
252315
316+ // Relative ranges differ per row, so they are drawn as per-row rectangles
317+ // instead of the chart-wide markArea used for typed ranges. Each shares its
318+ // name with the range's hover series so one legend entry toggles both.
319+ const rowBandSeries = relativeRanges
320+ ? sortedRanges . map ( ( { value, index, color } , i ) => ( {
321+ name : rangeName ( value , index ) ,
322+ type : 'custom' ,
323+ silent : true ,
324+ renderItem : renderRowBand ,
325+ encode : { x : [ 0 , 1 ] , y : 2 } ,
326+ // A custom `range_colors` entry wins, as for the markArea bands.
327+ itemStyle : {
328+ color : color || bandFills [ Math . min ( i , bandFills . length - 1 ) ] ,
329+ } ,
330+ data : rowRanges . flatMap ( ( thresholds , row ) =>
331+ thresholds . length ? [ [ axisMin , thresholds [ index ] , row ] ] : [ ] ,
332+ ) ,
333+ z : 1 ,
334+ } ) )
335+ : [ ] ;
336+
337+ // One target tick per row that has a target value.
338+ const targetSeries = definedTargets . length
339+ ? [
340+ {
341+ name : targetLabel ,
342+ type : 'scatter' ,
343+ data : targets . flatMap ( ( target , row ) =>
344+ target === undefined
345+ ? [ ]
346+ : [
347+ {
348+ value : [ target , row ] ,
349+ tooltip : {
350+ formatter : ( ) =>
351+ sanitizeHtml (
352+ `${ grouped ? `${ escapeHtml ( categories [ row ] ) } \u2014 ` : '' } ${ escapeHtml ( targetLabel ) } : <b>${ escapeHtml ( formatter ( target ) ) } </b>` ,
353+ ) ,
354+ } ,
355+ } ,
356+ ] ,
357+ ) ,
358+ symbol : 'rect' ,
359+ symbolSize : [ 3 , Math . max ( TARGET_TICK_FRACTION * rowHeight , 8 ) ] ,
360+ itemStyle : { color : theme . colorText } ,
361+ z : 25 ,
362+ } ,
363+ ]
364+ : [ ] ;
365+
253366 const echartOptions : EChartsCoreOption = {
254367 animation : false ,
255368 // Optional legend listing every range, marker and line as a toggleable
@@ -297,19 +410,24 @@ export default function transformProps(
297410 const prefix = grouped
298411 ? `${ escapeHtml ( categories [ i ] ) } \u2014 `
299412 : '' ;
300- const band = containingRangeLabel ( measures [ i ] ) ;
413+ const band = containingRangeLabel ( measures [ i ] , i ) ;
301414 const bandLine = band
302415 ? `<br/>${ escapeHtml ( t ( 'Range' ) ) } : ${ escapeHtml ( band ) } `
303416 : '' ;
417+ const target = targets [ i ] ;
418+ const targetLine =
419+ target === undefined
420+ ? ''
421+ : `<br/>${ escapeHtml ( targetLabel ) } : ${ escapeHtml ( formatter ( target ) ) } ` ;
304422 return sanitizeHtml (
305- `${ prefix } ${ escapeHtml ( metricLabel ) } : <b>${ escapeHtml ( formatter ( measures [ i ] ) ) } </b>${ bandLine } ` ,
423+ `${ prefix } ${ escapeHtml ( metricLabel ) } : <b>${ escapeHtml ( formatter ( measures [ i ] ) ) } </b>${ targetLine } ${ bandLine } ` ,
306424 ) ;
307425 } ,
308426 } ,
309427 z : 10 ,
310428 markArea : {
311429 silent : true ,
312- data : bands . map ( band => [
430+ data : ( relativeRanges ? [ ] : bands ) . map ( band => [
313431 {
314432 name : band . name ,
315433 itemStyle : band . itemStyle ,
@@ -323,18 +441,27 @@ export default function transformProps(
323441 // Invisible hover targets at each range threshold: per-item tooltips
324442 // (markArea is not reliably hoverable), optional on-chart labels, and a
325443 // named legend entry each.
444+ ...rowBandSeries ,
445+ ...targetSeries ,
326446 ...ranges . map ( ( value , index ) => ( {
327447 name : rangeName ( value , index ) ,
328448 type : 'scatter' ,
329- data : categories . map ( ( _ , row ) => ( {
330- value : [ value , row ] ,
331- tooltip : {
332- formatter : ( ) =>
333- sanitizeHtml (
334- `${ escapeHtml ( rangeLabels [ index ] || '' ) } \u2264 <b>${ escapeHtml ( formatter ( value ) ) } </b>` ,
335- ) ,
336- } ,
337- } ) ) ,
449+ data : categories . flatMap ( ( _ , row ) => {
450+ const threshold = rowRanges [ row ] [ index ] ;
451+ return threshold === undefined
452+ ? [ ]
453+ : [
454+ {
455+ value : [ threshold , row ] ,
456+ tooltip : {
457+ formatter : ( ) =>
458+ sanitizeHtml (
459+ `${ escapeHtml ( rangeLabels [ index ] || '' ) } \u2264 <b>${ escapeHtml ( formatter ( threshold ) ) } </b>` ,
460+ ) ,
461+ } ,
462+ } ,
463+ ] ;
464+ } ) ,
338465 symbol : 'rect' ,
339466 symbolSize : [ 14 , 40 ] ,
340467 itemStyle : { color : 'transparent' } ,
0 commit comments