thermometer.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div class="thermometer"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. export default {
  7. // 名称
  8. name: "thermometer",
  9. // 使用组件
  10. components: {},
  11. // 传入参数
  12. props: {
  13. width: {
  14. type: String,
  15. default: "9.259vh",
  16. },
  17. height: {
  18. type: String,
  19. default: "11.111vh",
  20. },
  21. min: {
  22. type: Number,
  23. default: -20,
  24. },
  25. max: {
  26. type: Number,
  27. default: 100,
  28. },
  29. value: {
  30. type: Number,
  31. default: 36.6,
  32. },
  33. title: {
  34. type: String,
  35. default: "温度",
  36. },
  37. },
  38. // 自定义事件
  39. emits: {},
  40. // 数据
  41. data() {
  42. return {
  43. option: {},
  44. };
  45. },
  46. // 函数
  47. methods: {
  48. initChart: function() {
  49. const chart = echarts.init(this.$el);
  50. let kd = []; // 刻度
  51. for (var i = 0, len = 135; i <= len; i++) {
  52. if (i < 30 || i > 130) {
  53. kd.push("");
  54. } else {
  55. if ((i - 30) % 20 === 0) {
  56. kd.push("-3");
  57. } else if ((i - 30) % 4 === 0) {
  58. kd.push("-1");
  59. } else {
  60. kd.push("");
  61. }
  62. }
  63. }
  64. let width = this.$el.offsetWidth;
  65. this.option = {
  66. backgroundColor: "transparent",
  67. grid: {
  68. top: "0",
  69. left: "0",
  70. right: "0",
  71. bottom: "45%",
  72. },
  73. yAxis: [
  74. {
  75. axisLine: {
  76. show: false,
  77. },
  78. axisLabel: {
  79. show: false,
  80. },
  81. axisTick: {
  82. show: false,
  83. },
  84. splitLine: {
  85. show: false,
  86. },
  87. },
  88. {
  89. show: false,
  90. data: [],
  91. min: 0,
  92. max: 135,
  93. axisLine: {
  94. show: false,
  95. },
  96. },
  97. ],
  98. xAxis: [
  99. {
  100. axisTick: "none",
  101. axisLine: "none",
  102. offset: 0,
  103. data: [this.title],
  104. axisLabel: {
  105. show: true,
  106. textStyle: {
  107. color: "#606769",
  108. fontSize: "14",
  109. },
  110. lineHeight: 40,
  111. },
  112. splitLine: {
  113. show: false,
  114. },
  115. },
  116. {
  117. axisTick: "none",
  118. axisLabel: {
  119. show: false,
  120. },
  121. data: [0],
  122. splitLine: {
  123. show: false,
  124. },
  125. axisLine: {
  126. show: false,
  127. },
  128. },
  129. {
  130. name: "单位:件",
  131. nameGap: "50",
  132. data: [],
  133. splitLine: {
  134. show: false,
  135. },
  136. axisLine: {
  137. show: false,
  138. color: "#ffffff",
  139. },
  140. },
  141. {
  142. show: false,
  143. min: 0 - width * 0.35,
  144. max: width * 0.5,
  145. },
  146. ],
  147. series: [
  148. {
  149. name: "℃",
  150. type: "bar",
  151. yAxisIndex: 0,
  152. xAxisIndex: 0,
  153. label: {
  154. show: true,
  155. position: "top",
  156. formatter: "{back|" + "{c}" + "}",
  157. rich: {
  158. back: {
  159. align: "right",
  160. lineHeight: 14,
  161. fontSize: 14,
  162. fontFamily: "微软雅黑",
  163. color: "#05bb4c",
  164. },
  165. },
  166. offset: [25, 15],
  167. },
  168. data: [this.value],
  169. barWidth: 5,
  170. itemStyle: {
  171. normal: {
  172. color: "#05bb4c",
  173. },
  174. },
  175. z: 2,
  176. },
  177. {
  178. name: "温度背景框",
  179. type: "bar",
  180. yAxisIndex: 0,
  181. xAxisIndex: 1,
  182. barGap: "-100%",
  183. data: [99],
  184. barWidth: 8,
  185. itemStyle: {
  186. normal: {
  187. color: "#040c0b",
  188. barBorderRadius: 5,
  189. },
  190. },
  191. z: 1,
  192. },
  193. {
  194. name: "外框",
  195. type: "bar",
  196. yAxisIndex: 0,
  197. xAxisIndex: 2,
  198. barGap: "-100%",
  199. data: [100],
  200. barWidth: width * 0.1,
  201. itemStyle: {
  202. normal: {
  203. color: "#606769",
  204. barBorderRadius: width * 0.5,
  205. },
  206. },
  207. z: 0,
  208. },
  209. {
  210. name: "外圆",
  211. type: "scatter",
  212. hoverAnimation: false,
  213. data: [0],
  214. yAxisIndex: 0,
  215. xAxisIndex: 2,
  216. symbolSize: width * 0.15,
  217. itemStyle: {
  218. normal: {
  219. color: "#05bb4c",
  220. opacity: 1,
  221. },
  222. },
  223. z: 2,
  224. },
  225. {
  226. name: "白圆",
  227. type: "scatter",
  228. hoverAnimation: false,
  229. data: [0],
  230. yAxisIndex: 0,
  231. xAxisIndex: 1,
  232. symbolSize: width * 0.2,
  233. itemStyle: {
  234. normal: {
  235. color: "#040c0b",
  236. opacity: 1,
  237. },
  238. },
  239. z: 1,
  240. },
  241. {
  242. name: "外圆",
  243. type: "scatter",
  244. hoverAnimation: false,
  245. data: [0],
  246. yAxisIndex: 0,
  247. xAxisIndex: 2,
  248. symbolSize: width * 0.23,
  249. itemStyle: {
  250. normal: {
  251. color: "#606769",
  252. opacity: 1,
  253. },
  254. },
  255. z: 0,
  256. },
  257. {
  258. name: "刻度",
  259. type: "bar",
  260. yAxisIndex: 1,
  261. xAxisIndex: 3,
  262. label: {
  263. normal: {
  264. show: false,
  265. },
  266. },
  267. barGap: "100%",
  268. data: kd,
  269. barWidth: 1,
  270. itemStyle: {
  271. normal: {
  272. color: "#606769",
  273. barBorderRadius: width * 1.2,
  274. },
  275. },
  276. z: 0,
  277. },
  278. ],
  279. };
  280. chart.setOption(this.option);
  281. },
  282. setValue: function() {
  283. this.option.series[0].data[0] = this.value;
  284. const chart = echarts.getInstanceByDom(this.$el);
  285. chart.setOption(this.option);
  286. },
  287. },
  288. // 生命周期钩子
  289. beforeCreate() {
  290. // 创建前
  291. },
  292. created() {
  293. // 创建后
  294. },
  295. beforeMount() {
  296. // 渲染前
  297. },
  298. mounted() {
  299. // 渲染后
  300. this.$nextTick(() => {
  301. this.$el.style.width = this.width;
  302. this.$el.style.height = this.height;
  303. this.initChart();
  304. });
  305. },
  306. beforeUpdate() {
  307. // 数据更新前
  308. },
  309. updated() {
  310. // 数据更新后
  311. this.setValue();
  312. },
  313. };
  314. </script>
  315. <style lang="less">
  316. .thermometer {
  317. }
  318. </style>