Vue Chart - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

The DevExtreme Chart is an interactive UI component that visualizes data using multiple series types: lines, bars, bubbles, candlesticks, and more.

This tutorial explains how to add a Chart to a page and configure component core settings.

Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:

View on GitHub

Create a Chart

Add DevExtreme to your Vue application and use the following code snippet to create a Chart:

App.vue
  • <template>
  • <DxChart id="chart">
  • <!-- Configuration goes here -->
  • </DxChart>
  • </template>
  •  
  • <script setup lang="ts">
  • import 'devextreme/dist/css/dx.fluent.blue.light.css';
  • import DxChart from 'devextreme-vue/chart';
  • </script>

Display Chart Data

To bind a DevExtreme Chart to data, specify the dataSource property. Ensure that the data source contains items with at least two fields: an argument and a value. The Chart looks for fields named arg and val. If field names are different, assign these names to series.argumentField and series.valueField properties:

App.vue
  • <script setup lang="ts">
  •  
  • const chartData = [
  • { month: 'January', targetSpend: 12000 },
  • { month: 'February', targetSpend: 15500 },
  • { month: 'March', targetSpend: 13500 },
  • { month: 'April', targetSpend: 16000 },
  • { month: 'May', targetSpend: 17000 },
  • { month: 'June', targetSpend: 20000 },
  • { month: 'July', targetSpend: 14500 },
  • { month: 'August', targetSpend: 21000 },
  • { month: 'September', targetSpend: 19000 },
  • { month: 'October', targetSpend: 22000 },
  • { month: 'November', targetSpend: 16000 },
  • { month: 'December', targetSpend: 25000 },
  • ];
  •  
  • </script>
  •  
  • <template>
  • <div>
  • <DxChart :data-source="chartData" >
  • <DxSeries
  • argument-field="month"
  • value-field="targetSpend"
  • />
  • </DxChart>
  • </div>
  • </template>

Configure Chart Series

To configure how a Chart displays data, specify series object properties. This example implements the following properties: type, name, and color.

App.vue
  • <template>
  • <div>
  • <DxChart :data-source="chartData" >
  • <DxSeries
  • type="bar"
  • argument-field="month"
  • value-field="targetSpend"
  • name="Budget"
  • color="#5996ff"
  • />
  • </DxChart>
  • </div>
  • </template>

Specify Multiple Series

To configure Chart with multiple series, update your data source with additional value fields and specify additional <DxSeries> selectors. This example configures two series to display a spline and a bar chart:

App.vue
  • <script setup lang="ts">
  •  
  • const chartData = [
  • { month: 'January', targetSpend: 12000, actualSpend: 9500 },
  • { month: 'February', targetSpend: 15500, actualSpend: 16500 },
  • { month: 'March', targetSpend: 13500, actualSpend: 12000 },
  • { month: 'April', targetSpend: 16000, actualSpend: 14000 },
  • { month: 'May', targetSpend: 17000, actualSpend: 18500 },
  • { month: 'June', targetSpend: 20000, actualSpend: 17500 },
  • { month: 'July', targetSpend: 14500, actualSpend: 11000 },
  • { month: 'August', targetSpend: 21000, actualSpend: 22500 },
  • { month: 'September', targetSpend: 19000, actualSpend: 18000 },
  • { month: 'October', targetSpend: 22000, actualSpend: 25000 },
  • { month: 'November', targetSpend: 16000, actualSpend: 14500 },
  • { month: 'December', targetSpend: 25000, actualSpend: 27000 },
  • ];
  •  
  • </script>
  •  
  • <template>
  • <div>
  • <DxChart :data-source="chartData" >
  • <DxSeries
  • type="bar"
  • argument-field="month"
  • value-field="targetSpend"
  • name="Budget"
  • color="#5996ff"
  • />
  • <DxSeries
  • type="spline"
  • argument-field="month"
  • value-field="actualSpend"
  • name="Amount Spent"
  • color="#cb4bfa"
  • />
  • </DxChart>
  • </div>
  • </template>

Add and Customize Tooltips

To add tooltips to a Chart, assign true to the tooltip.enabled property. Default tooltips display values when users hover over points. To implement custom tooltips, define the customizeTooltip function. This example uses customizeTooltip to display the difference between current and average actualSpend values (calculated in the calculateAverageSpend() method):

App.vue
  • <script setup lang="ts">
  • import { formatNumber } from 'devextreme/localization';
  •  
  • function calculateAverageSpend() {
  • const sum = chartData.reduce((accumulator, { actualSpend }) => accumulator + actualSpend, 0);
  •  
  • return sum / chartData.length;
  • }
  •  
  • const averageSpend = ref(calculateAverageSpend());
  •  
  • function customizeTooltip(data: { value: number; seriesName: string }) {
  • if (data.seriesName === 'Budget') {
  • return { text: formatNumber(data.value, 'currency') };
  • }
  • const isValueAboveAverage = data.value > calculateAverageSpend();
  • if (isValueAboveAverage) {
  • return { text: `${formatNumber(data.value, 'currency')}\n${formatNumber(data.value - averageSpend.value, 'currency')} above average spending.` };
  • }
  •  
  • return { text: `${formatNumber(data.value, 'currency')}\n${formatNumber(averageSpend.value - data.value, 'currency')} below average spending.` };
  • }
  • </script>
  •  
  • <template>
  • <div>
  • <DxChart :data-source="chartData" >
  • <DxTooltip
  • :enabled="true"
  • :customize-tooltip="customizeTooltip"
  • />
  • </DxChart>
  • </div>
  • </template>

Specify a Custom Gradient Background

You can customize the appearance of Chart elements. To configure a background gradient, set commonPaneSettings.backgroundColor to a ChartsColor object. In this object, assign the registerGradient return value to fillId:

App.vue
  • <script setup lang="ts">
  • import { registerGradient } from 'devextreme-vue/common/charts';
  •  
  • const chartGradient = registerGradient('linear', {
  • colors: [{
  • offset: '20%',
  • color: '#dee4ff',
  • }, {
  • offset: '90%',
  • color: '#ffdeff',
  • }],
  • });
  • </script>
  •  
  • <template>
  • <div>
  • <DxChart :data-source="chartData" >
  • <DxCommonPaneSettings>
  • <DxBackgroundColor
  • base="#ffffff"
  • :fill-id="chartGradient"
  • />
  • </DxCommonPaneSettings>
  • </DxChart>
  • </div>
  • </template>

Add a Constant Line

DevExtreme Chart can display horizontal or vertical constant lines as points of reference. The following example uses the valueAxis.constantLines property to display a horizontal constant line that indicates the yearly spending average (averageSpend):

App.vue
  • <template>
  • <div>
  • <DxChart :data-source="chartData" >
  • <DxValueAxis>
  • <DxConstantLine
  • :value="averageSpend"
  • color="#0000c7"
  • >
  • <DxLabel
  • text="Yearly Spend Average"
  • />
  • </DxConstantLine>
  • </DxValueAxis>
  • </DxChart>
  • </div>
  • </template>