Guides

Events

Click handlers that give you the clicked value, for cross-filtering and drill-downs.

The samples assume the ChartsProvider from step 2 of the quick start.

A chart calls your handler when a bar, point, slice or row is clicked, and passes the value that was clicked. Use it to filter another chart, open a detail view or navigate.

Filter one chart from another

Click a region on the left to filter the chart on the right.

RegionFilter.tsx
import { useState } from 'react';
import { BarChart, dimension, measure, results } from 'em-kit';

const region = dimension({ name: 'region', title: 'Region' });
const plan = dimension({ name: 'plan', title: 'Plan' });
const revenue = measure({ name: 'revenue', title: 'Revenue', inputs: { prefix: '$' } });

const byRegion = [
  { region: 'Americas', revenue: 527900 },
  { region: 'EMEA', revenue: 391500 },
  { region: 'APAC', revenue: 276300 },
];

const byRegionAndPlan = [
  { region: 'Americas', plan: 'Starter', revenue: 96400 },
  { region: 'Americas', plan: 'Growth', revenue: 181200 },
  { region: 'Americas', plan: 'Enterprise', revenue: 250300 },
  { region: 'EMEA', plan: 'Starter', revenue: 88100 },
  { region: 'EMEA', plan: 'Growth', revenue: 142900 },
  { region: 'EMEA', plan: 'Enterprise', revenue: 160500 },
  { region: 'APAC', plan: 'Starter', revenue: 71800 },
  { region: 'APAC', plan: 'Growth', revenue: 109300 },
  { region: 'APAC', plan: 'Enterprise', revenue: 95200 },
];

const grid = {
  display: 'grid',
  gridTemplateColumns: 'repeat(auto-fit, minmax(280px, 1fr))',
  gridAutoRows: 320,
  gap: 16,
};

export function RegionFilter() {
  const [selected, setSelected] = useState('EMEA');

  return (
    <div style={grid}>
      <BarChart
        title="Revenue by region"
        dimension={region}
        measures={[revenue]}
        results={results(byRegion)}
        onBarClicked={({ dimensionValue }) => {
          if (dimensionValue != null) setSelected(String(dimensionValue));
        }}
      />
      <BarChart
        title={`${selected} by plan`}
        dimension={plan}
        measures={[revenue]}
        results={results(byRegionAndPlan.filter((row) => row.region === selected))}
      />
    </div>
  );
}
Result

Revenue by region

EMEA by plan

What the handler receives

KeyValue
dimensionValueThe clicked category, such as 'EMEA'. undefined when the axis is a time dimension.
dimensionTimeRangeOn a time axis, the clicked period as a TimeRange. undefined on a category axis.
groupingDimensionValue, groupingDimensionTimeRangeThe same pair for the series that was clicked. Stacked and grouped charts only.
measureValuesEach measure’s value at the click, keyed by the measure’s name. Passed by , though their payload types leave it out.

A click payload’s TimeRange is undefined unless the clicked field is a time dimension, and its relativeTimeString is always undefined, so read it as range?.from. When you pass one, from and to are enough.

Handler names

The prop is named after what is clicked, and the names differ: some end in Clicked, some in Click. This table is generated from each component’s props.

HandlerComponents
onRowClickedTableScrollable, TablePaginated
onBarClickedBarChart, BarChartHorizontal, BarChartGrouped, BarChartGroupedHorizontal, BarChartStacked, BarChartStackedHorizontal, BarLineChart
onLineClickedBarLineChart, LineChart, LineChartGrouped, LineChartComparison, LineChartWithKpiTabs, LineChartComparisonWithKpiTabs
onPointClickedAreaChart
onAreaClickedAreaChart
onSegmentClickPieChart, DonutChart, DonutLabelChart
onPointClickScatterChart, BubbleChart
onCellClickedHeatMap

AreaChart takes onPointClicked; ScatterChart and BubbleChart take onPointClick. Each component page has the payload type, and its playground logs the payload as you click.

Clicks need a pointer. For keyboard users, offer the same action another way: see Accessibility.