按照 安装、基础用法、响应式更新、事件、模块、TypeScript、SSR/Next.js、常见问题 这些实际开发场景来讲,尽量做到可以直接上手。

Highcharts 可以在 Vue 中正常使用,常见方式有两种:
highcharts-vue 包
如果你是做普通业务图表,建议优先使用 highcharts-vue。
highcharts-vue介绍: highcharts-vue 是 Highcharts 官方适配 Vue 框架的可视化封装包,现已完整兼容 Vue3,同时向下兼容 Vue2,可在 Vue 项目快速接入全套 Highcharts 可视化能力,统一 Vue 响应式开发逻辑,无需手动操作原生 DOM 图表实例。
复制代码npm install highcharts highcharts-vue
如果你需要 Stock、Maps、Gantt 等产品,再安装对应包或模块。
highcharts-vuemain.js / main.ts 复制代码import { createApp } from 'vue';
import App from './App.vue';
import HighchartsVue from 'highcharts-vue';createApp(App)
.use(HighchartsVue)
.mount('#app');
App.vue 复制代码<script setup>
import { ref } from 'vue';
import Highcharts from 'highcharts';const chartOptions = ref({
title: {
text: 'Vue + Highcharts 示例'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May']
},
yAxis: {
title: {
text: '数值'
}
},
series: [
{
type: 'line',
name: '销量',
data: [1, 3, 2, 4, 5]
}
]
});const highcharts = Highcharts;
</script><template>
<highcharts
:highcharts="highcharts"
:options="chartOptions"
/>
</template>
highcharts-vue 会监听 options 变化并更新图表。推荐使用 ref 或 reactive。
复制代码<script setup>
import { ref } from 'vue';
import Highcharts from 'highcharts';const highcharts = Highcharts;const chartOptions = ref({
title: { text: '动态更新示例' },
xAxis: { categories: ['A', 'B', 'C', 'D'] },
series: [
{
type: 'column',
name: 'Series 1',
data: [2, 4, 1, 7]
}
]
});function updateData() {
chartOptions.value = {
...chartOptions.value,
series: [
{
type: 'column',
name: 'Series 1',
data: [5, 3, 6, 2]
}
]
};
}
</script><template>
<button @click="updateData">更新数据</button>
<highcharts :highcharts="highcharts" :options="chartOptions" />
</template>
如果你在高频刷新数据场景,建议直接拿到 chart 实例更新,而不是频繁重建 options。
复制代码<script setup>
import { ref } from 'vue';
import Highcharts from 'highcharts';const highcharts = Highcharts;
const chartComponentRef = ref(null);function updateSeries() {
const chart = chartComponentRef.value.chart;
chart.series[0].setData([10, 12, 8, 15]);
}
</script><template>
<button @click="updateSeries">更新 series</button>
<highcharts
ref="chartComponentRef"
:highcharts="highcharts"
:options="{
title: { text: '实例更新' },
series: [{ type: 'line', data: [1, 2, 3, 4] }]
}"
/>
</template>
title.text
复制代码title: {
text: '我的图表'
}
复制代码xAxis: {
categories: ['Mon', 'Tue', 'Wed']
},
yAxis: {
title: {
text: '数量'
}
}
复制代码series: [
{
type: 'line',
name: '访问量',
data: [1, 3, 2, 4]
}
]
复制代码tooltip: {
shared: true,
valueSuffix: ' 件'
}
复制代码plotOptions: {
series: {
dataLabels: {
enabled: true
}
}
}
Highcharts 支持很多图表类型,只要 series 里指定 type 即可。
复制代码series: [
{
type: 'line',
data: [1, 2, 3, 4]
}
]
复制代码series: [
{
type: 'column',
data: [3, 5, 2, 4]
}
]
复制代码series: [
{
type: 'pie',
data: [
['A', 30],
['B', 50],
['C', 20]
]
}
]
很多功能需要额外模块,比如:
复制代码import Highcharts from 'highcharts';
import Exporting from 'highcharts/modules/exporting';
import Accessibility from 'highcharts/modules/accessibility';Exporting(Highcharts);
Accessibility(Highcharts);
然后再把 Highcharts 传给 Vue 组件。
在 Vue + TS 中,建议显式给 options 标类型。
复制代码import type { Options } from 'highcharts';const chartOptions: Options = {
title: {
text: 'TypeScript 图表'
},
series: [
{
type: 'line',
data: [1, 2, 3]
}
]
};
如果你项目里类型报错,通常是:
highchartsseries.type 和数据结构不匹配 复制代码<script setup>
import { ref } from 'vue';
import Highcharts from 'highcharts';const highcharts = Highcharts;const options = ref({
chart: {
type: 'area'
},
title: {
text: '组合式 API 示例'
},
series: [
{
type: 'area',
data: [1, 2, 1, 4, 3]
}
]
});
</script><template>
<highcharts :highcharts="highcharts" :options="options" />
</template>
如果你还在用 Vue 2,highcharts-vue 也支持,但要注意:
如果你是新项目,建议直接上 Vue 3。
Highcharts 依赖浏览器环境,因此在 SSR 环境里要避免在服务端直接执行图表初始化。
window 在服务端直接访问client-onlymounted() 后再创建实例 复制代码tooltip: {
formatter() {
return `<b>${this.series.name}</b><br/>${this.x}: ${this.y}`;
}
}
注意:如果内容来自用户输入,请先做清理,避免 XSS。
如果数据量较大,建议:
optionssetData() 替代整体替换配置