Articles in this section

Add legend in table with x axis label in Chart

This article explains how to add a legend in a table with x-axis labels in the Chart component. The <e-annotation> tag is used to add a legend to the table, and the multiLevelLabels property is used to customize the axis label in table format.


To add a legend in a table with x-axis labels, follow the given steps:

  • Initialize the custom elements using the <e-annotation> tag.
  • Create table and rectangle shapes in the HTML page and set them as the content of the <e-annotation> tag.
  • By setting the coordinateUnit value to 'pixel' in the annotation object, you can place the legend in the chart based on pixel values.

[app.component.ts]

import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import { ChartComponent, IAxisLabelRenderEventArgs, IMouseEventArgs } from '@syncfusion/ej2-angular-charts';
 
@Component({
  selector: 'my-app',
    template:
            `<ejs-chart id="chart-container2" #chart  [margin]='columnMargin' [chartArea]='columnChart' [primaryXAxis]='columnXAxis'[primaryYAxis]='columnYAxis' [margin]='columnMargin' (axisLabelRender)='axisLabelRender($event)' (chartMouseClick)='chartMouseClick($event)'>  
               <e-annotations>
                 <e-annotation x='120' y='328' region='Chart' coordinateUnit = 'pixel'>
                        <ng-template #content>
                                <div style='width:80px; padding: 5px;'>
                                    <table style='width: 100%'>
                                        <tr> 
                                            <td><div id='first' style='width: 10px; height: 10px; background:skyblue;'></div></td>
                                            <td style='padding-left: 5px;'>Awarded</td>
                                        </tr>
                                        <tr>
                                            <td><div id='second' style='width: 10px; height: 10px; background:#b22222;'></div></td>
                                            <td style='padding-left: 5px;'>Paid</td>
                                        </tr>
                                    </table>
                                </div>
                        </ng-template>
                </e-annotation>
                 <e-annotation x='116' y='312' region='Chart' coordinateUnit = 'pixel'>
                        <ng-template #content>
                                <div style='width:80px; padding: 5px;'>
                                    <table style='width: 100%'>
                                        <tr> 
                                            <td><div style='border-style: solid;border-width: 1px; height:27px;width:85px; border-color: #b5b5b5;'></div></td>
                                        </tr>
                                  
                                    </table>
                                </div>
                        </ng-template>
                </e-annotation>
                <e-annotation x='116' y='340' region='Chart' coordinateUnit = 'pixel'>
                        <ng-template #content>
                                <div style='width:80px; padding: 5px;'>
                                    <table style='width: 100%'>
                                        <tr> 
                                            <td><div style='border-style: solid;border-width: 1px; height:27px;width:85px; border-color: #b5b5b5;'></div></td>
                                        </tr>
                                  
                                    </table>
                                </div>
                        </ng-template>
                </e-annotation>
            </e-annotations>    
    <e-series-collection>
        <e-series [dataSource]='columnData' type='Column' xName='x' yName='y' [marker]='columnMarker' fill='skyblue'></e-series> 
         <e-series [dataSource]='columnData' type='Column' xName='x' yName='y1' [marker]='columnMarker' fill='#b22222'></e-series> 
        </e-series-collection>
    </ejs-chart>`,
    encapsulation: ViewEncapsulation.None
})
export class AppComponent  {
    public columnMargin: Object = {
      left: 100, right: 100, bottom: 100
    };
    @ViewChild('chart')
    public chartInstance: ChartComponent;
    public firstClick: Boolean = true;
    public tooltip: Object = { enable: true};
    // intialize primary x-axis
    public columnXAxis: Object = {
      valueType: 'Category',
      border: { width: 1, type: 'Rectangle' },
      isIndexed: true,
      interval: 1,
        majorGridLines: { width: 0},
        multiLevelLabels: [
                {
                border: { type: 'Rectangle' },
                textStyle: { fontWeight: 'Bold' },
                categories: [
                    { start: -0.5, end: 3.5, text: 'Construction', },
                    { start: 3.5, end: 7.5, text: 'Professional Services', },
                ]
                },
                {
                    border: { type: 'Rectangle' },
                  categories: [
                    { start: -0.5, end: 0.5, text: '248,952,090' },
                    { start: 0.5, end: 1.5, text: '248,952,090' },
                    { start: 1.5, end: 2.5, text: '248,952,090' },
                    { start: 2.5, end: 3.5, text: '248,952,090' },
                    { start: 3.5, end: 4.5, text: '248,952,090' },
                    { start: 4.5, end: 5.5, text: '248,952,090' },
                    { start: 5.5, end: 6.5, text: '248,952,090' },
                    { start: 6.5, end: 7.5, text: '248,952,090' },
                  ]
                },
                {
                    border: { type: 'Rectangle' },
                  categories: [
                    { start: -0.5, end: 0.5, text: '248,952,090' },
                    { start: 0.5, end: 1.5, text: '248,952,090' },
                    { start: 1.5, end: 2.5, text: '248,952,090' },
                    { start: 2.5, end: 3.5, text: '248,952,090' },
                    { start: 3.5, end: 4.5, text: '248,952,090' },
                    { start: 4.5, end: 5.5, text: '248,952,090' },
                    { start: 5.5, end: 6.5, text: '248,952,090' },
                    { start: 6.5, end: 7.5, text: '248,952,090' },
                  ]
                },
                ]
    };
    public axisLabelRender(args: IAxisLabelRenderEventArgs): void {
        if(args.axis.name === 'primaryXAxis'){
          console.log(args);
          args.text = args.text.split(',')[0];
        }
    };
    public chartMouseClick(args: IMouseEventArgs): void {
       if((args.x >= 83 && args.x <= 155) && (args.y > 312 && args.y < 343)){
        this.chartInstance.series[0].dataSource = this.firstClick ? null : this.columnData;
        this.chartInstance.refresh();
        this.firstClick = !this.firstClick;
       } 
    };
    public columnMarker: Object = {
      visible: false,
      dataLabel : {
        visible : true
      }
    };
    public columnYAxis: Object = {
    labelFormat:'{value}%',
    };
    public chartArea: Object = {
      border: {width:0}
    };
    public columnChart: Object = {
      border: {width:0}
    };
    public primaryYAxis: Object = {
      majorGridLines: {width: 0},
      minimum: 0, maximum: 120,
     interval: 20
    };
    public marker: Object = {
      height: 20, width:20
    };
    public columnData: Object[] =  [
      { x: 'MWBE', y: 2800000, y1: 1000000 }, 
      { x: 'LDB', y: 1000000, y1:1550000 },
      { x: 'VBE', y: 2200000, y1:1200000 }, 
      { x: 'DBE', y: 3000000,y1: 1000000 },
      { x: 'MWBE', y: 1000000, y1: 800000 }, 
      { x: 'LDB', y: 500000, y1:400000 },
      { x: 'VBE', y: 2100000, y1:1500000 },
      { x: 'DBE', y: 900000, y1: 0 }
    ];
    };
 
 

 

Screenshot:

Chart legend in table with x-axis label

Description automatically generated

  Refer to the working sample for additional details and implementation:  View Sample in StackBlitz.

  

Conclusion


We hope you enjoyed learning about how to Add legend in table with x axis label in Chart.

You can refer to our Angular feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Angular chart example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied