Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 128a44c

Browse files
committed
Now rounding computed min and max.
1 parent 89149ca commit 128a44c

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "data-forge-plot",
3-
"version": "0.3.3",
3+
"version": "0.3.4",
44
"description": "Plotting API for use with Data-Forge.",
55
"main": "build/index.js",
66
"types": "build/index.d.ts",

src/apply-defaults.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ function extractValues(data: ISerializedDataFrame, seriesConfigs: IYAxisSeriesCo
1414
return flattened;
1515
}
1616

17-
function findMin(values: number[]): number {
18-
return Math.min(...values.filter(v => v !== undefined && v !== null && !Number.isNaN(v)));
17+
function computeMin(values: number[]): number {
18+
return Math.floor(Math.min(...values.filter(v => v !== undefined && v !== null && !Number.isNaN(v))) * 100) / 100;
1919
}
2020

21-
function findMax(values: number[]): number {
22-
return Math.max(...values.filter(v => v !== undefined && v !== null && !Number.isNaN(v) && Number.isFinite(v)));
21+
function computeMax(values: number[]): number {
22+
return Math.ceil(Math.max(...values.filter(v => v !== undefined && v !== null && !Number.isNaN(v) && Number.isFinite(v))) * 100) / 100;
2323
}
2424

2525
//
@@ -87,7 +87,7 @@ export function applyDefaults(inputChartDef: IChartDef, plotDefaults?: IPlotConf
8787
y1Values = extractValues(chartDef.data, chartDef.axisMap.y);
8888

8989
if (y1Values.length > 0) {
90-
chartDef.plotConfig.y.min = findMin(y1Values);
90+
chartDef.plotConfig.y.min = computeMin(y1Values);
9191
}
9292
}
9393

@@ -97,7 +97,7 @@ export function applyDefaults(inputChartDef: IChartDef, plotDefaults?: IPlotConf
9797
}
9898

9999
if (y1Values.length > 0) {
100-
chartDef.plotConfig.y.max = findMax(y1Values);
100+
chartDef.plotConfig.y.max = computeMax(y1Values);
101101
}
102102
}
103103

@@ -110,7 +110,7 @@ export function applyDefaults(inputChartDef: IChartDef, plotDefaults?: IPlotConf
110110
if (chartDef.plotConfig.y2.min === undefined) {
111111
y2Values = extractValues(chartDef.data, chartDef.axisMap.y2);
112112
if (y2Values.length > 0) {
113-
chartDef.plotConfig.y2.min = findMin(y2Values);
113+
chartDef.plotConfig.y2.min = computeMin(y2Values);
114114
}
115115
}
116116

@@ -120,7 +120,7 @@ export function applyDefaults(inputChartDef: IChartDef, plotDefaults?: IPlotConf
120120
}
121121

122122
if (y2Values.length > 0) {
123-
chartDef.plotConfig.y2.max = findMax(y2Values);
123+
chartDef.plotConfig.y2.max = computeMax(y2Values);
124124
}
125125
}
126126

src/test/apply-defaults.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,55 @@ describe("apply defaults", () => {
464464
expect(chartDef.plotConfig.y2!.min).toBeUndefined();
465465
expect(chartDef.plotConfig.y2!.max).toBeUndefined();
466466
});
467+
468+
it("computed min and max are rounded", () => {
469+
470+
const testData = {
471+
columnOrder: [ "a" ],
472+
columns: {
473+
a: "number",
474+
b: "number",
475+
},
476+
index: {
477+
type: "number",
478+
values: [2, 3, 4],
479+
},
480+
values: [
481+
{
482+
a: 10.123456,
483+
},
484+
{
485+
a: 20,
486+
},
487+
{
488+
a: 30.01234567,
489+
},
490+
],
491+
};
492+
493+
const inputChartDef: any = {
494+
data: testData,
495+
plotConfig: {
496+
y: {
497+
},
498+
y2: {
499+
},
500+
},
501+
axisMap: {
502+
y: [
503+
{
504+
series: "a",
505+
},
506+
{
507+
series: "b",
508+
},
509+
],
510+
},
511+
};
512+
513+
const chartDef = applyDefaults(inputChartDef);
514+
expect(chartDef.plotConfig.y!.min).toBe(10.12);
515+
expect(chartDef.plotConfig.y!.max).toBe(30.02);
516+
});
517+
467518
});

0 commit comments

Comments
 (0)