Getting Started with React Linear Gauge
16 May 202515 minutes to read
This section explains the steps required to create a Linear Gauge and demonstrates the basic usage of the Linear Gauge component.
You can explore some useful features in the Linear Gauge component using the following 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
Installation and configuration
To easily set up a React application, use 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-app
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 dev
To 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 dev
Adding Syncfusion® packages
All the available Essential® JS 2 packages are published in npmjs.com public registry. To install Linear Gauge package, use the following command.
npm install @syncfusion/ej2-react-lineargauge --save
Adding Linear Gauge component to the Project
Now, the Linear Gauge component can be added in the application. To initialize the Linear Gauge control in the React application, import the Linear Gauge control in the src/App.js or src/App.tsx as per the application. Please use the below code to include the Linear Gauge component in the application.
import React from 'react';
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return (<LinearGaugeComponent></LinearGaugeComponent>);
}
export default App;
Run the application
Now run the npm run dev
command in the console to start the development server. This command compiles your code and serves the application locally, opening it in the browser.
npm run dev
Module Injection
Linear Gauge component are segregated into individual feature-wise modules. In order to use a particular feature,
inject its feature service in the AppModule. Please find the feature service name and description as follows.
-
Annotations
- Inject this module in toservices
to use annotation feature. -
GaugeTooltip
- Inject this module in toservices
to use tooltip feature.
These modules should be injected into the services
section as follows,
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, Annotations, GaugeTooltip, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App(){
return(<LinearGaugeComponent>
<Inject services={[Annotations, GaugeTooltip]}/>
</LinearGaugeComponent>
);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Adding the Linear Gauge Title
The title can be added to the Linear Gauge component using the title
property in the Linear 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 />);
Axis Range
The range of the axis can be set using the minimum
and maximum
properties in the Linear Gauge.
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 />);
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 value of the pointer
The pointer value is changed in the below sample using 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 } 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 />);