Lattice Grid

blog

Voronoi Treemap of Market Share by Brand

Building…
Loading a live grid…

If you are the analyst reporting market share, the question is not which brand is biggest overall. It is how each brand stands inside its own category, with the categories kept apart, so a leader in a small category is not mistaken for a leader in a large one.

A Voronoi treemap answers that. Each brand is an organic cell whose area is its share, and the cells sit inside a region for their category, with a wider gap at the category boundary than between brands. The chart above is bound to the grid beneath it, one row per category and brand, so filtering the rows redraws the chart. Filter the Category column to Watches and only the watch brands are left, filling the whole shape.

Why a Voronoi treemap and not a rectangular treemap

A rectangular treemap packs the same hierarchy into slabs and strips, and a thin strip is hard to compare with a square. Voronoi cells stay round, the areas are fitted to within one percent of each brand’s share, and the category boundary reads as a region rather than a grid line.

The chart over a grid

The chart reads the grid’s rows, and the tree it draws is the grid’s own grouping. Group the grid by category and then brand, and the chart has one region per category with a cell per brand inside it. The measure, y, sizes the cells. Each region takes its own colour. Voronoi treemap is an opt-in chart type, so you import its module once after the charts module. Here is the example in plain JavaScript, with a few sample rows inline in place of your own.

import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { createChart } from '@toclocoinc/lattice-grid/modules/charts';
import '@toclocoinc/lattice-grid/modules/chart-voronoi';

const rows = [
  { id: 1, category: 'Phones', brand: 'Brand A', share: 34 },
  { id: 2, category: 'Phones', brand: 'Brand B', share: 22 },
  { id: 3, category: 'Phones', brand: 'Brand C', share: 44 },
  { id: 4, category: 'Watches', brand: 'Brand A', share: 38 },
  { id: 5, category: 'Watches', brand: 'Brand B', share: 21 },
  { id: 6, category: 'Watches', brand: 'Brand C', share: 41 },
];

const gridEl = document.createElement('div');
const chartEl = document.createElement('div');
gridEl.style.height = '240px';
chartEl.style.height = '420px';
document.body.append(gridEl, chartEl);

const grid = createGrid(gridEl, {
  rowKey: 'id',
  rows,
  columns: [
    { field: 'category', title: 'Category', filter: { type: 'set' }, group: { enabled: true, index: 0 } },
    { field: 'brand', title: 'Brand', group: { enabled: true, index: 1 } },
    { field: 'share', title: 'Share (%)', type: 'number', total: 'sum' },
  ],
});

createChart({ grid, container: chartEl, type: 'voronoiTreemap', y: 'share',
  title: 'Market share by brand' });

The same thing in React

The React adapter is a factory, so you build the component once at module scope. The ref’s grid is the real grid, which is what the chart binds to.

import React, { useEffect, useRef } from 'react';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { createLatticeGrid } from '@toclocoinc/lattice-grid/modules/react';
import { createChart } from '@toclocoinc/lattice-grid/modules/charts';
import '@toclocoinc/lattice-grid/modules/chart-voronoi';

const LatticeGrid = createLatticeGrid({ React, createGrid });
const rows = [
  { id: 1, category: 'Phones', brand: 'Brand A', share: 34 },
  { id: 2, category: 'Phones', brand: 'Brand B', share: 22 },
  { id: 3, category: 'Phones', brand: 'Brand C', share: 44 },
  { id: 4, category: 'Watches', brand: 'Brand A', share: 38 },
  { id: 5, category: 'Watches', brand: 'Brand B', share: 21 },
  { id: 6, category: 'Watches', brand: 'Brand C', share: 41 },
];
const columns = [
  { field: 'category', title: 'Category', filter: { type: 'set' }, group: { enabled: true, index: 0 } },
  { field: 'brand', title: 'Brand', group: { enabled: true, index: 1 } },
  { field: 'share', title: 'Share (%)', type: 'number', total: 'sum' },
];

export function MarketShare() {
  const gridRef = useRef(null);
  const chartRef = useRef(null);
  useEffect(() => {
    const chart = createChart({ grid: gridRef.current.grid, container: chartRef.current,
      type: 'voronoiTreemap', y: 'share' });
    return () => chart.destroy();
  }, []);
  return (<>
    <LatticeGrid ref={gridRef} rowKey="id" columns={columns} rows={rows} style={{ height: 220 }} />
    <div ref={chartRef} style={{ height: 380 }} />
  </>);
}

The options used

These are the rows of the charts reference for the options in the listings above.

Option Type What it does
grid Grid Required. The grid to read.
container Element or string Required. Where to draw.
type string 'voronoiTreemap' here, a treemap whose cells are the regions of a weighted Voronoi diagram.
y string The measure column. Here it is the share, which sets the area of each brand’s cell.
title string Drawn above the plot.

Two more options are worth knowing. clip sets the shape the cells fill, a circle by default or any polygon of [x, y] points, and tolerance sets the largest error any cell’s area may keep, under one percent unless you change it.

Reading the cells

Filter the Category column to one category and compare. In the example, a single brand holds more than a third of Phones and of Watches, while in Laptops and Headphones the leader holds under a third. With no filter the chart draws all four categories at once, each region sized by the shares of its brands, so filter to one category when you want its brands to fill the shape. Hover a cell for its path and share.

Next

See the Voronoi treemap beside the other part-to-whole charts in the part-to-whole family demo, read the full list of chart types on the charts page, or follow the how-to for a chart that follows the grid.

Read next

  • Packed Circle Chart of Teams by Headcount

    Bind a packed circle chart to a grid of teams: each circle is sized by headcount and packed inside its department. Filter the rows to redraw it.

  • Icicle Chart of Request Time by Span

    Bind an icicle chart to a grid of trace spans: width is time spent, depth is nesting, so the slow span stands out. Filter the rows to redraw it.

  • Waffle Chart for Survey Answers

    Bind a waffle chart to a grid of survey answers: a hundred squares per question, each one percent of respondents, redrawn when you filter the rows.

All posts RSS