chart.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. export default {
  2. template: "<div></div>",
  3. mounted() {
  4. setTimeout(() => {
  5. const imports = this.extras.map((extra) => import(window.path_prefix + extra));
  6. Promise.allSettled(imports).then(() => {
  7. this.seriesCount = this.options.series ? this.options.series.length : 0;
  8. this.options.plotOptions = this.options.plotOptions ?? {};
  9. this.options.plotOptions.series = this.options.plotOptions.series ?? {};
  10. this.options.plotOptions.series.point = this.options.plotOptions.series.point ?? {};
  11. this.options.plotOptions.series.point.events = this.options.plotOptions.series.point.events ?? {};
  12. function uncycle(e) {
  13. // Highcharts events are cyclic, so we need to uncycle them
  14. let { point, target, ...rest } = e;
  15. point = point ?? target;
  16. return {
  17. ...rest,
  18. point_index: point?.index,
  19. point_x: point?.x,
  20. point_y: point?.y,
  21. series_index: point?.series?.index,
  22. };
  23. }
  24. this.options.plotOptions.series.point.events.click = (e) => this.$emit("pointClick", uncycle(e));
  25. this.options.plotOptions.series.point.events.dragStart = (e) => this.$emit("pointDragStart", uncycle(e));
  26. this.options.plotOptions.series.point.events.drag = (e) => this.$emit("pointDrag", uncycle(e));
  27. this.options.plotOptions.series.point.events.drop = (e) => this.$emit("pointDrop", uncycle(e));
  28. this.chart = Highcharts[this.type](this.$el, this.options);
  29. this.chart.reflow();
  30. });
  31. }, 0); // NOTE: wait for window.path_prefix to be set in app.mounted()
  32. },
  33. beforeDestroy() {
  34. this.destroyChart();
  35. },
  36. beforeUnmount() {
  37. this.destroyChart();
  38. },
  39. methods: {
  40. update_chart() {
  41. if (this.chart) {
  42. while (this.seriesCount > this.options.series.length) {
  43. this.chart.series[0].remove();
  44. this.seriesCount--;
  45. }
  46. while (this.seriesCount < this.options.series.length) {
  47. this.chart.addSeries({}, false);
  48. this.seriesCount++;
  49. }
  50. this.chart.update(this.options);
  51. }
  52. },
  53. destroyChart() {
  54. if (this.chart) {
  55. this.chart.destroy();
  56. }
  57. },
  58. },
  59. props: {
  60. type: String,
  61. options: Object,
  62. extras: Array,
  63. },
  64. };