chart.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <!-- -->
  2. <template>
  3. <div id="container" class="chart"></div>
  4. </template>
  5. <script lang="ts">
  6. import { defineComponent, ref, watch, onMounted, PropType } from 'vue';
  7. import Highcharts from 'highcharts/highstock';
  8. import HighchartszhCN from '@/utils/highcahrts-zh_CN';
  9. import { defaultOpts } from '@/utils/chartOptions';
  10. HighchartszhCN(Highcharts);
  11. export default defineComponent({
  12. name: '',
  13. props: {
  14. chartId: {
  15. type: Number,
  16. required: true,
  17. },
  18. options: {
  19. type: Object,
  20. required: true,
  21. },
  22. },
  23. setup(props) {
  24. const chart = ref({});
  25. /* 设置options */
  26. const init = (): void => {
  27. const new_options: any = { ...defaultOpts, ...props.options };
  28. //stock不支持线形图只支持时间图 画散点图用原始chart
  29. let is_scatter = props.options.series ? props.options.series.every((_: any) => _.type === 'scatter') : false ;
  30. chart.value = is_scatter ? Highcharts.chart("container", new_options) : Highcharts.stockChart("container",new_options);
  31. };
  32. onMounted((): void => {
  33. init();
  34. });
  35. watch(
  36. () => props.options,
  37. (newval) => {
  38. console.log('reload');
  39. init();
  40. }
  41. // {
  42. // immediate: true
  43. // }
  44. );
  45. return {};
  46. },
  47. });
  48. </script>
  49. <style scoped lang="less">
  50. .chart {
  51. height: 100%;
  52. }
  53. </style>