lineChart2.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. },
  86. },
  87. ],
  88. };
  89. this.charts.setOption(option);
  90. },
  91. methods: {
  92. //绘制图表
  93. drawLine() {
  94. // 基于准备好的dom,初始化echarts实例
  95. let id = "echartBar" + Math.random() * 1000;
  96. this.$el.setAttribute("id", id);
  97. let myChartContainer = document.getElementById(id);
  98. // let myChartChina = echarts.init(myChartContainer);
  99. let myChartChina = echarts.init("aa");
  100. // 绘制图表
  101. let optionMap = {
  102. //折线图配置数据
  103. color: ["#6cacaf", "#e4a526", "#e4391d", "#a1cb37", "#8c97cb"],
  104. title: {
  105. x: "center",
  106. text: this.text,
  107. textStyle: {
  108. fontSize: this.titleSize,
  109. },
  110. },
  111. tooltip: {
  112. trigger: "axis",
  113. },
  114. legend: {
  115. top: [30],
  116. data: this.tipData,
  117. },
  118. grid: {
  119. left: "3%",
  120. right: "4%",
  121. bottom: "3%",
  122. containLabel: true,
  123. },
  124. toolbox: {
  125. show: this.download,
  126. top: [5],
  127. right: [20],
  128. feature: {
  129. saveAsImage: {},
  130. },
  131. },
  132. xAxis: {
  133. interval: ["0"], //标签显示间隔
  134. type: "category",
  135. boundaryGap: false,
  136. data: [
  137. "1月",
  138. "2月",
  139. "3月",
  140. "4月",
  141. "5月",
  142. "6月",
  143. "7月",
  144. "8月",
  145. "9月",
  146. "10月",
  147. "11月",
  148. "12月",
  149. ], //x轴数据
  150. },
  151. yAxis: {
  152. name: "(万元)",
  153. nameGap: [10],
  154. type: "value",
  155. },
  156. series: this.showData,
  157. };
  158. myChartChina.setOption(optionMap);
  159. window.onresize = function () {
  160. myChartChina.resize();
  161. };
  162. },
  163. },
  164. watch: {
  165. "LineChartData.data": function (val) {
  166. this.tipData = [];
  167. this.showData = [];
  168. for (let i = 0; i < val.length; i++) {
  169. let obj = {
  170. //折线图需要的数据结构
  171. name: "",
  172. type: "line",
  173. data: [],
  174. };
  175. obj.name = val[i].name;
  176. this.tipData.push(val[i].name);
  177. for (let j = 0; j < val[i].monthlySales.length; j++) {
  178. obj.data.push(parseFloat(val[i].monthlySales[j]));
  179. }
  180. this.showData.push(obj);
  181. }
  182. this.drawLine();
  183. },
  184. },
  185. destroyed() {
  186. console.log("destroyed");
  187. this.charts.dispose();
  188. },
  189. };
  190. </script>
  191. <style lang='less' scoped>
  192. .linechart {
  193. margin: 0 auto;
  194. }
  195. </style>