Get the minimum value in the chart rendered for flot.js - javascript

Is it possible to get the lowest value in the chart itself assuming that the data is dynamic?Take a look at this example Fiddle.
$(function () {
var d1 = [];
for (var i = 0; i < 14; i += 0.5)
d1.push([i, Math.sin(i)]);
$.plot($("#placeholder"), [ d1]);
});
How can I get the lowest value in this line chart?
Update: It seems my earlier example didn't quite make sense please take a look at this link: https://abtw.alliancebernstein.com.tw/APAC/TW/Funds/American-Income.htm?ShareClassId=60006908 make sure to turn off Flash plugin so that Flotchart will render. Now looking at the area chart I want to get the lowest value base on the chart rendered. Is this possible?

If you save your plot object like so
var plot = $.plot($("#placeholder"), [ d1]);
you can get the minimum value from it with
var minimum = plot.getData()[0].yaxis.datamin;
The same is possible for maximum value (datamax), for the xaxis and for other data series (the index behind getData()).

http://jsfiddle.net/fenderistic/Sf5Yr/
Simply keep a lowest variable, and check throughout the for-loop to see if the value lower, if so, replace the current lowest value with it.
$(function () {
var d1 = [];
//Assuming you're always starting at zero
var lowest = Math.sin(0);
for (var i = 0; i < 14; i += 0.5) {
d1.push([i, Math.sin(i)]);
if (Math.sin(i) < lowest) {
lowest = Math.sin(i);
}
}
alert(lowest)
$.plot($("#placeholder"), [d1]);
});

Related

Trim string fetched from a Google Spreadsheet range and return the largest value

I am building a small script and, on it, I need to "read" the largest value in a range of cells. The cells look like this: "a-10", "a-3", "a-4" and so on, but their real values are 10, 3 and 4 respectively. I cannot remove the "a-" from the cells.
What I have so far is this:
Code:
function getLargestValue() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheets=['Sheet1','Sheet2','Sheet3'];
var values=[];
for(var i=0;i<sheets.length;i++)
{
var sht=ss.getSheetByName(sheets[i]);
var rng=sht.getDataRange();
var rngA=rng.getValues();
for(var j=1;j<rngA.length;j++)
{
rngA[j][0] = substring(2, rngA[j][0]);
values.push(rngA[j][0]);
}
}
values.sort(function(a, b) {return a - b;});
var max = values[values.length-1];
SpreadsheetApp.getUi().alert('The max is ' + max); //Just checking to be sure it works
return max;
// must show the largest value on "Max" sheet
}
Link to spreadsheet:
https://docs.google.com/spreadsheets/d/16WrsztYlOV7qDOMBqGYU8az26Bw9NoqdQ4Imk_hZM90/edit?usp=sharing
I still need to trim the strings and insert the largest value in a different sheet. How could I do that?
Thank you.
Before your sort function I would try something like
// Use a regex to get any numbers from the cell value and make
// a new array
var numbers = values.map(function(el) { return /[0-9]+/.exec(el) }
and then at the end before returning
// Put the 'max' value in 'MAX' sheet in cell 'A1'
ss.getSheetByName('MAX').getRange("A1").setValue(max)
Side note: You can also get a maximum number like this:
var max = Math.max(1,2,3,4,5)
OR
var max = Math.max.apply(null,ARRAY_OF_NUMBERS)
function getLargestValue()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheets=['Sheet1','Sheet2','Sheet3'];
var values=[];
for(var i=0;i<sheets.length;i++)
{
var sht=ss.getSheetByName(sheets[i]);
var rng=sht.getDataRange();
var rngA=rng.getValues();//this will give you a 2D array
for (var items in rngA) //foreach loop, easier than for loop
{
var result = rngA[items][0].substring(2);//split string at 2nd character
if(!isNaN(parseFloat(result)) && isFinite(result))// check whether it is a number (since cell has strings like "Values")
{
values.push(result);//adds all numbers to the array
}
}
}
values.sort(function(a, b) {return a - b;});
var max = values[values.length-1];
//SpreadsheetApp.getUi().alert('The max is ' + max); //Just checking to be sure it works
//return max;
// must show the largest value on "Max" sheet
ss.getSheetByName('MAX').getRange("A2").setValue(max)
}

Auto increment row and column results to csv file in javascript

I'm converting XML results to CSV using the following code. It will automatically increment the 'row' but I am having to set each 'column' value. I attempted to alter the code but the outcome was not functional. I believe the issue lies within the 'new XML' line but I haven't been able to find any information relating to this. So my question is can how can this be coded to auto increment the column value as well?'
Thank you - Matt
var length = msg['result'].length();
var x = 0;
for(var i=0;i<length;i++)
{
tmp['row'][x] = new XML("<row/>");
tmp['row'][x]['column1'] = '"'+msg['result'][i]['this'].toString()+'"';
tmp['row'][x]['column2'] = '"'+msg['result'][i]['that'].toString()+'"';
tmp['row'][x]['column3'] = '"'+msg['result'][i]['other'].toString()+'"';
x++;
}
So after some more testing and guesswork, I came up with the following that is fucntional and returns the output I am needing:
var length = msg['result'].length();
var x = 0;
// This is the number of columns I know will be returned
var z = 3;
for(var i=0;i<length;i++)
{
tmp['row'][x] = new XML("<row/>");
tmp['column'][z] = new XML("<column/>");
tmp['row'][x]['column'][z] = '"'+msg['result'][i]['this'].toString()+'"';
tmp['row'][x]['column'][z] = '"'+msg['result'][i]['that'].toString()+'"';
tmp['row'][x]['column'][z] = '"'+msg['result'][i]['other'].toString()+'"';
z++;
x++;
}

Matrix conversion in a particular cluster Structure

I am trying to convert the Distance Matrix I got after calculating Euclidean Distance and taking Matrix form of it, to a manual defined clustered pattern.
In my case, this is the matrix suppose which normal pattern
Suppose, this is the desired cluster order, I have to convert i.e. from 1 2 3 4 to 3 1 2 4, I want to have something like this. I do not want to do it manually since my matrix size is 40 X 40.
It is not clicking to my mind, I can code but algorithm is not coming to my mind. If you could help or someone has done something like this before. Please help me out with it.
I tried reading the data from csv with the help of d3.csv which gives the desired result of 1234 like this :
d3.csv("final.csv", function(loadeddata) {
mydata = loadeddata.map(function(d) {return [+d["1"], +d["2"] , +d["3"], +d["4"]] ;});
to
d3.csv("final.csv", function(loadeddata) {
mydata = loadeddata.map(function(d) {return [+d["3"], +d["1"] , +d["2"], +d["4"]] ;});
so that I can get the matrix in desired clustered format but it did not work coz it shifting whole column to the desired place but not shifting the element wise.
Here is another try I made
var iMax = 4;
var jMax = 4;
var newdata = new Array();
for (i=0;i<iMax;i++) {
newdata[i]=new Array();
for (j=0;j<jMax;j++) {
newdata[i][j]=0;
}
}
var arraycomb = [3,1,2,4];
for ( i = 0; i < 4; i++) {
for ( j = 0; j < 4; j++) {
newdata[arraycomb[i]][arraycomb[j]] = mydata[i][j];
}
}

ParseFloat array values

I'm trying to parse the values in my array then add them to a chart. Problem is I'm getting some unexpected numbers on the out put. I loop through and populate my array. On the button click I loop through and parse the data and put it into another array to pass to a chart series. This in theory works but the data points received are in accurate. I've attached a picture of what the console out puts look like. Once I parse it either i'm getting the first digit of the element or half of the number.
Thank you
Here is the code :
var testData = [];
for (var i = 0; i < 5; i++) {
testData[i] = populationData[i].Census;
console.log(testData[i]);
}
// the button action
$('#button').click(function () {
var mySeries = [];
var data = [];
for (var i = 0; i < 5; i++) {
data[i] = parseFloat(testData[i]);
mySeries[i] = data[i];
}
console.log(mySeries);
var chart = $('#container').highcharts();
chart.series[0].setData(mySeries);
You need to remove the commas - JavaScript does not use thousand separators
parseFloat(testData[i].replace(/,/g,""));
or just
parseInt(testData[i].replace(/,/g,""),10);
if there are no decimals

Algorithm to remove extreme outliers in array

I've got an array which I use for the x-axis in a D3 graph, and it blows up because the chart size is too small for the size of the array. I had a look at data and there are extreme outliers in the data. See chart below.
The data around 0 (its not totally zero, its 0.00972 etc).
The data starts getting interesting around 70, then massive spikes about 100. the data then continues and then the same sort of thing on the other side about 200.
Can anyone help me with some algo that removes the extreme outliers? e.g. give me 95% or 90% percentiles and remove the contiguous elements (e.g. not just one element from the middle but x number of elements from the start of the array and the end of the array, where x depends on working out where best to do it based on the data? In Javascript as well please!
thanks!
ps you'll need to save the image to view it properly
Assuming the data is like
var data[] = {0.00972, 70, 70, ...};
first sort
data.sort(function(a,b){return a-b});
then take off the bottom 2.5% and top 2.5%
var l = data.length;
var low = Math.round(l * 0.025);
var high = l - low;
var data2 = data.slice(low,high);
An alternative would be to only show data within 3 standard deviations of the mean. If you data is normally distributed 99.7% will fall in this range.
var sum=0; // stores sum of elements
var sumsq = 0; // stores sum of squares
for(var i=0;i<data.length;++i) {
sum+=data[i];
sumsq+=data[i]*data[i];
}
var mean = sum/l;
var varience = sumsq / l - mean*mean;
var sd = Math.sqrt(varience);
var data3 = new Array(); // uses for data which is 3 standard deviations from the mean
for(var i=0;i<data.length;++i) {
if(data[i]> mean - 3 *sd && data[i] < mean + 3 *sd)
data3.push(data[i]);
}
Or similar using some multiple of the Inter-quartile range
var median = data[Math.round(l/2)];
var LQ = data[Math.round(l/4)];
var UQ = data[Math.round(3*l/4)];
var IQR = UQ-LQ;
var data4 = new Array();
for(var i=0;i<data.length;++i) {
if(data[i]> median - 2 * IQR && data[i] < mean + 2 * IQR)
data4.push(data[i]);
}

Categories