How can I help you?
Getting Started with React Linear Gauge
27 Feb 202619 minutes to read
This section explains the steps required to create a simple React Linear Gauge component and demonstrate its basic usage in a React environment.
Ready to streamline your Syncfusion® React development? Discover the full potential of Syncfusion® React components with Syncfusion® AI Coding Assistant. Effortlessly integrate, configure, and enhance your projects with intelligent, context-aware code suggestions, streamlined setups, and real-time insights—all seamlessly integrated into your preferred AI-powered IDEs like VS Code, Cursor, Syncfusion® CodeStudio and more. Explore Syncfusion® AI Coding Assistant.
To get started quickly with React Linear Gauge, you can watch this video:
Dependencies
Following is the list of minimum dependencies required to use the Linear Gauge.
+-- @syncfusion/ej2-react-lineargauge
|-- @syncfusion/ej2-lineargauge
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-svg-base
|-- @syncfusion/ej2-pdf-export
|-- @syncfusion/ej2-react-base
Setup for local development
Easily set up a React application using create-vite-app, which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.
Note: To create a React application using
create-react-app, refer to this documentation for more details.
To create a new React application, run the following command.
npm create vite@latest my-appThis command will prompt you for a few settings for the new project, such as selecting a framework and a variant.
To set up a React application in TypeScript environment, run the following command.
npm create vite@latest my-app -- --template react-ts
cd my-app
npm run devTo set up a React application in JavaScript environment, run the following command.
npm create vite@latest my-app -- --template react
cd my-app
npm run devAdding Syncfusion® React Linear Gauge packages
All the available Essential® JS 2 packages are published in the npmjs.com public registry. To install the Linear Gauge component, use the following command.
npm install @syncfusion/ej2-react-lineargauge --save
The –save will instruct NPM to include the Linear Gauge package inside of the dependencies section of the package.json.
Adding Linear Gauge component
The React Linear Gauge component can be added to the application by following these steps. To get started, add the Linear Gauge component to the src/App.tsx file using the following code.
The following Linear Gauge code should be placed in the src/App.tsx file.
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
import * as React from 'react';
function App() {
return <LinearGaugeComponent></LinearGaugeComponent>
}
export default App;import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
import * as React from 'react';
function App() {
return <LinearGaugeComponent></LinearGaugeComponent>
}
export default App;Module Injection
React Linear Gauge component features are segregated into individual feature-wise modules. In order to use a particular feature, you need to inject its feature service in the App. The annotation and tooltip features of the Linear Gauge are used in the current application. The relevant feature service names and descriptions are listed below.
-
Annotations- Inject this module to use annotation feature. -
GaugeTooltip- Inject this module to use tooltip feature.
These modules should be injected into the Linear Gauge using the Inject directive.
Adding the Linear Gauge Title
The title can be added to the Linear Gauge component using the title property in the Linear Gauge. Enable the title by setting the title property to display a meaningful heading for the gauge.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return (<LinearGaugeComponent title='Linear Gauge'></LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return (<LinearGaugeComponent title='Linear Gauge'></LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);Setting the Axis Range
The range of the axis can be set using the minimum and maximum properties in the Linear Gauge. This defines the start and end values that will be displayed on the gauge axis.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {
LinearGaugeComponent,
AxesDirective,
AxisDirective,
} from '@syncfusion/ej2-react-lineargauge';
export function App() {
return (
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200}></AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {
LinearGaugeComponent,
AxesDirective,
AxisDirective,
} from '@syncfusion/ej2-react-lineargauge';
export function App() {
return (
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200}></AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);Customizing Axis Labels
To denote the axis values with temperature units, add the °C as suffix to each label. This can be achieved by setting the {value}°C to the format property in the labelStyle object of the axis. Here, {value} acts as a placeholder for each axis label.
To change the pointer value from the default value of the gauge, set the value property in pointers object of the axis.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective, RangesDirective, RangeDirective } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200} labelStyle={ { format:'{value}°C' } }>
<PointersDirective>
<PointerDirective value={140}>
</PointerDirective>
</PointersDirective>
<RangesDirective>
<RangeDirective start={0} end={80} startWidth={15} endWidth={15}>
</RangeDirective>
<RangeDirective start={80} end={120} startWidth={15} endWidth={15}>
</RangeDirective>
<RangeDirective start={120} end={140} startWidth={15} endWidth={15}>
</RangeDirective>
<RangeDirective start={140} end={200} startWidth={15} endWidth={15}>
</RangeDirective>
</RangesDirective>
</AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective, RangesDirective, RangeDirective } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200} labelStyle={ { format:'{value}°C' } }>
<PointersDirective>
<PointerDirective value={140}>
</PointerDirective>
</PointersDirective>
<RangesDirective>
<RangeDirective start={0} end={80} startWidth={15} endWidth={15}>
</RangeDirective>
<RangeDirective start={80} end={120} startWidth={15} endWidth={15}>
</RangeDirective>
<RangeDirective start={120} end={140} startWidth={15} endWidth={15}>
</RangeDirective>
<RangeDirective start={140} end={200} startWidth={15} endWidth={15}>
</RangeDirective>
</RangesDirective>
</AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);Setting the Pointer Value
The pointer value is changed in the below sample using the value property in pointers object of the axis. The pointer can be customized with different colors using the color property to make it visually distinct.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200}>
<PointersDirective>
<PointerDirective value={140} color='green'>
</PointerDirective>
</PointersDirective>
</AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200}>
<PointersDirective>
<PointerDirective value={140} color='green'>
</PointerDirective>
</PointersDirective>
</AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);Run the application
Run the npm run dev command in the terminal to start the development server. This command compiles your code and serves the application locally, opening it in the browser.
npm run dev
The output appears as follows.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200}>
<PointersDirective>
<PointerDirective value={140} color='green'>
</PointerDirective>
</PointersDirective>
</AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent>
<AxesDirective>
<AxisDirective minimum={0} maximum={200}>
<PointersDirective>
<PointerDirective value={140} color='green'>
</PointerDirective>
</PointersDirective>
</AxisDirective>
</AxesDirective>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);Refer to the React Linear Gauge feature tour page for its groundbreaking feature representations. You can also explore our React Linear Gauge Component example that shows how to render the Linear Gauge in React.