Skip to content

Commit bcfe08b

Browse files
authored
Merge branch 'develop' into filip/select-timezone
2 parents 1ca1a64 + 256ded3 commit bcfe08b

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

assets/js/web-components/prpl-chart-line.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,52 @@ customElements.define(
169169
.join( ' ' ) }" /></g>`;
170170
};
171171

172+
/**
173+
* Get the number of steps for the Y axis.
174+
*
175+
* Choose between 3, 4, or 5 steps.
176+
* The result should be the number that when used as a divisor,
177+
* produces integer values for the Y labels - or at least as close as possible.
178+
*
179+
* @return {number} The number of steps.
180+
*/
181+
getYLabelsStepsDivider = () => {
182+
const maxValuePadded = this.getMaxValuePadded();
183+
184+
const stepsRemainders = {
185+
4: maxValuePadded % 4,
186+
5: maxValuePadded % 5,
187+
3: maxValuePadded % 3,
188+
};
189+
// Get the smallest remainder.
190+
const smallestRemainder = Math.min(
191+
...Object.values( stepsRemainders )
192+
);
193+
194+
// Get the key of the smallest remainder.
195+
const smallestRemainderKey = Object.keys( stepsRemainders ).find(
196+
( key ) => stepsRemainders[ key ] === smallestRemainder
197+
);
198+
return smallestRemainderKey;
199+
};
200+
172201
/**
173202
* Get the Y labels.
174203
*
175204
* @return {number[]} The Y labels.
176205
*/
177206
getYLabels = () => {
178207
const maxValuePadded = this.getMaxValuePadded();
179-
// Take the maximum value and divide it by 4 to get the step.
180-
const yLabelsStep = maxValuePadded / 4;
208+
const yLabelsStepsDivider = this.getYLabelsStepsDivider();
209+
const yLabelsStep = maxValuePadded / yLabelsStepsDivider;
181210
const yLabels = [];
182211
if ( 100 === maxValuePadded || 15 > maxValuePadded ) {
183-
for ( let i = 0; i <= 4; i++ ) {
212+
for ( let i = 0; i <= yLabelsStepsDivider; i++ ) {
184213
yLabels.push( parseInt( yLabelsStep * i ) );
185214
}
186215
} else {
187216
// Round the values to the nearest 10.
188-
for ( let i = 0; i <= 4; i++ ) {
217+
for ( let i = 0; i <= yLabelsStepsDivider; i++ ) {
189218
yLabels.push(
190219
Math.min(
191220
maxValuePadded,

0 commit comments

Comments
 (0)