Skip to content

[WC-2922]: Add aggregate utils for charts #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/pluggableWidgets/area-chart-web/src/AreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ChartWidgetProps,
SeriesMapper,
containerPropsEqual,
getPlotChartDataTransforms,
usePlotChartDataSeries
} from "@mendix/shared-charts/main";
import "@mendix/shared-charts/ui/Chart.scss";
Expand Down Expand Up @@ -37,6 +36,7 @@ export const AreaChart = memo(function AreaChart(props: AreaChartContainerProps)
const lineColorExpression = line.dataSet === "static" ? line.staticLineColor : line.dynamicLineColor;
const markerColorExpression = line.dataSet === "static" ? line.staticMarkerColor : line.dynamicMarkerColor;
const fillColorExpression = line.dataSet === "static" ? line.staticFillColor : line.dynamicFillColor;

return {
type: "scatter",
fill: "tonexty",
Expand All @@ -54,8 +54,7 @@ export const AreaChart = memo(function AreaChart(props: AreaChartContainerProps)
color: markerColorExpression
? getExpressionValue<string>(markerColorExpression, dataPoints.dataSourceItems)
: undefined
},
transforms: getPlotChartDataTransforms(line.aggregationType, dataPoints)
}
};
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,29 @@ describe("The AreaChart widget", () => {
);
});

it("sets the appropriate transforms on the data series based on the aggregation type", () => {
it("aggregates data based on the aggregation type", () => {
renderAreaChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);

expect(ChartWidget).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.arrayContaining([
expect.objectContaining({ transforms: [] }),
expect.objectContaining({
transforms: [
{
type: "aggregate",
groups: ["1", "2"],
aggregations: [
{
target: "y",
enabled: true,
func: "avg"
}
]
}
]
x: expect.arrayContaining([expect.any(Number)]),
y: expect.arrayContaining([expect.any(Number)])
}),
expect.objectContaining({
x: expect.arrayContaining([expect.any(Number)]),
y: expect.arrayContaining([expect.any(Number)])
})
])
}),
{}
);

renderAreaChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);
const mockCalls = (ChartWidget as jest.Mock).mock.calls;
const lastCallProps = mockCalls[mockCalls.length - 1][0];
expect(lastCallProps.data).toHaveLength(2);
});
});

Expand Down
12 changes: 3 additions & 9 deletions packages/pluggableWidgets/bar-chart-web/src/BarChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ChartWidget,
ChartWidgetProps,
containerPropsEqual,
getPlotChartDataTransforms,
usePlotChartDataSeries
} from "@mendix/shared-charts/main";
import { ChartWidget, ChartWidgetProps, containerPropsEqual, usePlotChartDataSeries } from "@mendix/shared-charts/main";
import "@mendix/shared-charts/ui/Chart.scss";
import classNames from "classnames";
import { ReactElement, createElement, memo, useCallback, useMemo } from "react";
Expand Down Expand Up @@ -51,15 +45,15 @@ export const BarChart = memo(function BarChart(props: BarChartContainerProps): R
useCallback((dataSeries, dataPoints, { getExpressionValue }) => {
const barColorExpression =
dataSeries.dataSet === "static" ? dataSeries.staticBarColor : dataSeries.dynamicBarColor;

return {
type: "bar",
orientation: "h",
marker: {
color: barColorExpression
? getExpressionValue<string>(barColorExpression, dataPoints.dataSourceItems)
: undefined
},
transforms: getPlotChartDataTransforms(dataSeries.aggregationType, dataPoints)
}
};
}, [])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,29 @@ describe("The BarChart widget", () => {
);
});

it("sets the appropriate transforms on the data series based on the aggregation type", () => {
it("aggregates data based on the aggregation type", () => {
renderBarChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);

expect(ChartWidget).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.arrayContaining([
expect.objectContaining({ transforms: expect.arrayContaining([]) }),
expect.objectContaining({
transforms: expect.arrayContaining([
expect.objectContaining({
type: "aggregate",
groups: expect.arrayContaining(["1", "2"]),
aggregations: expect.arrayContaining([
expect.objectContaining({ target: "y", enabled: true, func: "avg" })
])
})
])
x: expect.arrayContaining([expect.any(Number)]),
y: expect.arrayContaining([expect.any(Number)])
}),
expect.objectContaining({
x: expect.arrayContaining([expect.any(Number)]),
y: expect.arrayContaining([expect.any(Number)])
})
])
}),
{}
);

renderBarChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);
const mockCalls = (ChartWidget as jest.Mock).mock.calls;
const lastCallProps = mockCalls[mockCalls.length - 1][0];
expect(lastCallProps.data).toHaveLength(2);
});
});

Expand Down
12 changes: 3 additions & 9 deletions packages/pluggableWidgets/bubble-chart-web/src/BubbleChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ChartWidget,
ChartWidgetProps,
getPlotChartDataTransforms,
traceEqual,
usePlotChartDataSeries
} from "@mendix/shared-charts/main";
import { ChartWidget, ChartWidgetProps, traceEqual, usePlotChartDataSeries } from "@mendix/shared-charts/main";
import "@mendix/shared-charts/ui/Chart.scss";
import { defaultEqual, flatEqual } from "@mendix/widget-plugin-platform/utils/flatEqual";
import Big from "big.js";
Expand Down Expand Up @@ -75,6 +69,7 @@
);
const markerColorExpression =
line.dataSet === "static" ? line.staticMarkerColor : line.dynamicMarkerColor;

return {
type: "scatter",
mode: "markers",
Expand All @@ -85,10 +80,9 @@
symbol: ["circle"],
size,
...markerOptions
},
transforms: getPlotChartDataTransforms(line.aggregationType, dataPoints)
}
};
}, [])

Check warning on line 85 in packages/pluggableWidgets/bubble-chart-web/src/BubbleChart.tsx

View workflow job for this annotation

GitHub Actions / Run code quality check

React Hook useCallback has missing dependencies: 'props.height', 'props.heightUnit', 'props.width', and 'props.widthUnit'. Either include them or remove the dependency array
);
return (
<ChartWidget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,29 @@ describe("The Bubble widget", () => {
);
});

it("sets the appropriate transforms on the data series based on the aggregation type", () => {
it("aggregates data based on the aggregation type", () => {
renderBubbleChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);

expect(ChartWidget).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.arrayContaining([
expect.objectContaining({ transforms: expect.arrayContaining([]) }),
expect.objectContaining({
transforms: expect.arrayContaining([
expect.objectContaining({
type: "aggregate",
groups: expect.arrayContaining(["1", "2"]),
aggregations: expect.arrayContaining([
expect.objectContaining({ target: "y", enabled: true, func: "avg" })
])
})
])
x: expect.arrayContaining([expect.any(Number)]),
y: expect.arrayContaining([expect.any(Number)])
}),
expect.objectContaining({
x: expect.arrayContaining([expect.any(Number)]),
y: expect.arrayContaining([expect.any(Number)])
})
])
}),
{}
);

renderBubbleChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);
const mockCalls = (ChartWidget as jest.Mock).mock.calls;
const lastCallProps = mockCalls[mockCalls.length - 1][0];
expect(lastCallProps.data).toHaveLength(2);
});
});

Expand Down
11 changes: 2 additions & 9 deletions packages/pluggableWidgets/column-chart-web/src/ColumnChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ChartWidget,
ChartWidgetProps,
containerPropsEqual,
getPlotChartDataTransforms,
usePlotChartDataSeries
} from "@mendix/shared-charts/main";
import { ChartWidget, ChartWidgetProps, containerPropsEqual, usePlotChartDataSeries } from "@mendix/shared-charts/main";
import "@mendix/shared-charts/ui/Chart.scss";
import classNames from "classnames";
import { ReactElement, createElement, memo, useCallback, useMemo } from "react";
Expand Down Expand Up @@ -56,8 +50,7 @@ export const ColumnChart = memo(function ColumnChart(props: ColumnChartContainer
color: columnColorExpression
? getExpressionValue<string>(columnColorExpression, dataPoints.dataSourceItems)
: undefined
},
transforms: getPlotChartDataTransforms(dataSeries.aggregationType, dataPoints)
}
};
}, [])
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { ChartWidget } from "@mendix/shared-charts/main";
import { EditableValueBuilder, ListAttributeValueBuilder, list, listExp } from "@mendix/widget-plugin-test-utils";
import Big from "big.js";
import { mount, ReactWrapper } from "enzyme";
import { render, RenderResult } from "@testing-library/react";
import { createElement } from "react";
import { ColumnChartContainerProps, SeriesType } from "../../typings/ColumnChartProps";
import { ColumnChart } from "../ColumnChart";

jest.mock("@mendix/shared-charts/main", () => {
const actualModule = jest.requireActual("@mendix/shared-charts/main");
return {
...actualModule,
ChartWidget: jest.fn(() => null)
};
});

jest.mock("react-plotly.js", () => jest.fn(() => null));

describe("The ColumnChart widget", () => {
function renderColumnChart(
configs: Array<Partial<SeriesType>>,
chartProps?: Partial<ColumnChartContainerProps>
): ReactWrapper {
return mount(
): RenderResult {
return render(
<ColumnChart
name="column-chart-test"
class="column-chart-class"
Expand All @@ -36,47 +44,64 @@ describe("The ColumnChart widget", () => {
}

it("visualizes data as a bar chart", () => {
const columnChart = renderColumnChart([{}]);
const seriesOptions = columnChart.find(ChartWidget).prop("seriesOptions");
expect(seriesOptions).toHaveProperty("type", "bar");
expect(seriesOptions).toHaveProperty("orientation", "v");
renderColumnChart([{}]);

expect(ChartWidget).toHaveBeenCalledWith(
expect.objectContaining({
seriesOptions: expect.objectContaining({
type: "bar",
orientation: "v"
})
}),
expect.anything()
);
});

it("sets the bar color on the data series based on the barColor value", () => {
const columnChart = renderColumnChart([
{ staticBarColor: listExp(() => "red") },
{ staticBarColor: undefined }
]);
const data = columnChart.find(ChartWidget).prop("data");
renderColumnChart([{ staticBarColor: listExp(() => "red") }, { staticBarColor: undefined }]);

const mockCalls = (ChartWidget as jest.Mock).mock.calls;
const lastCallProps = mockCalls[mockCalls.length - 1][0];
const data = lastCallProps.data;

expect(data).toHaveLength(2);
expect(data[0]).toHaveProperty("marker.color", "red");
expect(data[1]).toHaveProperty("marker.color", undefined);
expect(data[0].marker.color).toBe("red");
expect(data[1].marker.color).toBeUndefined();
});

it("sets the appropriate transforms on the data series based on the aggregation type", () => {
const columnChart = renderColumnChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);
const data = columnChart.find(ChartWidget).prop("data");
it("aggregates data based on the aggregation type", () => {
renderColumnChart([{ aggregationType: "none" }, { aggregationType: "avg" }]);

const mockCalls = (ChartWidget as jest.Mock).mock.calls;
const lastCallProps = mockCalls[mockCalls.length - 1][0];
const data = lastCallProps.data;

expect(data).toHaveLength(2);
expect(data[0]).toHaveProperty("transforms", []);
expect(data[1]).toHaveProperty("transforms", [
{
type: "aggregate",
groups: ["1", "2"],
aggregations: [
{
target: "y",
enabled: true,
func: "avg"
}
]
}
]);

// Check that each data series has x and y arrays
expect(data[0]).toHaveProperty("x");
expect(data[0]).toHaveProperty("y");
expect(data[1]).toHaveProperty("x");
expect(data[1]).toHaveProperty("y");

// Verify the arrays contain numbers
expect(Array.isArray(data[0].x)).toBe(true);
expect(Array.isArray(data[0].y)).toBe(true);
expect(Array.isArray(data[1].x)).toBe(true);
expect(Array.isArray(data[1].y)).toBe(true);
});

it("sets the appropriate barmode on the layout based on the barmode type", () => {
const columnChart = renderColumnChart([], { barmode: "stack" });
const layoutOptions = columnChart.find(ChartWidget).prop("layoutOptions");
expect(layoutOptions).toHaveProperty("barmode", "stack");
renderColumnChart([], { barmode: "stack" });

expect(ChartWidget).toHaveBeenCalledWith(
expect.objectContaining({
layoutOptions: expect.objectContaining({
barmode: "stack"
})
}),
expect.anything()
);
});
});

Expand Down
11 changes: 2 additions & 9 deletions packages/pluggableWidgets/line-chart-web/src/LineChart.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
ChartWidget,
ChartWidgetProps,
getPlotChartDataTransforms,
traceEqual,
usePlotChartDataSeries
} from "@mendix/shared-charts/main";
import { ChartWidget, ChartWidgetProps, traceEqual, usePlotChartDataSeries } from "@mendix/shared-charts/main";
import "@mendix/shared-charts/ui/Chart.scss";
import { defaultEqual, flatEqual } from "@mendix/widget-plugin-platform/utils/flatEqual";
import classNames from "classnames";
Expand Down Expand Up @@ -54,8 +48,7 @@ export const LineChart = memo(
color: markerColorExpression
? getExpressionValue<string>(markerColorExpression, dataPoints.dataSourceItems)
: undefined
},
transforms: getPlotChartDataTransforms(line.aggregationType, dataPoints)
}
};
}, [])
);
Expand Down
Loading
Loading