Skip to content

Commit

Permalink
New options for scale creation (#21)
Browse files Browse the repository at this point in the history
* New options for scale creation
  • Loading branch information
EugeneElkin authored and ignatvilesov committed Oct 11, 2017
1 parent ac831eb commit ca6cb3f
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.5.0
* Added two new optional parameters for CreateScale function -- innerPadding and useRangePoint. The first lets set inner padding for scale instead of recieve it from constant. The second lets use rangePoint instead of rangeBands function for creation of ordinal scale.

## 1.4.0
* Remove width restriction of title in legend
* Added new option to drawDefaultLabelsForDataPointChart function to control behavior of collided labels
Expand Down
6 changes: 5 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ declare module powerbi.extensibility.utils.chart.axis {
disableNice?: boolean;
/** (optional) Disable "niceing" for numeric axis. Disabling nice will be applid only when creating scale obj (bestTickCount will be applied to 'ticks' method) */
disableNiceOnlyForScale?: boolean;
/** (optional) InnerPadding to be applied to the axis.*/
innerPadding?: number;
/** (optioanl) Apply for using of RangePoints function instead of RangeBands inside CreateOrdinal scale function.*/
useRangePoints?: boolean;
}
enum AxisOrientation {
top = 0,
Expand Down Expand Up @@ -364,7 +368,7 @@ declare module powerbi.extensibility.utils.chart.axis {
function wordBreak(text: d3.Selection<any>, axisProperties: IAxisProperties, maxHeight: number): void;
function clip(text: d3.Selection<any>, availableWidth: number, svgEllipsis: (textElement: SVGTextElement, maxWidth: number) => void): void;
}
function createOrdinalScale(pixelSpan: number, dataDomain: any[], outerPaddingRatio?: number): d3.scale.Ordinal<any, any>;
function createOrdinalScale(pixelSpan: number, dataDomain: any[], outerPaddingRatio?: number, innerPaddingRatio?: number, useRangePoints?: boolean): d3.scale.Ordinal<any, any>;
function isLogScalePossible(domain: any[], axisType?: ValueType): boolean;
function createNumericalScale(axisScaleType: string, pixelSpan: number, dataDomain: any[], dataType: ValueType, outerPadding?: number, niceCount?: number, shouldClamp?: boolean): d3.scale.Linear<any, any>;
function createLinearScale(pixelSpan: number, dataDomain: any[], outerPadding?: number, niceCount?: number, shouldClamp?: boolean): d3.scale.Linear<any, any>;
Expand Down
19 changes: 13 additions & 6 deletions lib/index.js

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

2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powerbi-visuals-utils-chartutils",
"version": "1.4.1",
"version": "1.5.0",
"description": "ChartUtils",
"main": "lib/index.js",
"repository": {
Expand Down
19 changes: 13 additions & 6 deletions src/axis/axis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ module powerbi.extensibility.utils.chart.axis {

const TickLabelPadding: number = 2;
const MinOrdinalRectThickness: number = 20;
const InnerPaddingRatio: number = 0.2;

/**
* Default ranges are for when we have a field chosen for the axis,
Expand Down Expand Up @@ -760,7 +759,9 @@ module powerbi.extensibility.utils.chart.axis {
shouldClamp = !!options.shouldClamp,
maxTickCount = options.maxTickCount,
disableNice = options.disableNice,
disableNiceOnlyForScale = options.disableNiceOnlyForScale;
disableNiceOnlyForScale = options.disableNiceOnlyForScale,
innerPadding = options.innerPadding,
useRangePoint = options.useRangePoints;

let dataType: ValueType = getCategoryValueType(metaDataColumn, isScalar);

Expand All @@ -786,7 +787,7 @@ module powerbi.extensibility.utils.chart.axis {
dataDomain = [];

if (isOrdinal(dataType)) {
scale = createOrdinalScale(pixelSpan, dataDomain, categoryThickness ? outerPadding / categoryThickness : 0);
scale = createOrdinalScale(pixelSpan, dataDomain, categoryThickness ? outerPadding / categoryThickness : 0, innerPadding, useRangePoint);
}
else {
scale = createNumericalScale(options.scaleType, pixelSpan, dataDomain, dataType, outerPadding, bestTickCount);
Expand Down Expand Up @@ -824,7 +825,7 @@ module powerbi.extensibility.utils.chart.axis {
scale = createLinearScale(pixelSpan, scalarDomain, outerPadding, null, shouldClamp); // DO NOT PASS TICKCOUNT
}
else if (dataType.text || dataType.dateTime || dataType.numeric || dataType.bool) {
scale = createOrdinalScale(pixelSpan, scalarDomain, categoryThickness ? outerPadding / categoryThickness : 0);
scale = createOrdinalScale(pixelSpan, scalarDomain, categoryThickness ? outerPadding / categoryThickness : 0, innerPadding, useRangePoint);
bestTickCount = maxTicks === 0 ? 0
: Math.min(
scalarDomain.length,
Expand Down Expand Up @@ -1338,10 +1339,16 @@ module powerbi.extensibility.utils.chart.axis {
}
}

export function createOrdinalScale(pixelSpan: number, dataDomain: any[], outerPaddingRatio: number = 0): d3.scale.Ordinal<any, any> {
export function createOrdinalScale(pixelSpan: number, dataDomain: any[], outerPaddingRatio: number = 0, innerPaddingRatio: number = 0.2, useRangePoints: boolean = false): d3.scale.Ordinal<any, any> {
if (useRangePoints === true) {
return d3.scale.ordinal()
.rangePoints([0, pixelSpan], outerPaddingRatio)
.domain(dataDomain);
}

return d3.scale.ordinal()
/* Avoid using rangeRoundBands here as it is adding some extra padding to the axis*/
.rangeBands([0, pixelSpan], InnerPaddingRatio, outerPaddingRatio)
.rangeBands([0, pixelSpan], innerPaddingRatio, outerPaddingRatio)
.domain(dataDomain);
}

Expand Down
4 changes: 4 additions & 0 deletions src/axis/axisInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ module powerbi.extensibility.utils.chart.axis {
disableNice?: boolean;
/** (optional) Disable "niceing" for numeric axis. Disabling nice will be applid only when creating scale obj (bestTickCount will be applied to 'ticks' method) */
disableNiceOnlyForScale?: boolean;
/** (optional) InnerPadding to be applied to the axis.*/
innerPadding?: number;
/** (optioanl) Apply for using of RangePoints function instead of RangeBands inside CreateOrdinal scale function.*/
useRangePoints?: boolean;
}

export enum AxisOrientation {
Expand Down
41 changes: 40 additions & 1 deletion test/axis/axisTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ module powerbi.extensibility.utils.chart.axis.test {
};

// TODO: add a getValueFn mock to provide to createAxis so we can test tickValue generation

it("create ordinal scale - without categoryThickness", () => {
let axisProperties = axisPropertiesBuilder.buildAxisPropertiesString();

Expand All @@ -267,6 +266,46 @@ module powerbi.extensibility.utils.chart.axis.test {
expect(xLabelMaxWidth).toBeCloseTo(29.7, 1);
});

it("create ordinal scale with defined inner padding", () => {
let axisProperties = axisPropertiesBuilder.buildAxisPropertiesWithDefinedInnerPadding();

let scale = <any>axisProperties.scale;
expect(scale).toBeDefined();

let values = <any>axisProperties.values;
expect(values).toBeDefined();
expect(values.length).toEqual(3);
expect(values[0]).toBe("Sun");

// Proves scale is ordinal
expect(scale.invert).toBeUndefined();

// x label max width is derived from the scale interval
let xLabelMaxWidth = <any>axisProperties.xLabelMaxWidth;
expect(xLabelMaxWidth).toBeDefined();
expect(xLabelMaxWidth).toBeCloseTo(34, 1);
});

it("create ordinal scale with defined inner padding and using of RangePoints", () => {
let axisProperties = axisPropertiesBuilder.buildAxisPropertiesWithRangePointsUsing();

let scale = <any>axisProperties.scale;
expect(scale).toBeDefined();

let values = <any>axisProperties.values;
expect(values).toBeDefined();
expect(values.length).toEqual(3);
expect(values[0]).toBe("Sun");

// Proves scale is ordinal
expect(scale.invert).toBeUndefined();

// x label max width is derived from the scale interval
let xLabelMaxWidth = <any>axisProperties.xLabelMaxWidth;
expect(xLabelMaxWidth).toBeDefined();
expect(xLabelMaxWidth).toBeCloseTo(44, 1);
});

it("create ordinal scale with linear values", () => {
let axisProperties = axisPropertiesBuilder.buildAxisPropertiesNumber();

Expand Down
13 changes: 13 additions & 0 deletions test/axis/helpers/axisPropertiesBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ module powerbi.extensibility.utils.chart.axis.test.helper {
return axis.createAxis(axisOptions);
}

export function buildAxisPropertiesWithDefinedInnerPadding(): IAxisProperties {
let axisOptions = getAxisOptions(metaDataColumnText);
axisOptions.innerPadding = 0.5;
return axis.createAxis(axisOptions);
}

export function buildAxisPropertiesWithRangePointsUsing(): IAxisProperties {
let axisOptions = getAxisOptions(metaDataColumnText);
axisOptions.innerPadding = 0.5;
axisOptions.useRangePoints = true;
return axis.createAxis(axisOptions);
}

export function buildAxisPropertiesNumeric(
dataDomain: any[],
categoryThickness?: number,
Expand Down

0 comments on commit ca6cb3f

Please sign in to comment.