uniapp中echart实例

1,自定义仪表盘

797_1706772047

index.vue

javascript 复制代码
import { useGaugeStore } from "@/stores/utils";
const { currentValueEndAngle, currentSplitNumber } = storeToRefs(
  useGaugeStore()
);
const gaugeStore = useGaugeStore();

const wenduGauge = ref<chartBaseOptionsType>({
  width: "400rpx",
  height: "400rpx",
  chartsId: "test",
  setOptions: {
    graphic: [
      
      {
        type: "text",
        left: 20,
        top: 184,
        scaleX: 1.4,
        scaleY: 1.4,
        style: {
          text: gaugeStore.minHeat + "°C",
          fill: "#007C84",
        },
      },
      {
        type: "text",
        left: 160,
        top: 184,
        scaleX: 1.4,
        scaleY: 1.4,
        style: {
          text: gaugeStore.maxHeat + "°C",
          fill: "#007C84",
        },
      },
    ],
    series: [],
  },
});


const setGaugeOption = () => {
  const dataArr = [
    {
      type: "gauge",
      radius: "90%",
      splitNumber: 1,
      axisLine: {
        show: true,
        lineStyle: {
          width: 1,
          opacity: 1,
          color: [[1, "#007C84"]],
        },
      },
      startAngle: gaugeStore.startAngle,
      endAngle: gaugeStore.endAngle,
      title: { show: false },
      detail: { show: false },
      splitLine: { show: false },
      axisTick: {
        length: 10,
        splitNumber: 60,
        lineStyle: {
          color: "rgba(0, 118, 135, 0.2)",
          width: 2,
        },
      },
      axisLabel: { show: false },
      pointer: { show: false },
      itemStyle: {},
    },
    {
      type: "gauge",
      radius: "90%",
      splitNumber: currentSplitNumber.value ? 1 : 0,
      axisLine: {
        show: false,
        lineStyle: {
          width: 0,
          opacity: 0,
        },
      },
      startAngle: gaugeStore.startAngle,
      endAngle: gaugeStore.getValueEndAngle(),
      title: { show: false },
      detail: {
        show: true,
        offsetCenter: [0, 0],
        color: "#00495F",
        fontSize: 34,
      },
      splitLine: { show: false },
      axisTick: {
        length: 11,
        splitNumber: currentSplitNumber.value,
        lineStyle: {
          color: "#23BAC4",
          width: 2,
        },
      },
      axisLabel: { show: false },
      pointer: { show: false },
      itemStyle: {},

      data: [{ value: popUpValue.value.temperature }],
    },
  ];
  wenduGauge.value.setOptions.series = dataArr;
};

const handlerHeat = (type: string) => {
  popUpValue.value.temperature =
    type === "jia"
      ? popUpValue.value.temperature + 1
      : popUpValue.value.temperature - 1;

  open();
};
const open = () => {
  gaugeStore.setCurrentValue(popUpValue.value.temperature);
  setGaugeOption();
};

utils.ts文件

javascript 复制代码
import { ref } from "vue";
import { defineStore } from "pinia";
export const useGaugeStore = defineStore("gauge", () => {
  const splitCount = 60; //刻度总数

  const startAngle = 225; // 开始角度

  const endAngle = -45; // 结束角度

  const totalAngle = 270; //总的角度

  const currentValueEndAngle = ref(0); //值现在的结束角度

  const currentSplitNumber = ref(0); //当前刻度数

  const maxHeat = 31;

  const minHeat = 16;

  const heatRange = maxHeat - minHeat;

  function setCurrentValue(currentHeat: number) {
    const a = totalAngle / splitCount;

    const b = (splitCount / heatRange) * (currentHeat - minHeat);
    console.log(a, b);

    currentSplitNumber.value = b;

    currentValueEndAngle.value = a * b;
    // 144 32
  }

  function getValueEndAngle() {
    return startAngle - currentValueEndAngle.value;
  }
  
  return {
    startAngle,
    endAngle,
    maxHeat,
    minHeat,
    currentValueEndAngle,
    currentSplitNumber,
    setCurrentValue,
    getValueEndAngle,
  };
});
相关推荐
gplitems1233 小时前
Consua WordPress Theme — Business Consulting Sites That Convert With Clarity
javascript
雾削木4 小时前
stm32解锁芯片
javascript·stm32·单片机·嵌入式硬件·gitee
2301_768350235 小时前
Vue第二期:组件及组件化和组件的生命周期
前端·javascript·vue.js
小周同学:5 小时前
Vue项目中将界面转换为PDF并导出的实现方案
javascript·vue.js·pdf
华洛6 小时前
公开一个AI产品的商业逻辑与设计方案——AI带来的涂色卡自由
前端·后端·产品
明远湖之鱼6 小时前
opentype.js 使用与文字渲染
前端·svg·字体
90后的晨仔7 小时前
Vue 3 组合式函数(Composables)全面解析:从原理到实战
前端·vue.js
今天头发还在吗7 小时前
【React】TimePicker进阶:解决开始时间可大于结束时间的业务场景与禁止自动排版
javascript·react.js·ant design
今天头发还在吗7 小时前
【React】动态SVG连接线实现:图片与按钮的可视化映射
前端·javascript·react.js·typescript·前端框架
小刘不知道叫啥7 小时前
React 源码揭秘 | suspense 和 unwind流程
前端·javascript·react.js