1. Data Grid docs
  2. Columns and formatting

Sparklines and charts

Draw a small line, column or win/loss chart in each cell of a column, and chart a range of selected cells or a pivot as columns, bars, a line or a pie.

A sparkline is a chart the size of a cell: a price's last six months next to the price, a team's results next to its name. A quick chart is the larger look at a range: select the cells of a few records and see them as columns, bars, a line or a pie, without leaving the grid. Both are drawn in the theme's colours.

Open the live example: a Trend sparkline in every row

In the grid

  • Sparkline… in a column's menu turns the column's cells into small charts. Pick the Type (line, column or win/loss), then either keep the numbers written in the cell, such as "3, 5, 2", or tick the fields whose values each record draws. Remove sparkline turns the column back into text.
  • Chart the selected cells… in the context menu charts the selected range in a dialog. The categories come from the range's first text column (or the record numbers, when it has none) and each number column becomes a series. Chart type switches between column, bar, line and pie, and Download SVG saves the picture.
  • In a pivot, Chart offers the same four chart types for the pivot's largest values.
SparklineDraws
lineA line through the values, with the last point marked
columnA bar per value, negative ones below the axis
winlossAn equal bar per value: up for a gain, down for a loss

From code

Method or settingDoes
setColumn(field, { sparkline: { type, fields } })Draws the column as sparklines: of the values of fields in each record, or without fields, of the numbers in the cell's own text
setColumn(field, { sparkline: null })Removes the sparkline
chartRange()Opens the chart dialog for the selected cells
getChartData(field)A pivot column's top 20 values, as the pivot's chart draws them
openDialog('sparkline', { field })Opens the Sparkline dialog of a column

In the state, a column's sparkline is sparkline: { type, fields? } in its entry of columns (type defaults to 'line'), so it is saved with views like widths and formats. Without fields, a cell's text such as "3, 5, 2" gives the numbers; an array of numbers in data reads as that text, so { Trend: [3, 5, 2] } draws too.

JavaScript

import { mountTable } from '@8sheets/data-grid';

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
const grid = await mountTable(document.querySelector('#prices'), {
  title: 'Prices',
  data: prices, // { Symbol, Price, Jan … Jun, Trend: '', Results: [1, -1, 1, 1] }
});

// A line through each record's six month columns, in the Trend column.
await grid.setColumn('Trend', { sparkline: { type: 'line', fields: months } });
// Gains and losses written in the cell itself.
await grid.setColumn('Results', { sparkline: { type: 'winloss' } });

// Chart a range: Symbol gives the categories, Jan to Mar a series each.
await grid.setRange({ anchor: { id: 0, field: 'Symbol' }, focus: { id: 9, field: 'Mar' } });
await grid.chartRange();

React

import { useMemo, useRef } from 'react';
import { DataGrid } from '@8sheets/data-grid-react';

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];

export function Prices({ prices }) {
  const ref = useRef(null);
  const options = useMemo(() => ({ title: 'Prices', data: prices }), [prices]);
  return (
    <>
      <button onClick={() => ref.current.chartRange()}>Chart the selection</button>
      <div style={{ height: 480 }}>
        <DataGrid ref={ref} options={options} onReady={(grid) => grid.setColumn('Trend', { sparkline: { type: 'line', fields: months } })} />
      </div>
    </>
  );
}

Vue

<script setup>
import { computed, ref } from 'vue';
import { DataGrid } from '@8sheets/data-grid-vue';

const props = defineProps({ prices: Array });
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];
const options = computed(() => ({ title: 'Prices', data: props.prices }));
const dataGrid = ref(null);
const onReady = (grid) => grid.setColumn('Trend', { sparkline: { type: 'line', fields: months } });
</script>

<template>
  <button @click="dataGrid.chartRange()">Chart the selection</button>
  <div style="height: 480px"><DataGrid ref="dataGrid" :options="options" @ready="onReady" /></div>
</template>

Angular

import { Component, Input, ViewChild } from '@angular/core';
import { DataGridComponent, DataGridOptions, MountedDataGrid } from '@8sheets/data-grid-angular';

const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'];

@Component({
  selector: 'app-prices',
  imports: [DataGridComponent],
  template: `
    <button (click)="grid.chartRange()">Chart the selection</button>
    <eight-data-grid #grid [options]="options" (ready)="onReady($event)" style="height: 480px" />`,
})
export class Prices {
  @Input() options: DataGridOptions = {};
  @ViewChild('grid') grid!: DataGridComponent;

  onReady(grid: MountedDataGrid) {
    grid.setColumn('Trend', { sparkline: { type: 'line', fields: MONTHS } });
  }
}

The range chart needs at least one number column in the range; without one, the grid says "Select cells with numbers to chart them."

With a server source

A sparkline of the cell's own numbers draws from the values your rows already carry. A sparkline of other fields needs their values: give each record's row a series object, by the sparkline column's field, such as series: { Trend: [120, 134, 98, 141, 150, 162] }. tableQueryService in the source download fills it in.

Limits

  • A sparkline replaces the cell's text on screen. Copies, exports and the record dialog hold the cell's value, not the picture.
  • Sparklines and quick charts are pictures of the data, not a chart editor: no axes to format, no titles to edit. For charts you can format and save in a workbook, open the data in Spreadsheet.
  • The range chart covers the selected cells, which are drawn records: up to about a screenful of them.