component.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { copyObject } from '../utils/tools'
  2. const generateVueComponent = function (Highcharts) {
  3. return {
  4. template: '<div ref="chart"></div>',
  5. render: createElement => createElement('div', {
  6. ref: 'chart'
  7. }),
  8. props: {
  9. constructorType: {
  10. type: String,
  11. default: 'chart'
  12. },
  13. options: {
  14. type: Object,
  15. required: true
  16. },
  17. callback: Function,
  18. updateArgs: {
  19. type: Array,
  20. default: () => [true, true]
  21. },
  22. highcharts: {
  23. type: Object
  24. },
  25. deepCopyOnUpdate: {
  26. type: Boolean,
  27. default: true
  28. }
  29. },
  30. watch: {
  31. options: {
  32. handler (newValue) {
  33. this.chart.update(copyObject(newValue, this.deepCopyOnUpdate), ...this.updateArgs)
  34. },
  35. deep: true
  36. }
  37. },
  38. mounted () {
  39. let HC = this.highcharts || Highcharts
  40. // Check wheather the chart configuration object is passed, as well as the constructor is valid
  41. if (this.options && HC[this.constructorType]) {
  42. this.chart = HC[this.constructorType](
  43. this.$refs.chart,
  44. copyObject(this.options, true), // Always pass the deep copy when generating a chart. #80
  45. this.callback ? this.callback : null
  46. )
  47. } else {
  48. (!this.options) ? console.warn('The "options" parameter was not passed.') : console.warn(`'${this.constructorType}' constructor-type is incorrect. Sometimes this error is caused by the fact, that the corresponding module wasn't imported.`)
  49. }
  50. },
  51. beforeDestroy () {
  52. // Destroy chart if exists
  53. if (this.chart) {
  54. this.chart.destroy()
  55. }
  56. }
  57. }
  58. }
  59. export default generateVueComponent