Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bar): add startValue option to specify where the bars start #17078

Merged
merged 6 commits into from
May 21, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/chart/bar/BarSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export interface BarSeriesOption

showBackground?: boolean

startValue?: number
Copy link
Member

Choose a reason for hiding this comment

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

This option is unused here and should be removed in the next version.


backgroundStyle?: ItemStyleOption & {
borderRadius?: number | number[]
}
Expand Down
1 change: 1 addition & 0 deletions src/coord/axisCommonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface AxisBaseOptionCommon extends ComponentOption,
* + null/undefined: auto decide max value (consider pretty look and boundaryGap).
*/
max?: ScaleDataValue | 'dataMax' | ((extent: {min: number, max: number}) => ScaleDataValue);
startValue?: number;

}

Expand Down
6 changes: 5 additions & 1 deletion src/coord/scaleRawExtentInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ export class ScaleRawExtentInfo {
const isOrdinal = this._isOrdinal = scale.type === 'ordinal';
this._needCrossZero = scale.type === 'interval' && model.getNeedCrossZero && model.getNeedCrossZero();

const modelMinRaw = this._modelMinRaw = model.get('min', true);
let axisMinValue = model.get('min', true);
if (axisMinValue == null) {
axisMinValue = model.get('startValue', true);
}
const modelMinRaw = this._modelMinRaw = axisMinValue;
if (isFunction(modelMinRaw)) {
// This callback alway provide users the full data extent (before data filtered).
this._modelMinNum = parseAxisModelMinMax(scale, modelMinRaw({
Expand Down
19 changes: 13 additions & 6 deletions src/layout/barGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,14 +512,13 @@ export function createProgressiveLayout(seriesType: string): StageHandler {
while ((dataIndex = params.next()) != null) {
const value = store.get(stacked ? stackedDimIdx : valueDimIdx, dataIndex);
const baseValue = store.get(baseDimIdx, dataIndex) as number;

let baseCoord = valueAxisStart;
let startValue;
let stackStartValue;

// Because of the barMinHeight, we can not use the value in
// stackResultDimension directly.
if (stacked) {
startValue = +value - (store.get(valueDimIdx, dataIndex) as number);
stackStartValue = +value - (store.get(valueDimIdx, dataIndex) as number);
}

let x;
Expand All @@ -530,7 +529,7 @@ export function createProgressiveLayout(seriesType: string): StageHandler {
if (isValueAxisH) {
const coord = cartesian.dataToPoint([value, baseValue]);
if (stacked) {
const startCoord = cartesian.dataToPoint([startValue, baseValue]);
const startCoord = cartesian.dataToPoint([stackStartValue, baseValue]);
baseCoord = startCoord[0];
}
x = baseCoord;
Expand All @@ -545,7 +544,7 @@ export function createProgressiveLayout(seriesType: string): StageHandler {
else {
const coord = cartesian.dataToPoint([baseValue, value]);
if (stacked) {
const startCoord = cartesian.dataToPoint([baseValue, startValue]);
const startCoord = cartesian.dataToPoint([baseValue, stackStartValue]);
baseCoord = startCoord[1];
}
x = coord[0] + columnOffset;
Expand Down Expand Up @@ -603,5 +602,13 @@ function isInLargeMode(seriesModel: BarSeriesModel) {

// See cases in `test/bar-start.html` and `#7412`, `#8747`.
function getValueAxisStart(baseAxis: Axis2D, valueAxis: Axis2D) {
return valueAxis.toGlobalCoord(valueAxis.dataToCoord(valueAxis.type === 'log' ? 1 : 0));
let startValue = valueAxis.model.get('startValue');
if (!startValue) {
startValue = 0;
}
return valueAxis.toGlobalCoord(
valueAxis.dataToCoord(
valueAxis.type === 'log'
? (startValue > 0 ? startValue : 1)
: startValue));
}
6 changes: 5 additions & 1 deletion src/layout/barPolar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import RadiusAxis from '../coord/polar/RadiusAxis';
import GlobalModel from '../model/Global';
import ExtensionAPI from '../core/ExtensionAPI';
import { Dictionary } from '../util/types';
import { PolarAxisModel } from '../coord/polar/AxisModel';

type PolarAxis = AngleAxis | RadiusAxis;

Expand Down Expand Up @@ -104,7 +105,10 @@ function barLayoutPolar(seriesType: string, ecModel: GlobalModel, api: Extension
const clampLayout = baseAxis.dim !== 'radius'
|| !seriesModel.get('roundCap', true);

const valueAxisStart = valueAxis.dataToCoord(0);
const valueAxisModel = valueAxis.model as PolarAxisModel;
const startValue = valueAxisModel.get('startValue');
const valueAxisStart = valueAxis.dataToCoord(startValue || 0);

for (let idx = 0, len = data.count(); idx < len; idx++) {
const value = data.get(valueDim, idx) as number;
const baseValue = data.get(baseDim, idx) as number;
Expand Down
225 changes: 225 additions & 0 deletions test/bar-startValue.html

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