@d3fc/d3fc-sample

中文文档
5.0.2发布于 2024/06/13MITESM + CJS近 30 天下载 8.7万

A data subsampler that chooses representative data points using a largest triangle or mode-median approach.

原始使用文档
文档目录6 节
  1. Installing
  2. API Reference
  3. Mode Median
  4. Largest Triangle One Bucket
  5. Largest Triangle Three Buckets
  6. Bucket

d3fc-sample

A D3 component for down-sampling data using a variety of methods. Data is typically partitioned into equally-sized buckets, and one data point from each bucket is chosen. The algorithms employed here are Largest Triangle 1 (or 3) Bucket, or Mode-Median as detailed in the thesis Downsampling Time Series for Visual Representation.

For a live demo, see the GitHub Pages site.

Main D3FC package

Installing

npm install @d3fc/d3fc-sample

API Reference

The sampling components provide an API for downsampling data. They are typically used to improve rendering performance of charts or maps when there is a significant amount of data. The data is passed to the component, which then returns a smaller downsampled array.

import {largestTriangleThreeBucket} from 'd3fc-sample';

// Create the sampler
const sampler = largestTriangleThreeBucket();

// Configure the x / y value accessors
sampler.x(d => d.x)
    .y(d => d.y);

// Configure the size of the buckets used to downsample the data.
sampler.bucketSize(10);

// Run the sampler
const sampledData = sampler(data);

Mode Median

The mode-median sampler is fairly basic. It partitions the data, then selects a representative piece of data from that set: the mode -- if it exists -- or the median.

# fc.modeMedian()

Constructs a new sampler.

# modeMedian(data)

Runs the sampler. It returns the downsampled data (it doesn't modify the data array itself). The downsampler selects the mode (if it exists), or the median value.

# modeMedian.value(accessorFunc)

Specifies the accessor function used to obtain the value from the supplied array of data. The accessor function is invoked exactly once per datum, and should return the value to be down-sampled.

# modeMedian.bucketSize(size)

Denotes the amount of data points for each bucket. The first and last data points are always their own bucket. The second-last bucket will be of size (data.length - 2) % size.

Largest Triangle One Bucket

Largest Triangle is a sampler where, given two pre-determined points, the point in the bucket that forms the largest triangle has the largest effective area and so is the most important in the bucket. The largest triangle algorithm comes in two flavours -- one bucket and three bucket. The algorithms are described in detail in the thesis Downsampling Time Series for Visual Representation, with a summary of their descriptions reproduced here:

This algorithm is very simple. First all the points are ranked by calculating their effective areas. Points with effective areas as null are excluded. The data points are then split up into approximately equal number of buckets as the specified downsample threshold. Finally, one point with the highest rank (largest effective area) is selected to represent each bucket in the downsampled data.

The effective area of a point is the area size of a triangle it forms with its two adjacent points.

# fc.largestTriangleOneBucket()

Constructs a new sampler.

#largestTriangle(data)

Runs the sampler, returning the downsampled data (it doesn't modify the data array itself). The sampler selects the point in the bucket with the largest area between two other points (determined by algorithm).

# largestTriangle.x(accessorFunc)

# largestTriangle.y(accessorFunc)

Specifies the accessor function used to obtain the x and y values from the supplied array of data. The accessor function is invoked exactly once per datum, and should return the value to be down-sampled.

# largestTriangle.bucketSize(size)

Denotes the amount of data points for each bucket. The first and last data points are always their own bucket. The second-last bucket will be of size (data.length - 2) % size.

Largest Triangle Three Buckets

The algorithm is similar to the above, except it works with three buckets at a time and proceeds from left to right. The first point which forms the left corner of the triangle (the effective area) is always fixed as the point that was previously selected and one of the points in the middle bucket shall be selected now.

The point used to form the triangle in the last bucket is a temporary point which is the average of all other points within that bucket.

# fc.largestTriangleThreeBucket()

Constructs a new sampler.

# largestTriangle(data)

Runs the sampler, returning the downsampled data (it doesn't modify the data array itself). The sampler selects the point in the bucket with the largest area between two other points (determined by algorithm).

# largestTriangle.x(accessorFunc)

# largestTriangle.y(accessorFunc)

Specifies the accessor function used to obtain the x and y values from the supplied array of data. The accessor function is invoked exactly once per datum, and should return the value to be down-sampled.

# largestTriangle.bucketSize(size)

Denotes the amount of data points for each bucket. The first and last data points are always their own bucket. The second-last bucket will be of size (data.length - 2) % size.

Bucket

d3fc-sample also comes with a data bucket utility, used by the algorithms. It partitions data into evenly-sized chunks.

# fc.bucket()

Construct a data bucket utility instance.

# bucket(data)

Partitions the data into evenly sized buckets, in the form:

// where n is the bucket size
[
    [data[0], data[1], ..., data[n - 1]],
    [data[n], data[n + 1], ..., data[2n - 1]],
    ...
    [..., data[data.length - 1]]
]

# bucket.bucketSize(size)

Denotes the amount of data points for each bucket. The last bucket will be of size data.length % size.

版本与发布记录

@d3fc/d3fc-sample 共发布 16 个版本,首次发布于 2018/08/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.2latest2024/06/132 年前
5.0.12020/07/146 年前
5.0.02020/04/166 年前
4.1.02020/03/196 年前
4.0.112019/11/286 年前
4.0.102019/09/047 年前

关于 @d3fc/d3fc-sample 的常见问题

@d3fc/d3fc-sample 是什么?

A data subsampler that chooses representative data points using a largest triangle or mode-median approach.

如何安装 @d3fc/d3fc-sample?

在项目目录运行 npm install @d3fc/d3fc-sample,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:d3-array(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@d3fc/d3fc-sample 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/d3fc__d3fc-sample 是否存在。

@d3fc/d3fc-sample 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@d3fc/d3fc-sample 可以商用吗?

@d3fc/d3fc-sample 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@d3fc/d3fc-sample 的下载量有多少?

根据 npm 官方下载统计,@d3fc/d3fc-sample 在 2026/10/11 – 2026/11/09 这 30 天内被下载 87,052 次,日均约 2,902 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@d3fc/d3fc-sample 还在维护吗?

最新版本 5.0.2 发布于 2024/06/13(2 年前);至今共发布 16 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/08/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。