lineChart2.vue 5.7 KB

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