feat: enhance sidebar functionality and styling
- Updated sidebar component to support dynamic width based on collapsed state. - Improved sidebar icon rendering and added user account management features. - Introduced new trend chart settings component for better user interaction. - Added utility functions for engineering units and trend chart settings. - Implemented color assignment logic for trend series to enhance visual representation. - Enhanced CSS styles for better responsiveness and usability across different screen sizes. - Introduced new configuration for Vite server to allow shared directory access. - Added .dockerignore file to optimize Docker build context.
This commit is contained in:
1 parent
2f5f86452f
commit
3203577a9d
28 files changed
+1508
-758
No files matched your search
@@ -0,0 +1,83 @@
|
||||
const icons = {
|
||||
dashboard: "M3 3h7v7H3z M14 3h7v7h-7z M3 14h7v7H3z M14 14h7v7h-7z",
|
||||
trends: "M3 4v16h18 M6 15l5-5 4 3 6-7",
|
||||
admin: "M12 22s7-3 7-10V5l-7-3-7 3v7c0 7 7 10 7 10Z M9 12l2 2 4-4",
|
||||
settings: "M4 6h6m4 0h6 M4 12h10m4 0h2 M4 18h2m4 0h10 M10 4v4m4 2v4m-8 2v4",
|
||||
expand: "M3 5h18v14H3z M9 5v14 M13 9l3 3-3 3",
|
||||
collapse: "M3 5h18v14H3z M9 5v14 M16 9l-3 3 3 3",
|
||||
login: "M14 4h6v16h-6 M4 12h12 M12 8l4 4-4 4",
|
||||
logout: "M10 4H4v16h6 M8 12h12 M16 8l4 4-4 4",
|
||||
};
|
||||
|
||||
function SidebarIcon({ name }) {
|
||||
return (
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
|
||||
<path d={icons[name]} />
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
|
||||
export function SmallFootnote() {
|
||||
return <div className="tiny-note">by J.H. | SE Inno GmbH</div>;
|
||||
}
|
||||
|
||||
export default function Sidebar({ collapsed, onToggle, views, activeView, onNavigate, user, userLabel, onLogout }) {
|
||||
const toggleLabel = collapsed ? "Seitenleiste ausklappen" : "Seitenleiste einklappen";
|
||||
const accountLabel = user ? "Abmelden" : "Anmelden";
|
||||
|
||||
return (
|
||||
<aside className={collapsed ? "sidebar collapsed" : "sidebar"}>
|
||||
<div className="sidebar-top">
|
||||
<div className="sidebar-brand-row">
|
||||
<div className="logo-lockup">
|
||||
<img src="/logo.png" alt="SE Logo" />
|
||||
{collapsed ? null : (
|
||||
<div>
|
||||
<div className="eyebrow">SE</div>
|
||||
<div className="logo-title">SE Local Trenddata.</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<button type="button" className="icon-button sidebar-toggle" title={toggleLabel} aria-label={toggleLabel} aria-expanded={!collapsed} onClick={onToggle}>
|
||||
<SidebarIcon name={collapsed ? "expand" : "collapse"} />
|
||||
</button>
|
||||
</div>
|
||||
<nav className="nav-list" aria-label="Hauptnavigation">
|
||||
{views.map((view) => (
|
||||
<button
|
||||
key={view.id}
|
||||
type="button"
|
||||
className={activeView === view.id ? "nav-button active" : "nav-button"}
|
||||
title={collapsed ? view.label : undefined}
|
||||
aria-label={view.label}
|
||||
aria-current={activeView === view.id ? "page" : undefined}
|
||||
onClick={() => onNavigate(view.id)}
|
||||
>
|
||||
<SidebarIcon name={view.id} />
|
||||
{collapsed ? null : <span>{view.label}</span>}
|
||||
</button>
|
||||
))}
|
||||
</nav>
|
||||
</div>
|
||||
<div className="sidebar-footer">
|
||||
{user && !collapsed ? (
|
||||
<div className="user-card">
|
||||
<strong>{userLabel}</strong>
|
||||
<span>{user.role === "admin" ? "Systemzugang" : user.role === "technician" ? "Technikzugang" : "Benutzerzugang"}</span>
|
||||
</div>
|
||||
) : null}
|
||||
<button
|
||||
className={collapsed ? "icon-button sidebar-account" : "ghost-button compact-button sidebar-account"}
|
||||
type="button"
|
||||
title={collapsed ? accountLabel : undefined}
|
||||
aria-label={accountLabel}
|
||||
onClick={user ? onLogout : () => onNavigate("settings")}
|
||||
>
|
||||
<SidebarIcon name={user ? "logout" : "login"} />
|
||||
{collapsed ? null : <span>{accountLabel}</span>}
|
||||
</button>
|
||||
{collapsed ? null : <SmallFootnote />}
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
+147
-205
@@ -1,16 +1,20 @@
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useMemo, useRef, useState } from "react";
|
||||
import { jsPDF } from "jspdf";
|
||||
import * as XLSX from "xlsx";
|
||||
import {
|
||||
CartesianGrid,
|
||||
Customized,
|
||||
Line,
|
||||
LineChart,
|
||||
ReferenceArea,
|
||||
ResponsiveContainer,
|
||||
Tooltip,
|
||||
XAxis,
|
||||
YAxis,
|
||||
} from "recharts";
|
||||
import TrendChartSettings from "./TrendChartSettings.jsx";
|
||||
import { getTimeZoom, getTrendAxes, getTrendAxisScale, seriesAxisId } from "./trend-scales.mjs";
|
||||
import { DEFAULT_CHART_SETTINGS } from "../../../shared/trend-chart-settings.mjs";
|
||||
import { assignTrendColors } from "./trend-colors.mjs";
|
||||
|
||||
function downloadBlob(filename, blob) {
|
||||
const url = URL.createObjectURL(blob);
|
||||
@@ -25,43 +29,68 @@ function quoteCell(value) {
|
||||
return `"${String(value ?? "").replaceAll('"', '""')}"`;
|
||||
}
|
||||
|
||||
function buildRandomColor(index) {
|
||||
const hue = Math.round((Math.random() * 360 + index * 41) % 360);
|
||||
const saturation = 72 + (index % 3) * 4;
|
||||
const lightness = 46 + (index % 4) * 3;
|
||||
return `hsl(${hue} ${saturation}% ${lightness}%)`;
|
||||
}
|
||||
|
||||
function getAnalogDomain(rows, visibleSeries) {
|
||||
const values = [];
|
||||
rows.forEach((row) => {
|
||||
visibleSeries.filter((item) => item.kind !== "digital").forEach((item) => {
|
||||
const value = row[item.key];
|
||||
if (Number.isFinite(value)) {
|
||||
values.push(value);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
if (!values.length) {
|
||||
return [0, 1];
|
||||
}
|
||||
|
||||
const min = Math.min(...values);
|
||||
const max = Math.max(...values);
|
||||
if (min === max) {
|
||||
return [min - 1, max + 1];
|
||||
}
|
||||
|
||||
const padding = (max - min) * 0.08;
|
||||
return [min - padding, max + padding];
|
||||
}
|
||||
|
||||
function formatTrendNumber(value) {
|
||||
const numeric = Number(value);
|
||||
return Number.isFinite(numeric) ? numeric.toFixed(2) : "--";
|
||||
}
|
||||
|
||||
function TimeZoomOverlay({ offset, onZoom }) {
|
||||
const activeDrag = useRef(null);
|
||||
const [drag, setDrag] = useState(null);
|
||||
if (!offset?.width || !offset.height) return null;
|
||||
|
||||
function position(event) {
|
||||
const bounds = event.currentTarget.getBoundingClientRect();
|
||||
return Math.max(0, Math.min(1, (event.clientX - bounds.left) / bounds.width));
|
||||
}
|
||||
|
||||
function cancel() {
|
||||
activeDrag.current = null;
|
||||
setDrag(null);
|
||||
}
|
||||
|
||||
return (
|
||||
<g>
|
||||
{drag ? (
|
||||
<rect
|
||||
x={offset.left + Math.min(drag.start, drag.end) * offset.width}
|
||||
y={offset.top}
|
||||
width={Math.abs(drag.end - drag.start) * offset.width}
|
||||
height={offset.height}
|
||||
fill="var(--brand-2)"
|
||||
fillOpacity={0.2}
|
||||
pointerEvents="none"
|
||||
/>
|
||||
) : null}
|
||||
<rect
|
||||
className="trend-zoom-target"
|
||||
x={offset.left} y={offset.top} width={offset.width} height={offset.height}
|
||||
fill="transparent"
|
||||
onPointerDown={(event) => {
|
||||
if (!event.isPrimary || event.button !== 0) return;
|
||||
const start = position(event);
|
||||
event.currentTarget.setPointerCapture(event.pointerId);
|
||||
activeDrag.current = { start, end: start, pointerId: event.pointerId };
|
||||
setDrag(activeDrag.current);
|
||||
}}
|
||||
onPointerMove={(event) => {
|
||||
if (activeDrag.current?.pointerId !== event.pointerId) return;
|
||||
setDrag({ ...activeDrag.current, end: position(event) });
|
||||
}}
|
||||
onPointerUp={(event) => {
|
||||
if (activeDrag.current?.pointerId !== event.pointerId) return;
|
||||
const { start } = activeDrag.current;
|
||||
const end = position(event);
|
||||
if (Math.abs(end - start) * offset.width >= 8) onZoom(start, end);
|
||||
cancel();
|
||||
}}
|
||||
onPointerCancel={cancel}
|
||||
onLostPointerCapture={cancel}
|
||||
/>
|
||||
</g>
|
||||
);
|
||||
}
|
||||
|
||||
export default function TrendChart({
|
||||
result,
|
||||
title = "Trendansicht",
|
||||
@@ -73,153 +102,47 @@ export default function TrendChart({
|
||||
allowFocusMode = true,
|
||||
compact = false,
|
||||
lineWidth = 1.8,
|
||||
settings = DEFAULT_CHART_SETTINGS,
|
||||
onSettingsChange,
|
||||
onSaveSettings,
|
||||
savingSettings,
|
||||
}) {
|
||||
const wrapperRef = useRef(null);
|
||||
const chartHostRef = useRef(null);
|
||||
const dragBoxRef = useRef(null);
|
||||
const domainRef = useRef({ x: [0, 1], y: [0, 1] });
|
||||
const [selectedKeys, setSelectedKeys] = useState([]);
|
||||
const [dragBox, setDragBox] = useState(null);
|
||||
const [zoomDomain, setZoomDomain] = useState(null);
|
||||
const [exportFormat, setExportFormat] = useState("csv");
|
||||
const [transparentBackground, setTransparentBackground] = useState(false);
|
||||
|
||||
const series = useMemo(() => {
|
||||
const fallbackColors = [];
|
||||
return (result?.series || []).map((item, index) => ({
|
||||
const series = useMemo(() => assignTrendColors(result?.series || [])
|
||||
.map((item) => ({
|
||||
...item,
|
||||
color: item.color || (fallbackColors[index] ||= buildRandomColor(index)),
|
||||
name: `${item.displayName} · ${item.alias}${item.unit ? ` (${item.unit})` : ""}`,
|
||||
}));
|
||||
}, [result]);
|
||||
})), [result]);
|
||||
|
||||
const rows = useMemo(() => (result?.rows || []).map((row) => ({
|
||||
...row,
|
||||
timestampMs: new Date(row.timestamp).getTime(),
|
||||
})), [result]);
|
||||
const rows = useMemo(() => (result?.rows || [])
|
||||
.map((row) => ({ ...row, timestampMs: new Date(row.timestamp).getTime() }))
|
||||
.filter((row) => Number.isFinite(row.timestampMs))
|
||||
.sort((left, right) => left.timestampMs - right.timestampMs), [result]);
|
||||
|
||||
const visibleSeries = selectedKeys.length ? series.filter((item) => selectedKeys.includes(item.key)) : series;
|
||||
const analogSeries = visibleSeries.filter((item) => item.kind !== "digital");
|
||||
const digitalSeries = visibleSeries.filter((item) => item.kind === "digital");
|
||||
|
||||
const baseXDomain = useMemo(() => {
|
||||
if (!rows.length) {
|
||||
return [0, 1];
|
||||
}
|
||||
return [rows[0].timestampMs, rows[rows.length - 1].timestampMs];
|
||||
}, [rows]);
|
||||
|
||||
const baseYDomain = useMemo(() => {
|
||||
if (analogSeries.length === 0 && digitalSeries.length > 0) {
|
||||
return [0, 2];
|
||||
}
|
||||
return getAnalogDomain(rows, visibleSeries);
|
||||
}, [analogSeries.length, digitalSeries.length, rows, visibleSeries]);
|
||||
|
||||
const xDomain = zoomDomain?.x || baseXDomain;
|
||||
const yDomain = zoomDomain?.y || baseYDomain;
|
||||
const hasFocusSelection = selectedKeys.length > 0 && selectedKeys.length < series.length;
|
||||
const baseXDomain = rows.length > 1
|
||||
? [rows[0].timestampMs, rows.at(-1).timestampMs]
|
||||
: [(rows[0]?.timestampMs ?? 0) - 1000, (rows[0]?.timestampMs ?? 0) + 1000];
|
||||
const activeZoom = zoomDomain && zoomDomain.result === result ? zoomDomain.domain : null;
|
||||
const xDomain = activeZoom || baseXDomain;
|
||||
const axes = useMemo(() => getTrendAxes(series, settings.assignments).map((axis) => ({
|
||||
...axis,
|
||||
scale: getTrendAxisScale(axis, settings.axes[axis.id], rows, activeZoom, settings.divisions),
|
||||
})), [series, settings.assignments, settings.axes, settings.divisions, rows, activeZoom]);
|
||||
const axisIndex = new Map(axes.map((axis, index) => [axis.id, index]));
|
||||
const activeKeys = new Set(series.filter((entry) => selectedKeys.includes(entry.key)).map((entry) => entry.key));
|
||||
const hasFocusSelection = activeKeys.size > 0 && activeKeys.size < series.length;
|
||||
const seriesLimited = result?.requestedSeriesCount > result?.maxSeries;
|
||||
const normalizedLineWidth = Number.isFinite(Number(lineWidth)) ? Number(lineWidth) : 1.8;
|
||||
|
||||
useEffect(() => {
|
||||
domainRef.current = { x: xDomain, y: yDomain };
|
||||
}, [xDomain, yDomain]);
|
||||
|
||||
const mapDragToDomain = (clientX, clientY) => {
|
||||
const rect = chartHostRef.current?.getBoundingClientRect() || wrapperRef.current?.getBoundingClientRect();
|
||||
if (!rect) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const activeDomain = domainRef.current;
|
||||
const xRatio = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
|
||||
const yRatio = Math.max(0, Math.min(1, (clientY - rect.top) / rect.height));
|
||||
const xValue = activeDomain.x[0] + (activeDomain.x[1] - activeDomain.x[0]) * xRatio;
|
||||
const yValue = activeDomain.y[1] - (activeDomain.y[1] - activeDomain.y[0]) * yRatio;
|
||||
|
||||
return {
|
||||
xValue,
|
||||
yValue,
|
||||
xPixel: clientX - rect.left,
|
||||
yPixel: clientY - rect.top,
|
||||
};
|
||||
};
|
||||
|
||||
const handleMouseDown = (event) => {
|
||||
if (event.button !== 0) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
const mapped = mapDragToDomain(event.clientX, event.clientY);
|
||||
if (!mapped) {
|
||||
return;
|
||||
}
|
||||
const nextDragBox = { start: mapped, end: mapped };
|
||||
dragBoxRef.current = nextDragBox;
|
||||
setDragBox(nextDragBox);
|
||||
};
|
||||
|
||||
const updateDragBox = (clientX, clientY) => {
|
||||
const mapped = mapDragToDomain(clientX, clientY);
|
||||
if (!mapped) {
|
||||
return;
|
||||
}
|
||||
setDragBox((current) => {
|
||||
if (!current) {
|
||||
return current;
|
||||
}
|
||||
const nextDragBox = { ...current, end: mapped };
|
||||
dragBoxRef.current = nextDragBox;
|
||||
return nextDragBox;
|
||||
});
|
||||
};
|
||||
|
||||
const finalizeDragBox = (box) => {
|
||||
if (!box) {
|
||||
return;
|
||||
}
|
||||
|
||||
const deltaX = Math.abs(box.end.xPixel - box.start.xPixel);
|
||||
const deltaY = Math.abs(box.end.yPixel - box.start.yPixel);
|
||||
if (deltaX < 12 && deltaY < 12) {
|
||||
dragBoxRef.current = null;
|
||||
setDragBox(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const currentDomain = domainRef.current;
|
||||
setZoomDomain({
|
||||
x: deltaX < 12 ? currentDomain.x : [Math.min(box.start.xValue, box.end.xValue), Math.max(box.start.xValue, box.end.xValue)],
|
||||
y: deltaY < 12 ? currentDomain.y : [Math.min(box.start.yValue, box.end.yValue), Math.max(box.start.yValue, box.end.yValue)],
|
||||
});
|
||||
dragBoxRef.current = null;
|
||||
setDragBox(null);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!dragBox) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const handleWindowMouseMove = (event) => {
|
||||
updateDragBox(event.clientX, event.clientY);
|
||||
};
|
||||
|
||||
const handleWindowMouseUp = () => {
|
||||
finalizeDragBox(dragBoxRef.current);
|
||||
};
|
||||
|
||||
window.addEventListener("mousemove", handleWindowMouseMove);
|
||||
window.addEventListener("mouseup", handleWindowMouseUp);
|
||||
return () => {
|
||||
window.removeEventListener("mousemove", handleWindowMouseMove);
|
||||
window.removeEventListener("mouseup", handleWindowMouseUp);
|
||||
};
|
||||
}, [dragBox, xDomain, yDomain]);
|
||||
|
||||
const resetZoom = () => {
|
||||
setZoomDomain(null);
|
||||
};
|
||||
function zoomTime(start, end) {
|
||||
const domain = getTimeZoom(xDomain, start, end);
|
||||
if (domain) setZoomDomain({ result, domain });
|
||||
}
|
||||
|
||||
const clearFocus = () => {
|
||||
setSelectedKeys([]);
|
||||
@@ -365,6 +288,7 @@ export default function TrendChart({
|
||||
<div className={compact ? "panel chart-panel compact-chart empty-state" : "panel chart-panel empty-state"}>
|
||||
<h3>{title}</h3>
|
||||
<p>Keine Daten im gewählten Zeitraum.</p>
|
||||
{seriesLimited ? <small>Diagrammlimit: {result.maxSeries} von {result.requestedSeriesCount} Datenpunkten.</small> : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -374,7 +298,8 @@ export default function TrendChart({
|
||||
<div className="panel-header compact-header chart-toolbar">
|
||||
<div>
|
||||
<h3>{title}</h3>
|
||||
<p>Ziehen mit gedrückter Maustaste zoomt Zeit und Wertebereich gemeinsam.</p>
|
||||
{rows.length > 1 && !compact ? <p>Zum Zeitzoom einen Bereich im Diagramm aufziehen.</p> : null}
|
||||
{seriesLimited ? <p>Diagrammlimit: {result.maxSeries} von {result.requestedSeriesCount} Datenpunkten.</p> : null}
|
||||
</div>
|
||||
<div className="chart-actions">
|
||||
{allowExports ? (
|
||||
@@ -403,41 +328,68 @@ export default function TrendChart({
|
||||
<button className="ghost-button" type="button" onClick={handleExport}>Export</button>
|
||||
</>
|
||||
) : null}
|
||||
{zoomDomain ? <button className="ghost-button" type="button" onClick={resetZoom}>Zoom zurück</button> : null}
|
||||
{onSettingsChange ? (
|
||||
<TrendChartSettings
|
||||
settings={settings}
|
||||
onChange={onSettingsChange}
|
||||
onSave={onSaveSettings}
|
||||
saving={savingSettings}
|
||||
axes={axes}
|
||||
series={series}
|
||||
/>
|
||||
) : null}
|
||||
{!compact && rows.length > 1 ? <button className="ghost-button" type="button" onClick={() => zoomTime(0.25, 0.75)}>Zeitzoom +</button> : null}
|
||||
{activeZoom ? <button className="ghost-button" type="button" onClick={() => setZoomDomain(null)}>Zoom zurück</button> : null}
|
||||
{allowFocusMode ? <button className="ghost-button" type="button" onClick={onToggleExpanded}>{expanded ? "Normalansicht" : "Vollbild"}</button> : null}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
ref={wrapperRef}
|
||||
className={dragBox ? "chart-wrapper dragging" : "chart-wrapper"}
|
||||
onMouseDown={handleMouseDown}
|
||||
>
|
||||
<div ref={chartHostRef} className="chart-host">
|
||||
<div className={settings.highContrast ? "chart-wrapper high-contrast" : "chart-wrapper"}>
|
||||
<div ref={chartHostRef} className="chart-host" style={{ minWidth: 220 + axes.length * 64 }}>
|
||||
<ResponsiveContainer width="100%" height={expanded ? 680 : compact ? 280 : 360}>
|
||||
<LineChart data={rows} margin={{ top: 8, right: 18, left: 0, bottom: 8 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--grid)" />
|
||||
<CartesianGrid
|
||||
horizontal={settings.grid === "both" || settings.grid === "horizontal"}
|
||||
vertical={settings.grid === "both" || settings.grid === "vertical"}
|
||||
horizontalValues={axes[0]?.scale.ticks}
|
||||
horizontalFill={settings.shadedBands ? ["transparent", "var(--trend-band)"] : []}
|
||||
strokeDasharray={settings.gridStyle === "dashed" ? "3 3" : undefined}
|
||||
stroke="var(--trend-grid)"
|
||||
/>
|
||||
<XAxis
|
||||
dataKey="timestampMs"
|
||||
type="number"
|
||||
scale="time"
|
||||
domain={xDomain}
|
||||
allowDataOverflow
|
||||
tickCount={settings.divisions + 1}
|
||||
tickFormatter={(value) => new Date(value).toLocaleString("de-DE", { day: "2-digit", month: "2-digit", hour: "2-digit", minute: "2-digit" })}
|
||||
minTickGap={28}
|
||||
stroke="var(--muted)"
|
||||
stroke="var(--trend-axis)"
|
||||
/>
|
||||
{analogSeries.length ? <YAxis yAxisId="analog" stroke="var(--muted)" domain={yDomain} width={52} tickFormatter={formatTrendNumber} /> : null}
|
||||
{digitalSeries.length ? (
|
||||
{axes.map((axis, index) => (
|
||||
<YAxis
|
||||
yAxisId="digital"
|
||||
orientation="right"
|
||||
domain={[0, 2]}
|
||||
ticks={[0, 1]}
|
||||
width={46}
|
||||
stroke="var(--muted)"
|
||||
allowDecimals={false}
|
||||
key={axis.id}
|
||||
yAxisId={index}
|
||||
orientation={settings.axes[axis.id]?.side || axis.side}
|
||||
domain={axis.scale.domain}
|
||||
ticks={axis.scale.ticks}
|
||||
width={64}
|
||||
stroke="var(--trend-axis)"
|
||||
allowDataOverflow
|
||||
interval={0}
|
||||
tick={{ fontSize: 10 }}
|
||||
tickFormatter={axis.digital ? String : formatTrendNumber}
|
||||
label={axis.unit ? {
|
||||
value: axis.unit,
|
||||
angle: -90,
|
||||
position: (settings.axes[axis.id]?.side || axis.side) === "left" ? "insideLeft" : "insideRight",
|
||||
fill: "var(--trend-axis)",
|
||||
fontSize: 10,
|
||||
offset: 2,
|
||||
} : undefined}
|
||||
/>
|
||||
) : null}
|
||||
))}
|
||||
<Tooltip
|
||||
labelFormatter={(value) => new Date(value).toLocaleString("de-DE")}
|
||||
formatter={(value, name) => [formatTrendNumber(value), name]}
|
||||
@@ -448,35 +400,25 @@ export default function TrendChart({
|
||||
fontSize: 12,
|
||||
}}
|
||||
/>
|
||||
{dragBox ? (
|
||||
<ReferenceArea
|
||||
x1={Math.min(dragBox.start.xValue, dragBox.end.xValue)}
|
||||
x2={Math.max(dragBox.start.xValue, dragBox.end.xValue)}
|
||||
y1={Math.min(dragBox.start.yValue, dragBox.end.yValue)}
|
||||
y2={Math.max(dragBox.start.yValue, dragBox.end.yValue)}
|
||||
strokeOpacity={0.5}
|
||||
fillOpacity={0.12}
|
||||
fill="var(--brand-2)"
|
||||
/>
|
||||
) : null}
|
||||
{series.map((entry) => {
|
||||
const isolated = selectedKeys.length > 0 && !selectedKeys.includes(entry.key);
|
||||
const isolated = hasFocusSelection && !activeKeys.has(entry.key);
|
||||
return (
|
||||
<Line
|
||||
key={entry.key}
|
||||
yAxisId={entry.kind === "digital" ? "digital" : "analog"}
|
||||
yAxisId={axisIndex.get(seriesAxisId(entry, settings.assignments))}
|
||||
type={entry.kind === "digital" ? "stepAfter" : "linear"}
|
||||
dataKey={entry.key}
|
||||
dot={false}
|
||||
connectNulls
|
||||
strokeWidth={selectedKeys.includes(entry.key) ? normalizedLineWidth + 0.55 : normalizedLineWidth}
|
||||
strokeWidth={normalizedLineWidth + (settings.highContrast ? 0.6 : 0) + (activeKeys.has(entry.key) ? 0.55 : 0)}
|
||||
stroke={entry.color}
|
||||
strokeOpacity={isolated ? 0.1 : 1}
|
||||
strokeOpacity={(isolated ? 0.1 : 1) * settings.opacity / 100}
|
||||
isAnimationActive={false}
|
||||
name={entry.name}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
{rows.length > 1 ? <Customized component={TimeZoomOverlay} onZoom={zoomTime} /> : null}
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
@@ -485,8 +427,8 @@ export default function TrendChart({
|
||||
<div className="legend-row">
|
||||
<div className="legend-grid">
|
||||
{series.map((entry) => {
|
||||
const dimmed = selectedKeys.length > 0 && !selectedKeys.includes(entry.key);
|
||||
const active = selectedKeys.includes(entry.key);
|
||||
const dimmed = hasFocusSelection && !activeKeys.has(entry.key);
|
||||
const active = activeKeys.has(entry.key);
|
||||
return (
|
||||
<button
|
||||
key={entry.key}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { unitAxisId } from "./trend-scales.mjs";
|
||||
import { CHART_DIVISIONS } from "../../../shared/trend-chart-settings.mjs";
|
||||
|
||||
export default function TrendChartSettings({ settings, onChange, onSave, saving, axes, series }) {
|
||||
function updateAxis(axis, change) {
|
||||
const current = settings.axes[axis.id] || {};
|
||||
onChange({
|
||||
axes: { ...settings.axes, [axis.id]: { ...current, ...change } },
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<details className="trend-settings">
|
||||
<summary className="ghost-button">Diagramm einstellen</summary>
|
||||
<div className="trend-settings-popover">
|
||||
<div className="trend-settings-fields">
|
||||
<label>
|
||||
<span>Achseneinteilung</span>
|
||||
<select value={settings.divisions} onChange={(event) => onChange({ divisions: Number(event.target.value) })}>
|
||||
{CHART_DIVISIONS.map((divisions) => <option key={divisions} value={divisions}>{divisions} Abschnitte</option>)}
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
<span>Gitternetz</span>
|
||||
<select value={settings.grid} onChange={(event) => onChange({ grid: event.target.value })}>
|
||||
<option value="both">Horizontal und vertikal</option>
|
||||
<option value="horizontal">Nur horizontal</option>
|
||||
<option value="vertical">Nur vertikal</option>
|
||||
<option value="none">Aus</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
<span>Rasterdarstellung</span>
|
||||
<select value={settings.shadedBands ? "bands" : settings.gridStyle} onChange={(event) => onChange({
|
||||
shadedBands: event.target.value === "bands",
|
||||
gridStyle: event.target.value === "solid" ? "solid" : "dashed",
|
||||
})}>
|
||||
<option value="dashed">Gestrichelt</option>
|
||||
<option value="solid">Durchgezogen</option>
|
||||
<option value="bands">Mit Hintergrundstreifen</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
<span>Kontrast</span>
|
||||
<select value={settings.highContrast ? "high" : "normal"} onChange={(event) => onChange({ highContrast: event.target.value === "high" })}>
|
||||
<option value="normal">Normal</option>
|
||||
<option value="high">Hoch</option>
|
||||
</select>
|
||||
</label>
|
||||
<label className="trend-opacity-field">
|
||||
<span>Transparenz der Linien · {100 - settings.opacity} %</span>
|
||||
<input type="range" min="20" max="100" value={settings.opacity} onChange={(event) => onChange({ opacity: Number(event.target.value) })} />
|
||||
</label>
|
||||
</div>
|
||||
<fieldset className="trend-settings-group">
|
||||
<legend>Y-Achsen</legend>
|
||||
{axes.map((axis) => {
|
||||
const config = settings.axes[axis.id] || {};
|
||||
const [min, max] = axis.scale.domain;
|
||||
return (
|
||||
<div key={axis.id} className="trend-axis-settings">
|
||||
<strong>{axis.name}</strong>
|
||||
<div className="trend-settings-fields">
|
||||
<label>
|
||||
<span>Seite</span>
|
||||
<select value={config.side || axis.side} onChange={(event) => updateAxis(axis, { side: event.target.value })}>
|
||||
<option value="left">Links</option>
|
||||
<option value="right">Rechts</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
<span>Skalierung</span>
|
||||
<select disabled={axis.digital} value={axis.digital ? "digital" : config.mode || "auto"} onChange={(event) => updateAxis(axis, {
|
||||
mode: event.target.value, min: config.min ?? Number(min.toPrecision(8)), max: config.max ?? Number(max.toPrecision(8)),
|
||||
})}>
|
||||
{axis.digital ? <option value="digital">Digital (0 / 1)</option> : <>
|
||||
<option value="auto">Automatisch</option>
|
||||
<option value="manual">Manuell</option>
|
||||
</>}
|
||||
</select>
|
||||
</label>
|
||||
{!axis.digital && config.mode === "manual" ? <>
|
||||
<label><span>Minimum</span><input type="number" step="any" value={config.min ?? ""} onChange={(event) => updateAxis(axis, { min: event.target.value })} /></label>
|
||||
<label><span>Maximum</span><input type="number" step="any" value={config.max ?? ""} onChange={(event) => updateAxis(axis, { max: event.target.value })} /></label>
|
||||
{axis.scale.invalid ? <small role="status">Minimum muss kleiner als Maximum sein. Bis dahin gilt die Automatik.</small> : null}
|
||||
</> : null}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</fieldset>
|
||||
{onSave ? (
|
||||
<div className="button-row trend-settings-save">
|
||||
<button type="button" className="primary-button compact-button" disabled={saving} onClick={onSave}>
|
||||
{saving ? "Speichert ..." : "Auswahl aktualisieren"}
|
||||
</button>
|
||||
</div>
|
||||
) : null}
|
||||
<fieldset className="trend-settings-group">
|
||||
<legend>Datenreihen zuordnen</legend>
|
||||
{series.map((entry) => (
|
||||
<label key={entry.key} className="trend-series-setting">
|
||||
<span>{entry.name}</span>
|
||||
<select value={settings.assignments[entry.key] || "unit"} onChange={(event) => onChange({
|
||||
assignments: { ...settings.assignments, [entry.key]: event.target.value },
|
||||
})}>
|
||||
<option value="unit">{unitAxisId(entry).slice(5) || "Ohne Einheit"} · gemeinsame Achse</option>
|
||||
<option value="own">Eigene Achse</option>
|
||||
</select>
|
||||
</label>
|
||||
))}
|
||||
</fieldset>
|
||||
</div>
|
||||
</details>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
const PALETTE = [
|
||||
"#0072b2", "#d55e00", "#009e73", "#a03a83", "#b77a00", "#665aca",
|
||||
"#e03b53", "#167b93", "#3e7b0d", "#87543f", "#d14caa", "#4d6090",
|
||||
];
|
||||
|
||||
function hexRgb(color) {
|
||||
const short = /^#([0-9a-f])([0-9a-f])([0-9a-f])$/i.exec(color);
|
||||
const hex = short ? short.slice(1).map((digit) => digit.repeat(2)).join("") : color.slice(1);
|
||||
if (color.startsWith("#") && /^[0-9a-f]{6}$/i.test(hex)) {
|
||||
return hex.match(/../g).map((pair) => parseInt(pair, 16));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function distance(left, right) {
|
||||
const [red, green, blue] = left.map((channel, index) => channel - right[index]);
|
||||
const redMean = (left[0] + right[0]) / 512;
|
||||
return (2 + redMean) * red ** 2 + 4 * green ** 2 + (3 - redMean) * blue ** 2;
|
||||
}
|
||||
|
||||
function hslColor(hue, saturation, lightness) {
|
||||
const amplitude = saturation * Math.min(lightness, 1 - lightness);
|
||||
return "#" + [0, 8, 4].map((offset) => {
|
||||
const angle = (offset + hue / 30) % 12;
|
||||
const channel = lightness - amplitude * Math.max(-1, Math.min(angle - 3, 9 - angle, 1));
|
||||
return Math.round(255 * channel).toString(16).padStart(2, "0");
|
||||
}).join("");
|
||||
}
|
||||
|
||||
function isAutomaticColor(color) {
|
||||
return !color || /^(?:black|#0{3}|#0{6}|rgb\(\s*0(?:\s*,\s*|\s+)0(?:\s*,\s*|\s+)0\s*\))$/i.test(color);
|
||||
}
|
||||
|
||||
export function assignTrendColors(series) {
|
||||
const automatic = series.filter((entry) => isAutomaticColor(entry.color?.trim()));
|
||||
if (!automatic.length) return series;
|
||||
|
||||
const seed = series.reduce((hash, { key }) => {
|
||||
for (const char of key) hash = (Math.imul(hash, 31) + char.charCodeAt(0)) >>> 0;
|
||||
return hash;
|
||||
}, 0);
|
||||
const offset = seed % PALETTE.length;
|
||||
const palette = PALETTE.slice(offset).concat(PALETTE.slice(0, offset));
|
||||
if (series.length > palette.length) {
|
||||
for (const lightness of [0.39, 0.49, 0.58]) {
|
||||
for (let hue = seed % 10; hue < 360; hue += 6) palette.push(hslColor(hue, 0.72, lightness));
|
||||
}
|
||||
}
|
||||
|
||||
const manual = series.filter((entry) => !isAutomaticColor(entry.color?.trim()));
|
||||
const used = manual.map((entry) => hexRgb(entry.color.trim())).filter(Boolean);
|
||||
const candidates = [...new Set(palette)].map((color) => ({
|
||||
color,
|
||||
rgb: hexRgb(color),
|
||||
contrast: Infinity,
|
||||
}));
|
||||
function compareTo(rgb) {
|
||||
for (const candidate of candidates) candidate.contrast = Math.min(candidate.contrast, distance(candidate.rgb, rgb));
|
||||
}
|
||||
used.forEach(compareTo);
|
||||
const colors = new Map();
|
||||
for (const entry of automatic) {
|
||||
const best = candidates.reduce((index, candidate, current) => candidate.contrast > candidates[index].contrast ? current : index, 0);
|
||||
const [chosen] = candidates.splice(best, 1);
|
||||
colors.set(entry.key, chosen.color);
|
||||
compareTo(chosen.rgb);
|
||||
}
|
||||
return series.map((entry) => colors.has(entry.key) ? { ...entry, color: colors.get(entry.key) } : entry);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { DIGITAL_UNIT, normalizeUnitSymbol } from "../../../shared/engineering-units.mjs";
|
||||
|
||||
export function unitAxisId(series) {
|
||||
return `unit:${series.kind === "digital" ? DIGITAL_UNIT : normalizeUnitSymbol(series.unit)}`;
|
||||
}
|
||||
|
||||
export function seriesAxisId(series, assignments) {
|
||||
return assignments[series.key] === "own" ? `point:${series.key}` : unitAxisId(series);
|
||||
}
|
||||
|
||||
export function getTrendAxes(series, assignments) {
|
||||
const groups = new Map();
|
||||
for (const entry of series) {
|
||||
const id = seriesAxisId(entry, assignments);
|
||||
if (!groups.has(id)) {
|
||||
const digital = entry.kind === "digital";
|
||||
const unit = digital ? DIGITAL_UNIT : normalizeUnitSymbol(entry.unit);
|
||||
groups.set(id, {
|
||||
id,
|
||||
unit,
|
||||
digital,
|
||||
name: id.startsWith("point:") ? `${entry.alias} (${unit || "ohne Einheit"})` : unit || "Ohne Einheit",
|
||||
side: groups.size % 2 === 0 ? "left" : "right",
|
||||
series: [],
|
||||
});
|
||||
}
|
||||
groups.get(id).series.push(entry);
|
||||
}
|
||||
return [...groups.values()];
|
||||
}
|
||||
|
||||
function findExtent(rows, series, window) {
|
||||
let min = Infinity;
|
||||
let max = -Infinity;
|
||||
for (const row of rows) {
|
||||
if (window && (row.timestampMs < window[0] || row.timestampMs > window[1])) continue;
|
||||
for (const entry of series) {
|
||||
const value = row[entry.key];
|
||||
if (!Number.isFinite(value)) continue;
|
||||
if (value < min) min = value;
|
||||
if (value > max) max = value;
|
||||
}
|
||||
}
|
||||
return [min, max];
|
||||
}
|
||||
|
||||
export function getTrendAxisScale(axis, settings, rows, window, divisions) {
|
||||
if (axis.digital) return { domain: [0, 2], ticks: [0, 1] };
|
||||
|
||||
let [min, max] = findExtent(rows, axis.series, window);
|
||||
if (min === Infinity && window) [min, max] = findExtent(rows, axis.series);
|
||||
if (min === Infinity) [min, max] = [0, 1];
|
||||
const padding = min === max ? Math.abs(min) * 0.05 || 1 : (max - min) * 0.08;
|
||||
const validManual = settings?.min !== "" && settings?.max !== ""
|
||||
&& Number.isFinite(Number(settings?.min)) && Number.isFinite(Number(settings?.max))
|
||||
&& Number(settings.min) < Number(settings.max);
|
||||
const domain = settings?.mode === "manual" && validManual
|
||||
? [Number(settings.min), Number(settings.max)]
|
||||
: [min - padding, max + padding];
|
||||
|
||||
return {
|
||||
domain,
|
||||
ticks: Array.from({ length: divisions + 1 }, (_, index) => domain[0] + (domain[1] - domain[0]) * index / divisions),
|
||||
invalid: settings?.mode === "manual" && !validManual,
|
||||
};
|
||||
}
|
||||
|
||||
export function getTimeZoom(domain, start, end) {
|
||||
const span = domain[1] - domain[0];
|
||||
const min = Math.round(domain[0] + span * Math.min(start, end));
|
||||
const max = Math.round(domain[0] + span * Math.max(start, end));
|
||||
return max > min && (min > domain[0] || max < domain[1]) ? [min, max] : null;
|
||||
}
|
||||
Reference in new issue
Block a user