Skip to content

Commit b60be96

Browse files
authored
feat(TimeTable): add other sparkline type options (#35180)
1 parent fd6da21 commit b60be96

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

superset-frontend/src/explore/components/controls/TimeSeriesColumnControl/index.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const propTypes = {
4747
bounds: PropTypes.array,
4848
d3format: PropTypes.string,
4949
dateFormat: PropTypes.string,
50+
sparkType: PropTypes.string,
5051
onChange: PropTypes.func,
5152
};
5253

@@ -64,6 +65,7 @@ const defaultProps = {
6465
bounds: [null, null],
6566
d3format: '',
6667
dateFormat: '',
68+
sparkType: 'line',
6769
};
6870

6971
const comparisonTypeOptions = [
@@ -80,6 +82,12 @@ const colTypeOptions = [
8082
{ value: 'avg', label: t('Period average'), key: 'avg' },
8183
];
8284

85+
const sparkTypeOptions = [
86+
{ value: 'line', label: t('Line Chart'), key: 'line' },
87+
{ value: 'bar', label: t('Bar Chart'), key: 'bar' },
88+
{ value: 'area', label: t('Area Chart'), key: 'area' },
89+
];
90+
8391
const StyledRow = styled(Row)`
8492
margin-top: ${({ theme }) => theme.sizeUnit * 2}px;
8593
display: flex;
@@ -130,6 +138,7 @@ export default class TimeSeriesColumnControl extends Component {
130138
bounds: this.props.bounds,
131139
d3format: this.props.d3format,
132140
dateFormat: this.props.dateFormat,
141+
sparkType: this.props.sparkType,
133142
popoverVisible: false,
134143
};
135144
}
@@ -229,6 +238,18 @@ export default class TimeSeriesColumnControl extends Component {
229238
/>,
230239
)}
231240
<Divider />
241+
{this.state.colType === 'spark' &&
242+
this.formRow(
243+
t('Chart type'),
244+
t('Type of chart to display in sparkline'),
245+
'spark-type',
246+
<Select
247+
ariaLabel={t('Chart Type')}
248+
value={this.state.sparkType || undefined}
249+
onChange={this.onSelectChange.bind(this, 'sparkType')}
250+
options={sparkTypeOptions}
251+
/>,
252+
)}
232253
{this.state.colType === 'spark' &&
233254
this.formRow(
234255
t('Width'),

superset-frontend/src/visualizations/TimeTable/components/Sparkline/Sparkline.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const Sparkline = ({
5555
yAxisBounds={yAxisBounds}
5656
showYAxis={column.showYAxis || false}
5757
entries={entries}
58+
sparkType={column.sparkType || 'line'}
5859
/>
5960
);
6061
};

superset-frontend/src/visualizations/TimeTable/components/SparklineCell/SparklineCell.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@ import { scaleLinear } from '@visx/scale';
2323
import {
2424
Axis,
2525
LineSeries,
26+
BarSeries,
27+
AreaSeries,
2628
Tooltip,
2729
XYChart,
2830
buildChartTheme,
31+
type SeriesProps,
32+
AxisScale,
2933
} from '@visx/xychart';
3034
import { extendedDayjs } from '@superset-ui/core/utils/dates';
3135
import {
3236
getSparklineTextWidth,
3337
createYScaleConfig,
3438
transformChartData,
3539
} from '../../utils';
40+
import { SparkType } from '../../types';
3641

3742
interface Entry {
3843
time: string;
@@ -51,6 +56,7 @@ interface SparklineCellProps {
5156
showYAxis?: boolean;
5257
width?: number;
5358
yAxisBounds?: [number | undefined, number | undefined];
59+
sparkType?: SparkType;
5460
}
5561

5662
const MARGIN = {
@@ -71,6 +77,7 @@ const SparklineCell = ({
7177
yAxisBounds = [undefined, undefined],
7278
showYAxis = false,
7379
entries = [],
80+
sparkType = 'line',
7481
}: SparklineCellProps): ReactElement => {
7582
const theme = useTheme();
7683

@@ -127,6 +134,17 @@ const SparklineCell = ({
127134
const xAccessor = (d: { x: number; y: number }) => d.x;
128135
const yAccessor = (d: { x: number; y: number }) => d.y;
129136

137+
const chartSeriesMap: Record<
138+
SparkType,
139+
(props: SeriesProps<AxisScale, AxisScale, object>) => JSX.Element
140+
> = {
141+
line: LineSeries,
142+
bar: BarSeries,
143+
area: AreaSeries,
144+
};
145+
146+
const SeriesComponent = chartSeriesMap[sparkType] || LineSeries;
147+
130148
if (validData.length === 0) return <div style={{ width, height }} />;
131149

132150
return (
@@ -165,7 +183,7 @@ const SparklineCell = ({
165183
/>
166184
</>
167185
)}
168-
<LineSeries
186+
<SeriesComponent
169187
data={chartData}
170188
dataKey={dataKey}
171189
xAccessor={xAccessor}

superset-frontend/src/visualizations/TimeTable/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* under the License.
1818
*/
1919

20+
export type SparkType = 'line' | 'bar' | 'area';
21+
2022
export interface ColumnConfig {
2123
key: string;
2224
label?: string;
@@ -32,6 +34,7 @@ export interface ColumnConfig {
3234
dateFormat?: string;
3335
yAxisBounds?: [number | undefined, number | undefined] | null[];
3436
showYAxis?: boolean;
37+
sparkType?: SparkType;
3538
}
3639

3740
export interface ColumnRow {

0 commit comments

Comments
 (0)