Skip to content

Commit

Permalink
Calculate exponent for intervals based on range between values
Browse files Browse the repository at this point in the history
Aims to resolve frappe#100
  • Loading branch information
kronthto committed Feb 13, 2018
1 parent ed4ffae commit f997f70
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/js/charts/AxisChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class AxisChart extends BaseChart {
values = values.concat(this.y_sums);
}

this.y_axis_values = calcIntervals(values, this.type === 'line');
this.y_axis_values = calcIntervals(values, this.type === 'line', this.y_axis_exp_based_on_range === true);

if(!this.y_old_axis_values) {
this.y_old_axis_values = this.y_axis_values.slice();
Expand Down
1 change: 1 addition & 0 deletions src/js/charts/LineChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class LineChart extends AxisChart {
this.dot_radius = args.dot_radius || 4;
this.heatline = args.heatline;
this.type = 'line';
this.y_axis_exp_based_on_range = args.y_axis_exp_based_on_range;

this.setup();
}
Expand Down
19 changes: 14 additions & 5 deletions src/js/utils/intervals.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,17 @@ function getRangeIntervals(max, min=0) {
return intervals;
}

function getIntervals(maxValue, minValue=0) {
let [normalMaxValue, exponent] = normalize(maxValue);
function getIntervals(maxValue, minValue=0, expBasedOnRange=false) {
let exponent, normalMaxValue;

if (expBasedOnRange) {
let range = maxValue - minValue;
exponent = normalize(range)[1];
normalMaxValue = maxValue/Math.pow(10, exponent);
} else {
[normalMaxValue, exponent] = normalize(maxValue);
}

let normalMinValue = minValue ? minValue/Math.pow(10, exponent): 0;

// Allow only 7 significant digits
Expand All @@ -71,7 +80,7 @@ function getIntervals(maxValue, minValue=0) {
return intervals;
}

export function calcIntervals(values, withMinimum=false) {
export function calcIntervals(values, withMinimum=false, expBasedOnRange=false) {
//*** Where the magic happens ***

// Calculates best-fit y intervals from given values
Expand Down Expand Up @@ -104,7 +113,7 @@ export function calcIntervals(values, withMinimum=false) {
if(!withMinimum) {
intervals = getIntervals(maxValue);
} else {
intervals = getIntervals(maxValue, minValue);
intervals = getIntervals(maxValue, minValue, expBasedOnRange);
}
}

Expand Down Expand Up @@ -144,7 +153,7 @@ export function calcIntervals(values, withMinimum=false) {
if(!withMinimum) {
intervals = getIntervals(pseudoMaxValue);
} else {
intervals = getIntervals(pseudoMaxValue, pseudoMinValue);
intervals = getIntervals(pseudoMaxValue, pseudoMinValue, expBasedOnRange);
}

intervals = intervals.reverse().map(d => d * (-1));
Expand Down

0 comments on commit f997f70

Please sign in to comment.