thermometer-v-chart.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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: "thermometer-chart",
  10. componentName: "thermometer-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "60px",
  19. },
  20. value: {
  21. type: Number,
  22. default: 80,
  23. },
  24. },
  25. data() {
  26. return {
  27. id: "",
  28. chart: null,
  29. };
  30. },
  31. computed: {
  32. barValue() {
  33. return [this.value * 0.8 + 16];
  34. },
  35. },
  36. methods: {
  37. hexToRgba(hex, opacity) {
  38. let rgbaColor = "";
  39. let reg = /^#[\da-f]{6}$/i;
  40. if (reg.test(hex)) {
  41. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  42. }
  43. return rgbaColor;
  44. },
  45. initChart() {
  46. var mercuryColor = "#05BB4C";
  47. var borderColor = "#B3BDC0";
  48. let option = {
  49. grid: {
  50. top: 0,
  51. bottom: 24,
  52. left: 16,
  53. right: 16,
  54. },
  55. xAxis: [
  56. {
  57. show: false,
  58. position: "left",
  59. },
  60. {
  61. show: false,
  62. data: [],
  63. min: 0,
  64. max: 9,
  65. },
  66. ],
  67. yAxis: [
  68. {
  69. show: false,
  70. data: [],
  71. max: 0,
  72. },
  73. {
  74. show: false,
  75. data: [],
  76. max: 0,
  77. },
  78. {
  79. show: false,
  80. data: [],
  81. max: 0,
  82. },
  83. {
  84. show: false,
  85. min: -5, //根据容器大小调整此处最小值和最大值,以正常显示刻度
  86. max: 100,
  87. },
  88. ],
  89. series: [
  90. {
  91. name: "条",
  92. type: "bar",
  93. yAxisIndex: 0,
  94. data: this.barValue,
  95. barWidth: 16,
  96. itemStyle: {
  97. normal: {
  98. color: mercuryColor,
  99. barBorderRadius: 100,
  100. borderWidth: 8,
  101. borderType: "dashed",
  102. borderColor: "transparent",
  103. },
  104. },
  105. z: 2,
  106. },
  107. {
  108. name: "外框",
  109. type: "bar",
  110. yAxisIndex: 2,
  111. barGap: "-100%",
  112. data: [100],
  113. barWidth: 20,
  114. itemStyle: {
  115. normal: {
  116. color: "transparent",
  117. barBorderRadius: 50,
  118. borderWidth: 2,
  119. borderColor: borderColor,
  120. },
  121. },
  122. z: 0,
  123. },
  124. {
  125. name: "刻度",
  126. type: "bar",
  127. xAxisIndex: 1,
  128. yAxisIndex: 3,
  129. label: {
  130. normal: {
  131. show: true,
  132. position: "bottom",
  133. distance: 8,
  134. color: borderColor,
  135. fontSize: 12,
  136. formatter: function(params) {
  137. if (params.dataIndex == 1) {
  138. return "0%";
  139. } else if (params.dataIndex == 5) {
  140. return "50%";
  141. } else if (params.dataIndex == 9) {
  142. return "100%";
  143. } else {
  144. return "";
  145. }
  146. },
  147. },
  148. },
  149. barGap: "-100%",
  150. data: [0, 10, 5, 10, 5, 10, 5, 10, 5, 10, 5],
  151. barWidth: 2,
  152. itemStyle: {
  153. normal: {
  154. color: borderColor,
  155. barBorderRadius: 10,
  156. },
  157. },
  158. z: 0,
  159. },
  160. ],
  161. };
  162. this.chart.setOption(option);
  163. },
  164. },
  165. created() {
  166. this.id = "pie-chart-" + util.newGUID();
  167. },
  168. mounted() {
  169. this.$nextTick(() => {
  170. this.$el.style.width = this.width;
  171. this.$el.style.height = this.height;
  172. this.chart = echarts.init(this.$el);
  173. this.initChart();
  174. });
  175. },
  176. updated() {
  177. this.initChart();
  178. },
  179. };
  180. </script>
  181. <style lang="less">
  182. .chart {
  183. width: 100%;
  184. height: 100%;
  185. display: inline-block;
  186. }
  187. </style>