Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@visactor/vchart",
"comment": "feat: support barpadding for histogram for #2629",
"type": "none"
}
],
"packageName": "@visactor/vchart"
}
6 changes: 5 additions & 1 deletion docs/assets/option/en/chart/histogram.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ y2 field. (y field is the left interval field of frequency statistics, y2 field
{{ use: chart-component(
axisType = 'cartesian',
noBandAxis = true
) }}
) }}

## barPadding(number)

The barPadding use to adjust the distance between each bar in the histogram.
7 changes: 5 additions & 2 deletions docs/assets/option/zh/chart/histogram.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@

## x2Field(string|string[])

x2 字段。(x字段为频率统计左区间字段,x2字段为频率统计右区间字段
x2 字段。(x 字段为频率统计左区间字段,x2 字段为频率统计右区间字段

## y2Field(string|string[])

y2 字段。(y字段为频率统计左区间字段,y2字段为频率统计右区间字段
y2 字段。(y 字段为频率统计左区间字段,y2 字段为频率统计右区间字段

{{ use: chart-component(
axisType = 'cartesian',
noBandAxis = true
) }}

## barPadding(number)

barPadding 字段用于调整直方图中每个柱子之间的距离。
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class BaseHistogramChartSpecTransformer<T extends IHistogramChartSpec> ex
x2Field: spec?.x2Field,
y2Field: spec?.y2Field,
barMinHeight: spec?.barMinHeight,
barBackground: spec?.barBackground
barBackground: spec?.barBackground,
barPadding: spec?.barPadding
};
}
}
36 changes: 30 additions & 6 deletions packages/vchart/src/series/bar/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,6 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
}

this._initStackBarMarkStyle();

this._initBandBarBackgroundMarkStyle();
}

Expand Down Expand Up @@ -528,12 +527,36 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
initLinearRectMarkStyle() {
const xScale = this._xAxisHelper?.getScale?.(0);
const yScale = this._yAxisHelper?.getScale?.(0);
const barPadding = this._spec.barPadding || 0;

/**
*
* @description 用于计算不同场景下barPadding对应的柱坐标位移值
*/
const barPaddingCompute = (field: 'x' | 'x1' | 'y' | 'y1', inverse: boolean) => {
let acturlPadding = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个写法还是挺写死的,
如果出现了 x1 对应的值 < x 的情况,还是会出错
也就是说这种写法假设了x1一定要大于x
要代码写的更严谨一点,可以基于vgrammar 的transform机制实现,

在完成所有的视觉通道映射后,来对 x,x1或者 y\y1 进行调整,这样能覆盖更多的场景

可以参考这个:

https://github.com/VisActor/VGrammar/blob/develop/packages/vgrammar-core/src/transforms/mark/mark-overlap.ts

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的谢谢,我研究下

if (inverse) {
if (field === 'x1' || field === 'y1') {
acturlPadding = barPadding / 2;
} else {
acturlPadding = -barPadding / 2;
}
} else {
if (field === 'x' || field === 'y') {
acturlPadding = barPadding / 2;
} else {
acturlPadding = -barPadding / 2;
}
}
return acturlPadding;
};

if (this.direction === Direction.horizontal) {
const inverse = this._yAxisHelper?.isInverse();
const yChannels = isValid(this._fieldY2)
? {
y: (datum: Datum) => valueInScaleRange(this._dataToPosY(datum), yScale),
y1: (datum: Datum) => valueInScaleRange(this._dataToPosY1(datum), yScale)
y: (datum: Datum) => valueInScaleRange(this._dataToPosY(datum) + barPaddingCompute('y', inverse), yScale),
y1: (datum: Datum) => valueInScaleRange(this._dataToPosY1(datum) + barPaddingCompute('y1', inverse), yScale)
}
: {
y: (datum: Datum) =>
Expand Down Expand Up @@ -562,10 +585,12 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
AttributeLevel.Series
);
} else {
const inverse = this._xAxisHelper?.isInverse();

const xChannels = isValid(this._fieldX2)
? {
x: (datum: Datum) => valueInScaleRange(this._dataToPosX(datum), xScale),
x1: (datum: Datum) => valueInScaleRange(this._dataToPosX1(datum), xScale)
x: (datum: Datum) => valueInScaleRange(this._dataToPosX(datum) + barPaddingCompute('x', inverse), xScale),
x1: (datum: Datum) => valueInScaleRange(this._dataToPosX1(datum) + barPaddingCompute('x1', inverse), xScale)
}
: {
x: (datum: Datum) =>
Expand Down Expand Up @@ -740,7 +765,6 @@ export class BarSeries<T extends IBarSeriesSpec = IBarSeriesSpec> extends Cartes
const barInGroup = array(this._spec.barGapInGroup);
let totalWidth: number = 0;
let offSet: number = 0;

for (let index = groupFields.length - 1; index >= 1; index--) {
const groupField = groupFields[index];
// const groupValues = this.getViewDataStatistics()?.latestData?.[groupField]?.values ?? [];
Expand Down
5 changes: 5 additions & 0 deletions packages/vchart/src/series/bar/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ export interface IBarSeriesSpec
* @since 1.10.0
*/
stackCornerRadius?: number | number[];

/**
* 柱条之间的距离
*/
barPadding?: number;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是理解有误,需求上是实现“分类轴”上的padding
堆叠柱是数值轴上的

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块儿我注释写的有问题,我改改。

}

export interface IBarBackgroundSpec {
Expand Down