UNPKG

@formant/ava-react

Version:

React components of AVA.

123 lines (121 loc) 5 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/InsightCard/ntvPlugins/components/G2Chart.tsx var G2Chart_exports = {}; __export(G2Chart_exports, { G2Chart: () => G2Chart, getSelectedColorSetFromStorage: () => getSelectedColorSetFromStorage }); module.exports = __toCommonJS(G2Chart_exports); var import_react = __toESM(require("react")); var import_g2 = require("@antv/g2"); var import_constants = require("../../constants"); var import_styled_components = require("styled-components"); var getSelectedColorSetFromStorage = () => { const storageThemeKey = "formant-ui-platform-theme"; return localStorage.getItem(storageThemeKey); }; var G2Chart = ({ spec, height, width }) => { const containerRef = (0, import_react.useRef)(null); const chartRef = import_react.default.useRef(null); const isRendering = import_react.default.useRef(false); const theme = (0, import_styled_components.useTheme)(); const themeList = getSelectedColorSetFromStorage(); const isDark = themeList !== "light"; const renderChart = async () => { var _a; const updatedSpec = { paddingTop: 20, axis: { ...spec.axis, label: { fillColorLight: "black", fillColorDark: "#ffffff" }, x: { autohide: true, labelAutoRotate: true }, style: { fill: "black" }, autohide: true, labelAutoRotate: true }, scale: { ...spec.scale, x: { nice: true }, y: { nice: true } } }; if (!(chartRef == null ? void 0 : chartRef.current)) { chartRef.current = new import_g2.Chart({ container: containerRef == null ? void 0 : containerRef.current, autoFit: true, padding: "auto" }); chartRef.current.options({ ...spec, ...updatedSpec, theme: isDark ? "classicDark" : "light" }); } else { chartRef.current.clear(); chartRef.current.options({ autoFit: true, padding: "auto" }); chartRef.current.options({ ...spec, ...updatedSpec, theme: isDark ? "classicDark" : "light" }); } if (!isRendering.current) { isRendering.current = true; await ((_a = chartRef.current) == null ? void 0 : _a.render()); isRendering.current = false; } }; (0, import_react.useEffect)(() => { renderChart(); }, [spec]); (0, import_react.useEffect)(() => { const handleResize = () => { var _a, _b, _c, _d; const updatedSpec = { paddingTop: 20, axis: { ...spec.axis, label: { fillColorLight: "black", fillColorDark: "#ffffff" }, x: { autohide: true, labelAutoRotate: true }, style: { fill: "black" }, autohide: true, labelAutoRotate: true }, scale: { ...spec.scale, x: { nice: true }, y: { nice: true } } }; (_c = chartRef.current) == null ? void 0 : _c.options({ width: (_a = containerRef.current) == null ? void 0 : _a.clientWidth, height: (_b = containerRef.current) == null ? void 0 : _b.clientHeight, // theme: "light", ...updatedSpec }); (_d = chartRef.current) == null ? void 0 : _d.render(); }; const resizeObserver = new ResizeObserver(handleResize); resizeObserver.observe(containerRef.current); return () => resizeObserver.disconnect(); }, []); return /* @__PURE__ */ import_react.default.createElement("div", { className: `${import_constants.INSIGHT_CARD_PREFIX_CLS}-chart-container` }, /* @__PURE__ */ import_react.default.createElement("div", { ref: containerRef, style: { height, width } })); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { G2Chart, getSelectedColorSetFromStorage });