Highlighting the hovered data item or series allows for easier differentiation, especially in charts with many series and data points.
import {
AgChartOptions,
AgCharts,
BarSeriesModule,
CategoryAxisModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
} from "ag-charts-community";
import { getData } from "./data";
ModuleRegistry.registerModules([
BarSeriesModule,
CategoryAxisModule,
LegendModule,
NumberAxisModule,
]);
const options: AgChartOptions = {
data: getData(),
title: {
text: "Station Entries",
},
subtitle: {
text: "Victoria Line (2023)",
},
series: [
{
type: "bar",
xKey: "station",
yKey: "early",
yName: "Early",
stacked: true,
normalizedTo: 100,
},
{
type: "bar",
xKey: "station",
yKey: "morningPeak",
yName: "Morning Peak",
stacked: true,
normalizedTo: 100,
},
{
type: "bar",
xKey: "station",
yKey: "interPeak",
yName: "Inter-peak",
stacked: true,
normalizedTo: 100,
},
{
type: "bar",
xKey: "station",
yKey: "afternoonPeak",
yName: "Afternoon Peak",
stacked: true,
normalizedTo: 100,
},
{
type: "bar",
xKey: "station",
yKey: "evening",
yName: "Evening",
stacked: true,
normalizedTo: 100,
},
],
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
export function getData() {
return [
{
station: "Finsbury Park",
early: 2454,
morningPeak: 16644,
interPeak: 9338,
afternoonPeak: 6346,
evening: 3547,
},
{
station: "Seven Sisters",
early: 3927,
morningPeak: 7581,
interPeak: 5421,
afternoonPeak: 3245,
evening: 2036,
},
{
station: "Tottenham Hale",
early: 6836,
morningPeak: 12740,
interPeak: 14964,
afternoonPeak: 12790,
evening: 8428,
},
{
station: "Warren Street",
early: 9108,
morningPeak: 2710,
interPeak: 5902,
afternoonPeak: 10947,
evening: 5574,
},
{
station: "Oxford Circus",
early: 7170,
morningPeak: 4996,
interPeak: 26616,
afternoonPeak: 32269,
evening: 3665,
},
{
station: "Green Park",
early: 6252,
morningPeak: 3911,
interPeak: 11971,
afternoonPeak: 19749,
evening: 11582,
},
];
}
In the above example:
- Hovering a bar segment will highlight it. The other segments in that series are partially dimmed.
- Hovering a bar segment in one series will dim the other series.
- Hovering a legend item will dim the other series.
Highlighting is enabled by default. Use highlight.enabled to configure it globally, or series[].highlight.enabled to override it per series.
Highlight Modes Copy Link
By default, hovering an item highlights only that item. Use highlight.mode: 'shared' to also highlight the item of every other series that shares the hovered item's category.
import {
AgCartesianChartOptions,
AgChartHighlightMode,
AgCharts,
BarSeriesModule,
CategoryAxisModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
} from "ag-charts-community";
import { getData } from "./data";
ModuleRegistry.registerModules([
BarSeriesModule,
CategoryAxisModule,
LegendModule,
NumberAxisModule,
]);
const options: AgCartesianChartOptions = {
data: getData(),
highlight: {
mode: "single",
},
series: [
{
type: "bar",
xKey: "quarter",
yKey: "productA",
yName: "Product A",
stacked: true,
},
{
type: "bar",
xKey: "quarter",
yKey: "productB",
yName: "Product B",
stacked: true,
},
{
type: "bar",
xKey: "quarter",
yKey: "productC",
yName: "Product C",
stacked: true,
},
],
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
function setHighlightMode(event: Event) {
const mode = (event.target as HTMLInputElement).value as AgChartHighlightMode;
options.highlight!.mode = mode;
chart.update(options);
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).setHighlightMode = setHighlightMode;
}
export function getData() {
return [
{ quarter: "Q1", productA: 120, productB: 90, productC: 60 },
{ quarter: "Q2", productA: 132, productB: 101, productC: 78 },
{ quarter: "Q3", productA: 101, productB: 137, productC: 95 },
{ quarter: "Q4", productA: 134, productB: 120, productC: 110 },
];
}
{
highlight: {
mode: 'shared', // or 'single'
},
}The options for mode are:
single- highlights the hovered item only.shared- highlights the item of every other series that shares the hovered item's category.
See Tooltip Modes to apply the same shared grouping to series highlighting.
Bring to Front Copy Link
By default, the highlighted series is brought to the front to make it stand out, especially when multiple series overlap.
import {
AgAreaSeriesOptions,
AgCartesianChartOptions,
AgCharts,
AreaSeriesModule,
CategoryAxisModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
} from "ag-charts-community";
import { getData } from "./data";
ModuleRegistry.registerModules([
AreaSeriesModule,
CategoryAxisModule,
LegendModule,
NumberAxisModule,
]);
const options: AgCartesianChartOptions = {
title: {
text: "Sales by Month",
},
data: getData(),
series: [
{
type: "area",
xKey: "month",
yKey: "subscriptions",
yName: "Subscriptions",
fillOpacity: 1,
strokeWidth: 4,
highlight: { bringToFront: true },
},
{
type: "area",
xKey: "month",
yKey: "services",
yName: "Services",
fillOpacity: 1,
strokeWidth: 4,
highlight: { bringToFront: true },
},
{
type: "area",
xKey: "month",
yKey: "products",
yName: "Products",
fillOpacity: 1,
strokeWidth: 4,
highlight: { bringToFront: true },
},
],
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
function bringToFrontChange(event: Event) {
const bringToFront = (event.target as HTMLInputElement).value === "true";
options.series!.forEach((series) => {
(series as AgAreaSeriesOptions).highlight = { bringToFront };
});
chart.update(options);
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).bringToFrontChange = bringToFrontChange;
}
export function getData() {
return [
{ month: "Jan", subscriptions: 222, services: 250, products: 200 },
{ month: "Feb", subscriptions: 240, services: 255, products: 210 },
{ month: "Mar", subscriptions: 280, services: 245, products: 195 },
{ month: "Apr", subscriptions: 300, services: 260, products: 205 },
{ month: "May", subscriptions: 350, services: 235, products: 215 },
{ month: "Jun", subscriptions: 420, services: 270, products: 200 },
{ month: "Jul", subscriptions: 300, services: 255, products: 225 },
{ month: "Aug", subscriptions: 270, services: 305, products: 210 },
{ month: "Sep", subscriptions: 260, services: 280, products: 250 },
{ month: "Oct", subscriptions: 385, services: 250, products: 205 },
{ month: "Nov", subscriptions: 320, services: 265, products: 215 },
{ month: "Dec", subscriptions: 330, services: 255, products: 220 },
];
}
The bringToFront property is enabled by default, and can be disabled in the highlight options if the default rendering order should be preserved.
{
highlight: {
bringToFront: false,
},
}In the above example:
- By default, hovering any series in the chart or legend will render it above all the other series.
- This behaviour can be toggled using the buttons to see the difference when
bringToFrontis disabled.
Customisation Copy Link
The highlight styles of each series can be customised with the highlight options.
import {
AgChartOptions,
AgCharts,
AreaSeriesModule,
CategoryAxisModule,
LegendModule,
ModuleRegistry,
NumberAxisModule,
} from "ag-charts-community";
var data = [
{ quarter: "Q1", coffee: 450, tea: 270, milk: 180 },
{ quarter: "Q2", coffee: 560, tea: 380, milk: 170 },
{ quarter: "Q3", coffee: 600, tea: 450, milk: 190 },
{ quarter: "Q4", coffee: 700, tea: 520, milk: 200 },
];
ModuleRegistry.registerModules([
AreaSeriesModule,
CategoryAxisModule,
LegendModule,
NumberAxisModule,
]);
const options: AgChartOptions = {
data: data,
theme: {
overrides: {
area: {
series: {
highlight: {
highlightedItem: {
fill: "yellow",
stroke: "gold",
strokeWidth: 2,
},
unhighlightedItem: {
fill: "maroon",
strokeWidth: 0,
},
highlightedSeries: {
fill: "red",
stroke: "maroon",
strokeWidth: 2,
},
unhighlightedSeries: {
opacity: 0.2,
},
},
},
},
},
},
title: {
text: "Beverage Expenses",
},
subtitle: {
text: "per quarter",
},
footnote: {
text: "Based on a sample size of 200 respondents",
},
series: [
{
type: "area",
xKey: "quarter",
yKey: "coffee",
yName: "Coffee",
marker: { enabled: true, size: 10 },
stacked: true,
},
{
type: "area",
xKey: "quarter",
yKey: "tea",
yName: "Tea",
marker: { enabled: true, size: 10 },
stacked: true,
},
{
type: "area",
xKey: "quarter",
yKey: "milk",
yName: "Milk",
marker: { enabled: true, size: 10 },
stacked: true,
},
],
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
{
highlight: {
// Attributes that apply to the currently highlighted item.
highlightedItem: {
fill: 'yellow',
stroke: 'gold',
strokeWidth: 2,
},
// Attributes that apply to the unhighlighted items within the highlighted series.
unhighlightedItem: {
fill: 'maroon',
strokeWidth: 0,
},
// Attributes that apply to the entire series containing the highlighted item.
highlightedSeries: {
fill: 'red',
stroke: 'maroon',
strokeWidth: 2,
},
// Attributes that apply to all other series.
unhighlightedSeries: {
opacity: 0.2,
},
},
}In this example:
- The hovered marker is highlighted using the
highlightedItemconfiguration. This changes thefilltoyellow, thestroketogold, and thestrokeWidthto2. - The non-hovered markers within the hovered series are styled using the
unhighlightedItemconfiguration. This changes thefilltomaroon, and removes the stroke by setting thestrokeWidthto0. - The hovered series is highlighted using the
highlightedSeriesconfiguration. This changes thefillandstroketo shades of red, with astrokeWidthof2. - The non-highlighted series are dimmed with an applied opacity of
0.2using theunhighlightedSeriesconfiguration.
In the above example we provided the same highlight for all of the series, but the style can be unique to each series.
For simplicity, we provided the highlight once within a chart Theme, rather than repeating it on each series.
Stylers Copy Link
All Styler callbacks receive a param.highlightState property which can be used to dynamically customise the chart style based on the highlighted state.
import {
AgCartesianChartOptions,
AgChartLabelStylerParams,
AgCharts,
CategoryAxisModule,
LegendModule,
LineSeriesModule,
ModuleRegistry,
NumberAxisModule,
} from "ag-charts-community";
import { getData } from "./data";
ModuleRegistry.registerModules([
CategoryAxisModule,
LegendModule,
LineSeriesModule,
NumberAxisModule,
]);
const options: AgCartesianChartOptions = {
title: { text: "Company Financials (Balance Sheet Overview)" },
data: getData(),
theme: {
overrides: {
line: {
series: {
highlight: { unhighlightedSeries: { opacity: 0.2 } },
label: {
enabled: true,
itemStyler: (
params: AgChartLabelStylerParams<unknown, unknown>,
) => {
switch (params.highlightState) {
case "highlighted-series":
return { fontSize: 10 };
case "unhighlighted-item":
return { color: "lightgray" };
case "highlighted-item":
return { fontWeight: "bold" };
default:
return { color: "transparent" };
}
},
},
},
},
},
},
series: [
{
type: "line",
xKey: "year",
yKey: "cash",
yName: "Cash",
},
{
type: "line",
xKey: "year",
yKey: "networth",
yName: "Net Worth",
},
{
type: "line",
xKey: "year",
yKey: "assets",
yName: "Assets",
},
{
type: "line",
xKey: "year",
yKey: "liabilities",
yName: "Liabilities",
},
],
axes: {
x: { type: "category", title: { text: "Year" } },
y: { type: "number", title: { text: "£ (Millions)" } },
},
tooltip: {
enabled: false,
},
legend: {
position: "right",
},
};
options.container = document.getElementById("myChart");
const chart = AgCharts.create(options);
export function getData() {
return [
{
year: 2018,
cash: 50,
networth: 120,
assets: 200,
liabilities: 80,
},
{
year: 2019,
cash: 65,
networth: 150,
assets: 240,
liabilities: 90,
},
{
year: 2020,
cash: 40,
networth: 130,
assets: 210,
liabilities: 80,
},
{
year: 2021,
cash: 70,
networth: 180,
assets: 280,
liabilities: 100,
},
{
year: 2022,
cash: 90,
networth: 220,
assets: 320,
liabilities: 100,
},
];
}
{
label: {
itemStyler: (params) => {
switch (params.highlightState) {
case 'highlighted-series':
return { fontSize: 10 };
case 'unhighlighted-item':
return { color: 'lightgray' };
case 'highlighted-item':
return { fontWeight: 'bold' };
default:
return { color: 'transparent' };
}
},
},
}In this example:
- The labels are hidden and only shown for the highlighted series.
- The label of the currently highlighted item is rendered in bold.
The highlightState parameter can have the following values:
'highlighted-item': The specific item is highlighted'unhighlighted-item': Another item is highlighted, but not this one'highlighted-series': The series containing this item is highlighted'unhighlighted-series': Another series is highlighted, but not this one'none': No highlighting is currently active
API Reference Copy Link
The available options differ between series types. See the Options API for more details.