Data label template in React Chart component

20 Jan 20237 minutes to read

You can bind text and interior information for a point from dataSource other than x and y value. To change color for the background in the datalabel template, you can use ${point.text}.
To use point.text, you have to bind the property from dataSource to name in the datalabel options.

Follow the given steps to show the table tooltip,

Step 1:

Initialize the datalabel template div as shown in the following html page,

    <script id="index" type="text/x-template">
    <div id='templateWrap' style="background-color: ${point.text}; border-radius: 3px;"><span>${point.y}</span></div>
    </script>

Step 2:

To show that datalabel template, set the element id to the template property in datalabel.

import * as React from "react";
import * as ReactDOM from "react-dom";
import { ChartComponent, SeriesCollectionDirective, SeriesDirective, Inject, StripLine, Legend, Category, Tooltip, DataLabel, LineSeries } from '@syncfusion/ej2-react-charts';
function App() {
    const data = [
        { x: 10, y: 7000, color: 'red' },
        { x: 20, y: 1000, color: 'yellow' },
        { x: 30, y: 12000, color: 'orange' },
        { x: 40, y: 14000, color: 'skyblue' },
        { x: 50, y: 11000, color: 'blue' },
        { x: 60, y: 5000, color: 'green' },
        { x: 70, y: 7300, color: 'pink' },
        { x: 80, y: 9000, color: 'white' },
        { x: 90, y: 12000, color: 'magenta' },
        { x: 100, y: 14000, color: 'purple' },
        { x: 110, y: 11000, color: 'teal' },
        { x: 120, y: 5000, color: 'gray' },
    ];
    const template = chartTemplate;
    const marker = { visible: true, dataLabel: { visible: true, name: 'color', template: template } };
    function chartTemplate(args) {
        return (<div id="templateWrap" style={{ border: '1px solid black', backgroundColor: 'red', padding: '3px 3px 3px 3px' }}>
          <div>{args.point.x}</div>
          <div>{args.point.y}</div>
        </div>);
    }
    return <ChartComponent id='charts'>
      <Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category, StripLine]}/>
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={data} xName='x' yName='y' type='Line' marker={marker}>
        </SeriesDirective>
      </SeriesCollectionDirective>
    </ChartComponent>;
}
;
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));
import * as React from "react";
import * as ReactDOM from "react-dom";
import { ChartComponent, SeriesCollectionDirective, AxesDirective, AxisDirective, SeriesDirective, Inject, StripLine, ColumnSeries, Legend, Category, Tooltip, DataLabel, Zoom, Crosshair, LineSeries,  Selection, StripLinesDirective, StripLineDirective} from'@syncfusion/ej2-react-charts';

function App() {

  const data: any[] = [
    { x: 10, y: 7000, color: 'red' },
    { x: 20, y: 1000, color: 'yellow' },
    { x: 30, y: 12000, color: 'orange' },
    { x: 40, y: 14000, color: 'skyblue' },
    { x: 50, y: 11000, color: 'blue' },
    { x: 60, y: 5000, color: 'green' },
    { x: 70, y: 7300, color: 'pink' },
    { x: 80, y: 9000, color: 'white' },
    { x: 90, y: 12000, color: 'magenta' },
    { x: 100, y: 14000, color: 'purple' },
    { x: 110, y: 11000, color: 'teal' },
    { x: 120, y: 5000, color: 'gray' },
  ];
  const template:any = chartTemplate;
  const marker = { visible: true, dataLabel: { visible: true, name: 'color', template: template } };
  function chartTemplate(args:any){
        return (<div id="templateWrap" style={{ border:'1px solid black', backgroundColor:'red', padding:'3px 3px 3px 3px'}}>
          <div>{args.point.x}</div>
          <div>{args.point.y}</div>
        </div>);
  }
    return <ChartComponent id='charts'>
      <Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category, StripLine]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={data} xName='x' yName='y' type='Line' marker={marker}>
        </SeriesDirective>
      </SeriesCollectionDirective>
    </ChartComponent>

};
export default App;
ReactDOM.render(<App />, document.getElementById("charts"));