Can I use different if-conditions in Highstock-code? - if-statement

I want that my Highstock-Chart is very dynamic. I extract the number of series and yAxis from var dataarray. So I can get e.g. 2 series or 6 or 3 or or or...
But now I have to set the code for the yAxis and series according to the number of series that are arriving. And if dataarray is e.g. 2 long the program should jump in the accordingly if-condition.
But that doesn't work in real life. Here is my code. What can I do instead? Can't I use JavaScript in Highstock-Code?

No you can't, and it's not because Highstock, but Javascript langauge.
When you create chart in that way:
new Highcharts.StokChart({
//options
})
you are passing a literal { abc: something, xyz: somethineElse } where you can't put if-else conditions.
I think you can create something like this:
var xAxis,
series;
if(x == 2) {
xAxis = { /* options */ };
series = [ /* options */ ];
} else {
// something else
}
var chartName = new Highcharts.StockChart({
xAxis: xAxis,
series: series
});

Related

Google Script Run Function IF text in another sheet's column contains a 'specific text'

I've done extensive search for this, but none of them seems to work. They all just give me a blank sheet.
Sample sheet
Basically I have a function that extracts data from Col. B in DATA, to Result. Then does some other things, split, trim etc...
I want to run this function when the text in Col. A in DATA is 250P.
So it would be like: IF (DATA!A1:A contains text "250p" then run function EXTRACT).
This is the code I have as of now:
//this extract works fine but I just need this to work for only those with value 250 in Col A//
function EXTRACT() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').setFormula('=EXTRACTDATA(DATA!A1:A)');
}
function IF250() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('DATA');
var range = sheet.getRange('DATA!A1:A');
var values = range.getValues();
if (values[i] == "250g") {
EXTRACT();
Better yet, If I can have the data set in 2 separate sheets. The 250s in one sheet & 500s in one sheet. But this is not necessary.
After reviewing your sheet, this is a possible solution
Code.gs
const sS = SpreadsheetApp.getActiveSpreadsheet()
function grabData() {
const sheetIn = sS.getSheetByName('data')
const sheetOut = sS.getSheetByName('Desired Outcome')
const range = 'A2:B'
/* Grab all the data from columns A and B and filter it */
const values = sheetIn.getRange(range).getValues().filter(n => n[0])
/* Retrieve only the names if it containes 250p */
/* In format [[a], [b], ...] */
const parsedValues = values.map((arr) => {
const [type, name] = arr
if (type.toLowerCase().includes('250p')) {
return name.split('\n')
}
})
.filter(n => n)
.flat()
.map(n => [n])
/* Add the values to the Desired Outcome Sheet */
sheetOut
.getRange(sheetOut.getLastRow() + 1, 1, parsedValues.length)
.setValues(parsedValues)
}
Try changing:
var values = range.getValues();
to
var values = range.getDisplayValues()
As this will read the value that is shown. Try logging the values with both to see why! (Blank)
You are also not currently iterating, or looping, your values.
If you're just looking to see if the column contains a cell containing the value 250p, try:
function IF250() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`DATA`)
const valueExists = sheet.getRange(`A1:A`)
.getDisplayValues()
.filter(String)
.some(row => row.includes(`250P`))
if (valueExists) EXTRACT()
}
Commented:
function IF250() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(`DATA`)
const valueExists = sheet.getRange(`A1:A`)
.getDisplayValues()
// Remove empty cells (not strictly necessary)
.filter(String)
// If the values include a row containing `250p` return true.
.some(row => row.includes(`250P`))
// If valueExists returns true:
if (valueExists) EXTRACT()
}

Google Sheets Scripts, IF date of a cell

I ve been getting some issues trying to use IF statement using items on the sheets as answers, mostly i been trying either using as firt statement a string directly or using another cell of the sheets with the statement comparing to the one i ve been trying to detect with out any results. Mostly my objective would be to, with a daly trigger, use an IF statement to detect every day if the date of today is the same as a date marked on a cell, my only current solution is and only thing is to make it detect if a cell is empty or not, so i just been using google Formulas that do the detect of the date, and if True make it empty, and if not make something appear
Here are some of the tests I ve been trying
Test 1:
var Sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SheetName");
else if (Sheet.getRange(1, 1).getValue() == "2/2/2020"){}
(cell A1=2/2/2020)
Test 2:
var Sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SheetName");
else if (Sheet.getRange(1, 1).getValue() == Sheet.getRange(1, 2).getValue()){}
(cell A1=2/2/2020)
(cell B1=2/2/2020)
Here are two solutions depending on what you are trying to accomplish.
This first one gets the values, formats them to a date format, and then compares them
function myFunction() {
var tz = Session.getScriptTimeZone();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('SheetName');
var data = sheet.getDataRange().getValues();
var aVal = Utilities.formatDate(data[0][0],tz,'MM/dd/YYYY');
var bVal = Utilities.formatDate(data[0][1],tz,'MM/dd/YYYY');
Logger.log([aVal,bVal])
if(aVal == bVal) {
//do something
}
}
This second section gets the display values and compares them. The first solution compares the date values while this second solution compares the dates as a string. Good luck!
function myFunction1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('SheetName');
var data = sheet.getDataRange().getDisplayValues();
var aVal = data[0][0]
var bVal = data[0][1]
Logger.log([aVal,bVal])
if(aVal == bVal) {
//do something
}
}

How can I scale my dataset values as a percentage of the index in chart.js?

Sorry if the question is poorly worded.Here is my chart
I am looking into scaling the chart's display of dataset(s) values as a percentage such as:
//input
data:{
datasets[{
label: 'data1',
data: [15, 22, 18, 35, 16, 29, 40]
},
{
label: 'data2',
data: [20, 21, 20, 19, 21, 22, 35]
}]
data1's points on the chart would be displayed as [42.9, 51.2, 47.4, 64.8, 43.2, 56.9, 57.1]
data2's points on the chart would be displayed as [57.1, 48.8, 52.6, 35.2, 56.8, 43.1, 42.9]
It should look like this. All visible lines should stack up to 100%. If a dataset is hidden, how can I recalculate the percentage and update the chart so that everything stays stacked up to 100%?
I thought about doing a plugin where I do the calculation using myLine.data.datasets but then I don't know how to remove a hidden dataset's values from the calculation and I'm not sure how to display it unless I overwrite the original datasets. I'm pretty sure this is the wrong approach.
Any help would be greatly appreciated.
So, I figured it out. I needed to write a function to calculate the percentage area of the points in the index and then update the datasets with the calculated percentage values.
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* DS_update calculates the percentage area of the input datasets
*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
function DS_update(dataset_in, ds_vis){
// make a deep copy (no references to the source)
var temp = jQuery.extend(true, [], dataset_in);
// gets the sum of all datasets at a given index
function getTotal(index){
total = 0;
// step through the datasets
dataset_in.forEach(function(e, i){
// inc total if the dataset is visible
if(ds_vis[i]){
total += e[index];
}
// do nothing if the dataset is hidden
});
return total;
}
// update temp array with calculated percentage values
temp.forEach(function(el, ind){
var j = ind;
el.forEach(function(e, i){
// calculate percentage to the hundredths place
temp[j][i] = Math.round((e / getTotal(i))*10000)/100;
});
});
return temp;
}
Once I tested the functions I had to run them before initial load of the chart or else the user would see the datasets as non area-percent (raw data). which looks something like this:
// Keep source array to use in the tool tips
var Src_ary = Input_data; // multidimensional array of input data
// holds the percent-area calculations as datapoints
var Prod_ary = DS_update(Src_ary, Init_visible(Src_ary));
Next up was updating the onClick for the legend. I need this to update the calculations every time an item's visibility is toggled:
legend: {
position: 'bottom',
usePointStyle: true,
onClick:
function(e, legendItem){
var index = legendItem.datasetIndex;
var ci = this.chart;
var meta = ci.getDatasetMeta(index);
var vis_ary = [];
var updatedSet = [];
// See controller.isDatasetVisible comment
meta.hidden = meta.hidden === null? !ci.data.datasets[index].hidden : null;
// load the visible array
for(var i = 0; i < (ci.data.datasets || []).length; i++){
switch (ci.getDatasetMeta(i).hidden){
case null:
vis_ary.push(true);
break;
default:
vis_ary.push(false);
break;
}
}
// update datasets using vis_ary to tell us which sets are visible
updatedSet = DS_update(Prod_ary, vis_ary);
myLine.data.datasets.forEach(function (e,i){
e.data = updatedSet[i];
});
// We did stuff ... rerender the chart
ci.update();
}
}
END RESULT
This is what I was trying to do: highchart fiddle
This is what I ended up with:fiddle
It took a few days and a lot of reading through chartjs.org's documentation to put this together. In the end I think it came out pretty good considering I am new to chart.js and borderline illiterate with javascript.

LineChart with certain columns

i am displaying a line chart and have data toggle on/off set up based on the answer (given by Abinaya Selvaraju) here:
Show/hide lines/data in Google Chart
it works great.
now i want to certain columns to be automatically grayed out when the chart first displays.
i think i need to do something like what's shown in the answer and came up with this:
for (var regionCol=0; regionCol<chartData.getNumberOfColumns();regionCol++){
if ((regionCol >= 2) && (regionCol <=7)){
columns[regionCol] = {
label: chartData.getColumnLabel(regionCol),
type: chartData.getColumnType(regionCol),
calc: function () {
return null;
}
};
// grey out the legend entry
//series[col - 1].color = '#CCCCCC';
series[Math.floor(regionCol/3)].color = '#CCCCCC';
}
else{
// show the data series
columns[regionCol] = regionCol;
//series[col - 1].color = null;
series[Math.floor(regionCol/3)].color = null;
}
}
var viewToHideRegions = new google.visualization.DataView(chartData);
viewToHideRegions.setColumns(columns);
chart.draw(viewToHideRegions, options);
/* code to set regions to be hidden */
This is how my chart data is defined:
chartData.addColumn('string', 'Date'); // Implicit series 1 data col.
chartData.addColumn('number', colIdxName); // Implicit domain label col.
chartData.addColumn({type:'string', role:'annotation'});
chartData.addColumn({type:'string', role:'annotationText'});
chartData.addColumn('number', dpndata[colGenIdx]['name']); // Implicit domain label col.
chartData.addColumn({type:'string', role:'annotation'});
chartData.addColumn({type:'string', role:'annotationText'});
When I run all of this, i get the message "All series on a given axis must be of the same data type"
I can't spot what I've got wrong.
Can anyone help?
There are a couple things you need to fix here. First, you only have 7 columns, and the column indices start at 0, so checking column indices 2-7 will start too late and overflow the end of the column list. Second, you always want your domain column (index 0) to be included, but you don't want to list it in the series option. Third, the "annotation" and "annotationText" columns need to have their roles specified in the view.
Your code should look something like this (to grey out all series by default):
var columns = [0];
for (var regionCol = 1; regionCol < chartData.getNumberOfColumns(); regionCol++) {
columns[regionCol] = {
label: chartData.getColumnLabel(regionCol),
type: chartData.getColumnType(regionCol),
role: chartData.getColumnProperty(regionCol, 'role'),
calc: function () {
return null;
}
};
// grey out the legend entry
if (regionCol % 3 == 1) {
series[Math.floor(regionCol / 3) - 1].color = '#CCCCCC';
}
}
var viewToHideRegions = new google.visualization.DataView(chartData);
viewToHideRegions.setColumns(columns);
chart.draw(viewToHideRegions, options);
/* code to set regions to be hidden */

google charts, tooltip replace column value

I'm using a combo chart from the google graph api (combo chart type). I want to add custom tooltips to add information about each point in the graph, but one of the value is replaced by the tooltip.
Here a very similar example graphic:
adding tooltip to graphs
Supposing that I'm using that graph. In my case, the value 106 (for the year 2011), is replaced by Growth 14% (the tooltip value)
Here the code that generates the data:
function gcomboChart () {
var data = new google.visualization.DataTable();
var dataVal =
[
["January",37903,655396,3411359,"Tooltip January"],
["February",33813,559595,3035931,"Tooltip February"],
["March",54073,725638,4561690,"Tooltip March"]
];
data.addColumn('string','month');
data.addColumn('number','Value1');
data.addColumn('number','Value2');
data.addColumn('number','Value3');
data.addColumn({type:'string', role:'tooltip'});
data.addRows(dataVal);
return(data);
}
//Here the code that generates the graph:
function drawChartComboChartID14cc19be5eef() {
var data = gcomboChart();
var options = { focusTarget: 'category'};
options["allowHtml"] = true;
options["seriesType"] = "bars";
options["series"] = {0: {targetAxisIndex:1,type:"line"}};
options["series"] = {0: {targetAxisIndex:2,type:"line"}};
options["vAxes"] = [{title:'Left Axis',format:'#,###',titleTextStyle:{color: 'orange'},textStyle:{color: 'orange'},textPosition:'out'},
{title:'Right Axis',format:'#,###',titleTextStyle:{color: 'blue'},textStyle:{color: 'blue'},textPosition:'out'}];
options["width"] = 1000;
options["height"] = 600;
options["pointSize"] = 9;
var chart = new google.visualization.ComboChart(
document.getElementById('ComboChart'));
chart.draw(data,options);
}
If you use the code, you'll see that the value of the third variable (Value3), is overwritten by the tooltip. I don't know hoy to get rid of that problem.
I want to show the three values of 'Value1-3' plus the tooltip
Can you please give me a hand?
Thanks!
Tooltips by default will replace the tooltip for that data point. It will not add an additional tooltip. To get around this, you need to add an additional series, and format the tooltip manually within that data value. You can then hide it from the legend, and have it display all nice as follows:
Here is the code:
function gcomboChart () {
var data = new google.visualization.DataTable();
//{v: x, f: y} allows you to set a manual format for each data point
var dataVal =
[
["January",37903,655396,3411359,{v: 0, f:"Tooltip January"}],
["February",33813,559595,3035931,{v: 0, f:"Tooltip February"}],
["March",54073,725638,4561690,{v: 0, f:"Tooltip March"}]
];
data.addColumn('string','month');
data.addColumn('number','Value1');
data.addColumn('number','Value2');
data.addColumn('number','Value3');
// Changed to standard data rather than tooltip role
data.addColumn('number','');
data.addRows(dataVal);
return(data);
}
//Here the code that generates the graph:
function drawVisualization() {
var data = gcomboChart();
var options = { focusTarget: 'category'};
options["allowHtml"] = true;
options["seriesType"] = "bars";
// the below line makes sure the tooltip is not shown in the legend
options["series"] = {0: {targetAxisIndex:0,type:"line"},3: {visibleInLegend:false}};
options["vAxes"] = [{title:'Left Axis',format:'#,###',titleTextStyle:{color: 'orange'},textStyle:{color: 'orange'},textPosition:'out'},
{title:'Right Axis',format:'#,###',titleTextStyle:{color: 'blue'},textStyle:{color: 'blue'},textPosition:'out'}];
options["width"] = 1000;
options["height"] = 600;
options["pointSize"] = 9;
var chart = new google.visualization.ComboChart(
document.getElementById('visualization'));
chart.draw(data,options);
}
Note: I should have switched series 3 to a line as well so that it doesn't push the bars over one. Change the series setting as follows to make it look nicer: options["series"] = {0: {targetAxisIndex:0,type:"line"},3: {visibleInLegend:false,type:"line"}};