|
1 | 1 | # Vue 3 - Highcharts JS |
2 | 2 |
|
3 | | -A simple Vue 3 component for rendering Highcharts.js Charts. |
| 3 | +A simple, fast, Vue 3 component for rendering Highcharts.js Charts written using the composition API. |
| 4 | + |
| 5 | +## Minimum Requirements |
| 6 | + |
| 7 | + |
| 8 | +[email protected] ( older versions may work but not tested ) |
| 9 | + |
| 10 | +Vue and Highcharts are not bundled with the module and need to be included in your projects dependencies. |
| 11 | + |
| 12 | +## Usage |
| 13 | +You can register the component in 2 ways. |
| 14 | + |
| 15 | +### Register as a global component. |
| 16 | +```js |
| 17 | +import { createApp } from 'vue'; |
| 18 | +import VueHighcharts from 'vue3-highcharts'; |
| 19 | + |
| 20 | +const app = createApp(..options); |
| 21 | +app.use(VueHighcharts); |
| 22 | +``` |
| 23 | + |
| 24 | +### Register as a local component |
| 25 | +```js |
| 26 | +import VueHighcharts from 'vue3-highcharts'; |
| 27 | + |
| 28 | +export default { |
| 29 | + name: 'MyChart', |
| 30 | + components: { |
| 31 | + VueHighcharts, |
| 32 | + }, |
| 33 | +}; |
| 34 | +``` |
| 35 | + |
| 36 | +### Props |
| 37 | +The following props can be passed to the component. Options is the only required one. |
| 38 | + |
| 39 | +| Name | Type | Required | Default | Notes | |
| 40 | +|---------|-------|------| ------- | ----- | |
| 41 | +| type | String | no | 'chart' | This should be the constructor type you'd use with highcharts. If you import the stock chat, you can pass 'stockChart' as this option for example. |
| 42 | +options | Object | yes | - |This should be a [Highcharts chart options](https://api.highcharts.com/highcharts/) object |
| 43 | +redrawOnUpdate | Boolean | no | true | If the chart should be redrawn when options update. |
| 44 | +oneToOneUpdate | Boolean | no | false| If the certain collections should be updated one to one. See [here](https://api.highcharts.com/class-reference/Highcharts.Chart#update). |
| 45 | +animateOnUpdate | Boolean | no | true | If the redraw should be animated. |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +### Events |
| 50 | +The following events are emitted from the component. No data is provided with any event. |
| 51 | +| Name | Notes | |
| 52 | +| ---- | ----- | |
| 53 | +| rendered | Emitted when the chart has been rendered on the dom | |
| 54 | +| updated | Emitted when the chart options have been updated and the chart has been updated | |
| 55 | +| destroyed | Emitted when the Highcharts chart instance has been destroyed ( happens when the component unmounts ) |
| 56 | + |
| 57 | + |
| 58 | +--- |
| 59 | +--- |
| 60 | +The Highcharts chart object is also exposed on the component instance as `chart` |
| 61 | + |
| 62 | +A wrapper div is also created with a `.vue-highcharts` class around the actual chart. |
| 63 | + |
| 64 | +### Simple Example |
| 65 | + |
| 66 | +```html |
| 67 | +<template> |
| 68 | + <vue-highcharts |
| 69 | + type="chart" |
| 70 | + :options="chartOptions" |
| 71 | + :redrawOnUpdate="true" |
| 72 | + :oneToOneUpdate="false" |
| 73 | + :animateOnUpdate="true" |
| 74 | + @rendered="onRender" |
| 75 | + @update="onUpdate" |
| 76 | + @destroy="onDestroy"/> |
| 77 | +<template> |
| 78 | +``` |
| 79 | +```js |
| 80 | +<script> |
| 81 | +import { ref } from 'vue'; |
| 82 | + |
| 83 | +export default { |
| 84 | + name: 'chart' |
| 85 | + setup() { |
| 86 | + const data = ref([1, 2, 3]) |
| 87 | + const chartData = computed(() =>{ |
| 88 | + return { |
| 89 | + series: [{ |
| 90 | + name: 'Test Series', |
| 91 | + data: data.value, |
| 92 | + }], |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + const onRender = () => { |
| 97 | + console.log('Chart rendered'); |
| 98 | + }; |
| 99 | + |
| 100 | + const onUpdate = () => { |
| 101 | + console.log('Chart updated'); |
| 102 | + }; |
| 103 | + |
| 104 | + const onDestroy = () => { |
| 105 | + console.log('Chart destroyed'); |
| 106 | + }; |
| 107 | + |
| 108 | + return { |
| 109 | + chartData, |
| 110 | + onRender, |
| 111 | + onUpdate, |
| 112 | + onDestroy, |
| 113 | + }; |
| 114 | + } |
| 115 | +} |
| 116 | +</script> |
| 117 | +``` |
| 118 | +```css |
| 119 | +<style> |
| 120 | +.vue-highcharts { |
| 121 | + width: 100%; |
| 122 | +} |
| 123 | +</style> |
| 124 | +``` |
| 125 | + |
| 126 | +### Using Stock/Map charts |
| 127 | +To use the stock map charts, you need to import and register them. For example, to use the map chart |
| 128 | + |
| 129 | +``` |
| 130 | +import HighCharts from 'highcharts'; |
| 131 | +import useMapCharts from 'highcharts/modules/map'; |
| 132 | + |
| 133 | +useMapCharts(HighCharts); |
| 134 | +``` |
0 commit comments