lineChart1.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div class="linechart" id="ee"></div>
  3. </template>
  4. <script>
  5. import echarts from "echarts";
  6. export default {
  7. name: "lineChart",
  8. props: {
  9. LineChartData: {
  10. text: String, //标题 不需要标题可不传
  11. titleSize: {
  12. //标题大小
  13. type: Number,
  14. default: 20,
  15. },
  16. download: {
  17. //是否显示下载按钮
  18. type: Boolean,
  19. default: false,
  20. },
  21. data: [],
  22. },
  23. },
  24. data() {
  25. return {
  26. tipData: [], //线数据
  27. showData: [], //展示数据
  28. // text: this.LineChartData.text, //大标题
  29. // titleSize: this.LineChartData.titleSize, //标题大小
  30. // download: this.LineChartData.download,
  31. };
  32. },
  33. computed: {
  34. text() {
  35. return this.LineChartData.text;
  36. },
  37. titleSize() {
  38. return this.LineChartData.titleSize;
  39. },
  40. download() {
  41. return this.LineChartData.download;
  42. },
  43. },
  44. mounted() {
  45. // this.drawLine();
  46. // import * as echarts from "echarts";
  47. // 基于准备好的dom,初始化echarts实例
  48. let myChart = this.$echarts.init(document.getElementById("ee"));
  49. let option = {
  50. xAxis: {
  51. data: ["A", "B", "C", "D", "E"],
  52. },
  53. yAxis: {},
  54. series: [
  55. {
  56. data: [10, 22, 28, 23, 19],
  57. type: "line",
  58. smooth: true,
  59. },
  60. ],
  61. };
  62. myChart.setOption(option);
  63. },
  64. methods: {
  65. //绘制图表
  66. drawLine() {
  67. // 基于准备好的dom,初始化echarts实例
  68. let id = "echartBar" + Math.random() * 1000;
  69. this.$el.setAttribute("id", id);
  70. let myChartContainer = document.getElementById(id);
  71. // let myChartChina = echarts.init(myChartContainer);
  72. let myChartChina = echarts.init("aa");
  73. // 绘制图表
  74. let optionMap = {
  75. //折线图配置数据
  76. color: ["#6cacaf", "#e4a526", "#e4391d", "#a1cb37", "#8c97cb"],
  77. title: {
  78. x: "center",
  79. text: this.text,
  80. textStyle: {
  81. fontSize: this.titleSize,
  82. },
  83. },
  84. tooltip: {
  85. trigger: "axis",
  86. },
  87. legend: {
  88. top: [30],
  89. data: this.tipData,
  90. },
  91. grid: {
  92. left: "3%",
  93. right: "4%",
  94. bottom: "3%",
  95. containLabel: true,
  96. },
  97. toolbox: {
  98. show: this.download,
  99. top: [5],
  100. right: [20],
  101. feature: {
  102. saveAsImage: {},
  103. },
  104. },
  105. xAxis: {
  106. interval: ["0"], //标签显示间隔
  107. type: "category",
  108. boundaryGap: false,
  109. data: [
  110. "1月",
  111. "2月",
  112. "3月",
  113. "4月",
  114. "5月",
  115. "6月",
  116. "7月",
  117. "8月",
  118. "9月",
  119. "10月",
  120. "11月",
  121. "12月",
  122. ], //x轴数据
  123. },
  124. yAxis: {
  125. name: "(万元)",
  126. nameGap: [10],
  127. type: "value",
  128. },
  129. series: this.showData,
  130. };
  131. myChartChina.setOption(optionMap);
  132. window.onresize = function () {
  133. myChartChina.resize();
  134. };
  135. },
  136. },
  137. watch: {
  138. "LineChartData.data": function (val) {
  139. this.tipData = [];
  140. this.showData = [];
  141. for (let i = 0; i < val.length; i++) {
  142. let obj = {
  143. //折线图需要的数据结构
  144. name: "",
  145. type: "line",
  146. data: [],
  147. };
  148. obj.name = val[i].name;
  149. this.tipData.push(val[i].name);
  150. for (let j = 0; j < val[i].monthlySales.length; j++) {
  151. obj.data.push(parseFloat(val[i].monthlySales[j]));
  152. }
  153. this.showData.push(obj);
  154. }
  155. this.drawLine();
  156. },
  157. },
  158. };
  159. </script>
  160. <style lang='less' scoped>
  161. .linechart {
  162. width: 900px;
  163. height: 300px;
  164. margin: 0 auto;
  165. }
  166. </style>