D3.js Traverse Through Nested Array to Find Max Value - d3.js

I have a nested array that looks like this:
Nested Array
I would like to traverse through the nested array and find the max value of the 'value' field situated in grouped_ratings > values > value
I have tried the following and can't seem to figure out the issue.
var yMax = d3.max(grouped_ratings, function(g) {
return d3.map(g.values, function(v) {
return d3.max(v.value)
})
});

Change d3.map to d3.max and in the inner-most function, you can just return v.value. Here's what it looks like:
d3.max(grouped_ratings, function (g) {
return d3.max(g.values, function (v) {
return v.value;
});
});
Or as a one-liner: d3.max(grouped_ratings, g => d3.max(g.values, v => v.value))
These snippets work when grouped_ratings has the format
grouped_ratings = [
{
key: '2011',
values: [
{ key: '1', value: 151 },
{ key: '2', value: 12 },
{ key: '3', value: 314 },
{ key: '4', value: 178 },
{ key: '5', value: 31 },
{ key: '6', value: 1 },
{ key: '7', value: 1 },
]
},
{
key: '2012',
values: [
{ key: '1', value: 203 },
{ key: '2', value: 4 },
{ key: '3', value: 41 },
{ key: '4', value: 87 },
{ key: '5', value: 13 },
{ key: '6', value: 10 },
{ key: '7', value: 100 },
]
},
]

Related

how to order array evenly by property

I have the following array:
arr = [
{ name: 'Apple', store: 'A' },
{ name: 'Banana', store: 'A' },
{ name: 'Carrot', store: 'B' },
{ name: 'Potato', store: 'B' },
{ name: 'Tomato', store: 'A' }
]
I need to sort the array by switching between each store like this:
arr = [
{ name: 'Apple', store: 'A' },
{ name: 'Carrot', store: 'B' },
{ name: 'Banana', store: 'A' },
{ name: 'Tomato', store: 'B' },
{ name: 'Potato', store: 'A' }
]
How can I do that with ruby ?
I came up with something :)
arr = [
{ name: 'Apple', store: 'A' },
{ name: 'Banana', store: 'A' },
{ name: 'Carrot', store: 'B' },
{ name: 'Potato', store: 'B' },
{ name: 'Tomato', store: 'A' }
]
first, *rest = arr.group_by { |h| h[:store] }.values
first.zip(*rest).flatten.compact
See it in action: Replit

Using nested datatable in laravel and build my own json

I am using Nested-datatables (https://github.com/AndrejGajdos/nested-datatables) so I have to use JSON.
I want to pass JSON from the controller to ajax and then use it in order to create a nested table.
The problem is that I need the exact JSON format so I will not get "undefined" result.
This is the example of the JSON format that I need (like in the link above) :
var dataInJson = [
{
data: {
name: 'b1',
street: 's1',
city: 'c1',
departments: 10,
offices: 15
},
kids: [
{
data: {
department: 'HR',
supervisor: 'Isidor Bristol',
floor: 1,
employees: 15
},
kids: [
{
data: {
name: 'Klement Nikodemos',
phone: '+938462',
hire_date: 'January 1, 2010',
id: 3456
},
kids: []
},
{
data: {
name: 'Madhava Helmuth',
phone: '+348902',
hire_date: 'May 23, 2002',
id: 1234
},
kids: []
},
{
data: {
name: 'Andria Jesse',
phone: '456123',
hire_date: 'October 23, 2011',
id: 9821
},
kids: []
}
]
},
{
data: {
department: 'development',
supervisor: 'Jim Linwood',
floor: 2,
employees: 18
},
kids: [
{
data: {
name: 'Origenes Maxwell',
phone: '345892',
hire_date: 'February 1, 2004',
id: 6234
},
kids: []
}
]
},
{
data: {
department: 'testing',
supervisor: 'Zekeriya Seok',
floor: 4,
employees: 11
},
kids: []
}
]
},
{
data: {
name: 'b2',
street: 's10',
city: 'c2',
departments: 3,
offices: 10
},
kids: [
{
data: {
department: 'development',
supervisor: 'Gallagher Howie',
floor: 8,
employees: 24
},
kids: [
{
data: {
name: 'Wat Dakota'
},
kids: []
}
]
},
{
data: {
department: 'testing',
supervisor: 'Shirley Gayle',
floor: 4,
employees: 11
},
kids: []
}
]
},
{
data: {
name: 'b3',
street: 's3',
city: 'c3',
departments: 2,
offices: 1
},
kids: [
{
data: {
department: 'development'
},
kids: [
{
data: {
name: 'Wat Dakota'
},
kids: []
}
]
},
{}
]
},
{
data: {
name: 'b4',
city: 'c4'
},
kids: []
}
];
In controller I have ajax function:
public function ajax(){
$parent = FirstLine::
select('yehida','data_type')
->where('first_year',2019)->where('second_year',2019)->where('first_period_no',1)->where('second_period_no',2)->where('periodical','רבעוני')
->addSelect(DB::raw('SUM(first_period) as first_period'))
->groupBy('yehida')
->groupBy('data_type')
->orderBy('yehida')
->orderBy('data_type')
->get();
$child = FirstLine::
select('yehida','hativa','data_type')
->where('first_year',2019)->where('second_year',2019)->where('first_period_no',1)->where('second_period_no',2)->where('periodical','רבעוני')
->addSelect(DB::raw('SUM(first_period) as first_period'))
->groupBy('yehida')
->groupBy('hativa')
->groupBy('data_type')
->orderBy('yehida')
->orderBy('hativa')
->orderBy('data_type')
->get();
$value = [];
foreach ($parent as $ch) {
$options = [];
foreach($child as $ch2) {
$options[] = ['data' => [
'yehida' => $ch2->yehida,
'hativa' => $ch2->hativa,
'data_type' => $ch2->data_type,
'first_period' => $ch2->first_period],
'kids' => []
];
if($ch->yehida == $ch2->yehida){
$value[] = [
'data' =>[
'yehida' => $ch->yehida,
'data_type' => $ch->data_type,
'first_period' => $ch->first_period
],
'kids' =>
$options,
];
}
}
}
$value = json_encode($value);
return response()->json($value);
}
My script with ajax call:
<script>
var temp;
$.ajax({
'async': false,
type: 'GET', //THIS NEEDS TO BE GET
url: '{!! route('get.ajax') !!}',
dataType: 'json',
success: function (data) {
temp = data;
},error:function(){
console.log(data);
}
});
var dataInJson = temp;
var settings = {
iDisplayLength: 20,
bLengthChange: false,
bFilter: false,
bSort: false,
bInfo: false
};
var tableT = new nestedTables.TableHierarchy(
'example',
dataInJson,
settings
);
tableT.initializeTableHierarchy();
var tableEle = document.querySelector('#example .table');
tableEle.addEventListener('onShowChildHierarchy', function(e) {
console.log(e);
});
// '#example' is wrapper ID for table hierarchy
var tableEle = document.querySelector('#example .table');
tableEle.addEventListener('onHideChildHierarchy', function(e) {
console.log(e);
});
</script>
How am I supposed to build my own JSON format so it will be like the JSON example? please help me

Lodash filter unique documents

How can I filter the current data:
[{
key: 'T1',
legs:[{ fno: 'W321',date: '2017-01-02 18:20:00.000+0200'}],
fare: { type: 'B', price: 25 }
},{
key: 'T1',
legs:[{ fno: 'W321', date: '2017-01-02T18:20:00.000+0200'}],
fare: { type: 'E', price: 23 }
},{
key: 'T1',
legs:[{ fno: 'W321', date: '2017-01-02T18:20:00.000+0200'}],
fare: { type: 'E', price: 20}
}]
I want to group by legs[0].fno, legs[0].date and fare.type, and keep the lowest priced items in each group. This is the expected result:
[{
key: 'T1',
legs:[{ fno: 'W321',date: '2017-01-02T18:20:00.000+0200'}],
fare: { type: 'B', price: 25}
},{
key: 'T1',
legs:[{ fno: 'W321',date: '2017-01-02T18:20:00.000+0200'}],
fare: { type: 'E', price: 20}
}]
Use _.groupBy() with a callback to create a string to group by, then _.map() each group to a single item using _.minBy():
var data = [{"key":"T1","legs":[{"fno":"W321","date":"2017-01-02 18:20:00.000+0200"}],"fare":{"type":"B","price":25}},{"key":"T1","legs":[{"fno":"W321","date":"2017-01-02T18:20:00.000+0200"}],"fare":{"type":"E","price":23}},{"key":"T1","legs":[{"fno":"W321","date":"2017-01-02T18:20:00.000+0200"}],"fare":{"type":"E","price":20}}];
var result = _(data)
// group by the combined group keys
.groupBy(function(o) {
// extract all group keys and join them to a string
return _.at(o, ['key', 'legs[0].date', 'fare.type']).join('');
})
.map(function(group) {
// get the object object with the minimum fare.price
return _.minBy(group, 'fare.price');
})
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

RethinkDb: Has many query

Have two table, Quiz and Questions
Quiz table store questions id and its display position.
Display position is different for every Quiz.
Quiz table:
{
id: '1'
name: 'Quiz 1'
questions: [
{
question_id: '1',
position: 4
},
{
question_id: '2',
position: 1
},
......
]
}
...
Question Table:
[
{
id: '1',
title: 'Question 1'
},
{
id: '2'
title: 'Question 2'
}
]
I want the the return result like this
{
id: '1',
name: 'Quiz 1',
questions: [
{
position: 4,
title: 'Question 1'
},
{
position: 1,
title: 'Question 2'
}
]
}
How can i get the desired result?
thanks
I got the desired result by using following query
r.db('database').table("quizes").get(id)
.do((quiz) => {
return quiz.merge({'questions': quiz('questions').map((q) => {
return q.merge((r.db('database').table('questions').get(q('id')).without('id')))
})
})
})

amCharts Map format currency

How would I make this amCharts Map format the min/max value of the legend into a US currency?
var map = AmCharts.makeChart( "chartdiv", {
type: "map",
"theme": "light",
colorSteps: 10,
dataProvider: {
map: "usaLow",
areas: [ {
id: "US-AL",
value: 4447100
}, {
id: "US-AK",
value: 626932
}, {
id: "US-AZ",
value: 5130632
}, {
id: "US-AR",
value: 2673400
}, {
id: "US-CA",
value: 33871648
}, {
id: "US-CO",
value: 4301261
}, {
id: "US-CT",
value: 3405565
}, {
id: "US-DE",
value: 783600
}, {
id: "US-FL",
value: 15982378
}, {
id: "US-GA",
value: 8186453
}, {
id: "US-HI",
value: 1211537
}, {
id: "US-ID",
value: 1293953
}, {
id: "US-IL",
value: 12419293
}, {
id: "US-IN",
value: 6080485
}, {
id: "US-IA",
value: 2926324
}, {
id: "US-KS",
value: 2688418
}, {
id: "US-KY",
value: 4041769
}, {
id: "US-LA",
value: 4468976
}, {
id: "US-ME",
value: 1274923
}, {
id: "US-MD",
value: 5296486
}, {
id: "US-MA",
value: 6349097
}, {
id: "US-MI",
value: 9938444
}, {
id: "US-MN",
value: 4919479
}, {
id: "US-MS",
value: 2844658
}, {
id: "US-MO",
value: 5595211
}, {
id: "US-MT",
value: 902195
}, {
id: "US-NE",
value: 1711263
}, {
id: "US-NV",
value: 1998257
}, {
id: "US-NH",
value: 1235786
}, {
id: "US-NJ",
value: 8414350
}, {
id: "US-NM",
value: 1819046
}, {
id: "US-NY",
value: 18976457
}, {
id: "US-NC",
value: 8049313
}, {
id: "US-ND",
value: 642200
}, {
id: "US-OH",
value: 11353140
}, {
id: "US-OK",
value: 3450654
}, {
id: "US-OR",
value: 3421399
}, {
id: "US-PA",
value: 12281054
}, {
id: "US-RI",
value: 1048319
}, {
id: "US-SC",
value: 4012012
}, {
id: "US-SD",
value: 754844
}, {
id: "US-TN",
value: 5689283
}, {
id: "US-TX",
value: 20851820
}, {
id: "US-UT",
value: 2233169
}, {
id: "US-VT",
value: 608827
}, {
id: "US-VA",
value: 7078515
}, {
id: "US-WA",
value: 5894121
}, {
id: "US-WV",
value: 1808344
}, {
id: "US-WI",
value: 5363675
}, {
id: "US-WY",
value: 493782
} ]
},
areasSettings: {
autoZoom: true
},
valueLegend: {
right: 10,
},
"export": {
"enabled": true
}
} );
#chartdiv {
width : 100%;
height : 500px;
}
<script src="http://www.amcharts.com/lib/3/ammap.js"></script>
<script src="http://www.amcharts.com/lib/3/maps/js/usaLow.js"></script>
<script src="http://www.amcharts.com/lib/3/themes/light.js"></script>
<div id="chartdiv"></div>
I'm currently hardcoding the min/max values to dollar amounts, but I'm unable figure out how to auto format it using amCharts.
You could use value legend's minValue and maxValue properties, to set any formatted text. I.e.:
valueLegend: {
right: 10,
minValue: "$0M",
maxValue: "$3.38M"
}
To do the same automatically, use "init" event to get the maximum value as well as amCharts' global function addPrefix to auto-format the values:
"listeners": [{
"event": "init",
"method": function(event) {
// find out the highest value
var map = event.chart;
var max = map.maxValueReal;
// define function that formats currency
function formatCurrency(val) {
return "$" + AmCharts.addPrefix(
val,
map.prefixesOfBigNumbers,
map.prefixesOfSmallNumbers, {
precision: map.precision,
decimalSeparator: map.decimalSeparator,
thousandsSeparator: map.thousandsSeparator
}
);
}
// update value legend
map.valueLegend.minValue = formatCurrency(0);
map.valueLegend.maxValue = formatCurrency(max);
// update the map
map.validateNow();
}
}]
Here's a working demo.

Resources