Skip to content

Commit 9a545ea

Browse files
committed
Merge branch 'main-check-merge' of https://github.com/communitiesuk/oflog_svelte_component_library into main-check-merge
2 parents 4e010d3 + db38906 commit 9a545ea

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"@types/dompurify": "^3.0.5",
7878
"csv-parser": "^3.0.0",
7979
"d3": "^7.9.0",
80+
"decimal.js": "^10.5.0",
8081
"dompurify": "^3.2.5",
8182
"flowbite-svelte-icons": "^2.0.3",
8283
"govuk-frontend": "^5.9.0",

src/lib/components/data-vis/axis/Ticks.svelte

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script>
22
import { tick } from "svelte";
3+
import Decimal from "decimal.js";
34
45
let {
56
ticksArray = $bindable(),
@@ -20,25 +21,34 @@
2021
$inspect(ticksArray);
2122
2223
function generateTicks(data, numTicks, baseline, setTick) {
23-
console.log(data, typeof data);
24-
let minVal = baseline !== null ? baseline : Math.min(...data);
25-
let maxVal = setTick !== null ? setTick : Math.max(...data);
26-
let rangeVal = maxVal - minVal;
24+
let minVal =
25+
baseline !== null
26+
? new Decimal(baseline)
27+
: Decimal.min(...data.map((val) => new Decimal(val)));
28+
let maxVal =
29+
setTick !== null
30+
? new Decimal(setTick)
31+
: Decimal.max(...data.map((val) => new Decimal(val)));
32+
let rangeVal = maxVal.minus(minVal);
2733
28-
let roughStep = rangeVal / (numTicks - 1);
34+
let roughStep = rangeVal.div(numTicks - 1);
2935
let normalizedSteps = [1, 2, 5, 10];
3036
31-
let stepPower = Math.pow(10, -Math.floor(Math.log10(roughStep)));
32-
let normalizedStep = roughStep * stepPower;
33-
let optimalStep =
34-
normalizedSteps.find((step) => step >= normalizedStep) / stepPower;
37+
let stepPower = Decimal.pow(
38+
10,
39+
-Math.floor(Math.log10(roughStep.toNumber())),
40+
);
41+
let normalizedStep = roughStep.mul(stepPower);
42+
let optimalStep = new Decimal(
43+
normalizedSteps.find((step) => step >= normalizedStep.toNumber()),
44+
).div(stepPower);
3545
36-
let scaleMin = Math.floor(minVal / optimalStep) * optimalStep;
37-
let scaleMax = Math.ceil(maxVal / optimalStep) * optimalStep;
46+
let scaleMin = minVal.div(optimalStep).floor().mul(optimalStep);
47+
let scaleMax = maxVal.div(optimalStep).ceil().mul(optimalStep);
3848
3949
let ticks = [];
40-
for (let i = scaleMin; i <= scaleMax; i += optimalStep) {
41-
ticks.push(i);
50+
for (let i = scaleMin; i.lte(scaleMax); i = i.plus(optimalStep)) {
51+
ticks.push(i.toNumber());
4252
}
4353
return ticks;
4454
}

0 commit comments

Comments
 (0)