line.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. line.vue<template>
  2. <div>
  3. <!-- <div class="chart-title">{{title}}</div> -->
  4. <div id="echarts"></div>
  5. </div>
  6. </template>
  7. <script>
  8. import * as echarts from 'echarts';
  9. export default {
  10. name: 'Echarts',
  11. props: {
  12. 'width': {
  13. type: Number,
  14. default: 800
  15. },
  16. 'height': {
  17. type: Number,
  18. default: 400
  19. },
  20. 'title': {
  21. type: String,
  22. default: '表格标题'
  23. },
  24. 'source': {
  25. type: Object,
  26. default: function () {
  27. return {
  28. rows: [],
  29. data: []
  30. }
  31. }
  32. }
  33. },
  34. data() {
  35. return {}
  36. },
  37. watch: {
  38. 'source': {
  39. deep: true,
  40. handler: function () {
  41. this.$nextTick(() => {
  42. this.initEcharts()
  43. })
  44. }
  45. }
  46. },
  47. mounted() {
  48. this.initEcharts()
  49. },
  50. computed: {
  51. 'seriesParse': function () {
  52. if (this.source.data.length == 0) {
  53. return []
  54. }
  55. let result2 = this.source.data.map((item) => {
  56. return {
  57. name: item.title,
  58. data: [...item.data],
  59. type: 'line',
  60. }
  61. })
  62. return result2
  63. },
  64. 'xAxisParse': function () {
  65. return this.source.rows
  66. }
  67. },
  68. methods: {
  69. initEcharts() {
  70. this.myChart = echarts.init(document.getElementById('echarts'));
  71. this.myChart.resize({
  72. width: this.width,
  73. height: this.height
  74. });
  75. this.myChart.setOption({
  76. legend: {
  77. orient: 'vertical',
  78. y: 'top',
  79. x: 'right'
  80. },
  81. title: {
  82. text: this.title,
  83. show: !!this.title,
  84. textAlign: 'auto'
  85. },
  86. tooltip: {
  87. trigger: 'axis'
  88. },
  89. xAxis: {
  90. data: [...this.xAxisParse]
  91. },
  92. yAxis: {},
  93. series: [...this.seriesParse]
  94. });
  95. }
  96. }
  97. }
  98. </script>
  99. <style>
  100. #echarts {
  101. width: 100%;
  102. height: 400px;
  103. margin-left: auto;
  104. margin-right: auto;
  105. /* float: left; */
  106. }
  107. .chart-title {
  108. text-align: center;
  109. }
  110. </style>