Data labels in React 3D Chart control
10 Jan 202424 minutes to read
Data labels are fields that includes information about the sample point connected to an output. It can be added to a chart series by enabling the visible
option in the dataLabel. By default, the labels will arrange smartly without overlapping.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 },
{ country: "France", gold: 50, silver: 45, bronze: 35 },
{ country: "Germany", gold: 40, silver: 30, bronze: 22 },
{ country: "Italy", gold: 40, silver: 35, bronze: 37 },
{ country: "Sweden", gold: 30, silver: 25, bronze: 27 }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column' dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 },
{ country: "France", gold: 50, silver: 45, bronze: 35 },
{ country: "Germany", gold: 40, silver: 30, bronze: 22 },
{ country: "Italy", gold: 40, silver: 35, bronze: 37 },
{ country: "Sweden", gold: 30, silver: 25, bronze: 27 }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column' dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Note: To use data label feature, we need to inject
DataLabel3D
module into the services.
Position
The position
property is used to place the label either on Top
, Middle
, or Bottom
.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 },
{ country: "France", gold: 50, silver: 45, bronze: 35 },
{ country: "Germany", gold: 40, silver: 30, bronze: 22 },
{ country: "Italy", gold: 40, silver: 35, bronze: 37 },
{ country: "Sweden", gold: 30, silver: 25, bronze: 27 }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 },
{ country: "France", gold: 50, silver: 45, bronze: 35 },
{ country: "Germany", gold: 40, silver: 30, bronze: 22 },
{ country: "Italy", gold: 40, silver: 35, bronze: 37 },
{ country: "Sweden", gold: 30, silver: 25, bronze: 27 }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Template
Label content can be formatted by using the template option. Inside the template, the placeholder text ${point.x}
and ${point.y}
can be added to display corresponding data points x & y value. Using template
property, the data label template can be set.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const dataLabel = {
template:'<div style="border: 1px solid black; padding: 3px 3px 3px 3px"><div>${point.x}</div><div>${point.y}</div></div>', visible: true
}
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel={dataLabel}>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const dataLabel = {
template:'<div style="border: 1px solid black; padding: 3px 3px 3px 3px"><div>${point.x}</div><div>${point.y}</div></div>', visible: true
}
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel={dataLabel}>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Text mapping
Text from the data source can be mapped using the name
property.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ x: 'Jan', y: 12, text: 'January : 12°C' },
{ x: 'Feb', y: 8, text: 'February : 8°C' },
{ x: 'Mar', y: 11, text: 'March : 11°C' },
{ x: 'Apr', y: 6, text: 'April : 6°C' }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='x' yName='y' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ x: 'Jan', y: 12, text: 'January : 12°C' },
{ x: 'Feb', y: 8, text: 'February : 8°C' },
{ x: 'Mar', y: 11, text: 'March : 11°C' },
{ x: 'Apr', y: 6, text: 'April : 6°C' }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='x' yName='y' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Format
Data label for the chart can be formatted using the format
property. The global formatting options can be used as ‘n’, ‘p’, and ‘c’.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Value | Format | Resultant Value | Description |
---|---|---|---|
1000 | n1 | 1000.0 | The number is rounded to 1 decimal place. |
1000 | n2 | 1000.00 | The number is rounded to 2 decimal places. |
1000 | n3 | 1000.000 | The number is rounded to 3 decimal place. |
0.01 | p1 | 1.0% | The number is converted to percentage with 1 decimal place. |
0.01 | p2 | 1.00% | The number is converted to percentage with 2 decimal place. |
0.01 | p3 | 1.000% | The number is converted to percentage with 3 decimal place. |
1000 | c1 | $1000.0 | The currency symbol is appended to number and number is rounded to 1 decimal place. |
1000 | c2 | $1000.00 | The currency symbol is appended to number and number is rounded to 2 decimal place. |
Margin
The margin
for data label can be applied by using left
, right
, bottom
and top
properties.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const dataLabel = {
visible: true,
border:{
width: 1,
color : 'red'
},
margin:{
left:5,
right:5,
top:5,
bottom:5
}
}
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel={dataLabel}>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const dataLabel = {
visible: true,
border:{
width: 1,
color : 'red'
},
margin:{
left:5,
right:5,
top:5,
bottom:5
}
};
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel={dataLabel}>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Customization
The stroke
and border
of data label can be customized using fill
and border
properties.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const dataLabel = {
visible: true,
border:{ width: 2, color : 'red'},
};
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel={dataLabel}>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const dataLabel = {
visible: true,
border:{ width: 2, color : 'red'},
};
return <Chart3DComponent id='charts'
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel={dataLabel}>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
Customizing specific label
A specific label can be customized by using the textRender
event. The textRender
event allows you to change the label text for the point.
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const textRender = function(args) {
if (args.point.index === 2) {
args.text = 'Label';
}
else {
args.cancel = true;
}
}
return <Chart3DComponent id='charts' textRender={textRender}
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import { Chart3DComponent, Chart3DSeriesCollectionDirective, Chart3DSeriesDirective, Category3D, Inject, Legend3D, DataLabel3D, Tooltip3D, ColumnSeries3D, Highlight3D } from '@syncfusion/ej2-react-charts';
import * as React from "react";
import * as ReactDOM from "react-dom";
function App() {
const data = [
{ country: "USA", gold: 50, silver: 70, bronze: 45 },
{ country: "China", gold: 40, silver: 60, bronze: 55 },
{ country: "Japan", gold: 70, silver: 60, bronze: 50 },
{ country: "Australia", gold: 60, silver: 56, bronze: 40 }
];
const textRender = function(args) {
if (args.point.index === 2) {
args.text = 'Label';
}
else {
args.cancel = true;
}
}
return <Chart3DComponent id='charts' textRender={textRender}
primaryXAxis=
wallColor='transparent'
enableRotation={true} rotation={7} tilt={10} depth={100}>
<Inject services={[ColumnSeries3D, Category3D, Legend3D, Tooltip3D, DataLabel3D, Highlight3D]} />
<Chart3DSeriesCollectionDirective >
<Chart3DSeriesDirective dataSource={data} xName='country' yName='gold' type='Column'
dataLabel=>
</Chart3DSeriesDirective>
</Chart3DSeriesCollectionDirective>
</Chart3DComponent>;
};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));