Vue UI Components Documentation
This tutorial adds the jQWidgets Vue Chart to a Vue 3.5 and TypeScript project created with create-vue. The chart shows a weekly exercise scorecard, and a drop-down switches it between a line chart and a column chart.
Earlier versions of this page used Vue CLI, which is in maintenance mode; create-vue and Vite replace it.
Answer Yes to TypeScript when create-vue asks, then install jQWidgets:
npm create vue@latest my-app
cd my-app
npm install
npm install jqwidgets-scripts
Add one setting to vite.config.ts, so that npm run build does not stop with Unexpected token Semicolon: some jQWidgets themes still contain Internet Explorer CSS hacks (*zoom: 1), which Vite's CSS minifier rejects, and errorRecovery drops those lines instead of stopping the build. Keep the rest of the file create-vue wrote:
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
// Some jQWidgets themes still contain Internet Explorer CSS hacks (*zoom: 1), which Vite's
// CSS minifier rejects: errorRecovery drops those lines instead of stopping the build.
css: { lightningcss: { errorRecovery: true } },
})
An app that also uses the Grid, Calendar, DateTimeInput, Form or Scheduler needs one more addition for npm run dev, shown in Add a Grid to a Vue 3 app.
Replace src/main.ts:
import { createApp } from 'vue';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.base.css';
import 'jqwidgets-scripts/jqwidgets/styles/jqx.tailwind-blue-dark.css';
import App from './App.vue';
createApp(App).mount('#app');
The Vue components are plain JavaScript files, so TypeScript's strict mode rejects importing them (TS7016: Could not find a declaration file). Declare them once in a new file, src/jqwidgets-vue3.d.ts:
// src/jqwidgets-vue3.d.ts
declare module 'jqwidgets-scripts/jqwidgets-vue3/*.vue' {
import type { DefineComponent } from 'vue';
const component: DefineComponent<Record<string, any>, Record<string, any>, any>;
export default component;
}
A chart needs the data (source), the category axis (xAxis, which field holds the labels) and one or more seriesGroups (which fields to draw and how). Replace src/App.vue:
<script setup lang="ts">
import { computed, ref } from 'vue';
import JqxChart from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxchart.vue';
const sampleData = [
{ Day: 'Monday', Running: 30, Swimming: 10, Cycling: 25 },
{ Day: 'Tuesday', Running: 25, Swimming: 15, Cycling: 10 },
{ Day: 'Wednesday', Running: 30, Swimming: 10, Cycling: 25 },
{ Day: 'Thursday', Running: 40, Swimming: 20, Cycling: 25 },
{ Day: 'Friday', Running: 45, Swimming: 20, Cycling: 25 },
{ Day: 'Saturday', Running: 30, Swimming: 20, Cycling: 30 },
{ Day: 'Sunday', Running: 20, Swimming: 30, Cycling: 10 }
];
const padding = { left: 10, top: 10, right: 15, bottom: 10 };
const titlePadding = { left: 90, top: 0, right: 0, bottom: 10 };
const xAxis = {
dataField: 'Day',
unitInterval: 1,
tickMarks: { visible: true, interval: 1 },
valuesOnTicks: false
};
const valueAxis = {
minValue: 0,
maxValue: 50,
unitInterval: 10,
title: { text: 'Time in minutes' },
labels: { horizontalAlignment: 'right' }
};
const seriesType = ref<'line' | 'column'>('line');
// Recomputed when the type changes; the chart redraws with the new groups.
const seriesGroups = computed(() => [
{
type: seriesType.value,
series: [
{ dataField: 'Running', displayText: 'Running', symbolType: 'circle' },
{ dataField: 'Swimming', displayText: 'Swimming', symbolType: 'square' },
{ dataField: 'Cycling', displayText: 'Cycling', symbolType: 'diamond' }
]
}
]);
</script>
<template>
<main>
<label>
Chart type
<select v-model="seriesType">
<option value="line">Line</option>
<option value="column">Column</option>
</select>
</label>
<JqxChart
class="chart"
title="Fitness & exercise weekly scorecard"
description="Time spent in vigorous exercise by activity"
colorScheme="scheme05"
:showLegend="true"
:enableAnimations="true"
:padding="padding"
:titlePadding="titlePadding"
:source="sampleData"
:xAxis="xAxis"
:valueAxis="valueAxis"
:seriesGroups="seriesGroups"
/>
</main>
</template>
<style scoped>
.chart {
width: 850px;
height: 500px;
}
</style>
The chart takes its size from CSS, here the chart class. A plain array works as source; use a jqx.dataAdapter when the data comes from a URL, as JSON, CSV or XML.
npm run dev
Open http://localhost:5173. Hover a point to see its tooltip, click a legend item to hide a series, and change the chart type in the drop-down.
Listen to chart events with @. The click event fires when the user clicks a data point, and event.args tells you which one:
<script setup lang="ts">
import { ref } from 'vue';
import JqxChart from 'jqwidgets-scripts/jqwidgets-vue3/vue_jqxchart.vue';
const sampleData = [
{ Month: 'Jan', Sales: 120 },
{ Month: 'Feb', Sales: 150 },
{ Month: 'Mar', Sales: 180 }
];
const xAxis = { dataField: 'Month' };
const seriesGroups = [
{ type: 'column', series: [{ dataField: 'Sales', displayText: 'Sales' }] }
];
const message = ref('Click a column');
function onChartClick(event: any) {
// A click on the chart background has no args.
if (!event.args) {
return;
}
const item = sampleData[event.args.elementIndex];
message.value = `${item.Month}: ${item.Sales}`;
}
</script>
<template>
<JqxChart
class="chart"
title="Sales"
description="First quarter"
:source="sampleData"
:xAxis="xAxis"
:seriesGroups="seriesGroups"
@click="onChartClick"
/>
<p>{{ message }}</p>
</template>
<style scoped>
.chart {
width: 500px;
height: 300px;
}
</style>
Next: Add a Grid to a Vue 3 app, Chart getting started, Chart API. Live Chart demos: jqwidgets.com/vue.