Lattice Grid

blog

Slope Chart for NPS Before and After

Building…
Loading a live grid…

If you lead customer experience, the question you bring to the year’s survey is which regions got better, which got worse, and by how much. A table with last year’s score and this year’s score side by side makes you subtract in your head, one row at a time.

A slope chart answers it. Each region is one line from its score last year to its score this year, so a line that climbs is an improvement and a line that falls is a decline, and the steepest lines are the biggest moves. The chart above is bound to the grid beneath it, so filtering the rows redraws the lines. Filter the Region column to compare a handful of regions without the others in the way.

Why a slope chart and not a clustered bar chart

Two bars per region ask the eye to compare the height of one bar with its neighbour, fourteen times. A slope chart turns each pair into a single line, so the direction and the size of the change are the first thing you see, and the lines that cross show where one region overtook another.

The chart over a grid

The grid holds one row per region and period: the region, the period, and the score. Name the period column in x, the region column in series and the score in y, and each region becomes one line across the two periods. Slope is an opt-in chart type, so you import its module once after the charts module. Here is the example in plain JavaScript, with 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-slope';

const rows = [
  ['North', 31, 42], ['North East', 44, 38], ['East', 27, 35], ['South', 18, 33],
  ['West', 36, 28], ['North West', 47, 55], ['Central', 22, 24], ['Coastal', 58, 63],
].flatMap(([region, before, after]) => [
  { region, period: 'Last year', nps: before },
  { region, period: 'This year', nps: after },
]).map((r, i) => ({ id: i + 1, ...r }));

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: 'region', title: 'Region', filter: { type: 'set' } },
    { field: 'period', title: 'Period', filter: { type: 'set' } },
    { field: 'nps', title: 'NPS', type: 'number' },
  ],
});
createChart({ grid, container: chartEl, type: 'slope', x: 'period', series: 'region', y: 'nps',
  title: 'NPS by region, last year to this year' });

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-slope';

const LatticeGrid = createLatticeGrid({ React, createGrid });
const rows = [
  ['North', 31, 42], ['North East', 44, 38], ['East', 27, 35], ['South', 18, 33],
  ['West', 36, 28], ['North West', 47, 55], ['Central', 22, 24], ['Coastal', 58, 63],
].flatMap(([region, before, after]) => [
  { region, period: 'Last year', nps: before },
  { region, period: 'This year', nps: after },
]).map((r, i) => ({ id: i + 1, ...r }));
const columns = [
  { field: 'region', title: 'Region', filter: { type: 'set' } },
  { field: 'period', title: 'Period', filter: { type: 'set' } },
  { field: 'nps', title: 'NPS', type: 'number' },
];

export function NpsSlope() {
  const gridRef = useRef(null);
  const chartRef = useRef(null);
  useEffect(() => {
    const chart = createChart({ grid: gridRef.current.grid, container: chartRef.current,
      type: 'slope', x: 'period', series: 'region', y: 'nps',
      title: 'NPS by region, last year to this year' });
    return () => chart.destroy();
  }, []);
  return (<>
    <LatticeGrid ref={gridRef} rowKey="id" columns={columns} rows={rows} style={{ height: 200 }} />
    <div ref={chartRef} style={{ height: 360 }} />
  </>);
}

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 'slope' here, one line per series across the periods.
x string The category column, here the period.
y string The measure column, here the score.
series string Splits the measure into one series per distinct value, here one per region.
title string Drawn above the plot.

Reading the lines

Look first at the steepest lines, then at the lines that cross. In the example, the south and the valley gained the most, while the west and the islands lost the most. A flat line is a region that held its score. Filter the Region column to the regions you answer for, and clear the filter to see all of them again.

Next

See the slope chart beside the other ranking and change charts in the ranking and change 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

All posts RSS