LineChartBlock.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <div class="grid-chart" :id="id">
  3. </div>
  4. </template>
  5. <script>
  6. import {copyObj, uuid} from '@/utils';
  7. import * as echarts from 'echarts';
  8. const basicSerie = {
  9. type: 'line',
  10. };
  11. const opts = {
  12. grid: {
  13. bottom: 20,
  14. },
  15. tooltip: {
  16. show: true,
  17. trigger: 'axis',
  18. },
  19. legend: {
  20. data: [],
  21. },
  22. series: [],
  23. };
  24. export default {
  25. props: {
  26. optCfg: {
  27. type: Object,
  28. default: () => {
  29. return {};
  30. },
  31. },
  32. },
  33. data() {
  34. return {
  35. id: uuid(),
  36. refCharts: null,
  37. };
  38. },
  39. mounted() {
  40. this.$nextTick(this.initChart);
  41. },
  42. created() {
  43. },
  44. methods: {
  45. initChart() {
  46. this.refCharts && this.refCharts.clear();
  47. const chartDom = document.getElementById(this.id);
  48. this.refCharts = echarts.init(chartDom);
  49. let option = copyObj(opts);
  50. const {series, xAxis, ...rest} = this.optCfg
  51. Object.assign(option, rest);
  52. const legend = [];
  53. series.forEach((item, index) => {
  54. const serie = copyObj(basicSerie);
  55. Object.assign(serie, item);
  56. const {name} = item;
  57. option.series.push(serie);
  58. if (name) {
  59. legend.push(name);
  60. }
  61. });
  62. option.legend.data = legend;
  63. option.xAxis = xAxis;
  64. option && this.refCharts && this.refCharts.setOption(option);
  65. this.refCharts.resize();
  66. },
  67. },
  68. };
  69. </script>
  70. <style src="./index.scss" lang="scss"/>