horizontal-bar-chart.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "multiple-bar-chart",
  10. componentName: "multiple-bar-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "800px",
  19. },
  20. // 传入数据
  21. data: {
  22. type: Object,
  23. default: () => {
  24. return {
  25. area: ["新荣区", "平城区", "云冈区", "云州区", "阳高县", "天镇县", "广灵县", "浑源县", "左云县"],
  26. legend: ["因病", "因残", "因学", "因灾", "缺土地", "缺水"],
  27. data: [
  28. [1320, 1302, 901, 634, 1390, 1330, 1320, 1000, 500],
  29. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  30. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  31. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  32. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  33. [320, 302, 301, 334, 390, 330, 320, 100, 50],
  34. ],
  35. };
  36. },
  37. },
  38. },
  39. data() {
  40. return {
  41. id: "",
  42. chart: null,
  43. color: ["#323E6F", "#1DA0D7", "#02BB4C", "#DB5520", "#EDB32F", "#EDEB2F"],
  44. };
  45. },
  46. computed: {
  47. datas() {
  48. return this.list.map((t) => {
  49. return t.value;
  50. });
  51. },
  52. xdata() {
  53. return this.list[0].value.map((t) => {
  54. return t.text;
  55. });
  56. },
  57. ydata() {
  58. let result = [];
  59. return result;
  60. },
  61. series() {
  62. let result = [];
  63. return result;
  64. },
  65. },
  66. methods: {
  67. initChart() {
  68. const chart = echarts.init(this.$el);
  69. let option = {
  70. color: this.color,
  71. grid: {
  72. left: 40,
  73. right: 16,
  74. bottom: 16,
  75. top: 16,
  76. containLabel: true,
  77. },
  78. tooltip: {
  79. trigger: "axis",
  80. backgroundColor: "rgba(0,0,0,0.4)",
  81. borderColor: partten.getColor("gray"),
  82. textStyle: {
  83. color: "#fff",
  84. fontSize: 14,
  85. },
  86. },
  87. yAxis: {
  88. type: "category",
  89. axisLabel: {
  90. color: partten.getColor("gray"),
  91. },
  92. axisLine: {
  93. lineStyle: {
  94. color: "#96A4F4",
  95. },
  96. width: 5,
  97. },
  98. axisTick: {
  99. show: false,
  100. },
  101. data: this.data.area,
  102. },
  103. xAxis: {
  104. type: "value",
  105. axisLabel: {
  106. color: partten.getColor("gray"),
  107. },
  108. axisLine: {
  109. lineStyle: {
  110. color: "#96A4F4",
  111. type: "dashed",
  112. },
  113. width: 5,
  114. },
  115. axisTick: {
  116. show: false,
  117. },
  118. splitLine: {
  119. lineStyle: {
  120. color: partten.getColor("gray") + "20",
  121. },
  122. },
  123. },
  124. series: [],
  125. };
  126. for (var i = 0; i < this.data.legend.length; i++) {
  127. option.series.push({
  128. name: this.data.legend[i],
  129. type: "bar",
  130. stack: "总量",
  131. barWidth: "10%",
  132. label: {
  133. show: false,
  134. position: "insideRight",
  135. },
  136. data: this.data.data[i],
  137. });
  138. }
  139. chart.setOption(option);
  140. },
  141. },
  142. created() {
  143. this.id = "pie-chart-" + util.newGUID();
  144. },
  145. mounted() {
  146. this.$nextTick(() => {
  147. this.$el.style.width = this.width;
  148. this.$el.style.height = this.height;
  149. this.initChart();
  150. });
  151. },
  152. updated() {
  153. this.$nextTick(() => {
  154. this.initChart();
  155. });
  156. },
  157. };
  158. </script>
  159. <style lang="less">
  160. .chart {
  161. width: 100%;
  162. height: 100%;
  163. display: inline-block;
  164. }
  165. </style>