React Chart with Logarithmic Axis Example

Demonstrates how to create a React Chart with Logarithmic axis using SciChart.js, High Performance JavaScript Charts

Fullscreen

Edit

 Edit

Docs

drawExample.ts

index.tsx

ExampleDataProvider.ts

theme.ts

Copy to clipboard
Minimise
Fullscreen
1import {
2    ENumericFormat,
3    EllipsePointMarker,
4    FastLineRenderableSeries,
5    LegendModifier,
6    LogarithmicAxis,
7    MouseWheelZoomModifier,
8    NumericAxis,
9    RubberBandXyZoomModifier,
10    SciChartSurface,
11    SweepAnimation,
12    XyDataSeries,
13    Thickness,
14    ZoomExtentsModifier,
15} from "scichart";
16import { appTheme } from "../../../theme";
17import { ExampleDataProvider } from "../../../ExampleData/ExampleDataProvider";
18
19const Y_AXIS_LINEAR_ID = "Y_AXIS_LINEAR_ID";
20const X_AXIS_LINEAR_ID = "X_AXIS_LINEAR_ID";
21
22export const drawExample = async (rootElement: string | HTMLDivElement) => {
23    // Create a SciChartSurface
24    const { sciChartSurface, wasmContext } = await SciChartSurface.create(rootElement, {
25        theme: {
26            ...appTheme.SciChartJsTheme,
27            majorGridLineBrush: appTheme.MutedSkyBlue + "55",
28            minorGridLineBrush: appTheme.MutedSkyBlue + "22",
29        },
30        title: "Logarithmic X & Y Axis",
31        titleStyle: {
32            fontSize: 20,
33            fontWeight: "bold",
34            placeWithinChart: true,
35            color: appTheme.ForegroundColor + "C4",
36            padding: Thickness.fromString("10 0 4 0"),
37        },
38    });
39
40    // Create an X and Y Axis
41    const xAxisLogarithmic = new LogarithmicAxis(wasmContext, {
42        logBase: 10,
43        labelFormat: ENumericFormat.Scientific,
44        labelPrecision: 2,
45        minorsPerMajor: 10,
46        useNativeText: false,
47    });
48    sciChartSurface.xAxes.add(xAxisLogarithmic);
49
50    // The LogarithmicAxis will apply logarithmic scaling and labelling to your data.
51    // Simply replace a NumericAxis for a LogarithmicAxis on X or Y to apply this scaling
52    // Note options logBase, labelFormat which lets you specify exponent on labels
53    const yAxisLogarithmic = new LogarithmicAxis(wasmContext, {
54        logBase: 10,
55        labelFormat: ENumericFormat.Scientific,
56        labelPrecision: 2,
57        minorsPerMajor: 10,
58        useNativeText: false,
59    });
60    sciChartSurface.yAxes.add(yAxisLogarithmic);
61
62    const xAxisLinear = new NumericAxis(wasmContext, {
63        labelFormat: ENumericFormat.Decimal,
64        labelPrecision: 2,
65        isVisible: false,
66        id: X_AXIS_LINEAR_ID,
67    });
68    sciChartSurface.xAxes.add(xAxisLinear);
69
70    const yAxisLinear = new NumericAxis(wasmContext, {
71        labelFormat: ENumericFormat.Decimal,
72        labelPrecision: 2,
73        isVisible: false,
74        id: Y_AXIS_LINEAR_ID,
75    });
76    sciChartSurface.yAxes.add(yAxisLinear);
77
78    // Create some data
79    const data0 = ExampleDataProvider.getExponentialCurve(2, 100);
80    const data1 = ExampleDataProvider.getExponentialCurve(2.2, 100);
81    const data2 = ExampleDataProvider.getExponentialCurve(2.4, 100);
82
83    sciChartSurface.renderableSeries.add(
84        new FastLineRenderableSeries(wasmContext, {
85            dataSeries: new XyDataSeries(wasmContext, {
86                xValues: data0.xValues,
87                yValues: data0.yValues,
88                dataSeriesName: "y = x ^ 2",
89            }),
90            stroke: appTheme.VividSkyBlue,
91            strokeThickness: 3,
92            pointMarker: new EllipsePointMarker(wasmContext, {
93                width: 7,
94                height: 7,
95                fill: appTheme.VividSkyBlue,
96                strokeThickness: 0,
97            }),
98            animation: new SweepAnimation({ duration: 800, delay: 0 }),
99        })
100    );
101
102    sciChartSurface.renderableSeries.add(
103        new FastLineRenderableSeries(wasmContext, {
104            dataSeries: new XyDataSeries(wasmContext, {
105                xValues: data1.xValues,
106                yValues: data1.yValues,
107                dataSeriesName: "y = x ^ 2.2",
108            }),
109            stroke: appTheme.VividPink,
110            strokeThickness: 3,
111            pointMarker: new EllipsePointMarker(wasmContext, {
112                width: 7,
113                height: 7,
114                fill: appTheme.VividPink,
115                strokeThickness: 0,
116            }),
117            animation: new SweepAnimation({ duration: 800, delay: 0 }),
118        })
119    );
120
121    sciChartSurface.renderableSeries.add(
122        new FastLineRenderableSeries(wasmContext, {
123            dataSeries: new XyDataSeries(wasmContext, {
124                xValues: data2.xValues,
125                yValues: data2.yValues,
126                dataSeriesName: "y = x ^ 2.4",
127            }),
128            stroke: appTheme.VividOrange,
129            strokeThickness: 3,
130            pointMarker: new EllipsePointMarker(wasmContext, {
131                width: 7,
132                height: 7,
133                fill: appTheme.VividOrange,
134                strokeThickness: 0,
135            }),
136            animation: new SweepAnimation({ duration: 800, delay: 0 }),
137        })
138    );
139
140    // Add some interactivity modifiers
141    sciChartSurface.chartModifiers.add(
142        new RubberBandXyZoomModifier(),
143        new MouseWheelZoomModifier(),
144        new ZoomExtentsModifier(),
145        new LegendModifier({ showCheckboxes: false })
146    );
147
148    sciChartSurface.zoomExtents();
149    return {
150        sciChartSurface,
151        wasmContext,
152        yAxisLogarithmic,
153        yAxisLinear,
154        xAxisLinear,
155        xAxisLogarithmic,
156    };
157};
158

Logarithmic Axis Example in React

Overview

This example demonstrates how to create a high performance chart using SciChart.js in a React application. It focuses on implementing logarithmic axes on both the X and Y axes and provides functionality for dynamically switching between logarithmic and linear representations using interactive controls.

Technical Implementation

The chart is implemented using the <SciChartReact/> component for React integration, with asynchronous initialization of the SciChartSurface ensuring a responsive and efficient setup. Interactive modifiers such as RubberBandZoomModifier, MouseWheelZoomModifier, and ZoomExtentsModifier are integrated to enable advanced chart interactions. Custom point markers and sweep animations are used to enhance visual feedback. For detailed information on integrating SciChart.js with React, refer to the React Charts with SciChart.js: Introducing "SciChart React" documentation.

The dynamic axis switching is managed by toggling the visibility and primary status of LogarithmicAxis and linear NumericAxis. This approach updates the FastLineRenderableSeries with the correct xAxisId and yAxisId properties based on the selected configuration. This method aligns with best practices described in the LogarithmicAxis Documentation - SciChart resource.

Features and Capabilities

The example offers dynamic update capabilities by allowing users to switch between three configurations: Logarithmic on both axes, Logarithmic X with Linear Y, and Linear on both axes. It leverages advanced features such as custom point markers for data visualization and sweep animations for smooth transitions. The inclusion of interactive modifiers ensures that users have control over zooming and panning, enhancing overall usability and performance.

Integration and Best Practices

The integration utilizes Material-UI's ToggleButtonGroup within the React environment to facilitate dynamic chart controls. This effective combination of React state management and SciChart.js configuration demonstrates scalable best practices for real-time data visualization. Developers can learn more about asynchronous initialization and component reuse in React from the Creating a SciChart React Component from the Ground Up guide. Additionally, the use of interactive behaviors is further detailed in the Tutorial 03 - Adding Zooming, Panning Behavior documentation, providing valuable insights for optimizing performance in dynamic charting applications.

react Chart Examples & Demos

See Also: Scientific & Medical Charts (10 Demos)

React Vital Signs ECG/EKG Medical Demo | SciChart.js Demo

React Vital Signs ECG/EKG Medical Demo

In this example we are simulating four channels of data showing that SciChart.js can be used to draw real-time ECG/EKG charts and graphs to monitor heart reate, body temperature, blood pressure, pulse rate, SPO2 blood oxygen, volumetric flow and more.

LiDAR 3D Point Cloud of Geospatial Data | SciChart.js

LiDAR 3D Point Cloud of Geospatial Data

Demonstrating the capability of SciChart.js to create JavaScript 3D Point Cloud charts and visualize LiDAR data from the UK Defra Survey.

React Chart with Vertically Stacked Axes | SciChart.js

React Chart with Vertically Stacked Axes

Demonstrates Vertically Stacked Axes on a React Chart using SciChart.js, allowing data to overlap

Realtime Audio Spectrum Analyzer Chart | SciChart.js Demo

Realtime Audio Spectrum Analyzer Chart Example

See the frequency of recordings with the React audio spectrum analyzer example from SciChart. This real-time audio visualizer demo uses a Fourier Transform.

Realtime Audio Analyzer Bars Demo | SciChart.js Demo

Realtime Audio Analyzer Bars Demo

Demonstrating the capability of SciChart.js to create a JavaScript Audio Analyzer Bars and visualize the Fourier-Transform of an audio waveform in realtime.

Interactive Waterfall Chart | React Charts | SciChart.js

Interactive Waterfall Spectral Chart

Demonstrates how to create a Waterfall chart in SciChart.js, showing chromotragraphy data with interactive selection of points.

Interactive Phasor Diagram chart | React Charts | SciChart.js

Phasor Diagram Chart Example

See the React Phasor Diagram example to combine a Cartesian surface with a Polar subsurface. Get seamless React integration with SciChart. View demo now.

NEW!
React Correlation Plot | React Charts | SciChart.js Demo

React Correlation Plot

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

NEW!
React Semiconductors Dashboard | JavaScript Charts | SciChart.js

Semiconductors Dashboard

React **Semiconductors Dashboard** using SciChart.js, by leveraging the **FastRectangleRenderableSeries**, and its `customTextureOptions` property to have a custom tiling texture fill.

NEW!
React Wafer Analysis Chart | JavaScript Charts | SciChart.js

Wafer Analysis Chart

React **Wafer Analysis Chart** using SciChart.js, by leveraging the **FastRectangleRenderableSeries**, and crossfilter to enable live filtering.

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