You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.vue 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <view class="gauge-container">
  3. <canvas class="canvas" canvas-id="canvas2"></canvas>
  4. </view>
  5. </template>
  6. <script>
  7. import Gauge from "./index.js";
  8. import { mapState } from 'vuex';
  9. export default {
  10. name: "gauge",
  11. data() {
  12. return {
  13. gaugeInstance: null,
  14. uidatasdataSOC:0// 用于保存 Gauge 实例
  15. };
  16. },
  17. computed: {
  18. ...mapState(['formattedTime']),
  19. ...mapState({
  20. uidatasdata: state => state.uidatasdata
  21. })
  22. },
  23. watch: {
  24. formattedTime: {
  25. handler(newVal, oldVal) {
  26. // 更新 uidatasdataSOC 的值
  27. if(newVal.SOC){
  28. this.uidatasdataSOC = (newVal.SOC * 1).toFixed(0);
  29. }else{
  30. this.uidatasdataSOC =80;
  31. }
  32. console.log(this.uidatasdataSOC);
  33. // 重新创建 Gauge 实例
  34. this.createGauge();
  35. },
  36. deep: true
  37. }
  38. },
  39. mounted() {
  40. this.createGauge();
  41. },
  42. methods: {
  43. createGauge() {
  44. // 销毁旧的 Gauge 实例,如果有的话
  45. if (this.gaugeInstance) {
  46. this.gaugeInstance = null;
  47. }
  48. // 创建新的 Gauge 实例
  49. this.gaugeInstance = new Gauge({
  50. canvasId: "canvas2",
  51. value: this.uidatasdataSOC,
  52. lineWidth: 20,
  53. progressColor: ["#B0D0FF", "#5B8FF9"],
  54. valueColor: "blue"
  55. });
  56. }
  57. }
  58. };
  59. </script>
  60. <style>
  61. .gauge-container {
  62. padding: 0rpx 20rpx;
  63. text-align: left;
  64. }
  65. .title {
  66. color: #000;
  67. font-size: 32rpx;
  68. font-weight: 500;
  69. }
  70. .canvas {
  71. width: 180px;
  72. height: 150px;
  73. margin-top: 10rpx;
  74. }
  75. </style>