55
66import { groupBy } from 'lodash' ;
77import { BarGaugeChartStyle } from './bar_gauge_vis_config' ;
8- import { VisColumn , AxisColumnMappings , VEGASCHEMA , Threshold } from '../types' ;
8+ import { VisColumn , AxisColumnMappings , VEGASCHEMA , Threshold , VisFieldType } from '../types' ;
99import { calculateValue } from '../utils/calculation' ;
10- import { DEFAULT_GREY , getColors , getUnfilledArea } from '../theme/default_colors' ;
10+ import { getColors } from '../theme/default_colors' ;
1111import { getSchemaByAxis } from '../utils/utils' ;
1212import {
1313 getBarOrientation ,
@@ -33,9 +33,7 @@ export const createBarGaugeSpec = (
3333 ) ;
3434
3535 const isXaxisNumerical = axisColumnMappings ?. x ?. schema === 'numerical' ;
36- const adjustEncoding =
37- ( ! isXaxisNumerical && styleOptions ?. exclusive ?. orientation === 'horizontal' ) ||
38- ( isXaxisNumerical && styleOptions ?. exclusive ?. orientation !== 'horizontal' ) ;
36+ const adjustEncoding = xAxis ?. schema === VisFieldType . Numerical ;
3937
4038 const processedSymbol = [
4139 `${ symbolOpposite ( styleOptions . exclusive . orientation , `${ isXaxisNumerical ? 'x' : 'y' } ` ) } ` ,
@@ -121,7 +119,9 @@ export const createBarGaugeSpec = (
121119 } ,
122120 {
123121 name : 'fontFactor' ,
124- expr : adjustEncoding ? `width/${ catCounts } /${ maxTextLength } /9` : `height/${ maxTextLength } /18` ,
122+ expr : ! adjustEncoding
123+ ? `width/${ catCounts } /${ maxTextLength } /8`
124+ : `height/${ catCounts } /${ maxTextLength } /10` ,
125125 } ,
126126 ...gradientParams ,
127127 ] ;
@@ -182,7 +182,7 @@ export const createBarGaugeSpec = (
182182 mark : {
183183 type : 'bar' ,
184184 clip : true ,
185- fill : getUnfilledArea ( ) ,
185+ fill : getColors ( ) . backgroundShade ,
186186 } ,
187187 encoding : {
188188 [ `${ processedSymbol } ` ] : {
@@ -205,10 +205,15 @@ export const createBarGaugeSpec = (
205205 return expression ;
206206 } ;
207207
208- // Handle invalid domain cases (minBase >= maxBase or minBase > maxNumber) by using stack logic
209- if ( styleOptions . exclusive . displayMode === 'stack' || minBase >= maxBase || minBase > maxNumber ) {
208+ let bars = [ ] as any ;
209+
210+ // Handle invalid domain cases (minBase >= maxBase or minBase > maxNumber)
211+ // valid cases will not add the layer .
212+ if ( minBase >= maxBase || minBase > maxNumber ) {
213+ bars = [ ] ;
214+ } else if ( styleOptions . exclusive . displayMode === 'stack' ) {
210215 // dispose the last threshold
211- const bars = processedThresholds . slice ( 0 , - 1 ) ?. map ( ( threshold , index ) => {
216+ bars = processedThresholds . slice ( 0 , - 1 ) ?. map ( ( threshold , index ) => {
212217 return {
213218 transform : [
214219 { calculate : `datum.threshold${ index } ` , as : 'gradStart' } ,
@@ -250,62 +255,67 @@ export const createBarGaugeSpec = (
250255 ] ,
251256 } ;
252257 } ) ;
253-
254- layers . push ( ...bars ) ;
255258 } else if ( styleOptions . exclusive . displayMode === 'gradient' ) {
256- const gradientBar = {
257- mark : { type : 'bar' } ,
258- transform : [
259- {
260- calculate : `datum['${ numericField } ']>=datum.maxVal?datum.maxVal:datum['${ numericField } ']` ,
261- as : 'barEnd' ,
262- } ,
263- { filter : `datum['${ numericField } '] >= datum.minVal` } ,
264- ] ,
265- encoding : {
266- [ `${ processedSymbol } ` ] : {
267- type : 'quantitative' ,
268- field : 'barEnd' ,
269- } ,
270- color : {
271- value : {
272- expr : generateTrans ( processedThresholds ) ,
259+ bars = [
260+ {
261+ mark : { type : 'bar' } ,
262+ transform : [
263+ {
264+ calculate : `datum['${ numericField } ']>=datum.maxVal?datum.maxVal:datum['${ numericField } ']` ,
265+ as : 'barEnd' ,
266+ } ,
267+ { filter : `datum['${ numericField } '] >= datum.minVal` } ,
268+ ] ,
269+ encoding : {
270+ [ `${ processedSymbol } ` ] : {
271+ type : 'quantitative' ,
272+ field : 'barEnd' ,
273+ } ,
274+ color : {
275+ value : {
276+ expr : generateTrans ( processedThresholds ) ,
277+ } ,
273278 } ,
274279 } ,
275280 } ,
276- } ;
277- layers . push ( gradientBar ) ;
281+ ] ;
278282 } else if ( styleOptions . exclusive . displayMode === 'basic' ) {
279- const gradientBar = {
280- mark : { type : 'bar' } ,
281- transform : [
282- {
283- calculate : `datum['${ numericField } ']>=datum.maxVal?datum.maxVal:datum['${ numericField } ']` ,
284- as : 'barEnd' ,
285- } ,
286- { filter : `datum['${ numericField } '] >= datum.minVal` } ,
287- ] ,
288- encoding : {
289- [ `${ processedSymbol } ` ] : {
290- type : 'quantitative' ,
291- field : 'barEnd' ,
292- } ,
293- color : {
294- field : numericField ,
295- type : 'quantitative' ,
296- scale : {
297- type : 'threshold' ,
298- // last threshold which is just for max value capping in gradient mode
299- domain : processedThresholds . map ( ( t ) => t . value ) . slice ( 0 , - 1 ) ,
300- range : [ DEFAULT_GREY , ...processedThresholds . map ( ( t ) => t . color ) ] . slice ( 0 , - 1 ) ,
283+ bars = [
284+ {
285+ mark : { type : 'bar' } ,
286+ transform : [
287+ {
288+ calculate : `datum['${ numericField } ']>=datum.maxVal?datum.maxVal:datum['${ numericField } ']` ,
289+ as : 'barEnd' ,
290+ } ,
291+ { filter : `datum['${ numericField } '] >= datum.minVal` } ,
292+ ] ,
293+ encoding : {
294+ [ `${ processedSymbol } ` ] : {
295+ type : 'quantitative' ,
296+ field : 'barEnd' ,
297+ } ,
298+ color : {
299+ field : numericField ,
300+ type : 'quantitative' ,
301+ scale : {
302+ type : 'threshold' ,
303+ // last threshold which is just for max value capping in gradient mode
304+ domain : processedThresholds . map ( ( t ) => t . value ) . slice ( 0 , - 1 ) ,
305+ range : [
306+ getColors ( ) . backgroundShade ,
307+ ...processedThresholds . map ( ( t ) => t . color ) ,
308+ ] . slice ( 0 , - 1 ) ,
309+ } ,
310+ legend : null ,
301311 } ,
302- legend : null ,
303312 } ,
304313 } ,
305- } ;
306- layers . push ( gradientBar ) ;
314+ ] ;
307315 }
308316
317+ layers . push ( ...bars ) ;
318+
309319 if ( styleOptions . exclusive . valueDisplay !== 'hidden' ) {
310320 const nameLayer = {
311321 mark : {
@@ -338,7 +348,10 @@ export const createBarGaugeSpec = (
338348 scale : {
339349 type : 'threshold' ,
340350 domain : processedThresholds . map ( ( t ) => t . value ) . slice ( 0 , - 1 ) ,
341- range : [ DEFAULT_GREY , ...processedThresholds . map ( ( t ) => t . color ) ] . slice ( 0 , - 1 ) ,
351+ range : [ getColors ( ) . backgroundShade , ...processedThresholds . map ( ( t ) => t . color ) ] . slice (
352+ 0 ,
353+ - 1
354+ ) ,
342355 } ,
343356 legend : null ,
344357 } ,
0 commit comments