Skip to content

Commit 8c75857

Browse files
rahmanunvergjulivan
authored andcommitted
refactor(charts-web): refactor for a generic approach
1 parent c5c0085 commit 8c75857

File tree

7 files changed

+26
-46
lines changed

7 files changed

+26
-46
lines changed

packages/pluggableWidgets/area-chart-web/src/AreaChart.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ export const AreaChart = memo(function AreaChart(props: AreaChartContainerProps)
5757
color: markerColorExpression
5858
? getExpressionValue<string>(markerColorExpression, pts.dataSourceItems)
5959
: undefined
60-
},
61-
x: pts.x,
62-
y: pts.y,
63-
hovertext: pts.hovertext,
64-
hoverinfo: pts.hoverinfo
60+
}
6561
};
6662
}, []);
6763

packages/pluggableWidgets/bar-chart-web/src/BarChart.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ export const BarChart = memo(function BarChart(props: BarChartContainerProps): R
5757
color: barColorExpression
5858
? getExpressionValue<string>(barColorExpression, pts.dataSourceItems)
5959
: undefined
60-
},
61-
x: pts.x,
62-
y: pts.y,
63-
hovertext: pts.hovertext,
64-
hoverinfo: pts.hoverinfo
60+
}
6561
};
6662
}, [])
6763
);

packages/pluggableWidgets/bubble-chart-web/src/BubbleChart.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ export const BubbleChart = memo(
8484
symbol: ["circle"],
8585
size,
8686
...markerOptions
87-
},
88-
x: pts.x,
89-
y: pts.y,
90-
hovertext: pts.hovertext,
91-
hoverinfo: pts.hoverinfo
87+
}
9288
};
9389
}, [])
9490
);

packages/pluggableWidgets/column-chart-web/src/ColumnChart.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ export const ColumnChart = memo(function ColumnChart(props: ColumnChartContainer
5555
color: columnColorExpression
5656
? getExpressionValue<string>(columnColorExpression, pts.dataSourceItems)
5757
: undefined
58-
},
59-
x: pts.x,
60-
y: pts.y,
61-
hovertext: pts.hovertext,
62-
hoverinfo: pts.hoverinfo
58+
}
6359
};
6460
}, [])
6561
);

packages/pluggableWidgets/line-chart-web/src/LineChart.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ export const LineChart = memo(
5252
color: markerColorExpression
5353
? getExpressionValue<string>(markerColorExpression, pts.dataSourceItems)
5454
: undefined
55-
},
56-
x: pts.x,
57-
y: pts.y,
58-
hovertext: pts.hovertext,
59-
hoverinfo: pts.hoverinfo
55+
}
6056
};
6157
}, [])
6258
);

packages/pluggableWidgets/time-series-chart-web/src/TimeSeries.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ export const TimeSeries = memo(
7272
},
7373
marker: {
7474
color: line.markerColor?.value
75-
},
76-
x: pts.x,
77-
y: pts.y,
78-
hovertext: pts.hovertext,
79-
hoverinfo: pts.hoverinfo
75+
}
8076
};
8177
}, [])
8278
);

packages/shared/charts/src/hooks/usePlotChartDataSeries.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ensure } from "@mendix/widget-plugin-platform/utils/ensure";
1313
import { Datum, PlotData } from "plotly.js-dist-min";
1414
import { executeAction } from "@mendix/widget-plugin-platform/framework/execute-action";
1515
import { ExtraTraceProps } from "../components/types";
16+
import { aggregateDataPoints, AggregationType } from "../utils/aggregations";
1617

1718
// Use "value" prop on EditableValue to extract AttributeValue, as AttributeValue not exported.
1819
type AttributeValue = EditableValue["value"];
@@ -41,6 +42,7 @@ export interface PlotDataSeries {
4142
customSeriesOptions: string | undefined;
4243
groupByAttribute?: ListAttributeValue<string | boolean | Date | Big>;
4344
staticDataSource?: ListValue;
45+
aggregationType?: AggregationType;
4446
dynamicDataSource?: ListValue;
4547
staticName?: DynamicValue<string>;
4648
dynamicName?: ListExpressionValue<string>;
@@ -94,16 +96,18 @@ function loadStaticSeries(series: PlotDataSeries, mapSerie: SeriesMapper<PlotDat
9496
throw Error("Expected series to be static");
9597
}
9698

97-
const dataPoints = extractDataPoints(series, staticName?.value);
98-
99-
if (!dataPoints) {
99+
const raw = extractDataPoints(series, staticName?.value);
100+
if (!raw) {
100101
return null;
101102
}
102103

104+
const agg = series.aggregationType as AggregationType;
105+
const points = agg && agg !== "none" ? aggregateDataPoints(agg, raw) : raw;
106+
103107
return {
104108
...(onClickAction ? { onClick: bindListAction(onClickAction) } : undefined),
105-
...dataPoints,
106-
...mapSerie(series, dataPoints, mapperHelpers),
109+
...points,
110+
...mapSerie(series, points, mapperHelpers),
107111
customSeriesOptions
108112
} as PlotChartSeries;
109113
}
@@ -115,28 +119,28 @@ function loadDynamicSeries(series: PlotDataSeries, mapSerie: SeriesMapper<PlotDa
115119
throw Error("Expected series to be dynamic");
116120
}
117121

118-
const dataSourceItemGroups = groupDataSourceItems(series);
119-
if (!dataSourceItemGroups) {
122+
const groups = groupDataSourceItems(series);
123+
if (!groups) {
120124
return null;
121125
}
122126

123-
const loadedSeries = dataSourceItemGroups
124-
.map(itemGroup => {
125-
const dataPoints = extractDataPoints(series, itemGroup.dynamicNameValue, itemGroup.items);
126-
if (!dataPoints) {
127+
return groups
128+
.map(group => {
129+
const raw = extractDataPoints(series, group.dynamicNameValue, group.items);
130+
if (!raw) {
127131
return null;
128132
}
133+
const agg = series.aggregationType as AggregationType;
134+
const points = agg && agg !== "none" ? aggregateDataPoints(agg, raw) : raw;
129135

130136
return {
131137
...(onClickAction ? { onClick: bindListAction(onClickAction) } : undefined),
132-
...dataPoints,
133-
...mapSerie(series, dataPoints, mapperHelpers),
138+
...points,
139+
...mapSerie(series, points, mapperHelpers),
134140
customSeriesOptions
135141
} as PlotChartSeries;
136142
})
137-
.filter((e): e is PlotChartSeries => Boolean(e));
138-
139-
return loadedSeries;
143+
.filter((x): x is PlotChartSeries => Boolean(x));
140144
}
141145

142146
function groupDataSourceItems(series: PlotDataSeries): DataSourceItemGroup[] | null {

0 commit comments

Comments
 (0)