camelcase intervals.js

This commit is contained in:
pratu16x7 2017-11-18 23:41:35 +05:30
parent 0b0c26002f
commit eeefeeb54d
6 changed files with 126 additions and 126 deletions

View File

@ -396,77 +396,77 @@ function normalize(x) {
return [sig * man, exp];
}
function get_range_intervals(max, min=0) {
let upper_bound = Math.ceil(max);
let lower_bound = Math.floor(min);
let range = upper_bound - lower_bound;
function getRangeIntervals(max, min=0) {
let upperBound = Math.ceil(max);
let lowerBound = Math.floor(min);
let range = upperBound - lowerBound;
let no_of_parts = range;
let part_size = 1;
let noOfParts = range;
let partSize = 1;
// To avoid too many partitions
if(range > 5) {
if(range % 2 !== 0) {
upper_bound++;
upperBound++;
// Recalc range
range = upper_bound - lower_bound;
range = upperBound - lowerBound;
}
no_of_parts = range/2;
part_size = 2;
noOfParts = range/2;
partSize = 2;
}
// Special case: 1 and 2
if(range <= 2) {
no_of_parts = 4;
part_size = range/no_of_parts;
noOfParts = 4;
partSize = range/noOfParts;
}
// Special case: 0
if(range === 0) {
no_of_parts = 5;
part_size = 1;
noOfParts = 5;
partSize = 1;
}
let intervals = [];
for(var i = 0; i <= no_of_parts; i++){
intervals.push(lower_bound + part_size * i);
for(var i = 0; i <= noOfParts; i++){
intervals.push(lowerBound + partSize * i);
}
return intervals;
}
function get_intervals(max_value, min_value=0) {
let [normal_max_value, exponent] = normalize(max_value);
let normal_min_value = min_value ? min_value/Math.pow(10, exponent): 0;
function getIntervals(maxValue, minValue=0) {
let [normalMaxValue, exponent] = normalize(maxValue);
let normalMinValue = minValue ? minValue/Math.pow(10, exponent): 0;
// Allow only 7 significant digits
normal_max_value = normal_max_value.toFixed(6);
normalMaxValue = normalMaxValue.toFixed(6);
let intervals = get_range_intervals(normal_max_value, normal_min_value);
let intervals = getRangeIntervals(normalMaxValue, normalMinValue);
intervals = intervals.map(value => value * Math.pow(10, exponent));
return intervals;
}
function calc_intervals(values, with_minimum=false) {
function calcIntervals(values, withMinimum=false) {
//*** Where the magic happens ***
// Calculates best-fit y intervals from given values
// and returns the interval array
let max_value = Math.max(...values);
let min_value = Math.min(...values);
let maxValue = Math.max(...values);
let minValue = Math.min(...values);
// Exponent to be used for pretty print
let exponent = 0, intervals = []; // eslint-disable-line no-unused-vars
function get_positive_first_intervals(max_value, abs_min_value) {
let intervals = get_intervals(max_value);
function getPositiveFirstIntervals(maxValue, absMinValue) {
let intervals = getIntervals(maxValue);
let interval_size = intervals[1] - intervals[0];
let intervalSize = intervals[1] - intervals[0];
// Then unshift the negative values
let value = 0;
for(var i = 1; value < abs_min_value; i++) {
value += interval_size;
for(var i = 1; value < absMinValue; i++) {
value += intervalSize;
intervals.unshift((-1) * value);
}
return intervals;
@ -474,52 +474,52 @@ function calc_intervals(values, with_minimum=false) {
// CASE I: Both non-negative
if(max_value >= 0 && min_value >= 0) {
exponent = normalize(max_value)[1];
if(!with_minimum) {
intervals = get_intervals(max_value);
if(maxValue >= 0 && minValue >= 0) {
exponent = normalize(maxValue)[1];
if(!withMinimum) {
intervals = getIntervals(maxValue);
} else {
intervals = get_intervals(max_value, min_value);
intervals = getIntervals(maxValue, minValue);
}
}
// CASE II: Only min_value negative
// CASE II: Only minValue negative
else if(max_value > 0 && min_value < 0) {
// `with_minimum` irrelevant in this case,
else if(maxValue > 0 && minValue < 0) {
// `withMinimum` irrelevant in this case,
// We'll be handling both sides of zero separately
// (both starting from zero)
// Because ceil() and floor() behave differently
// in those two regions
let abs_min_value = Math.abs(min_value);
let absMinValue = Math.abs(minValue);
if(max_value >= abs_min_value) {
exponent = normalize(max_value)[1];
intervals = get_positive_first_intervals(max_value, abs_min_value);
if(maxValue >= absMinValue) {
exponent = normalize(maxValue)[1];
intervals = getPositiveFirstIntervals(maxValue, absMinValue);
} else {
// Mirror: max_value => abs_min_value, then change sign
exponent = normalize(abs_min_value)[1];
let pos_intervals = get_positive_first_intervals(abs_min_value, max_value);
intervals = pos_intervals.map(d => d * (-1));
// Mirror: maxValue => absMinValue, then change sign
exponent = normalize(absMinValue)[1];
let posIntervals = getPositiveFirstIntervals(absMinValue, maxValue);
intervals = posIntervals.map(d => d * (-1));
}
}
// CASE III: Both non-positive
else if(max_value <= 0 && min_value <= 0) {
else if(maxValue <= 0 && minValue <= 0) {
// Mirrored Case I:
// Work with positives, then reverse the sign and array
let pseudo_max_value = Math.abs(min_value);
let pseudo_min_value = Math.abs(max_value);
let pseudoMaxValue = Math.abs(minValue);
let pseudoMinValue = Math.abs(maxValue);
exponent = normalize(pseudo_max_value)[1];
if(!with_minimum) {
intervals = get_intervals(pseudo_max_value);
exponent = normalize(pseudoMaxValue)[1];
if(!withMinimum) {
intervals = getIntervals(pseudoMaxValue);
} else {
intervals = get_intervals(pseudo_max_value, pseudo_min_value);
intervals = getIntervals(pseudoMaxValue, pseudoMinValue);
}
intervals = intervals.reverse().map(d => d * (-1));
@ -528,24 +528,24 @@ function calc_intervals(values, with_minimum=false) {
return intervals;
}
function calc_distribution(values, distribution_size) {
function calcDistribution(values, distributionSize) {
// Assume non-negative values,
// implying distribution minimum at zero
let data_max_value = Math.max(...values);
let dataMaxValue = Math.max(...values);
let distribution_step = 1 / (distribution_size - 1);
let distributionStep = 1 / (distributionSize - 1);
let distribution = [];
for(var i = 0; i < distribution_size; i++) {
let checkpoint = data_max_value * (distribution_step * i);
for(var i = 0; i < distributionSize; i++) {
let checkpoint = dataMaxValue * (distributionStep * i);
distribution.push(checkpoint);
}
return distribution;
}
function get_max_checkpoint(value, distribution) {
function getMaxCheckpoint(value, distribution) {
return distribution.filter(d => d < value).length;
}
@ -1099,7 +1099,7 @@ class AxisChart extends BaseChart {
values = values.concat(this.y_sums);
}
this.y_axis_values = calc_intervals(values, this.type === 'line');
this.y_axis_values = calcIntervals(values, this.type === 'line');
if(!this.y_old_axis_values) {
this.y_old_axis_values = this.y_axis_values.slice();
@ -2600,7 +2600,7 @@ class Heatmap extends BaseChart {
this.data_groups.textContent = '';
let data_values = Object.keys(this.data).map(key => this.data[key]);
this.distribution = calc_distribution(data_values, this.distribution_size);
this.distribution = calcDistribution(data_values, this.distribution_size);
this.month_names = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
@ -2668,7 +2668,7 @@ class Heatmap extends BaseChart {
}
if(data_value) {
color_index = get_max_checkpoint(data_value, this.distribution);
color_index = getMaxCheckpoint(data_value, this.distribution);
}
let x = 13 + (index + week_col_change) * 12;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
import $ from '../utils/dom';
import { UnitRenderer, make_x_line, make_y_line } from '../utils/draw';
import { runSVGAnimation } from '../utils/animate';
import { calc_intervals } from '../utils/intervals';
import { calcIntervals } from '../utils/intervals';
import { float_2, arrays_equal, get_string_width } from '../utils/helpers';
import BaseChart from './BaseChart';
@ -59,7 +59,7 @@ export default class AxisChart extends BaseChart {
values = values.concat(this.y_sums);
}
this.y_axis_values = calc_intervals(values, this.type === 'line');
this.y_axis_values = calcIntervals(values, this.type === 'line');
if(!this.y_old_axis_values) {
this.y_old_axis_values = this.y_axis_values.slice();

View File

@ -1,7 +1,7 @@
import BaseChart from './BaseChart';
import $ from '../utils/dom';
import { add_days, get_dd_mm_yyyy, get_weeks_between } from '../utils/date-utils';
import { calc_distribution, get_max_checkpoint } from '../utils/intervals';
import { calcDistribution, getMaxCheckpoint } from '../utils/intervals';
import { is_valid_color } from '../utils/colors';
export default class Heatmap extends BaseChart {
@ -97,7 +97,7 @@ export default class Heatmap extends BaseChart {
this.data_groups.textContent = '';
let data_values = Object.keys(this.data).map(key => this.data[key]);
this.distribution = calc_distribution(data_values, this.distribution_size);
this.distribution = calcDistribution(data_values, this.distribution_size);
this.month_names = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
@ -165,7 +165,7 @@ export default class Heatmap extends BaseChart {
}
if(data_value) {
color_index = get_max_checkpoint(data_value, this.distribution);
color_index = getMaxCheckpoint(data_value, this.distribution);
}
let x = 13 + (index + week_col_change) * 12;

View File

@ -21,77 +21,77 @@ function normalize(x) {
return [sig * man, exp];
}
function get_range_intervals(max, min=0) {
let upper_bound = Math.ceil(max);
let lower_bound = Math.floor(min);
let range = upper_bound - lower_bound;
function getRangeIntervals(max, min=0) {
let upperBound = Math.ceil(max);
let lowerBound = Math.floor(min);
let range = upperBound - lowerBound;
let no_of_parts = range;
let part_size = 1;
let noOfParts = range;
let partSize = 1;
// To avoid too many partitions
if(range > 5) {
if(range % 2 !== 0) {
upper_bound++;
upperBound++;
// Recalc range
range = upper_bound - lower_bound;
range = upperBound - lowerBound;
}
no_of_parts = range/2;
part_size = 2;
noOfParts = range/2;
partSize = 2;
}
// Special case: 1 and 2
if(range <= 2) {
no_of_parts = 4;
part_size = range/no_of_parts;
noOfParts = 4;
partSize = range/noOfParts;
}
// Special case: 0
if(range === 0) {
no_of_parts = 5;
part_size = 1;
noOfParts = 5;
partSize = 1;
}
let intervals = [];
for(var i = 0; i <= no_of_parts; i++){
intervals.push(lower_bound + part_size * i);
for(var i = 0; i <= noOfParts; i++){
intervals.push(lowerBound + partSize * i);
}
return intervals;
}
function get_intervals(max_value, min_value=0) {
let [normal_max_value, exponent] = normalize(max_value);
let normal_min_value = min_value ? min_value/Math.pow(10, exponent): 0;
function getIntervals(maxValue, minValue=0) {
let [normalMaxValue, exponent] = normalize(maxValue);
let normalMinValue = minValue ? minValue/Math.pow(10, exponent): 0;
// Allow only 7 significant digits
normal_max_value = normal_max_value.toFixed(6);
normalMaxValue = normalMaxValue.toFixed(6);
let intervals = get_range_intervals(normal_max_value, normal_min_value);
let intervals = getRangeIntervals(normalMaxValue, normalMinValue);
intervals = intervals.map(value => value * Math.pow(10, exponent));
return intervals;
}
export function calc_intervals(values, with_minimum=false) {
export function calcIntervals(values, withMinimum=false) {
//*** Where the magic happens ***
// Calculates best-fit y intervals from given values
// and returns the interval array
let max_value = Math.max(...values);
let min_value = Math.min(...values);
let maxValue = Math.max(...values);
let minValue = Math.min(...values);
// Exponent to be used for pretty print
let exponent = 0, intervals = []; // eslint-disable-line no-unused-vars
function get_positive_first_intervals(max_value, abs_min_value) {
let intervals = get_intervals(max_value);
function getPositiveFirstIntervals(maxValue, absMinValue) {
let intervals = getIntervals(maxValue);
let interval_size = intervals[1] - intervals[0];
let intervalSize = intervals[1] - intervals[0];
// Then unshift the negative values
let value = 0;
for(var i = 1; value < abs_min_value; i++) {
value += interval_size;
for(var i = 1; value < absMinValue; i++) {
value += intervalSize;
intervals.unshift((-1) * value);
}
return intervals;
@ -99,52 +99,52 @@ export function calc_intervals(values, with_minimum=false) {
// CASE I: Both non-negative
if(max_value >= 0 && min_value >= 0) {
exponent = normalize(max_value)[1];
if(!with_minimum) {
intervals = get_intervals(max_value);
if(maxValue >= 0 && minValue >= 0) {
exponent = normalize(maxValue)[1];
if(!withMinimum) {
intervals = getIntervals(maxValue);
} else {
intervals = get_intervals(max_value, min_value);
intervals = getIntervals(maxValue, minValue);
}
}
// CASE II: Only min_value negative
// CASE II: Only minValue negative
else if(max_value > 0 && min_value < 0) {
// `with_minimum` irrelevant in this case,
else if(maxValue > 0 && minValue < 0) {
// `withMinimum` irrelevant in this case,
// We'll be handling both sides of zero separately
// (both starting from zero)
// Because ceil() and floor() behave differently
// in those two regions
let abs_min_value = Math.abs(min_value);
let absMinValue = Math.abs(minValue);
if(max_value >= abs_min_value) {
exponent = normalize(max_value)[1];
intervals = get_positive_first_intervals(max_value, abs_min_value);
if(maxValue >= absMinValue) {
exponent = normalize(maxValue)[1];
intervals = getPositiveFirstIntervals(maxValue, absMinValue);
} else {
// Mirror: max_value => abs_min_value, then change sign
exponent = normalize(abs_min_value)[1];
let pos_intervals = get_positive_first_intervals(abs_min_value, max_value);
intervals = pos_intervals.map(d => d * (-1));
// Mirror: maxValue => absMinValue, then change sign
exponent = normalize(absMinValue)[1];
let posIntervals = getPositiveFirstIntervals(absMinValue, maxValue);
intervals = posIntervals.map(d => d * (-1));
}
}
// CASE III: Both non-positive
else if(max_value <= 0 && min_value <= 0) {
else if(maxValue <= 0 && minValue <= 0) {
// Mirrored Case I:
// Work with positives, then reverse the sign and array
let pseudo_max_value = Math.abs(min_value);
let pseudo_min_value = Math.abs(max_value);
let pseudoMaxValue = Math.abs(minValue);
let pseudoMinValue = Math.abs(maxValue);
exponent = normalize(pseudo_max_value)[1];
if(!with_minimum) {
intervals = get_intervals(pseudo_max_value);
exponent = normalize(pseudoMaxValue)[1];
if(!withMinimum) {
intervals = getIntervals(pseudoMaxValue);
} else {
intervals = get_intervals(pseudo_max_value, pseudo_min_value);
intervals = getIntervals(pseudoMaxValue, pseudoMinValue);
}
intervals = intervals.reverse().map(d => d * (-1));
@ -153,23 +153,23 @@ export function calc_intervals(values, with_minimum=false) {
return intervals;
}
export function calc_distribution(values, distribution_size) {
export function calcDistribution(values, distributionSize) {
// Assume non-negative values,
// implying distribution minimum at zero
let data_max_value = Math.max(...values);
let dataMaxValue = Math.max(...values);
let distribution_step = 1 / (distribution_size - 1);
let distributionStep = 1 / (distributionSize - 1);
let distribution = [];
for(var i = 0; i < distribution_size; i++) {
let checkpoint = data_max_value * (distribution_step * i);
for(var i = 0; i < distributionSize; i++) {
let checkpoint = dataMaxValue * (distributionStep * i);
distribution.push(checkpoint);
}
return distribution;
}
export function get_max_checkpoint(value, distribution) {
export function getMaxCheckpoint(value, distribution) {
return distribution.filter(d => d < value).length;
}