当前位置: 首页 > article >正文

echarts实现 水库高程模拟图表

  • 需求背景
  • 解决思路
  • 解决效果
  • index.vue

需求背景

需要做一个水库高程模拟的图表,x轴是水平距离,y轴是高程,需要模拟改水库的形状
echarts 图表集链接

解决思路

配合ui切图,模拟水库形状

解决效果

在这里插入图片描述

index.vue

<!--/**
 * @author: liuk
 * @date: 2024-10-24
 * @describe: 水情图表
 */-->

<template>
  <div ref="chatDom" class="waterConditionChart"></div>
</template>

<script lang="ts" setup>
import {ref, onMounted, watch, nextTick} from "vue"
import * as echarts from 'echarts'
import "echarts-liquidfill"
import {formatToFixed, getEquiUnit} from "@/utils/dictionary";

// Props
const props = defineProps(['data'])

let myChart = null // Vue3 使用 proxy 对象代理,而 echarts 则使用了大量的全等(===), 对比失败从而导致了bug。
const chatDom = ref(null)

watch(() => props.data, (data) => {
  nextTick(() => {
    if (!myChart) return
    drawChart()
    const option = myChart.getOption()
    myChart.clear()
    myChart.setOption(renderFn(option, data))
  })
}, {immediate: true})

onMounted(() => {
  drawChart()
  window.addEventListener('resize', () => {
    drawChart()
    const option = myChart.getOption()
    myChart.clear()
    myChart.setOption(renderFn(option, props.data,))
  });
})

const renderFn = (option, data) => {
  // option.series[0].data[0].value = data.num || 0
  // option.series[0].max = data.total || 0
  return option
}

const drawChart = () => {
  let chartDom = chatDom.value
  if (chartDom == null) {
    return
  }
  echarts.dispose(chartDom)
  myChart = echarts.init(chartDom)
  const option = {
    title: [{
      top: 125,
      left: 50,
      text: "当前水位:33.15m",
      textStyle: {
        color: "#fff",
        fontSize: 12,
        fontWeight: "normal"
      },
    }],
    tooltip: {
      trigger: 'axis',
      padding: [5, 10, 5, 10],
      backgroundColor: 'rgba(0,0,0,.6)',
      borderColor: 'transparent',
      formatter: (param) => `
          <div class="waterConditionChart-popup">
            ${
          param.filter(item=>item.seriesName==="当前水位").map(item => `
                  <p class="item">
                      <i class="icon" style="background-color:${item.color || 'red'}"></i>
                      <span class="name">${item.seriesName}</span>
                      <span class="value"><b>${formatToFixed(item.data)}</b>${item.data !== '--' ? getEquiUnit(item.seriesName) : ''}</span>
                  </p>`).join("")
      }
          </div>
        `
    },
    grid: {
      top: '35',
      left: '0',
      right: '10',
      bottom: '0',
      containLabel: true,
    },
    xAxis: [{
      type: 'category',
      boundaryGap: false,
      nameGap: 5,
      axisLine: {
        show: true,
        lineStyle: {
          color: '#999'
        },
      },
      axisLabel: {
        color: '#9eaaba',
      },
      axisTick: {
        show: false,
      },
      data: ['0', '50', '100', '150', '200', '250', '300'],
    }],
    yAxis: [{
      name: '高程(m)',
      nameTextStyle: {
        color: '#9eaaba',
      },
      offset: 0,
      min: 20,
      max: 60,
      type: 'value',
      interval: 5,
      axisLabel: {
        show: true,
        color: "#9eaaba",
      },
      axisLine: {show: false},
      splitLine: {
        show: true,
        lineStyle: {
          width: 1,
          color: "rgba(49,105,129,0.4)",
          type: 'dashed'
        }
      },
    }],
    series: [
      {
        name:"当前水位",
        type: 'line',
        smooth: true, //是否平滑曲线显示
        symbolSize: 0,
        lineStyle: {
          width: 1
        },
        areaStyle: {
          color: "rgba(0, 145, 255,0.5)"
        },
        data: [33, 33, 33, 32, 26, 25, 24],
        z: 1
      },
      {
        type: 'line',
        symbolSize: 0,
        areaStyle: {
          color: "rgb(194,137,44)"
        },
        lineStyle: {
          width: 0
        },
        data: [25, 25, 25, 25, 25, 25, 25],
        z: 2
      },
      {
        type: 'line',
        symbolSize: 0,
        lineStyle: {
          width: 2,
          type: "dashed",
          color: "rgba(114, 255, 198, 1)"
        },
        data: [50, 50, 50, 50, 50, 50, 50],
        z: 2
      }
    ]
  }
  option && myChart.setOption(option)
}

</script>

<style lang="scss" scoped>
.waterConditionChart {
  width: 100%;
  height: 100%;
}
</style>
<style lang="scss">
.waterConditionChart-popup {
  overflow: hidden;
  font-size: 12px;
  color: #fff;

  .top {
    //margin-bottom: 16px;
    font-weight: bold;
  }

  .item {
    display: flex;
    align-items: center;
    margin: 5px 0;

    &:last-child {
      margin-bottom: 0;
    }

    .icon {
      display: inline-block;
      width: 12px;
      height: 12px;
      margin-right: 10px;
      border-radius: 50%;
      background: rgba(0, 166, 255, 1);
    }

    .name {
      width: 50px;
      margin-right: 10px;
    }

    .value {
      flex: 1;
      text-align: right;
    }
  }
}
</style>


http://www.kler.cn/news/367226.html

相关文章:

  • 传奇开服教程之新GOM引擎登录器配置教程
  • 抽象类与接口的特点和区别
  • Electron 离线环境打包解决方案(electron-forge)
  • 颐驰06持续交付,明日科技赋能出行生活
  • 网络通信与并发编程(六)线程、进程池与线程池
  • JS 中 reduce()方法及使用
  • Qt的信号槽机制学习一
  • k8s 部署 mysql 故障恢复记录
  • 【ESP32S3】VSCode 开发环境搭建
  • 打卡图论10.24
  • Qt元对象系统 —— 属性系统
  • 【论文阅读】Tabbed Out: Subverting the Android Custom Tab Security Model
  • 6.stm32 OLED显示屏
  • spring响应式编程
  • Python 流程控制专题:pass 与接口
  • 安全知识见闻-编程语言
  • Java面试题十一
  • idea历史版本下载
  • Redis 过期策略 总结
  • 过采样与欠采样技术原理图解:基于二维数据的常见方法效果对比
  • git学习(1)(简单概述、代码版本控制方式(集中/分布))
  • JAVA基础:多线程 (学习笔记)
  • Tesseract OCR 安装
  • Llama 3.2-Vision 多模态大模型本地运行教程
  • 中国人寿财险青岛市分公司:科技赋能,车险服务再升级
  • QThread finished Qt::DirectionConnection可能导致start()不会返回的问题