gauge.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div id="charts"></div>
  3. </template>
  4. <script>
  5. import * as echarts from "echarts/core";
  6. import { GaugeChart } from "echarts/charts";
  7. import { LabelLayout, UniversalTransition } from "echarts/features";
  8. import { CanvasRenderer } from "echarts/renderers";
  9. echarts.use([GaugeChart, LabelLayout, UniversalTransition, CanvasRenderer]);
  10. export default {
  11. props: ["value", "gears"],
  12. data() {
  13. return {
  14. mychart: null,
  15. option: {
  16. series: {
  17. name: "Pressure",
  18. type: "gauge",
  19. itemStyle: {
  20. color: "#FFFFFF",
  21. },
  22. startAngle: 180,
  23. max: 60,
  24. endAngle: 0,
  25. axisLine: {
  26. lineStyle: {
  27. width: 1,
  28. },
  29. },
  30. axisTick: {
  31. distance: 0,
  32. length: 10,
  33. lineStyle: {
  34. color: "#FFFFFF",
  35. },
  36. },
  37. splitLine: {
  38. length: 15,
  39. distance: 0,
  40. lineStyle: {
  41. color: "#FFFFFF",
  42. },
  43. },
  44. axisLabel: {
  45. distance: 8,
  46. color: "#FFFFFF",
  47. },
  48. progress: {
  49. show: true,
  50. },
  51. radius: "160%",
  52. center: ["50%", "90%"],
  53. detail: {
  54. offsetCenter: [0, -25],
  55. valueAnimation: true,
  56. formatter: (value) => {
  57. return `{value|${value.toFixed(0)}}{unit|km/h}\n{num|${
  58. this.gears
  59. }}`;
  60. },
  61. rich: {
  62. value: {
  63. fontSize: 20,
  64. fontWeight: "bolder",
  65. color: "#FFFFFF",
  66. },
  67. unit: {
  68. fontSize: 20,
  69. color: "#FFFFFF",
  70. padding: [0, 0, 0, 10],
  71. },
  72. num: {
  73. fontSize: 20,
  74. color: "#FFFFFF",
  75. padding: [0, 0, 0, 10],
  76. },
  77. },
  78. },
  79. pointer: {
  80. show: false,
  81. },
  82. data: [50],
  83. },
  84. },
  85. };
  86. },
  87. mounted() {
  88. this.mychart = echarts.init(document.getElementById("charts"));
  89. this.mychart.setOption(this.option);
  90. },
  91. watch: {
  92. value(v) {
  93. if (!this.mychart) return;
  94. if (v >= 100) v = 100;
  95. if (v <= 0) v = 0;
  96. this.option.series.data[0] = v;
  97. this.mychart.setOption(this.option);
  98. console.log(1, v);
  99. },
  100. },
  101. };
  102. </script>
  103. <style scoped>
  104. #charts {
  105. width: 500px;
  106. height: 200px;
  107. }
  108. </style>