chart.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <div ref="chart" class="chart" :style="{height: '400px', width: '100%'}" />
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. require('echarts/theme/macarons') // echarts theme
  7. import resize from '@/utils/resize'
  8. export default {
  9. mixins: [resize],
  10. props: {
  11. sections: Array
  12. },
  13. mounted() {
  14. this.$nextTick(() => {
  15. this.chart = echarts.init(this.$refs.chart, 'macarons');
  16. if (this.sections.length > 0) {
  17. this.setOptions(this.sections);
  18. }
  19. })
  20. },
  21. beforeDestroy() {
  22. if (this.chart) {
  23. this.chart.dispose()
  24. this.chart = null
  25. }
  26. },
  27. watch: {
  28. sections(value) {
  29. this.setOptions(value)
  30. }
  31. },
  32. methods: {
  33. setOptions(sections) {
  34. const xAxisData = sections.map(({ x }) => x);
  35. const seriesData = sections.map(({ x, y }) => ([x, y]));
  36. this.chart.setOption({
  37. xAxis: {
  38. name: '起点距',
  39. boundaryGap: false,
  40. type: 'value',
  41. min: Math.min(...xAxisData),
  42. max: Math.max(...xAxisData),
  43. axisLine: {
  44. lineStyle: {
  45. color: '#FF8500'
  46. }
  47. },
  48. axisTick: {
  49. lineStyle: {
  50. color: '#8D99A4'
  51. }
  52. },
  53. axisLabel: {
  54. color: '#54606C'
  55. },
  56. nameTextStyle: {
  57. color: '#54606C'
  58. }
  59. },
  60. yAxis: [
  61. {
  62. name: '高程(m)',
  63. axisLine: {
  64. show: true,
  65. lineStyle: {
  66. color: '#FF8500'
  67. }
  68. },
  69. axisTick: {
  70. show: true,
  71. lineStyle: {
  72. color: '#8D99A4'
  73. }
  74. },
  75. axisLabel: {
  76. color: '#54606C'
  77. },
  78. nameTextStyle: {
  79. color: '#54606C'
  80. },
  81. min: function (value) {
  82. return Math.round(value.min - (value.max - value.min) * 0.1);
  83. },
  84. max: function (value) {
  85. return Math.round(value.max + (value.max - value.min) * 0.1);
  86. },
  87. },
  88. {
  89. axisLine: {
  90. show: true,
  91. lineStyle: {
  92. color: '#FF8500'
  93. }
  94. },
  95. axisTick: {
  96. show: false,
  97. },
  98. axisLabel: {
  99. show: false,
  100. },
  101. },
  102. ],
  103. grid: {
  104. left: 50,
  105. right: 50,
  106. bottom: 40,
  107. top: 40,
  108. containLabel: true
  109. },
  110. tooltip: {
  111. trigger: 'axis',
  112. axisPointer: {
  113. type: 'cross'
  114. },
  115. padding: [5, 10]
  116. },
  117. series: [{
  118. name: '高程',
  119. itemStyle: {
  120. normal: {
  121. color: '#FF8500',
  122. lineStyle: {
  123. color: '#FF8500',
  124. width: 2
  125. }
  126. }
  127. },
  128. areaStyle: {
  129. color: '#FF8500',
  130. opacity: 0.5
  131. },
  132. smooth: false,
  133. symbol: 'none',
  134. type: 'line',
  135. data: seriesData,
  136. animations: false,
  137. }]
  138. })
  139. }
  140. }
  141. }
  142. </script>
  143. <style scoped>
  144. </style>