Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate exponent for intervals based on range between values #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js/charts/AxisChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class AxisChart extends BaseChart {
}

calcYAxisParameters(dataValues, withMinimum = 'false') {
const yPts = calcChartIntervals(dataValues, withMinimum);
const yPts = calcChartIntervals(dataValues, withMinimum, this.rawChartArgs.y_axis_exp_based_on_range === true);
const scaleMultiplier = this.height / getValueRange(yPts);
const intervalHeight = getIntervalSize(yPts) * scaleMultiplier;
const zeroLine = this.height - (getZeroIndex(yPts) * intervalHeight);
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 @@ -61,8 +61,17 @@ function getChartRangeIntervals(max, min=0) {
return intervals;
}

function getChartIntervals(maxValue, minValue=0) {
let [normalMaxValue, exponent] = normalize(maxValue);
function getChartIntervals(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 @@ -73,7 +82,7 @@ function getChartIntervals(maxValue, minValue=0) {
return intervals;
}

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

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

Expand Down Expand Up @@ -146,7 +155,7 @@ export function calcChartIntervals(values, withMinimum=false) {
if(!withMinimum) {
intervals = getChartIntervals(pseudoMaxValue);
} else {
intervals = getChartIntervals(pseudoMaxValue, pseudoMinValue);
intervals = getChartIntervals(pseudoMaxValue, pseudoMinValue, expBasedOnRange);
}

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