Annotations in React Linear gauge component
29 Aug 202311 minutes to read
Annotations are used to mark the specific area of interest in the Linear Gauge with text, HTML elements, or images. Any number of annotations can be added to the Linear Gauge component.
Adding annotation
To render the custom HTML elements in the Linear Gauge component, use the content
property in the AnnotationDirective
. The annotation can be rendered either by specifying the id of the element or specifying the code to create a new element that needs to be displayed in the gauge area.
<head>
<script id='fruits' type="text/x-template">
<div id='apple'>
<img src='src/lineargauge/images/apple.png'>
</div>
</script>
</head>
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent>
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content="#fruits" x={100} zIndex='1' y={100}>
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Customization
The following properties are used to customize the annotation.
-
zIndex
- Bring the annotation to the front or back, when annotation overlaps with another element. -
axisValue
- To place the annotation in the specified axis value with respect to the provided axis index. -
axisIndex
- To place the annotation in the specified axis with respect to the provided axis value. -
horizontalAlignment
- To place the annotation horizontally. -
verticalAlignment
- To place the annotation vertically. -
x
,y
- To place the annotation in the specified location.
Changing the z-index
To change the stack order of an annotation element, the zIndex
property of the AnnotationDirective
can be used.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations,Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div id="first"><h1>Gauge</h1></div>' zIndex='1' >
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations,Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div id="first"><h1>Gauge</h1></div>' zIndex='1' >
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Positioning an annotation
The annotation can be placed anywhere in the Linear Gauge by setting the pixel value to the x
and y
properties in the AnnotationDirective
.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div id="first"><h1>Gauge</h1></div>' x={100} y={100} zIndex='1' >
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div id="first"><h1>Gauge</h1></div>' x={100} y={100} zIndex='1' >
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Alignment of annotation
The annotation can be aligned horizontally and vertically by using the horizontalAlignment
and verticalAlignment
properties respectively. The possible values can be Center, Far, Near, and None. The horizontalAlignment
and verticalAlignment
properties are not applicable when the x
and y
properties are set in the AnnotationDirective
.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div id="first"><h1>Gauge</h1></div>' verticalAlignment="Center" horizontalAlignment="Center" zIndex='1' >
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div id="first"><h1>Gauge</h1></div>' verticalAlignment="Center" horizontalAlignment="Center" zIndex='1' >
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
Multiple annotations
Multiple annotations can be added to the Linear Gauge component by adding the multiple AnnotationDirective
in the AnnotationsDirective
and customization for the annotation can be done with the AnnotationDirective
.
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div><h1 style="color:red;">Speed</h1></div>' verticalAlignment='Near' horizontalAlignment='Center' zIndex='1'>
</AnnotationDirective>
<AnnotationDirective content='<div><h1 style="color:blue;">Meter</h1></div>' zIndex='1' x={150} y={400}>
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);
import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinearGaugeComponent, AnnotationsDirective, AnnotationDirective, Annotations, Inject } from '@syncfusion/ej2-react-lineargauge';
export function App() {
return(
<LinearGaugeComponent >
<Inject services={[Annotations]}/>
<AnnotationsDirective>
<AnnotationDirective content='<div><h1 style="color:red;">Speed</h1></div>' verticalAlignment='Near' horizontalAlignment='Center' zIndex='1'>
</AnnotationDirective>
<AnnotationDirective content='<div><h1 style="color:blue;">Meter</h1></div>' zIndex='1' x={150} y={400}>
</AnnotationDirective>
</AnnotationsDirective>
</LinearGaugeComponent>);
}
const root = ReactDOM.createRoot(document.getElementById('container'));
root.render(<App />);