React Force Directed Graph

Creates a React Force Directed Graph using SciChart.js, visualizing US airport flight routes with a physics-based force simulation.

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.tsx

theme.ts

airportData.ts

nodeModifiers.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    SciChartSurface,
3    NumericAxis,
4    XyDataSeries,
5    XyxyDataSeries,
6    XyScatterRenderableSeries,
7    FastLineSegmentRenderableSeries,
8    EllipsePointMarker,
9    NumberRange,
10    ZoomPanModifier,
11    ZoomExtentsModifier,
12    MouseWheelZoomModifier,
13    PinchZoomModifier,
14    EAutoRange,
15} from "scichart";
16import { appTheme } from "../../../theme";
17import {
18    SimNode,
19    SimEdge,
20    EdgeHoverState,
21    NodeTooltipModifier,
22    NodeDragModifier,
23    NodeHoverPaletteProvider,
24    DragStateRef,
25} from "./nodeModifiers";
26import { AIRPORTS, ROUTES } from "./airportData";
27
28// ─── Build simulation state ───────────────────────────────────────────────────
29
30function buildSimulation(): { nodes: SimNode[]; edges: SimEdge[] } {
31    const iataToIdx = new Map<string, number>();
32    const centerLon = -96;
33    const centerLat = 38;
34    const scaleX = 4.5;
35    const scaleY = 5.5;
36
37    const nodes: SimNode[] = AIRPORTS.map((a, i) => {
38        iataToIdx.set(a.iata, i);
39        const geoX = (a.lon - centerLon) * scaleX;
40        const geoY = (a.lat - centerLat) * scaleY;
41        return { iata: a.iata, label: `${a.iata} – ${a.city}, ${a.state}`, x: geoX, y: geoY, vx: 0, vy: 0, geoX, geoY };
42    });
43
44    const edges: SimEdge[] = ROUTES.map((r) => {
45        const si = iataToIdx.get(r.origin);
46        const ti = iataToIdx.get(r.destination);
47        if (si === undefined || ti === undefined) return null;
48        return { sourceIdx: si, targetIdx: ti };
49    }).filter((e): e is SimEdge => e !== null);
50
51    return { nodes, edges };
52}
53
54// ─── Force simulation tick ────────────────────────────────────────────────────
55
56const REPULSION_STRENGTH = -120;
57const REPULSION_MIN_DIST = 1;
58const SPRING_K = 0.3;
59const SPRING_REST_LENGTH = 20;
60const GEO_ANCHOR_STRENGTH = 0.12;
61const VELOCITY_DECAY = 0.6;
62
63function tick(nodes: SimNode[], edges: SimEdge[], alpha: number): void {
64    for (let i = 0; i < nodes.length; i++) {
65        for (let j = i + 1; j < nodes.length; j++) {
66            const dx = nodes[j].x - nodes[i].x;
67            const dy = nodes[j].y - nodes[i].y;
68            const dist = Math.max(Math.sqrt(dx * dx + dy * dy), REPULSION_MIN_DIST);
69            const force = (REPULSION_STRENGTH * alpha) / (dist * dist);
70            const fx = force * (dx / dist);
71            const fy = force * (dy / dist);
72            nodes[i].vx += fx;
73            nodes[i].vy += fy;
74            nodes[j].vx -= fx;
75            nodes[j].vy -= fy;
76        }
77    }
78
79    for (const edge of edges) {
80        const src = nodes[edge.sourceIdx];
81        const tgt = nodes[edge.targetIdx];
82        const dx = tgt.x + tgt.vx - (src.x + src.vx);
83        const dy = tgt.y + tgt.vy - (src.y + src.vy);
84        const dist = Math.max(Math.sqrt(dx * dx + dy * dy), 1);
85        const force = SPRING_K * (dist - SPRING_REST_LENGTH) * alpha;
86        const fx = force * (dx / dist);
87        const fy = force * (dy / dist);
88        src.vx += fx * 0.5;
89        src.vy += fy * 0.5;
90        tgt.vx -= fx * 0.5;
91        tgt.vy -= fy * 0.5;
92    }
93
94    for (const node of nodes) {
95        node.vx += (node.geoX - node.x) * GEO_ANCHOR_STRENGTH * alpha;
96        node.vy += (node.geoY - node.y) * GEO_ANCHOR_STRENGTH * alpha;
97    }
98
99    for (const node of nodes) {
100        node.vx *= VELOCITY_DECAY;
101        node.vy *= VELOCITY_DECAY;
102        node.x += node.vx;
103        node.y += node.vy;
104    }
105}
106
107// ─── Chart initialization ─────────────────────────────────────────────────────
108
109export const drawExample = async (rootElement: string | HTMLDivElement) => {
110    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
111        theme: appTheme.SciChartJsTheme,
112    });
113
114    const xAxis = new NumericAxis(wasmContext, {
115        isVisible: false,
116        autoRange: EAutoRange.Never,
117        visibleRangeLimit: new NumberRange(-600, 600),
118    });
119    const yAxis = new NumericAxis(wasmContext, {
120        isVisible: false,
121        autoRange: EAutoRange.Never,
122        visibleRangeLimit: new NumberRange(-600, 600),
123    });
124    xAxis.visibleRange = new NumberRange(-300, 300);
125    yAxis.visibleRange = new NumberRange(-300, 300);
126    sciChartSurface.xAxes.add(xAxis);
127    sciChartSurface.yAxes.add(yAxis);
128
129    const { nodes, edges } = buildSimulation();
130
131    const edgeHover = new EdgeHoverState();
132
133    const edgeDataSeries = new XyxyDataSeries(wasmContext);
134    sciChartSurface.renderableSeries.add(
135        new FastLineSegmentRenderableSeries(wasmContext, {
136            dataSeries: edgeDataSeries,
137            stroke: "#47bde650",
138            strokeThickness: 2,
139        })
140    );
141
142    const edgeHighlightDataSeries = new XyxyDataSeries(wasmContext);
143    sciChartSurface.renderableSeries.add(
144        new FastLineSegmentRenderableSeries(wasmContext, {
145            dataSeries: edgeHighlightDataSeries,
146            stroke: "#47bde6",
147            strokeThickness: 3,
148        })
149    );
150
151    const nodeDataSeries = new XyDataSeries(wasmContext);
152    sciChartSurface.renderableSeries.add(
153        new XyScatterRenderableSeries(wasmContext, {
154            dataSeries: nodeDataSeries,
155            pointMarker: new EllipsePointMarker(wasmContext, {
156                width: 14,
157                height: 14,
158                fill: "#274b92",
159                stroke: "#47bde6",
160                strokeThickness: 1.5,
161            }),
162            paletteProvider: new NodeHoverPaletteProvider(edgeHover),
163        })
164    );
165
166    const dragState: DragStateRef = { current: null };
167
168    let alpha = 1.0;
169    let running = true;
170    let loopAlive = false;
171    let autoZoomed = false;
172    let animFrameId: number = 0;
173
174    function frame() {
175        if (!running || sciChartSurface.isDeleted) {
176            loopAlive = false;
177            return;
178        }
179
180        const simActive = alpha >= 0.001 || !!dragState.current;
181
182        if (simActive) {
183            tick(nodes, edges, alpha);
184            alpha *= 0.9772;
185
186            if (!autoZoomed && alpha < 0.5) {
187                autoZoomed = true;
188                sciChartSurface.zoomExtents(200);
189            }
190
191            if (dragState.current) {
192                const n = nodes[dragState.current.nodeIdx];
193                n.x = dragState.current.dataX;
194                n.y = dragState.current.dataY;
195                n.vx = 0;
196                n.vy = 0;
197                alpha = Math.max(alpha, 0.1);
198            }
199        }
200
201        const ex: number[] = [],
202            ey: number[] = [],
203            ex1: number[] = [],
204            ey1: number[] = [];
205        const hx: number[] = [],
206            hy: number[] = [],
207            hx1: number[] = [],
208            hy1: number[] = [];
209        const h = edgeHover.hoveredNodeIdx;
210        for (const edge of edges) {
211            const src = nodes[edge.sourceIdx],
212                tgt = nodes[edge.targetIdx];
213            if (h !== -1 && (edge.sourceIdx === h || edge.targetIdx === h)) {
214                hx.push(src.x);
215                hy.push(src.y);
216                hx1.push(tgt.x);
217                hy1.push(tgt.y);
218            } else {
219                ex.push(src.x);
220                ey.push(src.y);
221                ex1.push(tgt.x);
222                ey1.push(tgt.y);
223            }
224        }
225        edgeDataSeries.clear();
226        edgeDataSeries.appendRange(ex, ey, ex1, ey1);
227        edgeHighlightDataSeries.clear();
228        edgeHighlightDataSeries.appendRange(hx, hy, hx1, hy1);
229
230        nodeDataSeries.clear();
231        nodeDataSeries.appendRange(
232            nodes.map((n) => n.x),
233            nodes.map((n) => n.y)
234        );
235
236        if (simActive) {
237            animFrameId = requestAnimationFrame(frame);
238        } else {
239            loopAlive = false;
240        }
241    }
242
243    function startLoop() {
244        if (!loopAlive) {
245            loopAlive = true;
246            animFrameId = requestAnimationFrame(frame);
247        }
248    }
249
250    sciChartSurface.chartModifiers.add(
251        new NodeTooltipModifier(nodes, edges, edgeHover, () => startLoop()),
252        new NodeDragModifier(nodes, dragState, () => {
253            alpha = Math.max(alpha, 0.3);
254            startLoop();
255        }),
256        new ZoomPanModifier(),
257        new ZoomExtentsModifier(),
258        new MouseWheelZoomModifier(),
259        new PinchZoomModifier()
260    );
261
262    startLoop();
263
264    return {
265        sciChartSurface,
266        wasmContext,
267        stopAnimation: () => {
268            running = false;
269            if (animFrameId) cancelAnimationFrame(animFrameId);
270        },
271    };
272};
273

Force Directed Graph (React)

Overview

This example demonstrates a Force Directed Graph built with SciChart.js in React, visualizing ~60 US airports connected by ~2300 flight routes. The graph uses a custom physics simulation to position nodes, with geographic anchoring that keeps airports near their real-world lat/lon positions.

Technical Implementation

The chart is initialized through the <SciChartReact/> component using an initChart prop pointing to the drawExample function. Edges are rendered using FastLineSegmentRenderableSeries with an XyxyDataSeries. Airport nodes are rendered as XyScatterRenderableSeries with EllipsePointMarker. The physics loop uses requestAnimationFrame and applies repulsion, spring, and geographic anchor forces each tick.

Interactivity

Two custom ChartModifierBase2D subclasses provide interactivity: NodeTooltipModifier highlights connected routes and labels neighbours on hover, and NodeDragModifier allows dragging nodes to explore the graph structure. Standard ZoomPanModifier and MouseWheelZoomModifier are also included.

react Chart Examples & Demos

See Also: JavaScript Chart Types (41 Demos)

React Line Chart | React Charts | SciChart.js Demo

React Line Chart

Discover how to create a high performance React Line Chart with SciChart - the leading JavaScript library. Get your free demo now.

React Spline Line Chart | React Charts | SciChart.js Demo

React Spline Line Chart

Discover how to create a React Spline Line Chart with SciChart. Demo includes algorithm for smoother lines. Get your free trial now.

React Digital Line Chart | React Charts | SciChart.js Demo

React Digital Line Chart

Discover how to create a React Digital Line Chart with SciChart - your feature-rich JavaScript Chart Library. Get your free demo now.

React Band Chart | React Charts | SciChart.js Demo

React Band Chart

Easily create a React Band Chart or High-Low Fill with SciChart - high performance JavaScript Chart Library. Get your free trial now.

React Spline Band Chart | React Charts | SciChart.js Demo

React Spline Band Chart

SciChart's React Spline Band Chart makes it easy to draw thresholds or fills between two lines on a chart. Get your free demo today.

React Digital Band Chart | React Charts | SciChart.js Demo

React Digital Band Chart

Learn how to create a React Digital Band Chart or High-Low Fill Chart with SciChart's easy-to-follow demos. Get your free trial today.

React Bubble Chart | Online JavaScript Chart Examples

React Bubble Chart

Create a high performance React Bubble Chart with Sci-Chart. Demo shows how to draw point-markers at X,Y locations. Get your free demo now.

React Candlestick Chart | Online JavaScript Chart Examples

React Candlestick Chart

Discover how to create a React Candlestick Chart or Stock Chart using SciChart.js. For high Performance JavaScript Charts, get your free demo now.

React Column Chart | React Charts | SciChart.js Demo

React Column Chart

React Column Chart demo by SciChart supports gradient fill and paletteproviders for more custom coloring options. Get your free demo now.

React Population Pyramid | React Charts | SciChart.js Demo

React Population Pyramid

Population Pyramid of Europe and Africa

React Error Bars Char | React Charts | SciChart.js Demo

React Error Bars Chart

Create React Error Bars Chart using high performance SciChart.js. Display uncertainty or statistical confidence of a data-point. Get free demo now.

React Impulse Chart | React Charts | SciChart.js Demo

React Impulse Chart

Easily create React Impulse Chart or Stem Chart using SciChart.js - our own high performance JavaScript Chart Library. Get your free trial now.

React Text Chart | React Charts | SciChart.js Demo

React Text Chart

Create React Text Chart with high performance SciChart.js.

React Fan Chart | React Charts | SciChart.js Demo

React Fan Chart

Discover how to create React Fan Chart with SciChart. Zoom in to see the detail you can go to using our JavaScript Charts. Get your free demo today.

React Heatmap Chart | React Charts | SciChart.js Demo

React Heatmap Chart

Easily create a high performance React Heatmap Chart with SciChart. Get your free trial of our 5-star rated JavaScript Chart Component today.

React Non Uniform Heatmap Chart | React Charts | SciChart.js

React Non Uniform Heatmap Chart

Create React Non Uniform Chart using high performance SciChart.js. Display Heatmap with variable cell sizes. Get free demo now.

React Heatmap Chart With Contours | SciChart.js Demo

React Heatmap Chart With Contours Example

Design a highly dynamic React Heatmap Chart With Contours with SciChart's feature-rich JavaScript Chart Library. Get your free demo today.

React Map Chart with Heatmap overlay | SciChart.js Demo

React Map Chart with Heatmap overlay

Design a highly dynamic React Map Chart with Heatmap overlay with SciChart's feature-rich JavaScript Chart Library. Get your free demo today.

React Mountain Chart | React Charts | SciChart.js Demo

React Mountain Chart

Create React Mountain Chart with SciChart.js. Zero line can be zero or a specific value. Fill color can be solid or gradient as well. Get a free demo now.

React Spline Mountain Chart | React Charts | SciChart.js

React Spline Mountain Chart

React Spline Mountain Chart design made easy. Use SciChart.js' JavaScript Charts for high performance, feature-rich designs. Get free demo now.

React Digital Mountain Chart | React Charts | SciChart.js

React Digital Mountain Chart

Create React Digital Mountain Chart with a stepped-line visual effect. Get your free trial of SciChart's 5-star rated JavaScript Chart Component now.

React Realtime Mountain Chart | View Online At SciChart

React Realtime Mountain Chart

React Realtime Mountain Chart made easy. Add animated, real-time updates with SciChart.js - high performance JavaScript Charts. Get free trial now.

React Scatter Chart | React Charts | SciChart.js Demo

React Scatter Chart

Create React Scatter Chart with high performance SciChart.js. Easily render pre-defined point types. Supports custom shapes. Get your free trial now.

React Stacked Column Chart | Online JavaScript Charts

React Stacked Column Chart

Discover how to create a React Stacked Column Chart using our feature-rich JavaScript Chart Library, SciChart.js. Get your free demo today!

React Stacked Group Column Chart | View Examples Now

React Stacked Column Side by Side

Design React Stacked Group Column Chart side-by-side using our 5-star rated JavaScript Chart Framework, SciChart.js. Get your free demo now.

React Stacked Mountain Chart | React Charts | SciChart.js

React Stacked Mountain Chart

Design a high performance React Stacked Mountain Chart with SciChart.js - your one-stop JavaScript chart library. Get free demo now to get started.

React Smooth Stacked Mountain Chart | SciChart.js Demo

React Smooth Stacked Mountain Chart

Design a high performance React Stacked Mountain Chart with SciChart.js - your one-stop JavaScript chart library. Get free demo now to get started.

React Pie Chart | React Charts | SciChart.js Demo

React Pie Chart

Easily create and customise a high performance React Pie Chart with 5-star rated SciChart.js. Get your free trial now to access the whole library.

React Donut Chart | React Charts | SciChart.js Demo

React Donut Chart

Create React Donut Chart with 5-star rated SciChart.js chart library. Supports legends, text labels, animated updates and more. Get free trial now.

React Linear Gauges | React Charts | SciChart.js Demo

React Linear Gauges Example

View the React Linear Gauge Chart example to combine rectangles & annotations. Create a linear gauge dashboard with animated indicators and custom scales.

React Quadrant Chart using Background Annotations | SciChart

React Quadrant Chart using Background Annotations

Demonstrates how to color areas of the chart surface using background Annotations using SciChart.js Annotations API

React Histogram Chart | React Charts | SciChart.js Demo

React Histogram Chart

Create a React Histogram Chart with custom texture fills and patterns. Try the SciChartReact wrapper component for seamless React integration today.

React Gantt Chart | React Charts | SciChart.js Demo

React Gantt Chart Example

Build a React Gantt Chart with SciChart. View the demo for horizontal bars, rounded corners and data labels to show project timelines and task completion.

React Choropleth Map | React Charts | SciChart.js Demo

React Choropleth Map Example

Create a React Choropleth map, a type of thematic map where areas are shaded or patterned in proportion to the value of a variable being represented.

React Multi-Layer Map | React Charts | SciChart.js Demo

React Multi-Layer Map Example

Create a React Multi-Layer Map Example, using FastTriangleRenderableSeries with GeoJSON data-points using a constrained delaunay triangulation algorithm.

React Vector Field Plot | React Charts | SciChart.js Demo

React Vector Field Plot

View the React Vector Field Plot example from SciChart, including dynamic vector generation, gradient-colored segments, and interactive zoom/pan. Try demo.

React Waterfall Chart | Bridge Chart | SciChart.js Demo

React Waterfall Chart | Bridge Chart

Build a React Waterfall Chart with dynamic coloring, multi-line data labels and responsive design, using the SciChartReact component for seamless integration.

React Box Plot Chart | React Charts | SciChart.js Demo

React Box Plot Chart

Try the React Box Plot Chart example for React-friendly chart lifecycle management, dynamic sub-surface positioning, and custom styling. Try the demo now.

React Triangle Series | Triangle Mesh Chart | SciChart.js

React Triangle Series | Triangle Mesh Chart

Create React Triangle Meshes with the Triangle Series from SciChart. This demo supports strip mode, list mode and the drawing of polygons. View the example.

React Treemap Chart | React Charts | SciChart.js Demo

React Treemap Chart

Create a React Treemap Chart to define rectangle positions based on total value. Use SciChart FastRectangleRenderableSeries and d3-hierarchy.js layouts.

NEW!
React Parallel Coordinate Plot | SciChart.js Demo | SciChart

React Parallel Coordinate Plot

Create a high performance React Parallel Coordinate Plot with SciChart.js. Visualise multivariate data with one axis per dimension, reorder axes, highlight ranges and select records, with up to 100,000 records on screen.

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL.