Skip to content

boxplot/parallel/line3d chart #1548

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

Merged
merged 6 commits into from
Mar 3, 2025
Merged
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
25 changes: 25 additions & 0 deletions client/packages/lowcoder-comps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"agora-rtm-sdk": "^1.5.1",
"big.js": "^6.2.1",
"echarts-extension-gmap": "^1.6.0",
"echarts-gl": "^2.0.9",
"echarts-wordcloud": "^2.1.0",
"lowcoder-cli": "workspace:^",
"lowcoder-sdk": "workspace:^",
Expand Down Expand Up @@ -90,6 +91,30 @@
"h": 40
}
},
"boxplotChart": {
"name": "Boxplot Chart",
"icon": "./icons/icon-chart.svg",
"layoutInfo": {
"w": 12,
"h": 40
}
},
"parallelChart": {
"name": "Parallel Chart",
"icon": "./icons/icon-chart.svg",
"layoutInfo": {
"w": 12,
"h": 40
}
},
"line3dChart": {
"name": "Line3D Chart",
"icon": "./icons/icon-chart.svg",
"layoutInfo": {
"w": 12,
"h": 40
}
},
"imageEditor": {
"name": "Image Editor",
"icon": "./icons/icon-chart.svg",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as echarts from "echarts";
import "echarts-gl";
import "echarts-wordcloud";
import { EChartsReactProps, EChartsInstance, EChartsOptionWithMap } from "./types";
import EChartsReactCore from "./core";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
import {
changeChildAction,
changeValueAction,
CompAction,
CompActionTypes,
wrapChildAction,
} from "lowcoder-core";
import { AxisFormatterComp, EchartsAxisType } from "../basicChartComp/chartConfigs/cartesianAxisConfig";
import { boxplotChartChildrenMap, ChartSize, getDataKeys } from "./boxplotChartConstants";
import { boxplotChartPropertyView } from "./boxplotChartPropertyView";
import _ from "lodash";
import { useContext, useEffect, useMemo, useRef, useState } from "react";
import ReactResizeDetector from "react-resize-detector";
import ReactECharts from "../basicChartComp/reactEcharts";
import * as echarts from "echarts";
import {
childrenToProps,
depsConfig,
genRandomKey,
NameConfig,
UICompBuilder,
withDefault,
withExposingConfigs,
withViewFn,
ThemeContext,
chartColorPalette,
getPromiseAfterDispatch,
dropdownControl,
} from "lowcoder-sdk";
import { getEchartsLocale, i18nObjs, trans } from "i18n/comps";
import {
echartsConfigOmitChildren,
getEchartsConfig,
getSelectedPoints,
} from "./boxplotChartUtils";
import 'echarts-extension-gmap';
import log from "loglevel";

let clickEventCallback = () => {};

const chartModeOptions = [
{
label: "UI",
value: "ui",
}
] as const;

let BoxplotChartTmpComp = (function () {
return new UICompBuilder({mode:dropdownControl(chartModeOptions,'ui'),...boxplotChartChildrenMap}, () => null)
.setPropertyViewFn(boxplotChartPropertyView)
.build();
})();

BoxplotChartTmpComp = withViewFn(BoxplotChartTmpComp, (comp) => {
const mode = comp.children.mode.getView();
const onUIEvent = comp.children.onUIEvent.getView();
const onEvent = comp.children.onEvent.getView();
const echartsCompRef = useRef<ReactECharts | null>();
const [chartSize, setChartSize] = useState<ChartSize>();
const firstResize = useRef(true);
const theme = useContext(ThemeContext);
const defaultChartTheme = {
color: chartColorPalette,
backgroundColor: "#fff",
};

let themeConfig = defaultChartTheme;
try {
themeConfig = theme?.theme.chart ? JSON.parse(theme?.theme.chart) : defaultChartTheme;
} catch (error) {
log.error('theme chart error: ', error);
}

const triggerClickEvent = async (dispatch: any, action: CompAction<JSONValue>) => {
await getPromiseAfterDispatch(
dispatch,
action,
{ autoHandleAfterReduce: true }
);
onEvent('click');
}

useEffect(() => {
const echartsCompInstance = echartsCompRef?.current?.getEchartsInstance();
if (!echartsCompInstance) {
return _.noop;
}
echartsCompInstance?.on("click", (param: any) => {
document.dispatchEvent(new CustomEvent("clickEvent", {
bubbles: true,
detail: {
action: 'click',
data: param.data,
}
}));
triggerClickEvent(
comp.dispatch,
changeChildAction("lastInteractionData", param.data, false)
);
});
return () => {
echartsCompInstance?.off("click");
document.removeEventListener('clickEvent', clickEventCallback)
};
}, []);

useEffect(() => {
// bind events
const echartsCompInstance = echartsCompRef?.current?.getEchartsInstance();
if (!echartsCompInstance) {
return _.noop;
}
echartsCompInstance?.on("selectchanged", (param: any) => {
const option: any = echartsCompInstance?.getOption();
document.dispatchEvent(new CustomEvent("clickEvent", {
bubbles: true,
detail: {
action: param.fromAction,
data: getSelectedPoints(param, option)
}
}));

if (param.fromAction === "select") {
comp.dispatch(changeChildAction("selectedPoints", getSelectedPoints(param, option), false));
onUIEvent("select");
} else if (param.fromAction === "unselect") {
comp.dispatch(changeChildAction("selectedPoints", getSelectedPoints(param, option), false));
onUIEvent("unselect");
}

triggerClickEvent(
comp.dispatch,
changeChildAction("lastInteractionData", getSelectedPoints(param, option), false)
);
});
// unbind
return () => {
echartsCompInstance?.off("selectchanged");
document.removeEventListener('clickEvent', clickEventCallback)
};
}, [onUIEvent]);

const echartsConfigChildren = _.omit(comp.children, echartsConfigOmitChildren);
const childrenProps = childrenToProps(echartsConfigChildren);

const option = useMemo(() => {
return getEchartsConfig(
childrenProps as ToViewReturn<typeof echartsConfigChildren>,
chartSize,
themeConfig
);
}, [theme, childrenProps, chartSize, ...Object.values(echartsConfigChildren)]);

return (
<ReactResizeDetector
onResize={(w, h) => {
if (w && h) {
setChartSize({ w: w, h: h });
}
if (!firstResize.current) {
// ignore the first resize, which will impact the loading animation
echartsCompRef.current?.getEchartsInstance().resize();
} else {
firstResize.current = false;
}
}}
>
<ReactECharts
ref={(e) => (echartsCompRef.current = e)}
style={{ height: "100%" }}
notMerge
lazyUpdate
opts={{ locale: getEchartsLocale() }}
option={option}
mode={mode}
/>
</ReactResizeDetector>
);
});

function getYAxisFormatContextValue(
data: Array<JSONObject>,
yAxisType: EchartsAxisType,
yAxisName?: string
) {
const dataSample = yAxisName && data.length > 0 && data[0][yAxisName];
let contextValue = dataSample;
if (yAxisType === "time") {
// to timestamp
const time =
typeof dataSample === "number" || typeof dataSample === "string"
? new Date(dataSample).getTime()
: null;
if (time) contextValue = time;
}
return contextValue;
}

BoxplotChartTmpComp = class extends BoxplotChartTmpComp {
private lastYAxisFormatContextVal?: JSONValue;
private lastColorContext?: JSONObject;

updateContext(comp: this) {
// the context value of axis format
let resultComp = comp;
const data = comp.children.data.getView();
const yAxisContextValue = getYAxisFormatContextValue(
data,
comp.children.yConfig.children.yAxisType.getView(),
);
if (yAxisContextValue !== comp.lastYAxisFormatContextVal) {
comp.lastYAxisFormatContextVal = yAxisContextValue;
resultComp = comp.setChild(
"yConfig",
comp.children.yConfig.reduce(
wrapChildAction(
"formatter",
AxisFormatterComp.changeContextDataAction({ value: yAxisContextValue })
)
)
);
}
return resultComp;
}

override reduce(action: CompAction): this {
const comp = super.reduce(action);
if (action.type === CompActionTypes.UPDATE_NODES_V2) {
const newData = comp.children.data.getView();
// data changes
if (comp.children.data !== this.children.data) {
setTimeout(() => {
// update x-axis value
const keys = getDataKeys(newData);
if (keys.length > 0 && !keys.includes(comp.children.xAxisKey.getView())) {
comp.children.xAxisKey.dispatch(changeValueAction(keys[0] || ""));
}
if (keys.length > 0 && !keys.includes(comp.children.yAxisKey.getView())) {
comp.children.yAxisKey.dispatch(changeValueAction(keys[1] || ""));
}
}, 0);
}
return this.updateContext(comp);
}
return comp;
}

override autoHeight(): boolean {
return false;
}
};

let BoxplotChartComp = withExposingConfigs(BoxplotChartTmpComp, [
depsConfig({
name: "selectedPoints",
desc: trans("chart.selectedPointsDesc"),
depKeys: ["selectedPoints"],
func: (input) => {
return input.selectedPoints;
},
}),
depsConfig({
name: "lastInteractionData",
desc: trans("chart.lastInteractionDataDesc"),
depKeys: ["lastInteractionData"],
func: (input) => {
return input.lastInteractionData;
},
}),
depsConfig({
name: "data",
desc: trans("chart.dataDesc"),
depKeys: ["data", "mode"],
func: (input) =>[] ,
}),
new NameConfig("title", trans("chart.titleDesc")),
]);


export const BoxplotChartCompWithDefault = withDefault(BoxplotChartComp, {
xAxisKey: "date",
});
Loading
Loading