Dimple Specific Axis Variable Value - d3.js

My Data is nested in the hopes of adding in a value slider to dynamically change the year. One entry looks like:
key: "dot1"
values: Array[3]
[0]: Object
x: 3
y: 2
[1]: Object
x: 2
y: 5
[2]: Object
x: 3
y: 5
key: "dot2"
etc...
Is there a way to access the values to graph? Something like:
var chart = new dimple.chart(svg, rawData);
chart.setBounds(90, 35, 480, 325)
var myyAxis = chart.addMeasureAxis("y", "values[0][y]");
var myxAxis = chart.addMeasureAxis("x", "values[0][x]");
chart.addSeries(["key"], dimple.plot.bubble);
chart.draw(1000);

You can't add a computed string as a measure property, so the easiest way I know to accomplish this is to format the data to flatten out your internal arrays, so instead of
key: "dot1"
values: Array[3]
[0]: Object
x: 3
y: 2
[1]: Object
x: 2
y: 5
[2]: Object
x: 3
y: 5
you end up with
[
{ 'key' : 'dot1', 'x' : 3, 'y' : 2 },
{ 'key' : 'dot1', 'x' : 2, 'y' : 5 },
{ 'key' : 'dot1', 'x' : 3, 'y' : 5 },
]
I got a basic example of this working here : http://jsbin.com/rijuvoneyu/1/edit?js,output
It uses underscore.js to map each row in the dataset, map each value array inside (so for every 1 row in the dataset you end up with 3), then flatten the entire structure out. Now you can just use 'x' and 'y' as the properties for the measure axes.
You'll notice I added a unique identifier for each new data row. Without that, if I only did
var series = chart.addSeries(["key"], dimple.plot.bubble);
dimple was adding all the values sharing the same 'key' together, so only plotting two bubbles. I'm not sure if there is a better way to not have that happen without the unique id, but it works well enough.

Related

Google Sheets: Sort Two Columns

In a sheet I have two rows that I'd like to sort on:
ColA: Archived - True/False checkbox
ColB: SortOrder - number, a way I have at the moment to group things
Cols C-G: various details
I have a function that does this from a macro, as I'll add a button to the sheet later to run this:
function SortServices() {
var ss = SpreadsheetApp.getActive();
ss.getRange('A1:B').activate();
ss.getActiveRange().offset(1, 0, spreadsheet.getActiveRange().getNumRows() - 1).sort([{column: 1, ascending: true}, {column: 2, ascending: true}]);
};
I'm having a problem, as there are 1000 rows but only ~300 rows currently have data. Of these, about 200 are unchecked and 100 checked (as Archived). So this sorts the entire colA, then colB, which doesn't correspond to the data cols.
Struggling to explain this, imgs might be better. Here's an example, originally:
After applying the Sort:
The issue must be the rows without data, but ColA has checkboxes, which by default are unchecked. Is there a simple way to get around this? (I don't know much about Sheets)
[Edit] Sorry, should have added what I would like... the rows with no data at the end, same as the first img. When a row is checked as Archived, using Sort would then move it down into the lower section together with other Archived items:
Solution
Divide the data in two sets and sort in the desired order those that meet the requirements (have data in column B).
Code
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var lr = ss.getLastRow()
// original values
var range = ss.getRange(1, 1, lr, 3).getValues()
// cells without data in colB
var filtered = range.filter(function (value, index, arr) {
return value[1] == "";
});
// cells with data in colB
var original = range.filter(x => !filtered.includes(x));
// write
ss.getRange(1, 1, original.length, 3).setValues(original).sort([{ column: 1, ascending: true }, { column: 2, ascending: true }]);
ss.getRange(1 + original.length, 1, filtered.length, 3).setValues(filtered)
}
Thought there would be an easier answer, but this works OK:
Inserted a new colA with formula to create a single sortable column:
=if( isblank(D2), 20000, if(B2=true, 10000+C2, C2 ) )
Then used this script:
function SortServices() {
var ss = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActive().getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
ss.getActiveRange().offset(1, 0, ss.getActiveRange().getNumRows() - 1).sort({column: 1, ascending: true});
};
Probably add this to a custom menu.

Bar Chart on Dimension-1 and Stacked by Dimension-2

Summary
I want to display a bar chart whose dimension is days and is stacked by a different category (i.e. x-axis = days and stack = category-1). I can do this "manually" in that I can write if-then's to zero or display the quantity, but I'm wondering if there's a systematic way to do this.
JSFiddle https://jsfiddle.net/wostoj/rum53tn2/
Details
I have data with dates, quantities, and other classifiers. For the purpose of this question I can simplify it to this:
data = [
{day: 1, cat: 'a', quantity: 25},
{day: 1, cat: 'b', quantity: 15},
{day: 1, cat: 'b', quantity: 10},
{day: 2, cat: 'a', quantity: 90},
{day: 2, cat: 'a', quantity: 45},
{day: 2, cat: 'b', quantity: 15},
]
I can set up a bar chart, by day, that shows total units and I can manually add the stacks for 'a' and 'b' as follows.
var dayDim = xf.dimension(_ => _.day);
var bar = dc.barChart("#chart");
bar
.dimension(dayDim)
.group(dayDim.group().reduceSum(
_ => _.cat === 'a' ? _.quantity : 0
))
.stack(dayDim.group().reduceSum(
_ => _.cat === 'b' ? _.quantity : 0
));
However, this is easy when my data has only 2 categories, but I'm wondering how I'd scale this to 10 or an unknown number of categories. I'd imagine the pseudo-code I'm trying to do is something like
dc.barChart("#chart")
.dimension(xf.dimension(_ => _.day))
.stackDim(xf.dimension(_ => _.cat))
.stackGroup(xf.dimension(_ => _.cat).group().reduceSum(_ => _.quantity));
I mentioned this in my answer to your other question, but why not expand on it a little bit here.
In the dc.js FAQ there is a standard pattern for custom reductions to reduce more than one value at once.
Say that you have a field named type which determines which type of value is in the row, and the value is in a field named value (in your case these are cat and quantity). Then
var group = dimension.group().reduce(
function(p, v) { // add
p[v.type] = (p[v.type] || 0) + v.value;
return p;
},
function(p, v) { // remove
p[v.type] -= v.value;
return p;
},
function() { // initial
return {};
});
will reduce all the rows for each bin to an object where the keys are the types and the values are the sum of values with that type.
The way this works is that when crossfilter encounters a new key, it first uses the "initial" function to produce a new value. Here that value is an empty object.
Then for each row it encounters which falls into the bin labelled with that key, it calls the "add" function. p is the previous value of the bin, and v is the current row. Since we started with a blank object, we have to make sure we initialize each value; (p[v.type] || 0) will make sure that we start from 0 instead of undefined, because undefined + 1 is NaN and we hate NaNs.
We don't have to be as careful in the "remove" function, because the only way a row will be removed from a bin is if it was once added to it, so there must be a number in p[v.type].
Now that each bin contains an object with all the reduced values, the stack mixin has helpful extra parameters for .group() and .stack() which allow us to specify the name of the group/stack, and the accessor.
For example, if we want to pull items a and b from the objects for our stacks, we can use:
.group(group, 'a', kv => kv.value.a)
.stack(group, 'b', kv => kv.value.b)
It's not as convenient as it could be, but you can use these techniques to add stacks to a chart programmatically (see source).

Lodash _.filter by more than 1 key

i have array of objects like such :
var chart = [
{
"IDACCT": 28,
"LVLCOA": 2,
"IDPRNT": 1,
"NMACCI": "110-000 - ASSETS"
},
{
"IDACCT": 76,
"LVLCOA": 2,
"IDPRNT": 1,
"NMACCI": "170-000 - FIXED ASSETS"
}
];
i would like to return object where my search (predicate) is based using two keys :
d = _.filter(chart,function(o,e){
return o.LVLCOA === 2 && o.IDPRNT === e
});
this doesnt' work. i need to provide maybe some argument to the function. the problem e is not recognize in the function.
Of course it doesn't when you try to compare a number to an undeclared variable e. I guess you wanted to compare it to another number?
In your particular case, you can just setup a chain with two filter() calls and do away with predicate functions entirely. Passing filter() object shorthands should be sufficient:
var e = 1;
var d = _(chart)
.filter({ LVLCOA: 2 })
.filter({ IDPRNT: e })
.value();

Trouble with Linq in group by query

Maybe someone knows how to achieve this kind of query in linq (or lambda).
I have this set in a list
Filter: My input will be code 100 and 101, I need to get the "values", in this example = 1, 2.
Problem: If you input 100 and 101, you´ll get 3 results, because of 100 from group 1 and group 2. I just need the pair that matches in the same group. (And you don´t have group as an input param)
How can I solve this if the group fully exists?
thanks!
Starting with a simple representation in code of what you have in a picture:
var list = new[]
{
new{code = 100, value = 1, group = 1},
new{code = 101, value = 2, group = 1},
new{code = 100, value = 3, group = 2},
new{code = 103, value = 4, group = 2},
};
var inp = new[]{100, 103};
Then we can do:
list
.GroupBy(el => el.group) // Group by the "group" field.
.Where(grp => !inp.Except(grp.Select(el => el.code)).Any()) // Exclude groups that don't contain all input values
.Single() // Obtain the only such group (with a check that there is only one)
.Select(el => el.value); // Obtain the "value" fields.
If you could perhaps have inputs that were a subset of the "code" fields of some groups, you could also check that you match all of the group completely by excluding groups which have a different size:
list
.GroupBy(el => el.group)
.Where(grp =>
grp.Count‎() == inp.Count()
&& !inp.Except(grp.Select(el => el.code)).Any())
.Single()
.Select(el => el.value);
There are other variations that match other possible interpretations of your question (e.g. I'm assuming there can be only one matching group, but that wasn't clear).

auto sorting multiple columns, secondary column triggers resort too

I have a sorting question. I have two columns in my table that I need to sort by (primary=column 1, secondary=column 4). I am using the script below and it works for sorting by column 1, and it works for sorting column 4 if I also make a change in Column 1 but not by itself. I would like to be able to edit a value JUST column 4 value and have it do the secondary sort.
Ex: Column 1: Date, Column 2: LastName, Column 3: DOB, Column 4: Payment
I'd like to maintain the spreadsheet in date order (ascending, allowing repeat values), but secondarily organized by payment amount (ascending). That way I can see by date the order of highest to lowest payments. If I change JUST the payment data in a cell, I'd like it to resort by column 1 (date) and then 4 (payment). This script is not accomplishing that last bit. Any advice?
/**
* Automatically sorts the 1st column (not the header row) Ascending.
*/
function onEdit(event){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var editedCell = sheet.getActiveCell();
var columnToSortBy = 1;
var tableRange = "A2:T99"; // What to sort.
if(editedCell.getColumn() == columnToSortBy){
var range = sheet.getRange(tableRange);
range.sort( [1, 4] ); // or
range.sort( [{ column: 1, ascending: true }, 4] ); // or
range.sort( [{ column: 1, ascending: true }, { column: 4, ascending: true }] );
}
}
Probably I don't understand the problem? I suggest to change your if statement from
if(editedCell.getColumn() == columnToSortBy){
...
}
to
if((editedCell.getColumn() == columnToSortBy) || (editedCell.getColumn() == 4)){
...
}

Resources