Role on dynamic google chart (interval) - dynamic

How can I use this: https://developers.google.com/chart/interactive/docs/roles if I populate the graph dynamically?
This is part of my code:
...
data.addRows(dates.length);
for (i = 0; i < dates.length; i++){
if (i!=0){
data.setValue( i, 0, new Date(dates[i]) );
temp = graph[dates[i]];
var j = 0;
if (temp){
for (j = 0; j < groups.length; j++){
if ( groups[j] in temp){
var volume = parseFloat(temp[groups[j]]);
console.log(i + ' ' + j + ' ' + volume);
data.setValue( i, j+1, volume )
}
}
}
}else{
data.setValue( i, 0, new Date(dates[i]) );
var j = 0;
for (j = 0; j < groups.length; j++){
data.setValue( 0, j+1, 0 )
}
}
}
...
After I set the value with 'data.setValue', how can I set also the role? (I need for the interval value) Something like 'data.setRole' would be wonderful!! :-)

You can create the DataTable without any roles, and then create a DataView that assigns roles to the columns in the view. The documentation shows how to do this here:
DataView.setColumns Method
When creating a view, you can explicitly set the role of a column.
This is useful when creating a new calculated column. See
DataView.setColumns() for more information.
The DataView.setColumns() help file explains how to do this as follows:
setColumns(columnIndexes)
columnIndexes - An array of numbers and/or objects (can be mixed):
Numbers specify the index of the source data column to include in the view. The data is brought through unmodified. If you
need to explicitly define a role or additional column properties,
specify an object with a sourceColumn property.
Objects specify a calculated column. A calculated column creates a
value on the fly for each row and adds it to the view. The object
must have the following properties:
calc [function] - A function that will be called for each row in the column to calculate a value for that cell. The function
signature is func(dataTable, row), where dataTable is the source
DataTable, and row is the index of the source data row. The
function should return a single value of the type specified by
type.
type [string] - The JavaScript type of the value that the calc function returns.
label [Optional, string] - An optional label to assign to this generated column. If not specified, the view column will have no
label.
id [Optional, string] - An optional ID to assign to this generated column.
sourceColumn - [Optional, number] The source column to use as a value; if specified, do not specify the calc or the type property.
This is similar to passing in a number instead of an object, but
enables you to specify a role and properties for the new column.
properties [Optional, object] - An object containing any arbitrary properties to assign to this column. If not specified, the view
column will have no properties.
role [Optional, string] - A role to assign to this column. If not specified, the existing role will not be imported.
So if your interval column is column #3, for instance, you would say:
dataView.setColumns([0, 1, {sourceColumn: 2, role: 'interval'}]);
The above sets columns 0, and 1 as-is, without a role, while column 2 is assigned as an interval column.
EDIT
Responding to the comments, the intervals are in the data set. Here is an example:
function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['Year', 'Austria', 'Interval A', 'Interval B'],
['2003', 100, 95, 125],
['2004', 110, 96, 150],
['2005', 120, 97, 175],
['2006', 130, 98, 200],
['2007', 140, 99, 225],
['2008', 150, 100, 250]
]);
// Create Data View using columns 2 & 3 as intervals
var dataView = new google.visualization.DataView(data);
dataView.setColumns([0, 1, {sourceColumn: 2, role: 'interval'}, {sourceColumn: 3, role: 'interval'}]);
// Create and draw the visualization.
new google.visualization.LineChart(document.getElementById('visualization')).
draw(dataView,
{width:600, height:400,
hAxis: {title: "Year"}}
);
}
You include the interval values in the original data table as columns. You then use a dataview to change those columns to 'interval' role columns. Then you draw the dataview. This will provide error bars (interval columns) as expected.

Related

creating a random list of coins from a repository

I'm trying to build a simple app that would display a coin collection for each user I type into a textfield. The coin collection would have to be unique to each user. I already have the repository for the coins. How do I generate a new random coin collection for each user? Each collection could have multiple coins of the same value but with different years.
object CoinRepository {
fun getCoinCollection(): List<Coin> {
return listOf(
Coin(
id = 1,
name = "Penny",
year = (1900..2022).random()
),
Coin(
id = 2,
name = "Nickel",
year = (1900..2022).random()
),
Coin(
id = 3,
name = "Dime",
year = (1900..2022).random()
),
Coin(
id = 4,
name = "Quarter",
year = (1900..2022).random()
),
Coin(
id = 5,
name = "Dollar",
year = (1900..2022).random()
)
)
}
}
data class Coin(
val id: Int,
val name: String,
val year: Int
)
You could do something like this:
import kotlin.random.Random
// Define your specific data in an enum, with all the relevant properties
enum class Denomination(val id: Int, val label: String) {
PENNY(1, "Penny"),
NICKEL(2, "Nickel"),
DIME(3, "Dime"),
QUARTER(4, "Quarter"),
DOLLAR(5, "Dollar");
companion object {
// a simple way to return one of the instances at random - the property
// avoids creating a new values() array every time it's called
val values = values()
fun random() = values.random()
}
}
// a basic way to keep the random date logic in the Coin class itself, using
// a default parameter. No validation involved obviously!
data class Coin(val id: Int, val label: String, val year: Int = (1900..2022).random())
// get a random number of Coins, within a certain min/max
fun getCoinCollection() = List(Random.nextInt(1, 10)) {
// pulls a random coin type and creates a Coin, letting its constructor
// handle the random date (you could do it here if you want)
Denomination.random().run { Coin(id, label) }
}
There's more than one way to organise it, I've thrown a few things in there so you can get some ideas of how you might do it. But it's basically a function that creates a list of random length (within limits), and then creates a Coin for each item, using a random Denomination
The Denomination enum is just a way to define your data, a fixed set of possible items with certain properties. Because enums generate that values() array automatically (containing all its instances) you can easily pick one at random. You could also extend the properties here to include a valid date range for each coin type, etc
You could just automatically generate the label and id values from the enum's name and ordinal properties (e.g. "PENNY" and 0) so you don't need to declare them explicitly - I feel like it's usually a good idea to decouple the data from how it's represented in the enum in code, but that's your call - I've included it so you can see how

How do I use variables to avoid having to create a set of these for all of the columns in my sheet?

I'm trying to get my sheet to automatically recalculate a set of dates within a schedule, in both directions, when a cell is changed.
The code works fine, but I need to add a bunch more columns and I'd really rather not copy/paste/find/replace a load more times. I'm fairly certain I can do this with variables (just looking up the column identifier and feeding that into the code somehow), but I don't know-how.
functJon onEdJt(e) {
var sh = e.source.getActJveSheet();
Jf(sh.getName() === 'Date Calculator' && e.range.getA1NotatJon() === 'C9'
)
{
sh.getRange("C10").setFormula("=WORKDAY(C9,+$C$3)");
sh.getRange("C11").setFormula("=WORKDAY(C10,+10)");
sh.getRange("C12").setFormula("=WORKDAY(C11,+$C$4)");
sh.getRange("C13").setFormula("=WORKDAY(C12,+$C$3)");
sh.getRange("C14").setFormula("=WORKDAY(C13,+10)");
sh.getRange("C15").setFormula("=WORKDAY(C14,+1)");
sh.getRange("C16").setFormula("=WORKDAY(C15,+$C$5)");
}
else Jf (sh.getName() === 'Date Calculator' && e.range.getA1NotatJon()
=== 'C10' )
{
sh.getRange("C9").setFormula("=WORKDAY(C10,-$C$3)");
sh.getRange("C11").setFormula("=WORKDAY(C10,+10)");
sh.getRange("C12").setFormula("=WORKDAY(C11,+$C$4)");
sh.getRange("C13").setFormula("=WORKDAY(C12,+$C$3)");
sh.getRange("C14").setFormula("=WORKDAY(C13,+10)");
sh.getRange("C15").setFormula("=WORKDAY(C14,+1)");
sh.getRange("C16").setFormula("=WORKDAY(C15,+$C$5)");
Ideally the code should then just "work" for any number of columns in the sheet, so I don't need to add more code if I add more columns.
Update
Here's an example of what I'm trying (but it's not working) - attempting to check that the active cell is in row 9 of a specific column before then running the "set.Formula" functions:
function onEdit(e) {
var sh = e.source.getActiveSheet();
var col = e.source.getActiveSheet().getRange().getColumn();
var row = e.source.getActiveSheet().getRange().getRow();
if(sh.getName() === 'Date Calculator' && e.getRange('9',col) )
Event Objects
Even though the code was written as onEdit(e), you didn't take advantage of the Event Objects.
In this answer, the code returns the new value of the edited cell and also the range. The range is then used to work out the row, column and sheet name and these is used for validation as well as for building the ranges and the setFormula
Variables
The code includes variables for the valid range of columns that can be used for data entry (Column C to Column H), and respective input rows (rows 9 and 10). These are expressed as values, but they could just as easily be written into the spreadsheet as assumptions and the values obtained in the code by using getValue.
The absolute cell references used in the setFormula are partly variable (column reference) and part hard-coded (the respective rows-3,4 and 5). If desired, the rows could be variable as well.
Efficiency
There is just one if statement containing one version of the code to build setFormula.
This is achieved by designing the if statement:
1. if the sheet = "Date Calculator" AND
2. if the editColumn is between the valid ColumnStart and ColumnEnd values (Column C to H) AND
3. if the editRow is between the valid Row values (rows 9 or 10) AND
4. if the edited value isn't a blank (length != 0).
The last condition ("edited value is blank") ensures that if cell contents are been deleted (and/or have no value), then the code won't proceed.
Convert column number to letter
I used a routine written by #AdamL found at Convert column index into corresponding column letter; this converts a column number into a letter. It's used to build the "targetcolumn" address in Workdays. It's valid for the letters A-Z; there's a version for letters beyond Z.
Cleanup
If data is entered into row 10 of a given column, then any value in row 9 (of the same column) needs to be deleted. The code does this and also deletes any pre-existing formula dates in the rows below so there is no confusion about the dates derived by the data entry.
function onEdit(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "Date Calculator";
var sheet = ss.getSheetByName(sheetname);
// get the event source data
var editedCell = e.range;
var editRow = editedCell.getRow();
var editCol = editedCell.getColumn();
var eValue = e.value;
var editedSheet = editedCell.getSheet().getName();
//Logger.log("DEBUG: the cell = "+editedCell.getA1Notation()+", the column = "+editCol+", the row is "+editRow+", the value is "+eValue+", the edited sheet is "+editedSheet);
// create some variables for column and row range
var columnStart = 3; // Column C
var columnEnd = 8; // Column H
var rowOption1 = 9; // row 9
var rowOption2 = 10 // row 10
// create some variables for target cells
var absolutecolumn = "C";
//var absoluterow1 = 3; // not used
//var absoluterow2 = 4; // not used
//var absoluterow3 = 5; // not used
// test for valid edit in row option 1 // Row 9
if(editedSheet === sheetname && columnEnd >=editCol && editCol>=columnStart && rowOption2>=editRow && editRow>=rowOption1 && eValue.length !=0 ){
//Logger.log("DEBUG: You got the right sheet, the edit is in the right range of columns and the edited row was = "+rowOption1);
if (editRow == rowOption2){
// clear row 9
sheet.getRange((+editRow-1),editCol).clear();
}
// clear following 8 rows of data
sheet.getRange((+editRow+1),editCol,8).clear();
// set the targetcolumn as a letter
var targetcolumn = columnToLetter(editCol);
// set formula for row+1
sheet.getRange((+editRow+1),editCol).setFormula("=WORKDAY("+targetcolumn+editRow+",$"+absolutecolumn+"$3)"); //
// set formula row +2
sheet.getRange((+editRow+2),editCol).setFormula("=WORKDAY("+targetcolumn+(+editRow+1)+",+10)");
// set formula row +3
sheet.getRange((+editRow+3),editCol).setFormula("=WORKDAY("+targetcolumn+(+editRow+2)+",$"+absolutecolumn+"$4)");
// set formula row +4
sheet.getRange((+editRow+4),editCol).setFormula("=WORKDAY("+targetcolumn+(+editRow+3)+",$"+absolutecolumn+"$3)");
// set formula row + 5
sheet.getRange((+editRow+5),editCol).setFormula("=WORKDAY("+targetcolumn+(+editRow+4)+",+10)");
// set formula row + 6
sheet.getRange((+editRow+6),editCol).setFormula("=WORKDAY("+targetcolumn+(+editRow+5)+",+1)");
// set formula row + 7
sheet.getRange((+editRow+7),editCol).setFormula("=WORKDAY("+targetcolumn+(+editRow+6)+",$"+absolutecolumn+"$5)");
// change the background to show entry in rowoption1
sheet.getRange(editRow,editCol).setBackground("yellow");
sheet.getRange((+editRow+1),editCol).setBackground("white");
}
}
function columnToLetter(column)
{
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
Screenshot

How to create a Calculated Field in dimple.js

How do i add a calculated field (a field which does calculation of two or more data fields) in dimple.js?
eg. I have two fields
1. "Sales Value"
2. "Sales Volume"
Now i have to calculate a field ASP = Sales Value /Sales Volume.
I'm afraid dimple doesn't have a built in way to handle this. I assume dimple is aggregating the data for you - hence the difficulty. But here you have no option but to pre-aggregate to the level of a data point and add the calculated field yourself. For example if your data had Brand, SKU and Channel but your chart was at the Brand, Channel level you would need to pre-process the data like this:
// var chartData is going to be the aggregated level you use for your chart.
// var added is a dictionary of aggregated data returning a row index
// for each Brand/Channel combination.
var chartData = [],
added = {};
// Aggregate to the Brand/Channel level
data.forEach(function (d) {
var key = d["Brand"] + "|" + d["Channel"],
i = added[key];
// Check the output index
if (i !== undefined) {
// Brand/Channel have been added already so add the measures
chartData[i]["Sales Value"] += parseFloat(d["Sales Value"]);
chartData[i]["Sales Volume"] += parseFloat(d["Sales Volume"]);
} else {
// Get the index for the row we are about to add
added[key] = chartData.length;
// Insert a new output row for the Brand/Channel
chartData.push({
"Brand": d["Brand"],
"Channel": d["Channel"],
"Sales Value": parseFloat(d["Sales Value"]) || 0,
"Sales Volume": parseFloat(d["Sales Volume"]) || 0
});
}
});
// Calculate ASP
chartData.forEach(function (d) {
d["ASP"] = d["Sales Value"] / d["Sales Volume"];
});
// Draw the chart using chartData instead of data
...

Make line chart with values and dates

In my app i use ios-charts library (swift alternative of MPAndroidChart).
All i need is to display line chart with dates and values.
Right now i use this function to display chart
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Items count")
let lineChartData = LineChartData(xVals: dataPoints, dataSet: lineChartDataSet)
dateChartView.data = lineChartData
}
And this is my data:
xItems = ["27.05", "03.06", "17.07", "19.09", "20.09"] //String
let unitsSold = [25.0, 30.0, 45.0, 60.0, 20.0] //Double
But as you can see - xItems are dates in "dd.mm" format. As they are strings they have same paddings between each other. I want them to be more accurate with real dates. For example 19.09 and 20.09 should be very close. I know that i should match each day with some number in order to accomplish it. But i don't know what to do next - how i can adjust x labels margins?
UPDATE
After small research where i found out that many developers had asked about this feature but nothing happened - for my case i found very interesting alternative to this library in Swift - PNChart. It is easy to use, it solves my problem.
The easiest solution will be to loop through your data and add a ChartDataEntry with a value of 0 and a corresponding label for each missing date.
In response to the question in the comments here is a screenshot from one of my applications where I am filling in date gaps with 0 values:
In my case I wanted the 0 values rather than an averaged line from data point to data point as it clearly indicates there is no data on the days skipped (8/11 for instance).
From #Philipp Jahoda's comments it sounds like you could skip the 0 value entries and just index the data you have to the correct labels.
I modified the MPAndroidChart example program to skip a few data points and this is the result:
As #Philipp Jahoda mentioned in the comments the chart handles missing Entry by just connecting to the next data point. From the code below you can see that I am generating x values (labels) for the entire data set but skipping y values (data points) for index 11 - 29 which is what you want. The only thing remaining would be to handle the x labels as it sounds like you don't want 15, 20, and 25 in my example to show up.
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < count; i++) {
xVals.add((i) + "");
}
ArrayList<Entry> yVals = new ArrayList<Entry>();
for (int i = 0; i < count; i++) {
if (i > 10 && i < 30) {
continue;
}
float mult = (range + 1);
float val = (float) (Math.random() * mult) + 3;// + (float)
// ((mult *
// 0.1) / 10);
yVals.add(new Entry(val, i));
}
What I did is fully feed the dates for x data even no y data for it, and just not add the data entry for the specific xIndex, then it will not draw the y value for the xIndex to achieve what you want, this is the easiest way since you just write a for loop and continue if you detect no y value there.
I don't suggest use 0 or nan, since if it is a line chart, it will connect the 0 data or bad things will happen for nan. You might want to break the lines, but again ios-charts does not support it yet (I also asked a feature for this), you need to write your own code to break the line, or you can live with connecting the 0 data or just connect to the next valid data.
The down side is it may has performance drop since many xIndex there, but I tried ~1000 and it is acceptable. I already asked for such feature a long time ago, but it took lot of time to think about it.
Here's a function I wrote based on Wingzero's answer (I pass NaNs for the entries in the values array that are empty) :
func populateLineChartView(lineChartView: LineChartView, labels: [String], values: [Float]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<labels.count {
if !values[i].isNaN {
let dataEntry = ChartDataEntry(value: Double(values[i]), xIndex: i)
dataEntries.append(dataEntry)
}
}
let lineChartDataSet = LineChartDataSet(yVals: dataEntries, label: "Label")
let lineChartData = LineChartData(xVals: labels, dataSet: lineChartDataSet)
lineChartView.data = lineChartData
}
The solution which worked for me is splitting Linedataset into 2 Linedatasets. First would hold yvals till empty space and second after emptyspace.
//create 2 LineDataSets. set1- till empty space set2 after empty space
set1 = new LineDataSet(yVals1, "DataSet 1");
set2= new LineDataSet(yVals2,"DataSet 1");
//load datasets into datasets array
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1);
dataSets.add(set2);
//create a data object with the datasets
LineData data = new LineData(xVals, dataSets);
// set data
mChart.setData(data);

dojo how to get portlet position object that have value(column,row,width,height) in GridContainer?

hi my server tries to store information about portlet. I would like to get information(column,row,width,height) about the location of the portlet. but I think I have. more difficult to me..
for example there are two single portlet in GridContainer. I want to get information about the portlet on the column,row and the value of the width, height.if i imported value one of two portlet in GridContainer, the value of the first portlet row: 0, column: 0, width: 50px, height: 50px, the value of the second portlet row: 0, column: 1, width: 50px , height: 50px.
ask for advice.
my code
var selectedForm=registry.byId('tabContainer').get('selectedChildWidget');
var grid=selectedForm.getChildren();->have portlets in GridContainer
var oldcolumn=null;
var row=1;
for(var i =0; i<grid.length;i++)
{
for(var j=0;j<grid[i].length;j++){
if (oldcolumn==null)
oldcolumn=grid[i].getChildren()[j].domNode.parentNode.cellIndex;
console.log(' column:'+grid[i].getChildren()[j].domNode.parentNode.cellIndex);
if(oldcolumn == nodelist[i].domNode.parentNode.cellIndex)
console.log('column:'+grid[i].getChildren()[j].domNode.parentNode.cellIndex+'row:'+(++row));
else
{
row=1;
oldcolumn=grid[i].getChildren()[j].domNode.parentNode.cellIndex;
console.log(' column:'+grid[i].getChildren()[j].domNode.parentNode.cellIndex'row:'+(++row));
}
}
}
Column
If I read the documentation about the dojox/layout/GridContainer, it states the following:
By requiring the GridContainer, two new attributes are mixed into the
dijit/_WidgetBase class:
column: Holds the current column a widget is in. (If any). Defaults to “1”
dragRestriction: Is the widget draggable? You can use this to prohibit the dragging of a specific Widget
This means you could use the following to get the column:
myPortlet.get("column");
Row
There is no such thing as a "row", so I suppose you want to retrieve the zone. There's no way to retrieve the zone of the portlet though, but you can calculate it by yourself. If you use getChildren() on the GridContainer, the portlets are always returned in the same order based on their column and then their zone. However, this is undocumented and may change in the future.
So this means you could easily write a counter that increments each time a portlet is in the same column (which means he's in the next zone). For example:
var children = registry.byId("myGrid").getChildren();
var perColumn = [ ];
arrUtils.forEach(children, function(portlet) {
var id = portlet.get("id");
var col = portlet.get("column");
if (perColumn[col] !== undefined) {
perColumn[col]++;
} else {
perColumn[col] = 0;
}
var zn = perColumn[col];
console.log(id + ": " + col + " " + zn);
});
Get width + height of portlet
The width and the height of the portlet are determined by its content, so no width or height properties are available. However, you could get the DOM node of the content and retrieve the width and height from it using dojo.position (in recent versions it's the dojo/dom-geom module), for example:
require([ "dojo/dom-geom" ], function(domGeom) {
var box = domGeom.position(myPortlet.get("domNode"));
console.log(box.w + " " + box.h);
});
I also made an example to retrieve all properties (column + zone + width + height): http://jsfiddle.net/dF94F/