Search results

Getting started with React Linear Gauge component

22 Sep 2022 / 5 minutes to read

This section explains you the steps required to create a simple Linear Gauge and demonstrate the basic usage of the Linear Gauge control.

Dependencies

Following is the list of minimum dependencies required to use the Linear Gauge.

Copied to clipboard
+-- @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.

Copied to clipboard
npm install -g create-react-app

To create basic React application, run the following command.

Copied to clipboard
create-react-app quickstart

Now, the application is created in the quickstart folder. Run the following command one-by-one to navigate to the quickstart folder, and install the required npm packages.

Copied to clipboard
cd quickstart

npm install

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.

Copied to clipboard
create-react-app quickstart --template typescript

Now, the application is created in the quickstart folder. Run the following command one-by-one to navigate to the quickstart folder, and install the required npm packages.

Copied to clipboard
cd quickstart

npm install

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.

Copied to clipboard
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.

Copied to clipboard
import React from 'react';
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';

function App() {
   return (<LinearGaugeComponent></LinearGaugeComponent>);
}

export default App;
Copied to clipboard
import React from 'react';
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
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.

Copied to clipboard
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 to services to use annotation feature.
  • GaugeTooltip - Inject this module in to services to use tooltip feature.

These modules should be injected into the services section as follows,

Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, Annotations, GaugeTooltip} from '@syncfusion/ej2-react-lineargauge';

ReactDOM.render(<LinearGaugeComponent id='gauge'></LinearGaugeComponent>,
<Inject services={[Annotations, GaugeTooltip]}/>
document.getElementById('gauge'));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, Annotations, GaugeTooltip } from '@syncfusion/ej2-react-lineargauge';
ReactDOM.render(<LinearGaugeComponent id='gauge'></LinearGaugeComponent>, <Inject services={[Annotations, GaugeTooltip]}/>, document.getElementById('gauge'));

Adding the Linear Gauge Title

The title can be added to the Linear Gauge component using the title property in the Linear Gauge.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
ReactDOM.render(<LinearGaugeComponent id='gauge' title='Linear Gauge'>
</LinearGaugeComponent>, document.getElementById('gauge'));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';

ReactDOM.render(
<LinearGaugeComponent id='gauge' title='Linear Gauge'>
</LinearGaugeComponent>,document.getElementById('gauge'));

Axis Range

The range of the axis can be set using the minimum and maximum properties in the Linear Gauge.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective } from '@syncfusion/ej2-react-lineargauge';
ReactDOM.render(<LinearGaugeComponent id='gauge'>
    <AxesDirective>
        <AxisDirective minimum={0} maximum={200}>
        </AxisDirective>
    </AxesDirective>
</LinearGaugeComponent>, document.getElementById('gauge'));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective } from '@syncfusion/ej2-react-lineargauge';

ReactDOM.render(
<LinearGaugeComponent id='gauge'>
    <AxesDirective>
        <AxisDirective minimum={0} maximum={200}>
        </AxisDirective>
    </AxesDirective>
</LinearGaugeComponent>,document.getElementById('gauge'));

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.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective, RangesDirective, RangeDirective } from '@syncfusion/ej2-react-lineargauge';
ReactDOM.render(<LinearGaugeComponent id='gauge'>
    <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>, document.getElementById('gauge'));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective, RangesDirective, RangeDirective } from '@syncfusion/ej2-react-lineargauge';

ReactDOM.render(
<LinearGaugeComponent id='gauge'>
    <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>,document.getElementById('gauge'));

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.

Source
Preview
index.jsx
index.tsx
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective } from '@syncfusion/ej2-react-lineargauge';
ReactDOM.render(<LinearGaugeComponent id='gauge'>
    <AxesDirective>
        <AxisDirective minimum={0} maximum={200}>
            <PointersDirective>
                <PointerDirective value={140} color='green'>
                </PointerDirective>
            </PointersDirective>
        </AxisDirective>
    </AxesDirective>
</LinearGaugeComponent>, document.getElementById('gauge'));
Copied to clipboard
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AxesDirective, AxisDirective, PointersDirective, PointerDirective } from '@syncfusion/ej2-react-lineargauge';

ReactDOM.render(
<LinearGaugeComponent id='gauge'>
    <AxesDirective>
        <AxisDirective minimum={0} maximum={200}>
            <PointersDirective>
                <PointerDirective value={140} color='green'>
                </PointerDirective>
            </PointersDirective>
        </AxisDirective>
    </AxesDirective>
</LinearGaugeComponent>,document.getElementById('gauge'));