Search results

Getting started with React Linear Gauge component

22 Oct 2021 / 4 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

Below is the list of minimum dependencies required to use the linear gauge component.

Copied to clipboard
+-- @syncfusion/ej2-react-lineargauge
|-- @syncfusion/ej2-lineargauge
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-popups
        |-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-react-base

Installation and configuration

Use the create-react-app to setup the applications. To get started, run the below command to create a react app.

Copied to clipboard
npm install -g create-react-app
  • Start a new project using create-react-app command as follows
Copied to clipboard
create-react-app quickstart --scripts-version=react-scripts-ts

cd quickstart

npm install
  • To install the Linear Gauge component, use the following command.
Copied to clipboard
npm install @syncfusion/ej2-react-lineargauge --save

Adding Linear Gauge component to the Project

Now, you can start adding Linear Gauge components to the application. For getting started, add the Linear Gauge component in the src/index.tsx file using following code.

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

class App extends React.Component {
render() {
   return (<LinearGaugeComponent></LinearGaugeComponent>);
  }
}
export default App;
Copied to clipboard
import * as React from 'react';
import { LinearGaugeComponent } from '@syncfusion/ej2-react-lineargauge';
class App extends React.Component {
    render() {
        return (<LinearGaugeComponent></LinearGaugeComponent>);
    }
}
export default App;

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.tsx
index.jsx
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.tsx
index.jsx
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.tsx
index.jsx
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.tsx
index.jsx
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'));