Skip to content

Commit

Permalink
Merge pull request #107 from VisActor/release/0.16.0
Browse files Browse the repository at this point in the history
[Auto release] release 0.16.0
  • Loading branch information
xuefei1313 authored Sep 20, 2023
2 parents a2aa0b5 + ab12f38 commit e23d33d
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 38 deletions.
4 changes: 2 additions & 2 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions common/config/rush/version-policies.json
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
[
{
"definitionName": "lockStepVersion",
"policyName": "vutilMain",
"version": "0.15.14",
"nextBump": "patch"
}
]
[{"definitionName":"lockStepVersion","policyName":"vutilMain","version":"0.16.0","nextBump":"minor"}]
6 changes: 6 additions & 0 deletions packages/vdataset/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"name": "@visactor/vdataset",
"entries": [
{
"version": "0.16.0",
"tag": "@visactor/vdataset_v0.16.0",
"date": "Thu, 14 Sep 2023 08:04:22 GMT",
"comments": {}
},
{
"version": "0.15.14",
"tag": "@visactor/vdataset_v0.15.14",
Expand Down
7 changes: 6 additions & 1 deletion packages/vdataset/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log - @visactor/vdataset

This log was last generated on Wed, 13 Sep 2023 09:03:21 GMT and should not be manually modified.
This log was last generated on Thu, 14 Sep 2023 08:04:22 GMT and should not be manually modified.

## 0.16.0
Thu, 14 Sep 2023 08:04:22 GMT

_Version update only_

## 0.15.14
Wed, 13 Sep 2023 09:03:21 GMT
Expand Down
4 changes: 2 additions & 2 deletions packages/vdataset/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@visactor/vdataset",
"version": "0.15.14",
"version": "0.16.0",
"main": "cjs/index.js",
"module": "es/index.js",
"types": "es/index.d.ts",
Expand Down Expand Up @@ -35,7 +35,7 @@
"test-cov": "jest -w 16 --coverage"
},
"dependencies": {
"@visactor/vutils": "workspace:0.15.14",
"@visactor/vutils": "workspace:0.16.0",
"@turf/flatten": "^6.5.0",
"@turf/helpers": "^6.5.0",
"@turf/rewind": "^6.5.0",
Expand Down
24 changes: 24 additions & 0 deletions packages/vscale/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
{
"name": "@visactor/vscale",
"entries": [
{
"version": "0.16.0",
"tag": "@visactor/vscale_v0.16.0",
"date": "Thu, 14 Sep 2023 08:04:22 GMT",
"comments": {
"none": [
{
"comment": "feat: band scale support fixed band width"
},
{
"comment": "fix: solve the invalid rangeFactor() method of band scale"
},
{
"comment": "fix: update exported types of `IBandLikeScale`"
},
{
"comment": "fix: the start value or end value of rangeFactor in band scale is auto-reserved"
},
{
"comment": "fix: keep the `bandScale.calculateWholeRangeSize()` return result positive"
}
]
}
},
{
"version": "0.15.14",
"tag": "@visactor/vscale_v0.15.14",
Expand Down
13 changes: 12 additions & 1 deletion packages/vscale/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Change Log - @visactor/vscale

This log was last generated on Wed, 13 Sep 2023 09:03:21 GMT and should not be manually modified.
This log was last generated on Thu, 14 Sep 2023 08:04:22 GMT and should not be manually modified.

## 0.16.0
Thu, 14 Sep 2023 08:04:22 GMT

### Updates

- feat: band scale support fixed band width
- fix: solve the invalid rangeFactor() method of band scale
- fix: update exported types of `IBandLikeScale`
- fix: the start value or end value of rangeFactor in band scale is auto-reserved
- fix: keep the `bandScale.calculateWholeRangeSize()` return result positive

## 0.15.14
Wed, 13 Sep 2023 09:03:21 GMT
Expand Down
4 changes: 2 additions & 2 deletions packages/vscale/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@visactor/vscale",
"version": "0.15.14",
"version": "0.16.0",
"description": "Scales for visual encoding, used in VGrammar, VTable",
"keywords": [
"scale",
Expand Down Expand Up @@ -34,7 +34,7 @@
"test-cov": "jest -w 16 --coverage"
},
"dependencies": {
"@visactor/vutils": "workspace:0.15.14"
"@visactor/vutils": "workspace:0.16.0"
},
"devDependencies": {
"@internal/bundler": "workspace:*",
Expand Down
162 changes: 148 additions & 14 deletions packages/vscale/src/band-scale.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import { range, toNumber, isGreater, isLess } from '@visactor/vutils';
import { range, toNumber, isGreater, isLess, isNumber } from '@visactor/vutils';
import { OrdinalScale } from './ordinal-scale';
import { bandSpace } from './utils/utils';
import { bandSpace, calculateBandwidthFromWholeRangeSize, scaleWholeRangeSize } from './utils/utils';
import { ScaleEnum } from './type';
import { stepTicks, ticks } from './utils/tick-sample-int';
import type { DiscreteScaleType, IBandLikeScale, TickData } from './interface';

// band scale 各参数参考图示 https://raw.githubusercontent.com/d3/d3-scale/master/img/band.png
export class BandScale extends OrdinalScale implements IBandLikeScale {
readonly type: DiscreteScaleType = ScaleEnum.Band;
protected _step?: number;
protected _bandwidth?: number;
/** 是否固定了 bandwidth */
protected _isFixed?: boolean;
/** 用户配置的 bandwidth */
protected _userBandwidth?: number | 'auto';
protected _maxBandwidth?: number;
protected _minBandwidth?: number;
protected _round: boolean;
protected _paddingInner: number;
protected _paddingOuter: number;
Expand All @@ -19,6 +27,7 @@ export class BandScale extends OrdinalScale implements IBandLikeScale {
this._range = [0, 1];
this._step = undefined;
this._bandwidth = undefined;
this._isFixed = false;
this._round = false;
this._paddingInner = 0;
this._paddingOuter = 0;
Expand All @@ -34,7 +43,7 @@ export class BandScale extends OrdinalScale implements IBandLikeScale {
return this;
}
this._wholeRange = null;
const wholeRange = this._calculateRange(this._range);
const wholeRange = this._calculateWholeRange(this._range);
const n = super.domain().length;
const reverse = wholeRange[1] < wholeRange[0];
let start = wholeRange[Number(reverse) - 0];
Expand All @@ -46,18 +55,90 @@ export class BandScale extends OrdinalScale implements IBandLikeScale {
this._step = Math.floor(this._step);
}
start += (stop - start - this._step * (n - this._paddingInner)) * this._align;
this._bandwidth = this._step * (1 - this._paddingInner);
if (!this.isBandwidthFixed()) {
this._bandwidth = this._step * (1 - this._paddingInner);
}
if (this._round) {
start = Math.round(start);
this._bandwidth = Math.round(this._bandwidth);
if (!this.isBandwidthFixed()) {
this._bandwidth = Math.round(this._bandwidth);
}
}
const values = range(n).map(i => {
const values = range(n).map((i: number) => {
return start + this._step * i;
});
super.range(reverse ? values.reverse() : values);
return this;
}

/**
* 根据可见 range 计算 scale 的整体 range
* @param range 可见 range
* @returns
*/
protected _calculateWholeRange(range: any[]) {
if (this._wholeRange) {
return this._wholeRange;
}

if ((this._minBandwidth || this._maxBandwidth) && !this._isBandwidthFixedByUser()) {
const autoBandwidth = calculateBandwidthFromWholeRangeSize(
super.domain().length,
Math.abs(range[1] - range[0]),
this._paddingInner,
this._paddingOuter,
this._round
);
if (autoBandwidth < this._minBandwidth) {
this._bandwidth = this._minBandwidth;
this._isFixed = true;
} else if (autoBandwidth > this._maxBandwidth) {
this._bandwidth = this._maxBandwidth;
this._isFixed = true;
} else {
this._bandwidth = undefined;
this._isFixed = false;
}
}

if (this.isBandwidthFixed()) {
const wholeLength = scaleWholeRangeSize(
super.domain().length,
this._bandwidth,
this._paddingInner,
this._paddingOuter
);

const rangeFactorSize = Math.min((range[1] - range[0]) / wholeLength, 1);
if (this._rangeFactor?.length) {
const [rangeFactorStart, rangeFactorEnd] = this._rangeFactor;
const r0 = range[0] - wholeLength * rangeFactorStart;
const r1 = r0 + wholeLength;
this._wholeRange = [r0, r1];

if (rangeFactorStart + rangeFactorSize <= 1) {
this._rangeFactor = [rangeFactorStart, rangeFactorStart + rangeFactorSize];
} else if (rangeFactorEnd - rangeFactorSize >= 0) {
this._rangeFactor = [rangeFactorEnd - rangeFactorSize, rangeFactorEnd];
} else {
this._rangeFactor = [0, rangeFactorSize];
}
} else {
this._rangeFactor = [0, rangeFactorSize];
this._wholeRange = [range[0], range[0] + wholeLength];
}

return this._wholeRange;
}

return super._calculateWholeRange(range);
}

calculateWholeRangeSize() {
const wholeRange = this._calculateWholeRange(this._range);
return Math.abs(wholeRange[1] - wholeRange[0]);
}

calculateVisibleDomain(range: any[]) {
const domain = this._domain;

Expand Down Expand Up @@ -148,15 +229,15 @@ export class BandScale extends OrdinalScale implements IBandLikeScale {
// 找到index
let i = 0;
const halfStep = this.step() / 2;
const halfBandWidth = this.bandwidth() / 2;
const halfBandwidth = this.bandwidth() / 2;
const len = this._domain.length;
const range = this.range();
const start = range[0];
const stop = range[range.length - 1];
const reverse = start > stop;

for (i = 0; i < len; i++) {
const r = this.scale(this._domain[i]) + halfBandWidth;
const r = this.scale(this._domain[i]) + halfBandwidth;

if (i === 0 && ((!reverse && !isGreater(d, r + halfStep)) || (reverse && !isLess(d, r - halfStep)))) {
break;
Expand Down Expand Up @@ -213,10 +294,6 @@ export class BandScale extends OrdinalScale implements IBandLikeScale {
return this._paddingOuter;
}

bandwidth(): number {
return this._bandwidth;
}

step(): number {
return this._step;
}
Expand Down Expand Up @@ -252,13 +329,70 @@ export class BandScale extends OrdinalScale implements IBandLikeScale {
return this.rescale(slience);
}

bandwidth(): number;
bandwidth(_: number | 'auto', slience?: boolean): this;
bandwidth(_?: number | 'auto', slience?: boolean): this | number {
if (!_) {
return this._bandwidth;
}
if (_ === 'auto') {
this._bandwidth = undefined;
this._isFixed = false;
} else {
this._bandwidth = _;
this._isFixed = true;
}
this._userBandwidth = _;
return this.rescale(slience);
}

maxBandwidth(): number;
maxBandwidth(_: number | 'auto', slience?: boolean): this;
maxBandwidth(_?: number | 'auto', slience?: boolean): this | number {
if (!_) {
return this._maxBandwidth;
}
if (_ === 'auto') {
this._maxBandwidth = undefined;
} else {
this._maxBandwidth = _;
}
return this.rescale(slience);
}

minBandwidth(): number;
minBandwidth(_: number | 'auto', slience?: boolean): this;
minBandwidth(_?: number | 'auto', slience?: boolean): this | number {
if (!_) {
return this._minBandwidth;
}
if (_ === 'auto') {
this._minBandwidth = undefined;
} else {
this._minBandwidth = _;
}
return this.rescale(slience);
}

isBandwidthFixed() {
return this._isFixed && !!this._bandwidth;
}

protected _isBandwidthFixedByUser() {
return this._isFixed && this._userBandwidth && isNumber(this._userBandwidth);
}

clone(): IBandLikeScale {
return new BandScale(true)
const bandScale = new BandScale(true)
.domain(this._domain, true)
.range(this._range, true)
.round(this._round, true)
.paddingInner(this._paddingInner, true)
.paddingOuter(this._paddingOuter, true)
.align(this._align);
.align(this._align, true)
.bandwidth(this._userBandwidth ?? 'auto', true)
.maxBandwidth(this._maxBandwidth ?? 'auto', true)
.minBandwidth(this._maxBandwidth ?? 'auto');
return bandScale;
}
}
2 changes: 1 addition & 1 deletion packages/vscale/src/base-scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export abstract class BaseScale implements IRangeFactor {
abstract domain(): any[];
abstract invert(d: any): any;

protected _calculateRange(range: any[]) {
protected _calculateWholeRange(range: any[]) {
if (this._wholeRange) {
return this._wholeRange;
}
Expand Down
Loading

0 comments on commit e23d33d

Please sign in to comment.