lineChart1.vue 5.3 KB

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