Add: HistoryRecordManager

This commit is contained in:
2018-05-10 18:12:04 +09:00
parent 7fc6364fc0
commit b524614220
8 changed files with 175 additions and 57 deletions
+82
View File
@@ -0,0 +1,82 @@
class HistoryRecordData {
constructor(date, record) {
this.date = date;
this.record = record;
}
}
class HistoryRecordManager {
constructor() {
this.clear();
}
push(historyRecordData) {
this.historyRecords.push(historyRecordData);
this.minValueOfRecord = this.minValueOfArray();
this.maxValueOfRecord = this.maxValueOfArray();
}
clear() {
this.historyRecords = [];
this.minValueOfRecord = 0;
this.maxValueOfRecord = 0;
}
get count() {
return this.historyRecords.length;
}
getAt(index) {
return this.historyRecords[index];
}
getDateAt(index) {
return this.historyRecords[index].date;
}
getRecordAt(index) {
return this.historyRecords[index].record;
}
minValueOfArray() {
let numberArray = this.getRecordArray(this.historyRecords);
return Math.min.apply(Math, numberArray);
}
maxValueOfArray() {
let numberArray = this.getRecordArray(this.historyRecords);
return Math.max.apply(Math, numberArray);
}
underValueForGraph() {
let minNumberOfDigits = NumberUtil.numberOfDigits(this.minValueOfRecord);
let underMinValue =
Math.floor( (this.minValueOfRecord / Math.pow(10, minNumberOfDigits - 2) ) )
* Math.pow(10, minNumberOfDigits - 2 );
return underMinValue;
}
upperValueForGraph() {
let maxNumberOfDigits = NumberUtil.numberOfDigits(this.maxValueOfRecord);
let underMaxValue =
Math.ceil( (this.maxValueOfRecord / Math.pow(10, maxNumberOfDigits - 2) ) )
* Math.pow(10, maxNumberOfDigits - 2);
return underMaxValue;
}
getRecordArray() {
let numberArray = [];
for(let data of this.historyRecords) {
numberArray.push(data.record);
}
return numberArray;
}
}
+4
View File
@@ -4,4 +4,8 @@ class NumberUtil {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
static numberOfDigits(number) {
return Math.floor(Math.log(number) / Math.LN10 + 1); // bug : 1000 -> expected 4 but 3
}
}