Search results

Bind colors for each data label template from the data source

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.

Source
Preview
index.tsx
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';

class App extends React.Component<{}, {}> {

  public 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' },
  ];
  public template:any =this.chartTemplate;
  public marker = { visible: true, dataLabel: { visible: true, name: 'color', template: this.template } };
  public 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>);
  }
  render() {
    return <ChartComponent id='charts'>
      <Inject services={[LineSeries, Legend, Tooltip, DataLabel, Category, StripLine]} />
      <SeriesCollectionDirective>
        <SeriesDirective dataSource={this.data} xName='x' yName='y' type='Line' marker={this.marker}>
        </SeriesDirective>
      </SeriesCollectionDirective>
    </ChartComponent>
  }

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


{% endraw %}