Skip to content

Commit 0a12cf8

Browse files
LonelySnowmanxiaoluoHe
authored andcommitted
feat: linear bar add barpadding option
1 parent 4c3628f commit 0a12cf8

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

packages/vchart/src/chart/histogram/base/histogram-base-transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export class BaseHistogramChartSpecTransformer<T extends IHistogramChartSpec> ex
99
}
1010

1111
protected _getDefaultSeriesSpec(spec: T): any {
12-
return super._getDefaultSeriesSpec(spec, ['x2Field', 'y2Field', 'barMinHeight', 'barBackground']);
12+
return super._getDefaultSeriesSpec(spec, ['x2Field', 'y2Field', 'barMinHeight', 'barBackground', 'barPadding']);
1313
}
1414
}

packages/vchart/src/series/bar/bar.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,31 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
381381
return this.dataToPositionY1(datum);
382382
}
383383

384+
protected _getLinearBarRange = (x: number, x1: number) => {
385+
const realBarWidth = x1 - x;
386+
if (this._spec.barPadding) {
387+
const tempX = x + this._spec.barPadding;
388+
const tempX1 = x1 - this._spec.barPadding;
389+
if (tempX < tempX1) {
390+
x = tempX;
391+
x1 = tempX1;
392+
}
393+
}
394+
395+
const curBarWidth = x1 - x;
396+
if (this._spec.barMinWidth) {
397+
const barMinWidth = getActualNumValue(this._spec.barMinWidth, realBarWidth);
398+
if (curBarWidth < barMinWidth) {
399+
const widthDiff = barMinWidth - curBarWidth;
400+
const halfWidthDiff = widthDiff / 2;
401+
x -= halfWidthDiff;
402+
x1 += halfWidthDiff;
403+
}
404+
}
405+
406+
return [x, x1];
407+
};
408+
384409
protected _getBarXStart = (datum: Datum, scale: IBaseScale, useWholeRange?: boolean) => {
385410
if (this._shouldDoPreCalculate()) {
386411
this._calculateStackRectPosition(false);
@@ -403,6 +428,12 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
403428
return valueInScaleRange(this._dataToPosX1(datum), scale, useWholeRange);
404429
};
405430

431+
protected _getLinearBarXRange = (datum: Datum, scale: IBaseScale, useWholeRange?: boolean) => {
432+
const x = valueInScaleRange(this._dataToPosX(datum), scale, useWholeRange);
433+
const x1 = valueInScaleRange(this._dataToPosX1(datum), scale, useWholeRange);
434+
return this._getLinearBarRange(Math.min(x, x1), Math.max(x, x1));
435+
};
436+
406437
protected _getBarYStart = (datum: Datum, scale: IBaseScale) => {
407438
if (this._shouldDoPreCalculate()) {
408439
this._calculateStackRectPosition(true);
@@ -425,6 +456,12 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
425456
return valueInScaleRange(this._dataToPosY1(datum), scale);
426457
};
427458

459+
protected _getLinearBarYRange = (datum: Datum, scale: IBaseScale, useWholeRange?: boolean) => {
460+
const y = valueInScaleRange(this._dataToPosX(datum), scale, useWholeRange);
461+
const y1 = valueInScaleRange(this._dataToPosX1(datum), scale, useWholeRange);
462+
return this._getLinearBarRange(Math.min(y, y1), Math.max(y, y1));
463+
};
464+
428465
initBandRectMarkStyle() {
429466
const xScale = this._xAxisHelper?.getScale?.(0);
430467
const yScale = this._yAxisHelper?.getScale?.(0);
@@ -543,8 +580,8 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
543580
if (this.direction === Direction.horizontal) {
544581
const yChannels = isValid(this._fieldY2)
545582
? {
546-
y: (datum: Datum) => valueInScaleRange(this._dataToPosY(datum), yScale, true),
547-
y1: (datum: Datum) => valueInScaleRange(this._dataToPosY1(datum), yScale, true)
583+
y: (datum: Datum) => this._getLinearBarYRange(datum, yScale, true)[0],
584+
y1: (datum: Datum) => this._getLinearBarYRange(datum, yScale, true)[1]
548585
}
549586
: {
550587
y: (datum: Datum) =>
@@ -575,8 +612,8 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
575612
} else {
576613
const xChannels = isValid(this._fieldX2)
577614
? {
578-
x: (datum: Datum) => valueInScaleRange(this._dataToPosX(datum), xScale, true),
579-
x1: (datum: Datum) => valueInScaleRange(this._dataToPosX1(datum), xScale, true)
615+
x: (datum: Datum) => this._getLinearBarXRange(datum, xScale, true)[0],
616+
x1: (datum: Datum) => this._getLinearBarXRange(datum, xScale, true)[1]
580617
}
581618
: {
582619
x: (datum: Datum) =>

packages/vchart/src/series/bar/interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ export interface IBarSeriesSpec
119119
* 圆角支持回调配置 @since 1.12.4
120120
*/
121121
stackCornerRadius?: number | number[] | IStackCornerRadiusCallback;
122+
123+
/**
124+
* 柱条间 padding 值
125+
*/
126+
barPadding?: number;
122127
}
123128

124129
export interface IBarBackgroundSpec {

0 commit comments

Comments
 (0)