@opendata-ai/openchart-react 8.0.0-rc.12 → 8.0.0-rc.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import * as _opendata_ai_openchart_core from '@opendata-ai/openchart-core';
2
1
  import { BarListSpec, ThemeConfig, DarkMode, DataRow, ChartEventHandlers, ChartSpec, LayerSpec, GraphSpec, ElementRef, MapSpec, TableSpec, SortState, ChartLayout, SankeySpec, TileMapSpec, VizSpec } from '@opendata-ai/openchart-core';
3
2
  export * from '@opendata-ai/openchart-core';
4
- import { BarListInstance, ChartInstance, MountOptions, GraphInstance, TableInstance, TableState, TableMountOptions, MapMarkEvent, MapInstance, SankeyInstance, TileMapInstance } from '@opendata-ai/openchart-vanilla';
3
+ import { BarListInstance, ChartInstance, MountOptions, CameraFlightOptions, GraphHighlightTarget, GraphLegendData, GraphInstance, GraphTooltipFormatter, TableInstance, TableState, TableMountOptions, MapMarkEvent, MapInstance, SankeyInstance, TileMapInstance } from '@opendata-ai/openchart-vanilla';
5
4
  export { JPGExportOptions, PNGExportOptions, SVGExportOptions, exportCSV, exportJPG, exportPNG, exportSVG, exportSVGWithFonts } from '@opendata-ai/openchart-vanilla';
6
5
  export { ChartRenderer, CompileResult, CompiledGraphEdge, CompiledGraphNode, GraphCompilation, NormalizedBarListSpec, NormalizedChartSpec, NormalizedChrome, NormalizedGraphSpec, NormalizedMapSpec, NormalizedSankeySpec, NormalizedSpec, NormalizedTableSpec, NormalizedTileMapSpec, SimulationConfig, ValidationError, ValidationErrorCode, ValidationResult, clearRenderers, compile, compileBarList, compileChart, compileGraph, compileMap, compileSankey, compileTable, compileTileMap, getChartRenderer, normalizeSpec, registerChartRenderer, validateSpec } from '@opendata-ai/openchart-engine';
7
6
  import * as react from 'react';
@@ -153,6 +152,20 @@ interface DataTableProps {
153
152
  */
154
153
  declare function DataTable({ spec, theme: themeProp, darkMode, onRowClick, onSortChange, onSearchChange, onPageChange, className, style, sort, search, page, }: DataTableProps): react_jsx_runtime.JSX.Element;
155
154
 
155
+ /**
156
+ * useGraph: hook for imperative graph control.
157
+ *
158
+ * Provides a ref to pass to <Graph /> and exposes graph methods
159
+ * (search, zoom, select, fly, highlight) for programmatic control of the
160
+ * graph instance. The full vanilla GraphInstance API is threaded through.
161
+ */
162
+
163
+ /** Camera transform components (graph-space). */
164
+ type GraphCamera = {
165
+ x: number;
166
+ y: number;
167
+ k: number;
168
+ };
156
169
  interface UseGraphReturn {
157
170
  /** Ref to pass to <Graph ref={ref} />. */
158
171
  ref: React.RefObject<GraphHandle | null>;
@@ -160,25 +173,79 @@ interface UseGraphReturn {
160
173
  search: (query: string) => void;
161
174
  /** Clear the current search. */
162
175
  clearSearch: () => void;
163
- /** Zoom to fit all nodes in view. */
164
- zoomToFit: () => void;
165
- /** Zoom and center on a specific node. */
166
- zoomToNode: (nodeId: string) => void;
167
- /** Select a node by id. */
168
- selectNode: (nodeId: string) => void;
176
+ /** Node ids currently matching the active search query. */
177
+ getSearchMatches: () => string[];
178
+ /** Zoom to fit all nodes in view. Animated by default; `{ duration: 0 }` snaps. */
179
+ zoomToFit: (opts?: CameraFlightOptions & {
180
+ padding?: number;
181
+ }) => void;
182
+ /** Fly to a node and zoom in (default scale 2). */
183
+ zoomToNode: (nodeId: string, opts?: CameraFlightOptions & {
184
+ scale?: number;
185
+ }) => void;
186
+ /** Fly the camera to a graph-space target. */
187
+ flyTo: (target: {
188
+ x: number;
189
+ y: number;
190
+ k?: number;
191
+ }, opts?: CameraFlightOptions) => void;
192
+ /** Center the camera on a graph-space point (keeps current zoom). */
193
+ centerAt: (x: number, y: number, opts?: CameraFlightOptions) => void;
194
+ /** Current camera transform. */
195
+ getCamera: () => GraphCamera;
196
+ /** Select a node by id; `{ fly: true }` also flies to it. */
197
+ selectNode: (nodeId: string, opts?: {
198
+ fly?: boolean;
199
+ } & CameraFlightOptions) => void;
169
200
  /** Get the currently selected node ids. */
170
201
  getSelectedNodes: () => string[];
202
+ /** Emphasize a set of nodes; eased via the focus model. */
203
+ highlight: (target: GraphHighlightTarget, opts?: {
204
+ dimOpacity?: number;
205
+ }) => void;
206
+ /** Clear any programmatic highlight (and legend toggles). */
207
+ clearHighlight: () => void;
208
+ /** The currently highlighted node ids, or null when nothing is highlighted. */
209
+ getHighlight: () => string[] | null;
210
+ /** Headless snapshot of the legend. */
211
+ getLegend: () => GraphLegendData | null;
212
+ /** Set the active category filter declaratively (replaces legend toggle state). */
213
+ setActiveCategories: (values: string[]) => void;
214
+ /** Current active category values (empty = all active, no filter). */
215
+ getActiveCategories: () => string[];
171
216
  }
172
217
  /** Handle exposed by Graph component via forwardRef. */
173
218
  interface GraphHandle {
174
219
  search: (query: string) => void;
175
220
  clearSearch: () => void;
176
- zoomToFit: () => void;
177
- zoomToNode: (nodeId: string) => void;
178
- selectNode: (nodeId: string) => void;
221
+ getSearchMatches: () => string[];
222
+ zoomToFit: (opts?: CameraFlightOptions & {
223
+ padding?: number;
224
+ }) => void;
225
+ zoomToNode: (nodeId: string, opts?: CameraFlightOptions & {
226
+ scale?: number;
227
+ }) => void;
228
+ flyTo: (target: {
229
+ x: number;
230
+ y: number;
231
+ k?: number;
232
+ }, opts?: CameraFlightOptions) => void;
233
+ centerAt: (x: number, y: number, opts?: CameraFlightOptions) => void;
234
+ getCamera: () => GraphCamera;
235
+ selectNode: (nodeId: string, opts?: {
236
+ fly?: boolean;
237
+ } & CameraFlightOptions) => void;
179
238
  getSelectedNodes: () => string[];
239
+ highlight: (target: GraphHighlightTarget, opts?: {
240
+ dimOpacity?: number;
241
+ }) => void;
242
+ clearHighlight: () => void;
243
+ getHighlight: () => string[] | null;
244
+ getLegend: () => GraphLegendData | null;
245
+ setActiveCategories: (values: string[]) => void;
246
+ getActiveCategories: () => string[];
180
247
  /** Re-compile encoding/legend/chrome without restarting the simulation. */
181
- updateVisuals: (spec: _opendata_ai_openchart_core.GraphSpec) => void;
248
+ updateVisuals: (spec: GraphSpec) => void;
182
249
  /** The underlying GraphInstance from the vanilla adapter. */
183
250
  instance: GraphInstance | null;
184
251
  }
@@ -193,6 +260,15 @@ interface GraphHandle {
193
260
  */
194
261
  declare function useGraph(): UseGraphReturn;
195
262
 
263
+ /** Tooltip prop: `false` off, `true` default, or an object with a formatter. */
264
+ type GraphTooltipProp = boolean | {
265
+ formatter?: GraphTooltipFormatter;
266
+ };
267
+ /** Legend prop: `false` off, `true` default, or an object toggling interactivity/counts. */
268
+ type GraphLegendProp = boolean | {
269
+ interactive?: boolean;
270
+ counts?: boolean;
271
+ };
196
272
  interface GraphProps {
197
273
  /** The graph spec to render. */
198
274
  spec: GraphSpec;
@@ -210,10 +286,33 @@ interface GraphProps {
210
286
  onEdgeHover?: (edge: Record<string, unknown> | null) => void;
211
287
  /** Callback when selection changes. */
212
288
  onSelectionChange?: (nodeIds: string[]) => void;
213
- /** Show built-in tooltip on node/edge hover. Defaults to true. */
214
- tooltip?: boolean;
215
- /** Show built-in legend. Defaults to true. */
216
- legend?: boolean;
289
+ /** Fired when the user hovers a legend entry (null on leave). */
290
+ onLegendHover?: (entry: {
291
+ field: string;
292
+ value: string;
293
+ } | null) => void;
294
+ /** Fired when legend toggle state changes; `activeValues` is the active set (empty = all). */
295
+ onLegendToggle?: (activeValues: string[]) => void;
296
+ /** Fired whenever the highlight set changes (programmatic or legend), null when cleared. */
297
+ onHighlightChange?: (nodeIds: string[] | null) => void;
298
+ /** Camera change callback, rAF-coalesced (fires at most once per rendered frame). */
299
+ onCameraChange?: (camera: {
300
+ x: number;
301
+ y: number;
302
+ k: number;
303
+ }) => void;
304
+ /**
305
+ * Show built-in tooltip on node/edge hover. Defaults to true. Pass an object
306
+ * with a `formatter` to customize content.
307
+ */
308
+ tooltip?: GraphTooltipProp;
309
+ /**
310
+ * Show built-in legend. Defaults to true. Pass `false` if you render your own,
311
+ * or an object to toggle interactivity/counts.
312
+ */
313
+ legend?: GraphLegendProp;
314
+ /** Fit the graph to the viewport on the first tick. Default true. */
315
+ fitOnLoad?: boolean;
217
316
  /** CSS class name for the wrapper div. */
218
317
  className?: string;
219
318
  /** Inline styles for the wrapper div. */
package/dist/index.js CHANGED
@@ -541,6 +541,15 @@ import {
541
541
  useRef as useRef5
542
542
  } from "react";
543
543
  import { jsx as jsx6 } from "react/jsx-runtime";
544
+ function tooltipStructural(tooltip) {
545
+ if (tooltip === false) return false;
546
+ return true;
547
+ }
548
+ function legendStructural(legend) {
549
+ if (legend === false) return "false";
550
+ if (legend === true || legend === void 0) return "true";
551
+ return JSON.stringify({ interactive: legend.interactive, counts: legend.counts });
552
+ }
544
553
  var Graph = forwardRef3(function Graph2({
545
554
  spec,
546
555
  theme: themeProp,
@@ -550,8 +559,13 @@ var Graph = forwardRef3(function Graph2({
550
559
  onNodeHover,
551
560
  onEdgeHover,
552
561
  onSelectionChange,
562
+ onLegendHover,
563
+ onLegendToggle,
564
+ onHighlightChange,
565
+ onCameraChange,
553
566
  tooltip,
554
567
  legend,
568
+ fitOnLoad,
555
569
  className,
556
570
  style
557
571
  }, ref) {
@@ -562,13 +576,19 @@ var Graph = forwardRef3(function Graph2({
562
576
  const containerRef = useRef5(null);
563
577
  const graphRef = useRef5(null);
564
578
  const specRef = useRef5("");
579
+ const prevMountDepsRef = useRef5(null);
565
580
  const handlersRef = useRef5({});
566
581
  handlersRef.current = {
567
582
  onNodeClick,
568
583
  onNodeDoubleClick,
569
584
  onNodeHover,
570
585
  onEdgeHover,
571
- onSelectionChange
586
+ onSelectionChange,
587
+ onLegendHover,
588
+ onLegendToggle,
589
+ onHighlightChange,
590
+ onCameraChange,
591
+ tooltipFormatter: typeof tooltip === "object" ? tooltip.formatter : void 0
572
592
  };
573
593
  const stableOnNodeClick = useCallback4(
574
594
  (node) => handlersRef.current.onNodeClick?.(node),
@@ -590,6 +610,26 @@ var Graph = forwardRef3(function Graph2({
590
610
  (nodeIds) => handlersRef.current.onSelectionChange?.(nodeIds),
591
611
  []
592
612
  );
613
+ const stableOnLegendHover = useCallback4(
614
+ (entry) => handlersRef.current.onLegendHover?.(entry),
615
+ []
616
+ );
617
+ const stableOnLegendToggle = useCallback4(
618
+ (activeValues) => handlersRef.current.onLegendToggle?.(activeValues),
619
+ []
620
+ );
621
+ const stableOnHighlightChange = useCallback4(
622
+ (nodeIds) => handlersRef.current.onHighlightChange?.(nodeIds),
623
+ []
624
+ );
625
+ const stableOnCameraChange = useCallback4(
626
+ (camera) => handlersRef.current.onCameraChange?.(camera),
627
+ []
628
+ );
629
+ const stableTooltipFormatter = useCallback4(
630
+ (item, defaults) => handlersRef.current.tooltipFormatter?.(item, defaults) ?? defaults,
631
+ []
632
+ );
593
633
  useImperativeHandle4(
594
634
  ref,
595
635
  () => ({
@@ -599,18 +639,48 @@ var Graph = forwardRef3(function Graph2({
599
639
  clearSearch() {
600
640
  graphRef.current?.clearSearch();
601
641
  },
602
- zoomToFit() {
603
- graphRef.current?.zoomToFit();
642
+ getSearchMatches() {
643
+ return graphRef.current?.getSearchMatches() ?? [];
644
+ },
645
+ zoomToFit(opts) {
646
+ graphRef.current?.zoomToFit(opts);
647
+ },
648
+ zoomToNode(nodeId, opts) {
649
+ graphRef.current?.zoomToNode(nodeId, opts);
650
+ },
651
+ flyTo(target, opts) {
652
+ graphRef.current?.flyTo(target, opts);
604
653
  },
605
- zoomToNode(nodeId) {
606
- graphRef.current?.zoomToNode(nodeId);
654
+ centerAt(x, y, opts) {
655
+ graphRef.current?.centerAt(x, y, opts);
607
656
  },
608
- selectNode(nodeId) {
609
- graphRef.current?.selectNode(nodeId);
657
+ getCamera() {
658
+ return graphRef.current?.getCamera() ?? { x: 0, y: 0, k: 1 };
659
+ },
660
+ selectNode(nodeId, opts) {
661
+ graphRef.current?.selectNode(nodeId, opts);
610
662
  },
611
663
  getSelectedNodes() {
612
664
  return graphRef.current?.getSelectedNodes() ?? [];
613
665
  },
666
+ highlight(target, opts) {
667
+ graphRef.current?.highlight(target, opts);
668
+ },
669
+ clearHighlight() {
670
+ graphRef.current?.clearHighlight();
671
+ },
672
+ getHighlight() {
673
+ return graphRef.current?.getHighlight() ?? null;
674
+ },
675
+ getLegend() {
676
+ return graphRef.current?.getLegend() ?? null;
677
+ },
678
+ setActiveCategories(values) {
679
+ graphRef.current?.setActiveCategories(values);
680
+ },
681
+ getActiveCategories() {
682
+ return graphRef.current?.getActiveCategories() ?? [];
683
+ },
614
684
  updateVisuals(spec2) {
615
685
  graphRef.current?.updateVisuals(spec2);
616
686
  },
@@ -620,23 +690,36 @@ var Graph = forwardRef3(function Graph2({
620
690
  }),
621
691
  []
622
692
  );
693
+ const tooltipOn = tooltipStructural(tooltip);
694
+ const legendKey = legendStructural(legend);
623
695
  useEffect5(() => {
624
696
  const container = containerRef.current;
625
697
  if (!container) return;
698
+ const tooltipOption = tooltipOn ? { formatter: stableTooltipFormatter } : false;
699
+ const mountDeps = [theme, resolvedDarkMode, tooltipOn, legendKey, fitOnLoad];
700
+ const prevDeps = prevMountDepsRef.current;
701
+ const suppressEntrance = prevDeps !== null && mountDeps.some((d, i) => !Object.is(d, prevDeps[i]));
626
702
  const options = {
627
703
  theme,
628
704
  darkMode: resolvedDarkMode,
629
- tooltip,
705
+ tooltip: tooltipOption,
630
706
  legend,
707
+ fitOnLoad,
631
708
  onNodeClick: stableOnNodeClick,
632
709
  onNodeDoubleClick: stableOnNodeDoubleClick,
633
710
  onNodeHover: stableOnNodeHover,
634
711
  onEdgeHover: stableOnEdgeHover,
635
712
  onSelectionChange: stableOnSelectionChange,
636
- responsive: true
713
+ onLegendHover: stableOnLegendHover,
714
+ onLegendToggle: stableOnLegendToggle,
715
+ onHighlightChange: stableOnHighlightChange,
716
+ onCameraChange: stableOnCameraChange,
717
+ responsive: true,
718
+ suppressEntrance
637
719
  };
638
720
  graphRef.current = createGraph(container, spec, options);
639
721
  specRef.current = JSON.stringify(spec);
722
+ prevMountDepsRef.current = mountDeps;
640
723
  return () => {
641
724
  graphRef.current?.destroy();
642
725
  graphRef.current = null;
@@ -644,36 +727,26 @@ var Graph = forwardRef3(function Graph2({
644
727
  }, [
645
728
  theme,
646
729
  resolvedDarkMode,
647
- tooltip,
648
- legend,
730
+ tooltipOn,
731
+ legendKey,
732
+ fitOnLoad,
649
733
  stableOnNodeClick,
650
734
  stableOnNodeDoubleClick,
651
735
  stableOnNodeHover,
652
736
  stableOnEdgeHover,
653
- stableOnSelectionChange
737
+ stableOnSelectionChange,
738
+ stableOnLegendHover,
739
+ stableOnLegendToggle,
740
+ stableOnHighlightChange,
741
+ stableOnCameraChange,
742
+ stableTooltipFormatter
654
743
  ]);
655
744
  useEffect5(() => {
656
745
  const graph = graphRef.current;
657
746
  if (!graph) return;
658
747
  const specString = JSON.stringify(spec);
659
748
  if (specString === specRef.current) return;
660
- const prevSpec = specRef.current;
661
749
  specRef.current = specString;
662
- if (prevSpec) {
663
- try {
664
- const prev = JSON.parse(prevSpec);
665
- const sameNodes = prev.nodes.length === spec.nodes.length && prev.nodes.every((n, i) => n.id === spec.nodes[i].id);
666
- const sameEdges = prev.edges.length === spec.edges.length && prev.edges.every(
667
- (e, i) => e.source === spec.edges[i].source && e.target === spec.edges[i].target
668
- );
669
- const sameLayout = prev.layout?.clustering?.field === spec.layout?.clustering?.field;
670
- if (sameNodes && sameEdges && sameLayout) {
671
- graph.updateVisuals(spec);
672
- return;
673
- }
674
- } catch {
675
- }
676
- }
677
750
  graph.update(spec);
678
751
  }, [spec]);
679
752
  return /* @__PURE__ */ jsx6(
@@ -767,26 +840,75 @@ function useGraph() {
767
840
  const clearSearch = useCallback5(() => {
768
841
  ref.current?.clearSearch();
769
842
  }, []);
770
- const zoomToFit = useCallback5(() => {
771
- ref.current?.zoomToFit();
843
+ const getSearchMatches = useCallback5(() => {
844
+ return ref.current?.getSearchMatches() ?? [];
772
845
  }, []);
773
- const zoomToNode = useCallback5((nodeId) => {
774
- ref.current?.zoomToNode(nodeId);
846
+ const zoomToFit = useCallback5((opts) => {
847
+ ref.current?.zoomToFit(opts);
775
848
  }, []);
776
- const selectNode = useCallback5((nodeId) => {
777
- ref.current?.selectNode(nodeId);
849
+ const zoomToNode = useCallback5(
850
+ (nodeId, opts) => {
851
+ ref.current?.zoomToNode(nodeId, opts);
852
+ },
853
+ []
854
+ );
855
+ const flyTo = useCallback5(
856
+ (target, opts) => {
857
+ ref.current?.flyTo(target, opts);
858
+ },
859
+ []
860
+ );
861
+ const centerAt = useCallback5((x, y, opts) => {
862
+ ref.current?.centerAt(x, y, opts);
863
+ }, []);
864
+ const getCamera = useCallback5(() => {
865
+ return ref.current?.getCamera() ?? { x: 0, y: 0, k: 1 };
778
866
  }, []);
867
+ const selectNode = useCallback5(
868
+ (nodeId, opts) => {
869
+ ref.current?.selectNode(nodeId, opts);
870
+ },
871
+ []
872
+ );
779
873
  const getSelectedNodes = useCallback5(() => {
780
874
  return ref.current?.getSelectedNodes() ?? [];
781
875
  }, []);
876
+ const highlight = useCallback5((target, opts) => {
877
+ ref.current?.highlight(target, opts);
878
+ }, []);
879
+ const clearHighlight = useCallback5(() => {
880
+ ref.current?.clearHighlight();
881
+ }, []);
882
+ const getHighlight = useCallback5(() => {
883
+ return ref.current?.getHighlight() ?? null;
884
+ }, []);
885
+ const getLegend = useCallback5(() => {
886
+ return ref.current?.getLegend() ?? null;
887
+ }, []);
888
+ const setActiveCategories = useCallback5((values) => {
889
+ ref.current?.setActiveCategories(values);
890
+ }, []);
891
+ const getActiveCategories = useCallback5(() => {
892
+ return ref.current?.getActiveCategories() ?? [];
893
+ }, []);
782
894
  return {
783
895
  ref,
784
896
  search,
785
897
  clearSearch,
898
+ getSearchMatches,
786
899
  zoomToFit,
787
900
  zoomToNode,
901
+ flyTo,
902
+ centerAt,
903
+ getCamera,
788
904
  selectNode,
789
- getSelectedNodes
905
+ getSelectedNodes,
906
+ highlight,
907
+ clearHighlight,
908
+ getHighlight,
909
+ getLegend,
910
+ setActiveCategories,
911
+ getActiveCategories
790
912
  };
791
913
  }
792
914