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

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,73 @@ 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;
171
212
  }
172
213
  /** Handle exposed by Graph component via forwardRef. */
173
214
  interface GraphHandle {
174
215
  search: (query: string) => void;
175
216
  clearSearch: () => void;
176
- zoomToFit: () => void;
177
- zoomToNode: (nodeId: string) => void;
178
- selectNode: (nodeId: string) => void;
217
+ getSearchMatches: () => string[];
218
+ zoomToFit: (opts?: CameraFlightOptions & {
219
+ padding?: number;
220
+ }) => void;
221
+ zoomToNode: (nodeId: string, opts?: CameraFlightOptions & {
222
+ scale?: number;
223
+ }) => void;
224
+ flyTo: (target: {
225
+ x: number;
226
+ y: number;
227
+ k?: number;
228
+ }, opts?: CameraFlightOptions) => void;
229
+ centerAt: (x: number, y: number, opts?: CameraFlightOptions) => void;
230
+ getCamera: () => GraphCamera;
231
+ selectNode: (nodeId: string, opts?: {
232
+ fly?: boolean;
233
+ } & CameraFlightOptions) => void;
179
234
  getSelectedNodes: () => string[];
235
+ highlight: (target: GraphHighlightTarget, opts?: {
236
+ dimOpacity?: number;
237
+ }) => void;
238
+ clearHighlight: () => void;
239
+ getHighlight: () => string[] | null;
240
+ getLegend: () => GraphLegendData | null;
180
241
  /** Re-compile encoding/legend/chrome without restarting the simulation. */
181
- updateVisuals: (spec: _opendata_ai_openchart_core.GraphSpec) => void;
242
+ updateVisuals: (spec: GraphSpec) => void;
182
243
  /** The underlying GraphInstance from the vanilla adapter. */
183
244
  instance: GraphInstance | null;
184
245
  }
@@ -193,6 +254,15 @@ interface GraphHandle {
193
254
  */
194
255
  declare function useGraph(): UseGraphReturn;
195
256
 
257
+ /** Tooltip prop: `false` off, `true` default, or an object with a formatter. */
258
+ type GraphTooltipProp = boolean | {
259
+ formatter?: GraphTooltipFormatter;
260
+ };
261
+ /** Legend prop: `false` off, `true` default, or an object toggling interactivity/counts. */
262
+ type GraphLegendProp = boolean | {
263
+ interactive?: boolean;
264
+ counts?: boolean;
265
+ };
196
266
  interface GraphProps {
197
267
  /** The graph spec to render. */
198
268
  spec: GraphSpec;
@@ -210,10 +280,33 @@ interface GraphProps {
210
280
  onEdgeHover?: (edge: Record<string, unknown> | null) => void;
211
281
  /** Callback when selection changes. */
212
282
  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;
283
+ /** Fired when the user hovers a legend entry (null on leave). */
284
+ onLegendHover?: (entry: {
285
+ field: string;
286
+ value: string;
287
+ } | null) => void;
288
+ /** Fired when legend toggle state changes; `activeValues` is the active set (empty = all). */
289
+ onLegendToggle?: (activeValues: string[]) => void;
290
+ /** Fired whenever the highlight set changes (programmatic or legend), null when cleared. */
291
+ onHighlightChange?: (nodeIds: string[] | null) => void;
292
+ /** Camera change callback, rAF-coalesced (fires at most once per rendered frame). */
293
+ onCameraChange?: (camera: {
294
+ x: number;
295
+ y: number;
296
+ k: number;
297
+ }) => void;
298
+ /**
299
+ * Show built-in tooltip on node/edge hover. Defaults to true. Pass an object
300
+ * with a `formatter` to customize content.
301
+ */
302
+ tooltip?: GraphTooltipProp;
303
+ /**
304
+ * Show built-in legend. Defaults to true. Pass `false` if you render your own,
305
+ * or an object to toggle interactivity/counts.
306
+ */
307
+ legend?: GraphLegendProp;
308
+ /** Fit the graph to the viewport on the first tick. Default true. */
309
+ fitOnLoad?: boolean;
217
310
  /** CSS class name for the wrapper div. */
218
311
  className?: string;
219
312
  /** 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,42 @@ 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);
604
650
  },
605
- zoomToNode(nodeId) {
606
- graphRef.current?.zoomToNode(nodeId);
651
+ flyTo(target, opts) {
652
+ graphRef.current?.flyTo(target, opts);
607
653
  },
608
- selectNode(nodeId) {
609
- graphRef.current?.selectNode(nodeId);
654
+ centerAt(x, y, opts) {
655
+ graphRef.current?.centerAt(x, y, opts);
656
+ },
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
+ },
614
678
  updateVisuals(spec2) {
615
679
  graphRef.current?.updateVisuals(spec2);
616
680
  },
@@ -620,23 +684,36 @@ var Graph = forwardRef3(function Graph2({
620
684
  }),
621
685
  []
622
686
  );
687
+ const tooltipOn = tooltipStructural(tooltip);
688
+ const legendKey = legendStructural(legend);
623
689
  useEffect5(() => {
624
690
  const container = containerRef.current;
625
691
  if (!container) return;
692
+ const tooltipOption = tooltipOn ? { formatter: stableTooltipFormatter } : false;
693
+ const mountDeps = [theme, resolvedDarkMode, tooltipOn, legendKey, fitOnLoad];
694
+ const prevDeps = prevMountDepsRef.current;
695
+ const suppressEntrance = prevDeps !== null && mountDeps.some((d, i) => !Object.is(d, prevDeps[i]));
626
696
  const options = {
627
697
  theme,
628
698
  darkMode: resolvedDarkMode,
629
- tooltip,
699
+ tooltip: tooltipOption,
630
700
  legend,
701
+ fitOnLoad,
631
702
  onNodeClick: stableOnNodeClick,
632
703
  onNodeDoubleClick: stableOnNodeDoubleClick,
633
704
  onNodeHover: stableOnNodeHover,
634
705
  onEdgeHover: stableOnEdgeHover,
635
706
  onSelectionChange: stableOnSelectionChange,
636
- responsive: true
707
+ onLegendHover: stableOnLegendHover,
708
+ onLegendToggle: stableOnLegendToggle,
709
+ onHighlightChange: stableOnHighlightChange,
710
+ onCameraChange: stableOnCameraChange,
711
+ responsive: true,
712
+ suppressEntrance
637
713
  };
638
714
  graphRef.current = createGraph(container, spec, options);
639
715
  specRef.current = JSON.stringify(spec);
716
+ prevMountDepsRef.current = mountDeps;
640
717
  return () => {
641
718
  graphRef.current?.destroy();
642
719
  graphRef.current = null;
@@ -644,36 +721,26 @@ var Graph = forwardRef3(function Graph2({
644
721
  }, [
645
722
  theme,
646
723
  resolvedDarkMode,
647
- tooltip,
648
- legend,
724
+ tooltipOn,
725
+ legendKey,
726
+ fitOnLoad,
649
727
  stableOnNodeClick,
650
728
  stableOnNodeDoubleClick,
651
729
  stableOnNodeHover,
652
730
  stableOnEdgeHover,
653
- stableOnSelectionChange
731
+ stableOnSelectionChange,
732
+ stableOnLegendHover,
733
+ stableOnLegendToggle,
734
+ stableOnHighlightChange,
735
+ stableOnCameraChange,
736
+ stableTooltipFormatter
654
737
  ]);
655
738
  useEffect5(() => {
656
739
  const graph = graphRef.current;
657
740
  if (!graph) return;
658
741
  const specString = JSON.stringify(spec);
659
742
  if (specString === specRef.current) return;
660
- const prevSpec = specRef.current;
661
743
  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
744
  graph.update(spec);
678
745
  }, [spec]);
679
746
  return /* @__PURE__ */ jsx6(
@@ -767,26 +834,67 @@ function useGraph() {
767
834
  const clearSearch = useCallback5(() => {
768
835
  ref.current?.clearSearch();
769
836
  }, []);
770
- const zoomToFit = useCallback5(() => {
771
- ref.current?.zoomToFit();
837
+ const getSearchMatches = useCallback5(() => {
838
+ return ref.current?.getSearchMatches() ?? [];
772
839
  }, []);
773
- const zoomToNode = useCallback5((nodeId) => {
774
- ref.current?.zoomToNode(nodeId);
840
+ const zoomToFit = useCallback5((opts) => {
841
+ ref.current?.zoomToFit(opts);
775
842
  }, []);
776
- const selectNode = useCallback5((nodeId) => {
777
- ref.current?.selectNode(nodeId);
843
+ const zoomToNode = useCallback5(
844
+ (nodeId, opts) => {
845
+ ref.current?.zoomToNode(nodeId, opts);
846
+ },
847
+ []
848
+ );
849
+ const flyTo = useCallback5(
850
+ (target, opts) => {
851
+ ref.current?.flyTo(target, opts);
852
+ },
853
+ []
854
+ );
855
+ const centerAt = useCallback5((x, y, opts) => {
856
+ ref.current?.centerAt(x, y, opts);
778
857
  }, []);
858
+ const getCamera = useCallback5(() => {
859
+ return ref.current?.getCamera() ?? { x: 0, y: 0, k: 1 };
860
+ }, []);
861
+ const selectNode = useCallback5(
862
+ (nodeId, opts) => {
863
+ ref.current?.selectNode(nodeId, opts);
864
+ },
865
+ []
866
+ );
779
867
  const getSelectedNodes = useCallback5(() => {
780
868
  return ref.current?.getSelectedNodes() ?? [];
781
869
  }, []);
870
+ const highlight = useCallback5((target, opts) => {
871
+ ref.current?.highlight(target, opts);
872
+ }, []);
873
+ const clearHighlight = useCallback5(() => {
874
+ ref.current?.clearHighlight();
875
+ }, []);
876
+ const getHighlight = useCallback5(() => {
877
+ return ref.current?.getHighlight() ?? null;
878
+ }, []);
879
+ const getLegend = useCallback5(() => {
880
+ return ref.current?.getLegend() ?? null;
881
+ }, []);
782
882
  return {
783
883
  ref,
784
884
  search,
785
885
  clearSearch,
886
+ getSearchMatches,
786
887
  zoomToFit,
787
888
  zoomToNode,
889
+ flyTo,
890
+ centerAt,
891
+ getCamera,
788
892
  selectNode,
789
- getSelectedNodes
893
+ getSelectedNodes,
894
+ highlight,
895
+ clearHighlight,
896
+ getHighlight,
897
+ getLegend
790
898
  };
791
899
  }
792
900