horizontal-bar-chart.vue 3.7 KB

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