warn-zb.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <view class="warn-zb">
  3. <view ref="coord" class="coords" :style="{ width: widthV, height: heightV }">
  4. <view class="triangle-x"></view>
  5. <view class="triangle-y"></view>
  6. <view class="name-x">时间</view>
  7. <view class="name-y">温度(℃)</view>
  8. <view class="coord-dw">
  9. <view class="dw-item" :style="{ bottom: `${coordDw[0]}px` }">12</view>
  10. <view class="dw-item" :style="{ bottom: `${coordDw[1]}px` }">200</view>
  11. </view>
  12. <view class="coord-bj">
  13. <view class="bj-qfq" :style="{ width: widthCanvas == '302px' ? '100px' : '401px' }">
  14. <view class="left-jt"></view>
  15. <view class="line"></view>
  16. <view class="right-jt"></view>
  17. <view class="text">缓慢氧化阶段</view>
  18. </view>
  19. <view class="bj-zrq" :style="{ width: widthCanvas == '302px' ? '50px' : '250px' }">
  20. <view class="left-jt"></view>
  21. <view class="line"></view>
  22. <view class="right-jt"></view>
  23. <view class="text">加速氧化阶段</view>
  24. </view>
  25. <view class="bj-rsq" :style="{ width: widthCanvas == '302px' ? '100px' : '570px' }">
  26. <view class="left-jt"></view>
  27. <view class="line"></view>
  28. <view class="text">剧烈氧化阶段</view>
  29. </view>
  30. </view>
  31. <view class="coord-area"
  32. :style="{ width: 'calc(100% - 10px)', height: 'calc(100% - 10px)', overflow: 'hidden' }">
  33. <canvas :id="`myCanvas${resetIndex}`" :style="{ width: widthCanvas, height: heightCanvas }"></canvas>
  34. </view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import api from "@/api/api";
  40. export default {
  41. name: 'warnZb',
  42. props: {
  43. widthV: {
  44. type: String,
  45. default: '',
  46. },
  47. heightV: {
  48. type: String,
  49. default: '',
  50. },
  51. coordDw: {
  52. type: Array,
  53. default: () => {
  54. return [];
  55. },
  56. },
  57. widthCanvas: {
  58. type: String,
  59. default: 0,
  60. },
  61. heightCanvas: {
  62. type: String,
  63. default: 0,
  64. },
  65. warnLevel: {
  66. type: String,
  67. default: ''
  68. },
  69. resetIndex:{
  70. type:Number,
  71. default:0
  72. }
  73. },
  74. data() {
  75. return {
  76. coord: null,
  77. lengY: 0,
  78. //与x,y轴相交最大值坐标
  79. maxY: 0,
  80. maxX: 0,
  81. };
  82. },
  83. watch: {
  84. resetIndex:{
  85. handler(newR,oldR){
  86. this.getCanvas(newR)
  87. },
  88. immediate: true,
  89. }
  90. },
  91. mounted() {
  92. this.getAreas()
  93. },
  94. methods: {
  95. getAreas() {
  96. if (this.coord) {
  97. let width = this.coord.offsetWidth;
  98. let height = this.coord.offsetHeight;
  99. this.lengY = Math.ceil((height - 10) / 10);
  100. }
  101. },
  102. getCanvas(index) {
  103. let that = this
  104. that.$nextTick(() => {
  105. // 获取canvas元素
  106. let canvas = document.getElementById(`myCanvas${index}`).childNodes[0];
  107. let ctx = canvas.getContext('2d');
  108. let x = 0;
  109. let step = that.widthCanvas == '302px' ? 0.01 : 0.05; // 设置每次递增的长度
  110. let y = that.widthCanvas == '302px' ? 194 : 325; // 初始y坐标
  111. // 设置线条样式
  112. ctx.strokeStyle = '#3df6ff'; // 红色线条
  113. ctx.lineWidth = 1; // 线宽为2
  114. // 初始化曲线数据
  115. let xValues = [];
  116. let yValues = [];
  117. for (let i = 0; i < 30000; i++) {
  118. x += step;
  119. if (that.widthCanvas == '302px') {
  120. y -= (step * Math.random() * i) / 12000; // 随机增加长度,使曲线更加平滑
  121. } else {
  122. y -= (step * Math.random() * i) / 30000; // 随机增加长度,使曲线更加平滑
  123. }
  124. xValues[i] = x;
  125. yValues[i] = y;
  126. }
  127. // 绘制递增曲线
  128. ctx.beginPath();
  129. ctx.moveTo(xValues[0], yValues[0]);
  130. for (var i = 1; i < xValues.length; i++) {
  131. ctx.lineTo(xValues[i], yValues[i]);
  132. if (that.warnLevel == '绿色预警') {
  133. ctx.fillStyle = 'rgba(145, 230, 9)'; // 设置填充颜色
  134. if (that.widthCanvas == '302px' && i <= 10000) {
  135. ctx.fillRect(xValues[i], yValues[i], step, canvas.height - yValues[i]);
  136. } else if (i <= 8000) {
  137. ctx.fillRect(xValues[i], yValues[i], step, canvas.height - yValues[i]);
  138. }
  139. } else if (that.warnLevel == '黄色预警') {
  140. ctx.fillStyle = 'rgba(255, 255, 53)'; // 设置填充颜色
  141. if (that.widthCanvas == '302px' && i <= 15000) {
  142. ctx.fillRect(xValues[i], yValues[i], step, canvas.height - yValues[i]);
  143. } else if (i <= 13000) {
  144. ctx.fillRect(xValues[i], yValues[i], step, canvas.height - yValues[i]);
  145. }
  146. } else if (that.warnLevel == '红色预警') {
  147. ctx.fillStyle = 'rgba(255, 0, 0)'; // 设置填充颜色
  148. if (that.widthCanvas == '302px' && i > 15000) {
  149. ctx.fillRect(xValues[i], yValues[i], step, canvas.height - yValues[i]);
  150. } else if (i > 13000) {
  151. ctx.fillRect(xValues[i], yValues[i], step, canvas.height - yValues[i]);
  152. }
  153. }
  154. }
  155. ctx.stroke();
  156. ctx.beginPath();
  157. ctx.arc(xValues[that.widthCanvas == '302px' ? 10000 : 8000], yValues[that.widthCanvas == '302px' ? 10000 : 8000], 5, 0, 2 * Math.PI);
  158. ctx.fillStyle = '#ff6363';
  159. ctx.fill();
  160. //标记点2
  161. ctx.beginPath();
  162. ctx.arc(xValues[that.widthCanvas == '302px' ? 15000 : 13000], yValues[that.widthCanvas == '302px' ? 15000 : 13000], 5, 0, 2 * Math.PI);
  163. ctx.fillStyle = '#ff6363';
  164. ctx.fill();
  165. // 设置线条样式(颜色、宽度等)
  166. ctx.strokeStyle = 'rgba(14, 180, 252,.6)';
  167. ctx.lineWidth = 1;
  168. ctx.lineCap = 'round';
  169. // 定义虚线模式:[线段长度, 间隔长度]
  170. ctx.setLineDash([5, 5]);
  171. //绘制标记点1线条-x
  172. ctx.beginPath();
  173. ctx.moveTo(0, yValues[that.widthCanvas == '302px' ? 10000 : 8000]); // 开始绘制的点
  174. ctx.lineTo(xValues[that.widthCanvas == '302px' ? 10000 : 8000], yValues[that.widthCanvas == '302px' ? 10000 : 8000]); // 结束绘制的点
  175. ctx.stroke(); // 进行绘制
  176. //绘制标记点1线条-y
  177. ctx.beginPath();
  178. ctx.moveTo(xValues[that.widthCanvas == '302px' ? 10000 : 8000], yValues[that.widthCanvas == '302px' ? 10000 : 8000]); // 开始绘制的点
  179. ctx.lineTo(xValues[that.widthCanvas == '302px' ? 10000 : 8000], canvas.height); // 结束绘制的点
  180. ctx.stroke(); // 进行绘制
  181. //绘制标记点2线条-x
  182. ctx.beginPath();
  183. ctx.moveTo(0, yValues[that.widthCanvas == '302px' ? 15000 : 13000]); // 开始绘制的点
  184. ctx.lineTo(xValues[that.widthCanvas == '302px' ? 15000 : 13000], yValues[that.widthCanvas == '302px' ? 15000 : 13000]); // 结束绘制的点
  185. ctx.stroke(); // 进行绘制
  186. //绘制标记点2线条-y
  187. ctx.beginPath();
  188. ctx.moveTo(xValues[that.widthCanvas == '302px' ? 15000 : 13000], yValues[that.widthCanvas == '302px' ? 15000 : 13000]); // 开始绘制的点
  189. ctx.lineTo(xValues[that.widthCanvas == '302px' ? 15000 : 13000], canvas.height); // 结束绘制的点
  190. ctx.stroke(); // 进行绘制
  191. })
  192. }
  193. },
  194. computed: {},
  195. };
  196. </script>
  197. <style lang="scss" scoped>
  198. .warn-zb {
  199. position: relative;
  200. height: 100%;
  201. .coords {
  202. position: absolute;
  203. left: 50%;
  204. top: 50%;
  205. border-left: 1px solid #0eb4fc;
  206. border-bottom: 1px solid #0eb4fc;
  207. transform: translate(-50%, -55%);
  208. .triangle-x {
  209. position: absolute;
  210. left: -4px;
  211. top: -10px;
  212. width: 0px;
  213. height: 0px;
  214. border-top: 3px solid transparent;
  215. border-left: 4px solid transparent;
  216. border-right: 3px solid transparent;
  217. border-bottom: 8px solid #0eb4fc;
  218. }
  219. .triangle-y {
  220. position: absolute;
  221. right: -10px;
  222. bottom: -4px;
  223. width: 0px;
  224. height: 0px;
  225. border-top: 4px solid transparent;
  226. border-left: 8px solid #0eb4fc;
  227. border-right: 3px solid transparent;
  228. border-bottom: 3px solid transparent;
  229. }
  230. .name-x {
  231. position: absolute;
  232. right: 0px;
  233. bottom: -20px;
  234. color: #b3b8cc;
  235. font-size: 12px;
  236. }
  237. .name-y {
  238. width: 20px;
  239. position: absolute;
  240. left: -24px;
  241. top: 0px;
  242. color: #b3b8cc;
  243. font-size: 12px;
  244. text-align: center;
  245. }
  246. .coord-dw {
  247. position: absolute;
  248. left: -32px;
  249. top: 0;
  250. width: 30px;
  251. height: 100%;
  252. .dw-item {
  253. position: absolute;
  254. left: 50%;
  255. transform: translate(-50%, 0);
  256. color: #b3b8cc;
  257. font-size: 12px;
  258. }
  259. }
  260. .coord-bj {
  261. display: flex;
  262. position: absolute;
  263. left: -1px;
  264. bottom: -31px;
  265. width: 100%;
  266. height: 30px;
  267. border-left: 1px solid #0eb4fc;
  268. .left-jt {
  269. position: absolute;
  270. left: -1px;
  271. top: 23px;
  272. width: 0px;
  273. height: 0px;
  274. border-top: 3px solid transparent;
  275. border-right: 8px solid #0eb4fc;
  276. border-left: 3px solid transparent;
  277. border-bottom: 3px solid transparent;
  278. }
  279. .right-jt {
  280. position: absolute;
  281. right: -1px;
  282. top: 23px;
  283. width: 0px;
  284. height: 0px;
  285. border-top: 3px solid transparent;
  286. border-left: 8px solid #0eb4fc;
  287. border-right: 3px solid transparent;
  288. border-bottom: 3px solid transparent;
  289. }
  290. .text {
  291. width: 100%;
  292. position: absolute;
  293. left: 50%;
  294. top: 50%;
  295. transform: translate(-50%, -55%);
  296. font-size: 10px;
  297. color: #db9753;
  298. text-align: center;
  299. }
  300. .line {
  301. width: calc(100% - 20px);
  302. height: 1px;
  303. position: absolute;
  304. left: 10px;
  305. top: 26px;
  306. background-color: #0eb4fc;
  307. }
  308. .bj-qfq {
  309. flex-shrink: 0;
  310. position: relative;
  311. // width: 401px;
  312. height: 100%;
  313. border-right: 1px dashed #0eb4fc;
  314. }
  315. .bj-zrq {
  316. flex-shrink: 0;
  317. position: relative;
  318. // width: 699px;
  319. height: 100%;
  320. border-right: 1px dashed #0eb4fc;
  321. }
  322. .bj-rsq {
  323. flex-shrink: 0;
  324. position: relative;
  325. // width: 415px;
  326. height: 100%;
  327. }
  328. }
  329. .coord-area {
  330. position: absolute;
  331. left: 0;
  332. top: 10px;
  333. }
  334. }
  335. }
  336. </style>