1
+ import { t } from "@lingui/macro" ;
1
2
import { ascending , descending , group , rollup , rollups } from "d3-array" ;
2
3
import produce from "immer" ;
3
4
import get from "lodash/get" ;
@@ -563,7 +564,7 @@ export const getInitialConfig = (
563
564
) as TableFields ,
564
565
} ;
565
566
case "comboLineSingle" : {
566
- // It's guaranteed by getPossibleChartTypes that there are at least two units.
567
+ // It's guaranteed by getEnabledChartTypes that there are at least two units.
567
568
const mostCommonUnit = rollups (
568
569
numericalMeasures . filter ( ( d ) => d . unit ) ,
569
570
( v ) => v . length ,
@@ -597,7 +598,7 @@ export const getInitialConfig = (
597
598
} ;
598
599
}
599
600
case "comboLineDual" : {
600
- // It's guaranteed by getPossibleChartTypes that there are at least two units.
601
+ // It's guaranteed by getEnabledChartTypes that there are at least two units.
601
602
const [ firstUnit , secondUnit ] = Array . from (
602
603
new Set ( numericalMeasures . filter ( ( d ) => d . unit ) . map ( ( d ) => d . unit ) )
603
604
) ;
@@ -634,7 +635,7 @@ export const getInitialConfig = (
634
635
} ;
635
636
}
636
637
case "comboLineColumn" : {
637
- // It's guaranteed by getPossibleChartTypes that there are at least two units.
638
+ // It's guaranteed by getEnabledChartTypes that there are at least two units.
638
639
const [ firstUnit , secondUnit ] = Array . from (
639
640
new Set ( numericalMeasures . filter ( ( d ) => d . unit ) . map ( ( d ) => d . unit ) )
640
641
) ;
@@ -1948,17 +1949,22 @@ const adjustSegmentSorting = ({
1948
1949
return newSorting ;
1949
1950
} ;
1950
1951
1951
- export const getPossibleChartTypes = ( {
1952
+ const categoricalEnabledChartTypes : RegularChartType [ ] = [ "column" , "pie" ] ;
1953
+ const geoEnabledChartTypes : RegularChartType [ ] = [ "column" , "map" , "pie" ] ;
1954
+ const multipleNumericalMeasuresEnabledChartTypes : RegularChartType [ ] = [
1955
+ "scatterplot" ,
1956
+ ] ;
1957
+ const timeEnabledChartTypes : RegularChartType [ ] = [ "area" , "column" , "line" ] ;
1958
+
1959
+ export const getEnabledChartTypes = ( {
1952
1960
dimensions,
1953
1961
measures,
1954
- allowedChartTypes,
1955
1962
cubeCount,
1956
1963
} : {
1957
1964
dimensions : Dimension [ ] ;
1958
1965
measures : Measure [ ] ;
1959
- allowedChartTypes ?: ChartType [ ] ;
1960
1966
cubeCount : number ;
1961
- } ) : ChartType [ ] => {
1967
+ } ) => {
1962
1968
const numericalMeasures = measures . filter ( isNumericalMeasure ) ;
1963
1969
const ordinalMeasures = measures . filter ( isOrdinalMeasure ) ;
1964
1970
const categoricalDimensions = getCategoricalDimensions ( dimensions ) ;
@@ -1967,23 +1973,76 @@ export const getPossibleChartTypes = ({
1967
1973
( d ) => isTemporalDimension ( d ) || isTemporalEntityDimension ( d )
1968
1974
) ;
1969
1975
1970
- const categoricalEnabled : RegularChartType [ ] = [ "column" , "pie" ] ;
1971
- const geoEnabled : RegularChartType [ ] = [ "column" , "map" , "pie" ] ;
1972
- const multipleNumericalMeasuresEnabled : RegularChartType [ ] = [ "scatterplot" ] ;
1973
- const timeEnabled : RegularChartType [ ] = [ "area" , "column" , "line" ] ;
1976
+ const possibleChartTypesDict = Object . fromEntries (
1977
+ chartTypes . map ( ( chartType ) => [
1978
+ chartType ,
1979
+ {
1980
+ enabled : chartType === "table" ,
1981
+ message : undefined ,
1982
+ } ,
1983
+ ] )
1984
+ ) as Record <
1985
+ ChartType ,
1986
+ {
1987
+ enabled : boolean ;
1988
+ message : string | undefined ;
1989
+ }
1990
+ > ;
1991
+ const enableChartType = ( chartType : ChartType ) => {
1992
+ possibleChartTypesDict [ chartType ] = {
1993
+ enabled : true ,
1994
+ message : undefined ,
1995
+ } ;
1996
+ } ;
1997
+ const enableChartTypes = ( chartTypes : ChartType [ ] ) => {
1998
+ for ( const chartType of chartTypes ) {
1999
+ enableChartType ( chartType ) ;
2000
+ }
2001
+ } ;
2002
+ const maybeDisableChartType = ( chartType : ChartType , message : string ) => {
2003
+ if (
2004
+ ! possibleChartTypesDict [ chartType ] . enabled &&
2005
+ ! possibleChartTypesDict [ chartType ] . message
2006
+ ) {
2007
+ possibleChartTypesDict [ chartType ] = {
2008
+ enabled : false ,
2009
+ message,
2010
+ } ;
2011
+ }
2012
+ } ;
2013
+ const maybeDisableChartTypes = ( chartTypes : ChartType [ ] , message : string ) => {
2014
+ for ( const chartType of chartTypes ) {
2015
+ maybeDisableChartType ( chartType , message ) ;
2016
+ }
2017
+ } ;
1974
2018
1975
- const possibles : ChartType [ ] = [ "table" ] ;
1976
2019
if ( numericalMeasures . length > 0 ) {
1977
2020
if ( categoricalDimensions . length > 0 ) {
1978
- possibles . push ( ...categoricalEnabled ) ;
2021
+ enableChartTypes ( categoricalEnabledChartTypes ) ;
2022
+ } else {
2023
+ maybeDisableChartTypes (
2024
+ categoricalEnabledChartTypes ,
2025
+ t ( {
2026
+ id : "controls.chart.disabled.categorical" ,
2027
+ message : "At least one categorical dimension is required." ,
2028
+ } )
2029
+ ) ;
1979
2030
}
1980
2031
1981
2032
if ( geoDimensions . length > 0 ) {
1982
- possibles . push ( ...geoEnabled ) ;
2033
+ enableChartTypes ( geoEnabledChartTypes ) ;
2034
+ } else {
2035
+ maybeDisableChartTypes (
2036
+ geoEnabledChartTypes ,
2037
+ t ( {
2038
+ id : "controls.chart.disabled.geographical" ,
2039
+ message : "At least one geographical dimension is required." ,
2040
+ } )
2041
+ ) ;
1983
2042
}
1984
2043
1985
2044
if ( numericalMeasures . length > 1 ) {
1986
- possibles . push ( ... multipleNumericalMeasuresEnabled ) ;
2045
+ enableChartTypes ( multipleNumericalMeasuresEnabledChartTypes ) ;
1987
2046
1988
2047
if ( temporalDimensions . length > 0 ) {
1989
2048
const measuresWithUnit = numericalMeasures . filter ( ( d ) => d . unit ) ;
@@ -1992,7 +2051,16 @@ export const getPossibleChartTypes = ({
1992
2051
) ;
1993
2052
1994
2053
if ( uniqueUnits . length > 1 ) {
1995
- possibles . push ( ...comboDifferentUnitChartTypes ) ;
2054
+ enableChartTypes ( comboDifferentUnitChartTypes ) ;
2055
+ } else {
2056
+ maybeDisableChartTypes (
2057
+ comboDifferentUnitChartTypes ,
2058
+ t ( {
2059
+ id : "controls.chart.disabled.different-unit" ,
2060
+ message :
2061
+ "At least two numerical measures with different units are required." ,
2062
+ } )
2063
+ ) ;
1996
2064
}
1997
2065
1998
2066
const unitCounts = rollup (
@@ -2002,29 +2070,75 @@ export const getPossibleChartTypes = ({
2002
2070
) ;
2003
2071
2004
2072
if ( Array . from ( unitCounts . values ( ) ) . some ( ( d ) => d > 1 ) ) {
2005
- possibles . push ( ...comboSameUnitChartTypes ) ;
2073
+ enableChartTypes ( comboSameUnitChartTypes ) ;
2074
+ } else {
2075
+ maybeDisableChartTypes (
2076
+ comboSameUnitChartTypes ,
2077
+ t ( {
2078
+ id : "controls.chart.disabled.same-unit" ,
2079
+ message :
2080
+ "At least two numerical measures with the same unit are required." ,
2081
+ } )
2082
+ ) ;
2006
2083
}
2084
+ } else {
2085
+ maybeDisableChartTypes (
2086
+ comboChartTypes ,
2087
+ t ( {
2088
+ id : "controls.chart.disabled.temporal" ,
2089
+ message : "At least one temporal dimension is required." ,
2090
+ } )
2091
+ ) ;
2007
2092
}
2093
+ } else {
2094
+ maybeDisableChartTypes (
2095
+ [ ...multipleNumericalMeasuresEnabledChartTypes , ...comboChartTypes ] ,
2096
+ t ( {
2097
+ id : "controls.chart.disabled.multiple-measures" ,
2098
+ message : "At least two numerical measures are required." ,
2099
+ } )
2100
+ ) ;
2008
2101
}
2009
2102
2010
2103
if ( temporalDimensions . length > 0 ) {
2011
- possibles . push ( ...timeEnabled ) ;
2104
+ enableChartTypes ( timeEnabledChartTypes ) ;
2105
+ } else {
2106
+ maybeDisableChartTypes (
2107
+ timeEnabledChartTypes ,
2108
+ t ( {
2109
+ id : "controls.chart.disabled.temporal" ,
2110
+ message : "At least one temporal dimension is required." ,
2111
+ } )
2112
+ ) ;
2012
2113
}
2114
+ } else {
2115
+ maybeDisableChartTypes (
2116
+ chartTypes . filter ( ( d ) => d !== "table" ) ,
2117
+ t ( {
2118
+ id : "controls.chart.disabled.numerical" ,
2119
+ message : "At least one numerical measure is required." ,
2120
+ } )
2121
+ ) ;
2013
2122
}
2014
2123
2015
2124
if ( ordinalMeasures . length > 0 && geoDimensions . length > 0 ) {
2016
- possibles . push ( "map" ) ;
2125
+ enableChartType ( "map" ) ;
2126
+ } else {
2127
+ maybeDisableChartType (
2128
+ "map" ,
2129
+ "At least one ordinal measure and one geographical dimension are required."
2130
+ ) ;
2017
2131
}
2018
2132
2019
2133
const chartTypesOrder = getChartTypeOrder ( { cubeCount } ) ;
2020
-
2021
- return chartTypes
2022
- . filter (
2023
- ( d ) =>
2024
- possibles . includes ( d ) &&
2025
- ( ! allowedChartTypes || allowedChartTypes . includes ( d ) )
2026
- )
2134
+ const enabledChartTypes = chartTypes
2135
+ . filter ( ( d ) => possibleChartTypesDict [ d ] . enabled )
2027
2136
. sort ( ( a , b ) => chartTypesOrder [ a ] - chartTypesOrder [ b ] ) ;
2137
+
2138
+ return {
2139
+ enabledChartTypes,
2140
+ possibleChartTypesDict,
2141
+ } ;
2028
2142
} ;
2029
2143
2030
2144
export const getFieldComponentIds = ( fields : ChartConfig [ "fields" ] ) => {
0 commit comments