Getting Started with React Linear Gauge
22 Nov 202315 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 get started with the React application, create-react-app can be used to setup the application. To install create-react-app run the following command.
npm install -g create-react-app
To create basic React application, run the following command.
create-react-app quickstart
Now, the application is created in the quickstart folder. Run the following command to navigate to the quickstart folder, and install the required npm packages.
cd quickstart
In the quickstart application, the Syncfusion component is added in the JavaScript file.
Creating a React application with TypeScript
To create React application with TypeScript, use the following command.
create-react-app quickstart --template typescript
Now, the application is created in the quickstart folder. Run the following command to navigate to the quickstart folder, and install the required npm packages.
cd quickstart
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
The Linear Gauge control is now included in the quickstart application. Use the following command to run the application.
npm start
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} 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 />);