---
title: "Sparklines - Sparkline Data"
enterprise: true
framework: vue
version: "36.1.0"
---

# Sparklines - Sparkline Data

This section starts off by comparing the different supported data formats before discussing how data can be formatted using a [Value Getter](https://www.ag-grid.com/vue-data-grid/value-getters/) for sparklines and then shows how data updates are handled.

## Supported Data Formats

Sparklines are configured on a per-column basis and are supplied data based on their column configuration, just like any other grid cell, i.e. columns are configured with a `field` attribute or [Value Getter](https://www.ag-grid.com/vue-data-grid/value-getters/).

The data supplied to sparklines can be in the following formats:

- [Array of Numbers](https://www.ag-grid.com/vue-data-grid/sparklines-data/#array-of-numbers)
- [Array of Tuples](https://www.ag-grid.com/vue-data-grid/sparklines-data/#array-of-tuples)
- [Array of Objects](https://www.ag-grid.com/vue-data-grid/sparklines-data/#array-of-objects)

In each of the formats above, Y values must be of type `number`, whereas X values can be a `number`, `string`, `Date` or objects with a `toString` method, if they are provided.

It may be necessary to [Format Sparkline Data](https://www.ag-grid.com/vue-data-grid/sparklines-data/#formatting-sparkline-data) using Value Getters if the data supplied to the grid is not in the correct format.

### Array of Numbers

The simplest data format supported by the sparkline is the `number[]` format. This does not require any further configuration, simply provide the array of numbers to the grid for that specific field.

Alternatively, a `valueGetter` can be added to return an array of numbers for each cell in the sparkline column.

- The numbers in the data array correspond to Y values.
- The X value for each data point will be the index of the value in the data array. For this reason, the data points will be evenly spaced out along the width of the sparkline.
- Note that the data for the `rateOfChange` field in the data.js file is a `number[]`.

#### Sparkline Data - Array of Numbers

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AgChartsCommunityModule,
  AgSparklineOptions,
} from "ag-charts-community";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
import {
  ClipboardModule,
  ContextMenuModule,
  SparklinesModule,
} from "ag-grid-enterprise";
import { getStockData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  SparklinesModule.with(AgChartsCommunityModule),
  ClipboardModule,
  ContextMenuModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :rowData="rowData"
      :rowHeight="rowHeight"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "symbol", maxWidth: 110 },
      { field: "name", minWidth: 250 },
      {
        field: "rateOfChange",
        cellRenderer: "agSparklineCellRenderer",
        cellRendererParams: {
          sparklineOptions: {
            type: "area",
          } as AgSparklineOptions,
        },
      },
      { field: "volume", maxWidth: 140 },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
    });
    const rowData = ref<any[] | null>(getStockData());
    const rowHeight = ref(50);

    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      rowData,
      rowHeight,
      onGridReady,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Sparkline Data - Array of Numbers](https://www.ag-grid.com/examples/sparklines-data/sparkline-data-number-array/vue3)

### Array of Tuples

Another supported format is the tuples array. In this format, each tuple in the array can contain two values, X and Y.

- At index 0 will be the X value and index 1, the Y value.
- The Y value should be a `number` whereas the X can be a `number`, `string`, `Date` or an object with a `toString` method.
- The `rateOfChange` field is of type `[Date, number][]`, where X values are `Date` objects.

#### Sparkline Data - Array of Tuples

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AgChartsCommunityModule,
  AgSparklineOptions,
} from "ag-charts-community";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
import {
  ClipboardModule,
  ContextMenuModule,
  SparklinesModule,
} from "ag-grid-enterprise";
import { getStockData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  SparklinesModule.with(AgChartsCommunityModule),
  ClipboardModule,
  ContextMenuModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :rowData="rowData"
      :rowHeight="rowHeight"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "symbol", maxWidth: 110 },
      { field: "name", minWidth: 250 },
      {
        field: "rateOfChange",
        headerName: "Rate of Change",
        cellRenderer: "agSparklineCellRenderer",
        cellRendererParams: {
          sparklineOptions: {
            type: "area",
            axis: {
              type: "time",
            },
            marker: {
              size: 3,
            },
          } as AgSparklineOptions,
        },
      },
      { field: "volume", maxWidth: 140 },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
    });
    const rowData = ref<any[] | null>(getStockData());
    const rowHeight = ref(50);

    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      rowData,
      rowHeight,
      onGridReady,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Sparkline Data - Array of Tuples](https://www.ag-grid.com/examples/sparklines-data/sparkline-data-tuple-array/vue3)

### Array of Objects

The sparkline also supports an array of objects as a data format. This requires setting the `xKey` and `yKey` properties in the `sparklineOptions` to the corresponding property names in the objects you’re providing, as shown in the code snippet below:

```ts
<ag-grid-vue
    :columnDefs="columnDefs"
    /* other grid options ... */>
</ag-grid-vue>

this.columnDefs = [
    {
        field: 'rateOfChange',
        cellRenderer: 'agSparklineCellRenderer',
        cellRendererParams: {
            sparklineOptions: {
                type: 'line',
                // set xKey and yKey to the keys which can be used to retrieve X and Y values from the supplied data
                xKey: 'xVal',
                yKey: 'yVal',
            }
        },
    },
    // other column definitions ...
];
```

Note in the example below:

- The data is an array of objects with the `xVal` and `yVal` keys.
- `xKey` and `yKey` can be any `string` value as long as they are specified in the options.
- By default, the `xKey` and `yKey` are `'x'` and `'y'` respectively, so data objects with `'x'` and `'y'` keys would work fine without further configuration.

#### Sparkline Data - Array of Objects

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AgChartsCommunityModule,
  AgSparklineOptions,
} from "ag-charts-community";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
import {
  ClipboardModule,
  ContextMenuModule,
  SparklinesModule,
} from "ag-grid-enterprise";
import { getStockData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  SparklinesModule.with(AgChartsCommunityModule),
  ClipboardModule,
  ContextMenuModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :rowData="rowData"
      :rowHeight="rowHeight"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "symbol", maxWidth: 110 },
      { field: "name", minWidth: 250 },
      {
        field: "rateOfChange",
        cellRenderer: "agSparklineCellRenderer",
        cellRendererParams: {
          sparklineOptions: {
            type: "bar",
            direction: "vertical",
            xKey: "xVal",
            yKey: "yVal",
            axis: {
              type: "number",
            },
          } as AgSparklineOptions,
        },
      },
      { field: "volume", maxWidth: 140 },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
    });
    const rowData = ref<any[] | null>(getStockData());
    const rowHeight = ref(50);

    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      rowData,
      rowHeight,
      onGridReady,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Sparkline Data - Array of Objects](https://www.ag-grid.com/examples/sparklines-data/sparkline-data-object-array/vue3)

## Formatting Sparkline Data

If the data is not already in the required format, it is possible to provide `valueGetter` in the column definitions to format and supply data to the sparkline column.

The formatted data from `valueGetter` will be supplied to the sparkline automatically by `agSparklineCellRenderer`.

The following example demonstrates how data can be formatted using `valueGetter`.

- In this example, the data for the `rateOfChange` field is an object with `x` and `y` keys, both containing an array of numbers.
- A `valueGetter` is used to format this data into `[number, number][]`, entering the values for X and Y at each index in two arrays for the `rateOfChange` object.

#### Formatting Sparkline Data

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import {
  AgChartsCommunityModule,
  AgSparklineOptions,
} from "ag-charts-community";
import {
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  ValueGetterParams,
  enableDevValidations,
} from "ag-grid-community";
import {
  ClipboardModule,
  ContextMenuModule,
  SparklinesModule,
} from "ag-grid-enterprise";
import { getData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  SparklinesModule.with(AgChartsCommunityModule),
  ClipboardModule,
  ContextMenuModule,
]);

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <ag-grid-vue
      style="width: 100%; height: 100%;"
      @grid-ready="onGridReady"
      :columnDefs="columnDefs"
      :defaultColDef="defaultColDef"
      :rowData="rowData"
      :rowHeight="rowHeight"></ag-grid-vue>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "symbol", maxWidth: 110 },
      { field: "name", minWidth: 250 },
      {
        headerName: "Rate of Change",
        cellRenderer: "agSparklineCellRenderer",
        cellRendererParams: {
          sparklineOptions: {
            type: "area",
          } as AgSparklineOptions,
        },
        valueGetter: (params: ValueGetterParams) => {
          const formattedData: any = [];
          const rateOfChange = params.data.rateOfChange;
          const { x, y } = rateOfChange;
          x.map((xVal: any, i: number) => formattedData.push([xVal, y[i]]));
          return formattedData;
        },
      },
      { field: "volume", maxWidth: 140 },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
    });
    const rowData = ref<any[] | null>(getData());
    const rowHeight = ref(50);

    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      rowData,
      rowHeight,
      onGridReady,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Formatting Sparkline Data](https://www.ag-grid.com/examples/sparklines-data/formatting-sparkline-data/vue3)

## Missing Data Points

Missing or invalid X and Y values need to be handled differently and are described in the following sections:

### Missing Y values

If the Y value of the data point is `Infinity`, `null`, `undefined`, `NaN`, a `string` or an `object`, i.e. if it's not a `number`, it will be classified as missing or invalid.

```js
// Missing Y Values
const data = [
    0.17,
    0.20,
    undefined,
    0.39,
    0.26,
    null,
    0.68,
    0.28
];
```

When a data point has a missing or invalid Y value, it will be rendered as a gap, this is illustrated in the images below:

![Line sparkline.](https://www.ag-grid.com/_astro/line-sparkline.CV0lG2nH.png)

No missing Y values

![Line sparkline with gaps for invalid Y values.](https://www.ag-grid.com/_astro/line-sparkline-invalid-y-values.B5xpVxiK.png)

Missing Y values

![Column Sparkline](https://www.ag-grid.com/_astro/column-sparkline.B-szrnhm.png)

No missing Y values

![Column sparkline with gaps for invalid Y values](https://www.ag-grid.com/_astro/column-sparkline-invalid-y-values.DRq7r8dr.png)

Missing Y values

![Area Sparkline](https://www.ag-grid.com/_astro/area-sparkline.DUkU_XCB.png)

No missing Y values

![Area sparkline with gaps for invalid Y values](https://www.ag-grid.com/_astro/area-sparkline-invalid-y-values.3bKq8LcQ.png)

Missing Y values

### Missing X values

If X values are supplied in the sparkline data but are inconsistent with the configured [axis type](https://www.ag-grid.com/vue-data-grid/sparklines-axis-types/), they are considered invalid and will be skipped in the sparkline.

There won't be any gaps, only the data points with valid x values will appear in the sparklines.

For example if the axis is configured to be a [Number Axis](https://www.ag-grid.com/vue-data-grid/sparklines-axis-types/#number-axis), but some data points have X values which are not of type `number`, these values will be considered invalid and will be ignored when the sparkline is rendered.

```js
// Missing X Values
const data = [
    [2.1, 0.17],
    [2.3, 0.202],
    [undefined, 0.28],
    [2.9, 0.39],
    [3.3, 0.26],
    [null, 0.41],
    [3.9, 0.68],
    [4.3, 0.28],
];
```

The following images show the line, column and area sparklines with 8 complete data points on the left, and on the right, with 6 valid X values and 2 invalid X values:

![Line sparkline.](https://www.ag-grid.com/_astro/line-sparkline.CV0lG2nH.png)

No missing X values

![Line sparkline with gaps for invalid Y values.](https://www.ag-grid.com/_astro/line-sparkline-invalid-x-values.t1nJuX51.png)

Missing X values

![Column Sparkline](https://www.ag-grid.com/_astro/column-sparkline.B-szrnhm.png)

No missing X values

![Column sparkline with gaps for invalid Y values](https://www.ag-grid.com/_astro/column-sparkline-invalid-x-values.Ch_tz1SY.png)

Missing X values

![Area Sparkline](https://www.ag-grid.com/_astro/area-sparkline.DUkU_XCB.png)

No missing X values

![Area sparkline with gaps for invalid Y values](https://www.ag-grid.com/_astro/area-sparkline-invalid-x-values.BMLsRshc.png)

Missing X values

## Updating Sparkline Data

Updating Sparkline data is no different from updating any other cell data, for more details see [Updating Data](https://www.ag-grid.com/vue-data-grid/data-update/).

The following example demonstrates Sparkline data updates using the [Transaction Update API](https://www.ag-grid.com/vue-data-grid/data-update-transactions/#transaction-update-api).

#### Sparkline Data Updates

```ts
import {
  createApp,
  defineComponent,
  onBeforeMount,
  ref,
  shallowRef,
} from "vue";
import { AgGridVue } from "ag-grid-vue3";
import { AgChartsCommunityModule } from "ag-charts-community";
import {
  ClientSideRowModelApiModule,
  ClientSideRowModelModule,
  ColDef,
  ColGroupDef,
  GridApi,
  GridOptions,
  GridReadyEvent,
  ModuleRegistry,
  enableDevValidations,
} from "ag-grid-community";
import {
  ClipboardModule,
  ContextMenuModule,
  SparklinesModule,
} from "ag-grid-enterprise";
import { getData } from "./data";
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelApiModule,
  ClientSideRowModelModule,
  SparklinesModule.with(AgChartsCommunityModule),
  ClipboardModule,
  ContextMenuModule,
]);

let intervalId: any;

const VueExample = defineComponent({
  template: `
        <div style="height: 100%">
                <div style="height: 100%; display: flex; flex-direction: column">
      <div style="margin-bottom: 4px">
        <button v-on:click="start()">► Start</button>
        <button v-on:click="stop()">■ Stop</button>
      </div>
      <div style="flex-grow: 1">
        <ag-grid-vue
          style="width: 100%; height: 100%;"
          @grid-ready="onGridReady"
          :columnDefs="columnDefs"
          :defaultColDef="defaultColDef"
          :rowData="rowData"
          :rowHeight="rowHeight"></ag-grid-vue>
        </div>
      </div>
        </div>
    `,
  components: {
    "ag-grid-vue": AgGridVue,
  },
  setup(props) {
    const gridApi = shallowRef<GridApi | null>(null);
    const columnDefs = ref<ColDef[]>([
      { field: "symbol", maxWidth: 120 },
      { field: "name", minWidth: 250 },
      {
        field: "change",
        cellRenderer: "agSparklineCellRenderer",
      },
      {
        field: "volume",
        maxWidth: 140,
      },
    ]);
    const defaultColDef = ref<ColDef>({
      flex: 1,
      minWidth: 100,
    });
    const rowData = ref<any[] | null>(getData());
    const rowHeight = ref(50);

    function start() {
      if (intervalId) {
        return;
      }
      const updateData = () => {
        const itemsToUpdate: any[] = [];
        gridApi.value!.forEachNodeAfterFilterAndSort(function (rowNode) {
          const data = rowNode.data;
          if (!data) {
            return;
          }
          const n = data.change.length;
          const v =
            window.agRandom() > 0.5
              ? Number(window.agRandom())
              : -Number(window.agRandom());
          data.change = [...data.change.slice(1, n), v];
          itemsToUpdate.push(data);
        });
        gridApi.value!.applyTransaction({ update: itemsToUpdate });
      };
      intervalId = setInterval(updateData, 300);
    }
    function stop() {
      if (intervalId === undefined) {
        return;
      }
      clearInterval(intervalId);
      intervalId = undefined;
    }
    const onGridReady = (params: GridReadyEvent) => {
      gridApi.value = params.api;
    };

    return {
      gridApi,
      columnDefs,
      defaultColDef,
      rowData,
      rowHeight,
      onGridReady,
      start,
      stop,
    };
  },
});

const app = createApp(VueExample);
app.mount("#app");
```

[Live example: Sparkline Data Updates](https://www.ag-grid.com/examples/sparklines-data/sparkline-data-updates/vue3)
