UNPKG

@vectara/vectara-ui

Version:

Vectara's design system, codified as a React and Sass component library

35 lines (34 loc) 3.23 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useState } from "react"; import { CartesianGrid, Legend, ResponsiveContainer, Scatter, ScatterChart as RechartsScatterChart, Tooltip, XAxis, YAxis } from "recharts"; import { getChartColor, getChartColorByIndex } from "./palette"; import { chartAxisLineStyle, chartLegendProps, chartTickStyle, chartTooltipProps } from "./chartTheme"; const DEFAULT_RADIUS = 4; const ACTIVE_RADIUS = 7; export const VuiScatterChart = (_a) => { var { series, xKey = "x", yKey = "y", height = 320, showLegend = series.length > 1, showGrid = true, showTooltip = true } = _a, rest = __rest(_a, ["series", "xKey", "yKey", "height", "showLegend", "showGrid", "showTooltip"]); // Recharts fires activeShape for the active point of every series at the // shared hover index, which would light up points across colors at once. // Tracking the hovered series lets only that series emphasize its point; // the others render an ordinary dot. const [hoveredSeries, setHoveredSeries] = useState(null); return (_jsx("div", Object.assign({ className: "vuiScatterChart" }, rest, { children: _jsx(ResponsiveContainer, Object.assign({ width: "100%", height: height }, { children: _jsxs(RechartsScatterChart, { children: [showGrid && _jsx(CartesianGrid, { stroke: "var(--vui-color-border-light)" }), _jsx(XAxis, { type: "number", dataKey: xKey, tick: chartTickStyle, axisLine: chartAxisLineStyle, tickLine: false }), _jsx(YAxis, { type: "number", dataKey: yKey, tick: chartTickStyle, axisLine: chartAxisLineStyle, tickLine: false }), showTooltip && (_jsx(Tooltip, Object.assign({ cursor: { strokeDasharray: "3 3", stroke: "var(--vui-color-border-medium)" } }, chartTooltipProps))), showLegend && _jsx(Legend, Object.assign({}, chartLegendProps)), series.map((s, index) => { var _a; const color = s.color ? getChartColor(s.color) : getChartColorByIndex(index); // Only the hovered series rings its active point; ring it with the // surface color so it lifts above the points it overlaps. const renderActivePoint = ({ cx, cy }) => hoveredSeries === index ? (_jsx("circle", { cx: cx, cy: cy, r: ACTIVE_RADIUS, fill: color, stroke: "var(--vui-color-empty-shade)", strokeWidth: 2 })) : (_jsx("circle", { cx: cx, cy: cy, r: DEFAULT_RADIUS, fill: color })); return (_jsx(Scatter, { name: s.name, data: s.data, fill: color, activeShape: renderActivePoint, onMouseEnter: () => setHoveredSeries(index), onMouseLeave: () => setHoveredSeries(null) }, (_a = s.name) !== null && _a !== void 0 ? _a : index)); })] }) })) }))); };