Color Mapping in Angular TreeMap
11 Aug 202624 minutes to read
Apply Color Mapping
The color mapping feature allows you to customize item colors based on the underlying values in the data source. Specify the field whose values should be evaluated using the equalColorValuePath or rangeColorValuePath property.
Color mapping can be applied to both group and leaf items in the TreeMap. The following color mapping types are supported:
- Range color mapping
- Equal color mapping
- Desaturation color mapping
- Palette color mapping
- Desaturation with multiple colors
- Color for items excluded from color mapping
- Binding colors from the data source
The following example demonstrates how to apply color mapping in a TreeMap:
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' height='350px' [dataSource]='data' [legendSettings]='legendSettings' equalColorValuePath='Count' weightValuePath='Count' [leafItemSettings]='leafItemSettings'>
</ejs-treemap>`
})
export class AppComponent {
public legendSettings: object = {
visible: true,
};
public data: object[] = [
{ Title: 'State wise International Airport count in South America', State: 'Brazil', Count: 25 },
{ Title: 'State wise International Airport count in South America', State: 'Colombia', Count: 12 },
{ Title: 'State wise International Airport count in South America', State: 'Argentina', Count: 9 },
{ Title: 'State wise International Airport count in South America', State: 'Ecuador', Count: 7 },
{ Title: 'State wise International Airport count in South America', State: 'Chile', Count: 6 },
{ Title: 'State wise International Airport count in South America', State: 'Peru', Count: 3 },
{ Title: 'State wise International Airport count in South America', State: 'Venezuela', Count: 3 },
{ Title: 'State wise International Airport count in South America', State: 'Bolivia', Count: 2 },
{ Title: 'State wise International Airport count in South America', State: 'Paraguay', Count: 2 },
{ Title: 'State wise International Airport count in South America', State: 'Uruguay', Count: 2 },
{ Title: 'State wise International Airport count in South America', State: 'Falkland Islands',Count: 1 },
{ Title: 'State wise International Airport count in South America', State: 'French Guiana', Count:1 },
{ Title: 'State wise International Airport count in South America', State: 'Guyana', Count: 1 },
{ Title: 'State wise International Airport count in South America', State: 'Suriname', Count: 1 },
];
public leafItemSettings: object = {
labelPath: 'State',
colorMapping: [
{
value: 25,
color: '#634D6F'
},
{
value: 12,
color: '#B34D6D'
},
{
value: 9,
color: '#557C5C'
},
{
value: 7,
color: '#44537F'
},
{
value: 6,
color: '#637392'
},
{
value: 3,
color: '#7C754D'
},
{
value: 2,
color: '#2E7A64'
},
{
value: 1,
color: '#95659A'
},
]
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Range color mapping
Range color mapping is used to apply color to the items by giving specific ranges in the DataSource, and it should be specifying the data source properties to the rangeColorValuePath. The color mapping ranges to be specified in the from and to properties of the colorMapping.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' [dataSource]='data' weightValuePath='count' rangeColorValuePath='count'
[leafItemSettings]='leafItemSettings'>
</ejs-treemap>`
})
export class AppComponent {
public data: object[] = [{ fruit:'Apple', count:5000 },
{ fruit:'Mango', count:3000 },
{ fruit:'Orange', count:2300 },
{ fruit:'Banana', count:500 },
{ fruit:'Grape', count:4300 },
{ fruit:'Papaya', count:1200 },
{ fruit:'Melon', count:4500 }
];
public leafItemSettings: object = {
labelPath: 'fruit',
colorMapping:[
{
from:500,
to:3000,
color:'orange'
},
{
from:3000,
to:5000,
color:'green'
}
]
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Equal color mapping
Equal color mapping is used to fill colors to each item by specifying equal value present in the data source, that can be specified in the equalColorValuePath property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' [dataSource]='data' weightValuePath='count' equalColorValuePath='Brand'
[leafItemSettings]='leafItemSettings'>
</ejs-treemap>`
})
export class AppComponent {
public data: object[] = [
{ Car:'Mustang', Brand:'Ford', count:232 },
{ Car:'EcoSport', Brand:'Ford', count:121 },
{ Car:'Swift', Brand:'Maruti', count:143 },
{ Car:'Baleno', Brand:'Maruti', count:454 },
{ Car:'Vitara Brezza', Brand:'Maruti', count:545 },
{ Car:'A3 Cabriolet', Brand:'Audi',count:123 },
{ car:'RS7 Sportback', Brand:'Audi', count:523 }
];
public leafItemSettings: object = {
labelPath: 'Car',
colorMapping:[
{
value:'Ford',
color:'green'
},
{
value:'Audi',
color:'red'
},
{
value:'Maruti',
color:'orange'
}
]
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Desaturation color mapping
Desaturation color mapping is used to apply colors to the items based on the minOpacity and maxOpacity properties in the colorMapping.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' [dataSource]='data' weightValuePath='count' rangeColorValuePath='count'
[leafItemSettings]='leafItemSettings'>
</ejs-treemap>`
})
export class AppComponent {
public data: object[] = [
{ fruit:'Apple', count:5000 },
{ fruit:'Mango', count:3000 },
{ fruit:'Orange', count:2300 },
{ fruit:'Banana', count:500 },
{ fruit:'Grape', count:4300 },
{ fruit:'Papaya', count:1200 },
{ fruit:'Melon', count:4500 }
];
public leafItemSettings: object = {
labelPath: 'fruit',
colorMapping:[
{
from:500,
to:3000,
minOpacity:0.2,
maxOpacity:0.5,
color:'orange'
},
{
from:3000,
to:5000,
minOpacity:0.5,
maxOpacity:0.8,
color:'green'
}
]
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Palette color mapping
The palette color mapping is used to fill the color to each group or leaf item by given colors in the palette property.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' [dataSource]='data' weightValuePath='count' [palette]='palette'
[leafItemSettings]='leafItemSettings'>
</ejs-treemap>`
})
export class AppComponent {
public data: object[] = [
{ Car:'Mustang', Brand:'Ford', count:232 },
{ Car:'EcoSport', Brand:'Ford', count:121 },
{ Car:'Swift', Brand:'Maruti', count:143 },
{ Car:'Baleno', Brand:'Maruti', count:454 },
{ Car:'Vitara Brezza', Brand:'Maruti', count:545 },
{ Car:'A3 Cabriolet', Brand:'Audi',count:123 },
{ car:'RS7 Sportback', Brand:'Audi', count:523 }
];
public leafItemSettings: object = {
labelPath: 'Car'
};
public palette: object= ['red','green'];
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Desaturation with multiple colors
Multiple colors are used as gradient effect to specific shapes based on the ranges in datasource. By using color property, you can set n number of colors.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' [dataSource]='data' weightValuePath='count' rangeColorValuePath='count' [leafItemSettings]='leafItemSettings' [legendSettings] ='legendSettings'>
</ejs-treemap>`
})
export class AppComponent {
public data: object[] = [
{ fruit:'Apple', count:5000 },
{ fruit:'Mango', count:3000 },
{ fruit:'Orange', count:2300 },
{ fruit:'Banana', count:500 },
{ fruit:'Grape', count:4300 },
{ fruit:'Papaya', count:1200 },
{ fruit:'Melon', count:4500 }
];
public legendSettings:object ={
visible:true
}
public leafItemSettings: object = {
labelPath: 'fruit',
colorMapping:[
{
from:500,
to:2500,
color:['orange','pink']
},
{
from:3000,
to:5000,
color:['green','red','blue']
}
]
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Color for items excluded from color mapping
Get the excluded ranges from data source using the color mapping and apply the specific color to those items, without specifying the from and to properties.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' [dataSource]='data' weightValuePath='count' rangeColorValuePath='count' [leafItemSettings]='leafItemSettings'>
</ejs-treemap>`
})
export class AppComponent {
public data: object[] = [
{ fruit:'Apple', count:5000 },
{ fruit:'Mango', count:3000 },
{ fruit:'Orange', count:2300 },
{ fruit:'Banana', count:500 },
{ fruit:'Grape', count:4300 },
{ fruit:'Papaya', count:1200 },
{ fruit:'Melon', count:4500 }
];
public leafItemSettings: object = {
labelPath: 'fruit',
colorMapping:[
{
from:500,
to:2500,
color:'orange'
},
{
from:3000,
to:4000,
color:'green'
},
{
color:'red'
}
]
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Bind the colors to the items from data source
To set the color for each item from the data source, bind the data source property to the colorValuePath.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { TreeMapModule, TreeMapLegendService, TreeMapTooltipService, TreeMapAllModule } from '@syncfusion/ej2-angular-treemap'
import { Component } from '@angular/core';
@Component({
imports: [
TreeMapModule, TreeMapAllModule
],
providers: [TreeMapLegendService, TreeMapTooltipService],
standalone: true,
selector: 'app-container',
template: `<ejs-treemap id='container' style='display: block;' [dataSource]='data' weightValuePath='count' colorValuePath='color' [leafItemSettings]='leafItemSettings'>
</ejs-treemap>`
})
export class AppComponent {
public data: object[] = [
{ fruit:'Apple', count:5000, color: 'red'},
{ fruit:'Mango', count:3000, color:'blue'},
{ fruit:'Orange', count:2300, color:'green' },
{ fruit:'Banana', count:500, color:'yellow' },
{ fruit:'Grape', count:4300, color:'orange' },
{ fruit:'Papaya', count:1200, color:'pink' },
{ fruit:'Melon', count:4500, color:'violet' }
];
public leafItemSettings: object = {
labelPath: 'fruit'
};
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));