1.0.2 • Published 1 month ago

@tanzhenxing/zx-charts-scatter v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

zx-charts-scatter 散点图组件

组件简介

zx-charts-scatter 是基于 Vue3 + + uni-app 实现的高性能多端散点图表组件,支持 H5、微信/支付宝/百度/字节小程序、App 等平台。无需依赖浏览器专属 API,适合二维数据分布可视化场景。

属性(Props)

属性名类型默认值说明
legendObject{show: false, size: 24, color: '#333'}图例配置
tooltipBooleantrue是否显示 tooltip 提示
xAxisObject{min: 0, max: 100, splitNumber: 20}x轴配置
yAxisObject{min: 0, max: 100, splitNumber: 20}y轴配置
xAxisTickObject{height: '12rpx', color: '#e3e3e3'}x轴刻度线配置
yAxisTickObject{width: '12rpx', color: '#e3e3e3'}y轴刻度线配置
xAxisLineObject{color: '#e3e3e3', itemGap: 100}x轴线条配置
yAxisLineObject{color: '#e3e3e3', itemGap: 80}y轴线条配置
xAxisLabelObject{color: '#333', size: 24}x轴标签配置
yAxisLabelObject{color: '#333', size: 24}y轴标签配置
splitLineObject{color: '#e3e3e3', type: 'dashed'}分割线配置

draw 方法数据格式

通过 draw(dataset) 方法传入,格式如下:

[
  {
    name: 'A类样本',
    color: '#3b82f6',
    width: 18, // 散点大小(rpx)
    source: [
      [10, 20, 'A1'],
      [30, 40, 'A2']
    ]
  },
  {
    name: 'B类样本',
    color: '#f59e42',
    width: 18,
    source: [
      [20, 30, 'B1'],
      [40, 50, 'B2']
    ]
  }
]

事件

事件名说明回调参数
click点击某一散点时触发{ datasetIndex, sourceIndex }

方法

方法名说明参数
draw绘制/更新图表(dataset)

基本用法示例

详见下方示例或 src/pages/components/charts/scatter.vue

<template>
  <view class="demo-scatter">
    <zx-charts-scatter
      ref="scatterRef"
      :legend="{ show: true, size: 24, color: '#333' }"
      :tooltip="true"
      :xAxis="{ min: 0, max: 100, splitNumber: 20 }"
      :yAxis="{ min: 0, max: 100, splitNumber: 20 }"
      :xAxisLine="{ color: '#e3e3e3', itemGap: 100 }"
      :yAxisLine="{ color: '#e3e3e3', itemGap: 80 }"
      :xAxisTick="{ height: '12rpx', color: '#e3e3e3' }"
      :yAxisTick="{ width: '12rpx', color: '#e3e3e3' }"
      :xAxisLabel="{ color: '#333', size: 24 }"
      :yAxisLabel="{ color: '#333', size: 24 }"
      :splitLine="{ color: '#e3e3e3', type: 'dashed' }"
      @click="onScatterClick"
    />
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import zxChartsScatter from '@/components/zx-charts-scatter/zx-charts-scatter.vue';

const scatterRef = ref();
const dataset = [
  {
    name: 'A类样本',
    color: '#3b82f6',
    width: 18,
    source: [
      [10, 20, 'A1'],
      [30, 40, 'A2'],
      [50, 60, 'A3'],
      [70, 80, 'A4']
    ]
  },
  {
    name: 'B类样本',
    color: '#f59e42',
    width: 18,
    source: [
      [20, 30, 'B1'],
      [40, 50, 'B2'],
      [60, 70, 'B3'],
      [80, 90, 'B4']
    ]
  }
];

function onScatterClick(e) {
  const { datasetIndex, sourceIndex } = e;
  if (datasetIndex !== undefined && sourceIndex !== undefined) {
    const item = dataset[datasetIndex];
    const point = item.source[sourceIndex];
    uni.showToast({ title: `点击:${item.name} - ${point[2] || point[0] + ',' + point[1]}` });
  }
}

onMounted(() => {
  scatterRef.value.draw(dataset);
});
</script>

注意事项

  • 组件为多端兼容实现,不依赖 window/document。
  • 建议在页面 onReady 或 onMounted 后调用 draw 方法。
  • dataset.source 为二维数组,格式为 x, y, name?
  • 支持自定义颜色、图例、tooltip、坐标轴、分割线等。
1.0.2

1 month ago

1.0.1

2 months ago