ucharts写的折线图实现手指滑动根据滑动的位置显示相关的数据信息(vue3写法)

<template>
  <view class="container">
    <!-- 图表容器 -->
    <qiun-data-charts type="line" :chartData="chartData" :opts="chartOptions" @touchmove="handleTouch" />
    <!-- 自定义提示框 -->
    <view v-if="showToolTip" class="tooltip" :style="{ top: tooltipTop + 'px', left: tooltipLeft + 'px' }">
      <view class="tooltip-content">
        <view class="tooltip-item">日期:{{ time }}</view>
        <view class="tooltip-item" v-for="item in seriesData" :key="item.name">
          {{ item.name }}: {{ item.value }}
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, getCurrentInstance } from 'vue';

// 获取当前组件实例
const instance = getCurrentInstance();

// 图表数据
const chartData = reactive({
  categories: ["1月", "2月", "3月", "4月", "5月", "6月", "7月"],
  series: [
    {
      name: "成交量A",
      data: [35, 8, 25, 37, 4, 20, 15]
    },
    {
      name: "成交量B",
      data: [70, 40, 65, 100, 44, 68, 50]
    }
  ]
});

// 图表配置项
const chartOptions = reactive({
  color: ["#1890FF", "#FACC14"],
  padding: [15, 15, 0, 15],
  legend: {
    show: true,
    position: "bottom",
    float: "center"
  },
  xAxis: {
    disableGrid: true
  },
  yAxis: {
    gridType: "dash",
    dashLength: 2
  },
  extra: {
    line: {
      type: "curve"
    }
  }
});

// 提示框状态
const showToolTip = ref(false);
const time = ref(''); // 日期
const seriesData = ref([]); // 系列数据
const tooltipTop = ref(0);
const tooltipLeft = ref(0);

// 处理触摸事件
function handleTouch(e: any) {
  // 获取触摸点的坐标
  const touchX = e.changedTouches[0].pageX;
  const touchY = e.changedTouches[0].pageY;

  // 使用 wx.createSelectorQuery 获取图表容器的尺寸和位置
  const query = wx.createSelectorQuery().in(instance?.proxy);
  query.select('.container').boundingClientRect((rect) => {
    if (!rect) {
      console.error('无法获取图表容器的尺寸和位置');
      return;
    }

    // 获取图表容器的尺寸和位置
    const chartWidth = rect.width;
    const chartHeight = rect.height;
    const chartLeft = rect.left;
    const chartTop = rect.top;

    // 计算触摸点在图表中的相对位置
    const relativeX = touchX - chartLeft;
    const relativeY = touchY - chartTop;

    // 计算触摸点对应的图表数据索引
    const dataLength = chartData.series[0].data.length;
    const index = Math.floor((relativeX / chartWidth) * dataLength);

    // 确保索引在有效范围内
    if (index >= 0 && index < dataLength) {
      // 获取对应的时间
      time.value = chartData.categories[index];

      // 获取每个系列的数据值
      seriesData.value = chartData.series.map(series => ({
        name: series.name,
        value: series.data[index]
      }));

      // 更新提示框的内容和位置
      showToolTip.value = true;
      tooltipTop.value = touchY - 20; // 调整提示框位置,使其在触摸点上方
      tooltipLeft.value = touchX - 20; // 调整提示框位置,使其在触摸点左侧
    }
  }).exec();
}
</script>

<style>
.container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative; /* 确保提示框可以相对于容器定位 */
}

.tooltip {
  position: absolute;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 14px;
  display: flex;
  flex-direction: column; /* 使内容竖着排列 */
}
.tooltip-item {
  margin-bottom: 5px; /* 每行之间的间距 */
}
</style>

`qiun-data-charts`是一个基于JavaScript的数据可视化库,它提供了一系列图表绘制功能,并支持自定义样式和数据处理。在创建折线图时,如果希望按照不同区间展示背景颜色,通常需要通过设置区域填充的颜色属性来进行操作。 以下是利用 `qiun-data-charts` 实现按不同区间展示背景颜色的基本步骤: ### 步骤一:引入库 首先,在HTML文件中引入`qiun-data-charts`库。你可以从其官方文档下载最新版本或直接通过CDN链接添加到项目中。 ```html <!-- 示例引入链接 --> <script src="https://cdn.jsdelivr.net/npm/qiun-data-charts@latest/dist/index.js"></script> ``` ### 步骤二:准备数据 假设我们有以下数据集,用于构建折线图并应用不同区间的背景颜色: ```javascript const data = [ { x: 0, y: 1 }, { x: 5, y: 4 }, { x: 10, y: 7 }, // ... 更多数据点 ]; ``` ### 步骤三:配置和绘制图表 在JavaScript代码中,你需要初始化图表实例,并使用合适的选项来指定折线图及其分段背景色: ```javascript qiunDataCharts({ element: &#39;chart&#39;, // 图表容器元素ID type: &#39;line&#39;, data, options: { series: [{ name: &#39;数据系列&#39;, type: &#39;line&#39;, color: &#39;#ff0000&#39;, // 默认线条颜色 lineStyle: { width: 3, } }], xAxis: { min: 0, max: 10, interval: 5, // 横轴刻度间隔 }, yAxis: { min: 0, max: 10, interval: 5, }, regions: [ // 定义区间及对应颜色 { from: 0, to: 3, backgroundColor: &#39;#ffff99&#39; }, { from: 3, to: 6, backgroundColor: &#39;#ffcc99&#39; }, { from: 6, to: 10, backgroundColor: &#39;#ff9999&#39; } ] } }).render(); ``` 在这个例子中,我们指定了三个不同的区间(分别从0至33至6、6至10),并在每个区间上设置了相应的背景颜色。`regions`数组包含了区间定义以及对应的背景颜色值。 ### 相关问题: 1. **如何调整背景颜色的透明度?** 可以通过修改 `backgroundColor` 的颜色值以包含透明度参数,例如使用 `rgba(r, g, b, a)` 格式,其中 `a` 表示alpha通道透明度。 2. **能否动态更改背景颜色?** 是的,可以通过监听数据变化并更新 `regions` 数组来实现实时更改背景颜色的功能。 3. **如何将折线图转换为面积图并保持颜色效果一致?** 转换为面积图通常只需要改变 `type` 属性即可,如将 `type: &#39;line&#39;` 改为 `type: &#39;area&#39;`,同时保留原有的颜色和区域定义以保持一致性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值