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

CROWDEEG-79 Fix y-axis limit #94

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
238 changes: 190 additions & 48 deletions client/Annotators/EDF/timeSeriesAnnotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
y_axis_limited: [],
y_limit_lower: [],
y_limit_upper: [],
y_limit_middle: [],
showTitle: true,
latestClick: null,
loading: false,
Expand Down Expand Up @@ -1990,42 +1991,110 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
//console.log("hehe");
newyData = [];

that.vars.chart.series[i].yData = [...that.vars.chart.original_series[i]];
let i = that.vars.selectedChannelIndex;
//that.vars.chart.series[i].yData = [...original_series[i]];
that._updateSingleChannelDataInSeries(that.vars.chart.series, that.vars.currentWindowData,that.vars.real, i);
that.options.y_axis_limited[i] = true;
//set lower value
var lowerlimit = document.querySelector('#y-limit-min').value;
that.options.y_limit_lower[i] = lowerlimit;
//set upper value
var upperlimit = document.querySelector('#y-limit-max').value;
that.options.y_limit_upper[i] = upperlimit;


console.log(that.options.y_axis_limited_values);
//save the limit values in our preferences
var newLimited = that.options.y_axis_limited_values.filter(function(el){
return el.index != i;
});
newLimited.push({"index":i, "lowerlimit": lowerlimit, "upperlimit":upperlimit});
that.options.y_axis_limited_values = newLimited;
that._savePreferences({
limitedYAxis: that.options.y_axis_limited_values,
});

for (let j = 0; j < that.vars.chart.series[i].yData.length; j++) {
if ((that.vars.chart.series[i].realyData[j]) >= lowerlimit && (that.vars.chart.series[i].realyData[j]) <= upperlimit) {

newyData.push(that.vars.chart.series[i].yData[j]);

}
else {
newyData.push({
y: that.vars.chart.series[i].yData[j],
color: '#FFFFFF'
});

}
}
that.vars.chart.series[i].yData = newyData;
//console.log(that.vars);
that.vars.chart.redraw();
that._populateGraph();

// console.log(that.options.y_axis_limited_values.length);
// const zeroPosition = that._getOffsetForChannelIndexPostScale(i);
// var max = that.vars.chart.series[i].realyData[1];
// var min = that.vars.chart.series[i].realyData[1];
// var val = null;
// var realval = null;
// var difference = zeroPosition;
// var difference2 = 1000000000000;
// var middle = (Number(lowerlimit) + Number(upperlimit))/2;
// var middleVal = null;
// var tempMiddle = that.vars.chart.series[i].yData[1];
// console.log("MIDDLE", middle);
// //console.log(that.vars.chart.series[i].realyData);
// console.log(that.vars.chart.series[i]);
// for (let j = 0; j < that.vars.chart.series[i].yData.length; j++) {
// if(j != 0 && j != that.vars.chart.series[i].yData.length-1){
// temp = Math.abs(zeroPosition - that.vars.chart.series[i].yData[j]);
// if(temp === 0){
// val = that.vars.chart.series[i].yData[j];
// realval = that.vars.chart.series[i].realyData[j];
// }
// if(temp < difference){
// difference = temp;
// val = that.vars.chart.series[i].yData[j];
// realval = that.vars.chart.series[i].realyData[j];
// }
// // if(that.vars.chart.series[i].realyData[j] > max){
// // console.log("2");
// // max = that.vars.chart.series[i].realyData[j];
// // } else if (that.vars.chart.series[i].realyData[j] < min) {
// // console.log("3");
// // min = that.vars.chart.series[i].realyData[j];
// // }
// temp2 = Math.abs(middle - that.vars.chart.series[i].realyData[j]);
// if(temp2 === 0){
// middleVal = that.vars.chart.series[i].yData[j];
// }
// if(temp2 < difference2){
// difference2 = temp2;
// middleVal = that.vars.chart.series[i].yData[j];
// }

// }

// if ((that.vars.chart.series[i].realyData[j]) >= Number(lowerlimit) && (that.vars.chart.series[i].realyData[j]) <= Number(upperlimit)) {

// newyData.push(that.vars.chart.series[i].yData[j]);

// }
// else {
// //that.vars.chart.series[i].xData.splice(j - itemsRemoved, 1);
// //itemsRemoved++;

// newyData.push({
// y: that.vars.chart.series[i].yData[j],
// color: '#FFFFFF'
// });

// //newyData.push(null);

// }
// }
// that.vars.chart.series[i].yData = newyData;
// console.log(val);
// console.log(zeroPosition);
// console.log(middleVal);
// // console.log(realval);
// //if outside the bounds of the channels placement of the chart, only shift by half the space then scale
// //var shift = middle === 0 ? 0 : middle - realval > 200 ? 100 : middle - realval < -200 ? -100 : middle-realval
// //var shift = middle === 0 ? 0 : middle-realval === 0 ? 0 : (middle-realval);
// // var shift = middle === 0 ? 0 : (zeroPosition - middleVal) * that.vars.scalingFactors[i];
// // console.log(shift);
// // console.log(middle);
// // console.log(realval);
// // console.log(middle - realval);
// // //that._scaleToScreen(i);
// // that._customTranslation(i, shift);
// // //that._scaleToScreen(i);
// // console.log(that.vars.chart.series[i].yData);
// //console.log(maxChannelData);

// //that._scaleToScreen(i);
// /*
// that._savePreferences({
// scalingFactors: that.vars.scalingFactors,
// });
// */
// //console.log(that.vars);
// that.vars.chart.redraw();
}
else{
console.log("channel not selected");
Expand Down Expand Up @@ -5368,32 +5437,74 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
},

//same as the normal limiting but based on index for the purpose of preferences
_limitYAxisByIndex: function(index, lowerlimit, upperlimit, original_series){
_limitYAxisByIndex: function(i, lowerlimit, upperlimit, original_series, newMiddleValue = null){
newyData = [];
channel = that.vars.currentWindowData.channels[i];

that.vars.chart.series[index].yData = [...original_series[index]];
that.options.y_axis_limited[index] = true;

that.options.y_limit_lower[index] = lowerlimit;

that.options.y_limit_upper[index] = upperlimit;

for (let j = 0; j < that.vars.chart.series[index].yData.length; j++) {
that.vars.chart.series[i].yData = [...original_series[i]];
that.options.y_axis_limited[i] = true;

that.options.y_limit_lower[i] = lowerlimit;

that.options.y_limit_upper[i] = upperlimit;
const zeroPosition = that._getOffsetForChannelIndexPostScale(i);
var middleVal = null;
var difference = newMiddleValue ? Math.abs(zeroPosition - newMiddleValue) : that.options.y_limit_middle[i] ? Math.abs(zeroPosition - that.options.y_limit_middle[i]) : 100000000000;
console.log(difference);
var middle = (Number(lowerlimit) + Number(upperlimit))/2;
var check = difference === 0 ? false : true;

for (let j = 0; j < that.vars.chart.series[i].yData.length; j++) {
if(j != 0 && j != that.vars.chart.series[i].yData.length-1){
if(check){
temp = Math.abs(middle - that.vars.chart.series[i].realyData[j]);
if(temp === 0){
middleVal = that.vars.chart.series[i].yData[j];
check = false;
}
//console.log(difference2);
if(temp < difference){
//console.log("ji")
difference = temp;
middleVal = that.vars.chart.series[i].yData[j];
}
}
}

if ((that.vars.chart.series[index].realyData[j]) >= lowerlimit && (that.vars.chart.series[index].realyData[j]) <= upperlimit) {
if ((that.vars.chart.series[i].realyData[j]) >= Number(lowerlimit) && (that.vars.chart.series[i].realyData[j]) <= Number(upperlimit)) {

newyData.push(that.vars.chart.series[index].yData[j]);
newyData.push(that.vars.chart.series[i].yData[j]);

}
else {
newyData.push({
y: that.vars.chart.series[index].yData[j],
y: that.vars.chart.series[i].yData[j],
color: '#FFFFFF'
});

}
}
that.vars.chart.series[index].yData = newyData;


that.vars.chart.series[i].yData = newyData;
console.log(that.vars.chart.series[i]);
that.options.y_limit_middle[i] = middleVal;
var newLimited = that.options.y_axis_limited_values.filter(function(el){
return el.index != i;
})
newLimited.push({"index":i, "lowerlimit": lowerlimit, "upperlimit":upperlimit, "middleVal": middleVal});
that.options.y_axis_limited_values = newLimited;
that._savePreferences({
limitedYAxis: that.options.y_axis_limited_values,
})
console.log(middleVal);
console.log(zeroPosition);
console.log(middle);
that._customTranslation(i, zeroPosition-middleVal);
that._scaleToScreen(i);
console.log(that.vars.chart.series[i].yData);


//that.vars.chart.redraw();
},

_setupGraphFunctions: function(){
Expand All @@ -5417,6 +5528,7 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
that.options.y_axis_limited[i] = false;
that.options.y_limit_lower[i] = -200;
that.options.y_limit_upper[i] = 200;
that.options.y_limit_middle[i] = null;
}

console.log(that.vars.scalingFactors);
Expand Down Expand Up @@ -5453,7 +5565,7 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
//limit each channel that needs to be
if(that.options.context.preferences.annotatorConfig.limitedYAxis != null){
that.options.context.preferences.annotatorConfig.limitedYAxis.forEach((item)=> {
that._limitYAxisByIndex(item.index, item.lowerlimit, item.upperlimit, that.vars.chart.original_series);
that._limitYAxisByIndex(item.index, item.lowerlimit, item.upperlimit, that.vars.chart.original_series, item.middleVal);
});
that.options.y_axis_limited_values = that.options.context.preferences.annotatorConfig.limitedYAxis;
}
Expand Down Expand Up @@ -5554,12 +5666,14 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
that.options.y_axis_limited[i] = false;
that.options.y_limit_lower[i] = -200;
that.options.y_limit_upper[i] = 200;
that.options.y_limit_middle[i] = null;
//something is changing the scale factor afterwards
//that.vars.scalingFactors[i] = scaleFactor;

// set the scaling factor to the original one and save it
that.vars.scalingFactors[i] = that.vars.originalScalingFactors[i];
console.log(that.vars.originalScalingFactors);
console.log("HERE", that.vars.scalingFactors[i]);
that._savePreferences({
scalingFactors: that.vars.scalingFactors,
});
Expand Down Expand Up @@ -5705,7 +5819,8 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {

for (let i = 0;i< that.options.y_axis_limited.length;i++){
if (that.options.y_axis_limited[i] && that.options.maskedChannels.indexOf(i) == -1) {
that._limitYData(i);
//that._limitYData(i);
that._limitYAxisByIndex(i, that.options.y_limit_lower[i], that.options.y_limit_upper[i], original_series);
}
//console.log(i + "redraw after")
//console.log(that);
Expand Down Expand Up @@ -5846,6 +5961,9 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {

//if(c===2){console.log(channel.values);}
// gets the values
// console.log("FPAG", flipFactorAndGain);
// console.log("offsetPostScale", offsetPostScale);
// console.log("offsetPreScle", offsetPreScale);
samplesScaledAndOffset = channel.values.map(function (value, v) {
return (value + offsetPreScale) * flipFactorAndGain + offsetPostScale;
});
Expand Down Expand Up @@ -5910,6 +6028,23 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
});
},

_initYAxis: function (data){
var that = this;
var channels = data.channels;
var recordingEndInSecondsSnapped = that._getRecordingEndInSecondsSnapped();
var percentageString = String.toString(100 / channels.length) + '%';
var percentage = 100 / channels.length;
return channels.map(function (channel, c) {
return {
title:{
text: channel.name
},
height: percentageString,
top: c === 0 ? undefined : String.toString(percentage * c) + '%',
offset: 0,
}
});
},
_initSeries: function (data) {
var that = this;
var samplingRate = data.sampling_rate;
Expand All @@ -5926,6 +6061,7 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
[0, offset],
[recordingEndInSecondsSnapped, offset],
],
//yAxis: c
};
});
},
Expand Down Expand Up @@ -6295,6 +6431,7 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {
//showFull: false,
//},
},
//yAxis: _initYAxis(data),
scrollbar: {
liveRedraw: false,
},
Expand Down Expand Up @@ -9485,10 +9622,13 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {

// console.log(that.vars.chart.series[index].yData);
that.vars.chart.series[index].yData.forEach((point, idx) => {
that.vars.chart.series[index].yData[idx] =
if(typeof point === "number"){
that.vars.chart.series[index].yData[idx] =
// some math that checks if the point is above or below the zero position and then scaling that value, then readding it to zeroposition
// to get an accurate percentage scaling;
point + value;
}

});

// code allowing the scaling to persist when you switch windows
Expand Down Expand Up @@ -9549,8 +9689,10 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {

_customAmplitude: function (index, scaleFactor) {
//converts scaleFactor to a decimal from percentage
//console.log(scaleFactor);
scaleFactor = scaleFactor / 100;
//if(index===2){console.log(scaleFactor);}
//if(index===5){console.log(scaleFactor);}
//console.log(scaleFactor);

var that = this;
//console.log("customAmplitude")
Expand All @@ -9568,7 +9710,7 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {

// console.log(that.vars.chart.series[index].yData);
let newData = that.vars.chart.series[index].yData.map((point, i) => {
if (point !== zeroPosition) {
if (point !== zeroPosition && typeof point === "number") {
// some math that checks if the point is above or below the zero position and then scaling that value, then readding it to zeroposition
// to get an accurate percentage scaling
return [that.vars.chart.series[index].xData[i], zeroPosition + (point - zeroPosition) * (1 + scaleFactor)];
Expand Down Expand Up @@ -9598,7 +9740,7 @@ $.widget("crowdeeg.TimeSeriesAnnotator", {

that.vars.allChannels.forEach((channel, idx) => {
that._scaleToScreen(idx);
console.log(that.vars.scalingFactors[idx]);
//console.log(that.vars.scalingFactors[idx]);
});
that.vars.originalScalingFactors = that.vars.scalingFactors;
},
Expand Down