LineChartBlock.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. legend: {
  13. top: '5%',
  14. },
  15. grid: {
  16. top: 10,
  17. bottom: 20,
  18. },
  19. tooltip: {
  20. show: true,
  21. trigger: 'axis',
  22. },
  23. xAxis: {
  24. type: 'category',
  25. boundaryGap: false,
  26. },
  27. yAxis: {
  28. type: 'value',
  29. name: 'kW·h',
  30. minInterval: 1,
  31. nameLocation: 'middle',
  32. nameTextStyle: {
  33. color: '#000',
  34. fontSize: '14px',
  35. lineHeight: 56,
  36. },
  37. },
  38. series: [],
  39. };
  40. export default {
  41. props: {
  42. title: {
  43. default: '标题',
  44. type: String,
  45. },
  46. optCfg: {
  47. type: Object,
  48. default: () => {
  49. return {};
  50. },
  51. },
  52. },
  53. watch: {
  54. optCfg: {
  55. handler(val) {
  56. this.initChart();
  57. },
  58. deep: true,
  59. },
  60. },
  61. data() {
  62. return {
  63. id: uuid(),
  64. refCharts: null,
  65. };
  66. },
  67. mounted() {
  68. this.$nextTick(this.initChart);
  69. },
  70. created() {
  71. },
  72. methods: {
  73. initChart() {
  74. this.refCharts && this.refCharts.clear();
  75. const chartDom = document.getElementById(this.id);
  76. this.refCharts = echarts.init(chartDom);
  77. const option = copyObj(opts);
  78. const legend = [];
  79. this.optCfg.series.forEach((item, index) => {
  80. const serie = copyObj(basicSerie);
  81. Object.assign(serie, item);
  82. const {name} = item;
  83. option.series.push(serie);
  84. if (name) {
  85. legend.push(name);
  86. }
  87. });
  88. option.legend = {
  89. data: legend,
  90. };
  91. option.xAxis = this.optCfg.xAxis;
  92. option.yAxis.name = this.optCfg.unit || "kW·h"
  93. option && this.refCharts && this.refCharts.setOption(option);
  94. this.refCharts.resize();
  95. },
  96. },
  97. };
  98. </script>
  99. <style src="./index.scss" lang="scss"/>