LineChartBlock.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div
  3. class="grid-content bg-purple"
  4. v-resize='initChart'
  5. >
  6. <div class="grid-title">{{ title }}</div>
  7. <div class="filters">
  8. <slot name="filters"></slot>
  9. </div>
  10. <div class="grid-chart" :id="id">
  11. </div>
  12. </div>
  13. </template>
  14. <script>
  15. import { copyObj, uuid } from '@/utils';
  16. import * as echarts from 'echarts';
  17. import SwitchTag from '../../SwitchTag/index.vue';
  18. const basicSerie = {
  19. type: 'line',
  20. areaStyle: {
  21. color: '#d7e4fc',
  22. emphasis: {
  23. color: '#6093f5',
  24. },
  25. },
  26. };
  27. const opts = {
  28. grid: {
  29. top: 10,
  30. bottom: 20,
  31. },
  32. tooltip: {
  33. show: true,
  34. trigger: 'axis',
  35. },
  36. xAxis: {
  37. type: 'category',
  38. boundaryGap: false,
  39. },
  40. yAxis: {
  41. type: 'value',
  42. name: 'kw·h',
  43. nameLocation: 'middle',
  44. nameTextStyle: {
  45. color: '#000',
  46. fontSize: '14px',
  47. lineHeight: 56,
  48. },
  49. },
  50. series: [],
  51. };
  52. export default {
  53. components: { SwitchTag },
  54. props: {
  55. title: {
  56. default: '标题',
  57. type: String,
  58. },
  59. optCfg: {
  60. type: Object,
  61. default: () => {
  62. return {};
  63. },
  64. },
  65. },
  66. watch: {
  67. optCfg: {
  68. handler(val) {
  69. this.initChart();
  70. },
  71. deep: true,
  72. },
  73. },
  74. data() {
  75. return {
  76. id: uuid(),
  77. refCharts: null,
  78. };
  79. },
  80. mounted() {
  81. this.$nextTick(this.initChart);
  82. },
  83. created() {
  84. },
  85. methods: {
  86. initChart() {
  87. this.refCharts && this.refCharts.clear();
  88. const chartDom = document.getElementById(this.id);
  89. this.refCharts = echarts.init(chartDom);
  90. const option = copyObj(opts);
  91. this.optCfg.series.forEach((item, index) => {
  92. const serie = copyObj(basicSerie);
  93. Object.assign(serie, item);
  94. option.series.push(serie);
  95. });
  96. option.xAxis = this.optCfg.xAxis;
  97. option.yAxis.name = this.optCfg.unit || "kw·h"
  98. option && this.refCharts && this.refCharts.setOption(option);
  99. this.refCharts.resize();
  100. },
  101. },
  102. };
  103. </script>
  104. <style src="./index.scss" lang="scss" />