Aggregate array of objects into a single array in elastic search - elasticsearch

I'm having few documents which is of the form
{
...id information and so on,
coordinates: [{x, y}]
...few other indexes
}
I want to generate an elastic search aggregation which will combine whole coordinates array into a single array. For example
The documents are
[
{
...
coordinates: [
{x: 20, y: 30},
{x: 50, y: 10},
{x: 40, y: 20}
],
...
},
{
...
coordinates: [
{x: 10, y: 5},
{x: 20, y: 15},
{x: 60, y: 90},
{x: 70, y: 50}
],
...
},
{
...
coordinates: [
{x: 90, y: 20},
{x: 80, y: 70}
],
...
},
]
In the output I need to generate something like this using the aggregation
[
{x: 20, y: 30},
{x: 50, y: 10},
{x: 40, y: 20},
{x: 10, y: 5},
{x: 20, y: 15},
{x: 60, y: 90},
{x: 70, y: 50},
{x: 90, y: 20},
{x: 80, y: 70}
]

Related

How to update keys of map in dart

I have a simple map "objects" lets assume this :
{
ServoP180V1: {X: 100.0, Y: 0.0},
ServoP180V3: {X: 100.0, Y: 0.0}
ServoP180V5: {X: 100.0, Y: 0.0}
}
How I can sort the keys in such way that they will be in an order, like this:
{
ServoP180V1: {X: 100.0, Y: 0.0},
ServoP180V2: {X: 100.0, Y: 0.0}
ServoP180V3: {X: 100.0, Y: 0.0}
}
I tried this code but it has problems with returning null sometimes, not sure Im in right way
sortObjects() {
int i = 1;
for (var key in objects.keys) {
objects.update(
key.substring(0, key.length - 1) + i.toString(),
null,
);
i++;
}
}
The method 'call' was called on null.
Receiver: null
Tried calling: call()
also this way
sortObjects() {
int i = 1;
objects.forEach((key, value) {
objects.update(
key.substring(0, key.length - 1) + i.toString(),
(existingValue) => value,
ifAbsent: () => value,
);
i++;
});
}
giving such an error
Exception: Concurrent modification during iteration:
Thank you in advance !
So you are changing the key during the foearch loop, this is illegal. I would change the keys by generating another map and then replacing the old one. It is an answer.
Map.update()
Update method of the map only updates the content of the key and only if it exists, but does not change the key itself. I didn't find anything related to changing keys for maps at runtime.
Map<String, dynamic> oldMap = {
"ServoP180V1": {"X": 100.0, "Y": 0.0},
"ServoP180V3": {"X": 100.0, "Y": 0.0},
"ServoP180V5": {"X": 100.0, "Y": 0.0}
};
Map<String, dynamic> newMap = {};
int i = 1;
oldMap.keys.toList().forEach((key) {
newMap.addAll({
"${key.substring(0, key.length - 1)}$i":
oldMap[key]
});
i++;
});
print(oldMap);
print(newMap);
result:
{ServoP180V1: {X: 100.0, Y: 0.0}, ServoP180V3: {X: 100.0, Y: 0.0}, ServoP180V5: {X: 100.0, Y: 0.0}}
{ServoP180V1: {X: 100.0, Y: 0.0}, ServoP180V2: {X: 100.0, Y: 0.0}, ServoP180V3: {X: 100.0, Y: 0.0}}
void main(List<String> args) {
final objects = {
'ServoP180V1': {'X': 100.0, 'Y': 0.0},
'ServoP180V3': {'X': 100.0, 'Y': 0.0},
'ServoP180V5': {'X': 100.0, 'Y': 0.0}
};
var keys = objects.keys.toList()..sort();
var newData = <String, Map<String, double>>{};
keys.forEach((k) => newData[k] = objects[k]);
print(newData);
}
Result:
{ServoP180V1: {X: 100.0, Y: 0.0}, ServoP180V3: {X: 100.0, Y: 0.0}, ServoP180V5: {X: 100.0, Y: 0.0}}
Map<String, dynamic> oldMap = {
"ServoP180V1": {"X": 100.0, "Y": 0.0},
"ServoP180V3": {"X": 100.0, "Y": 0.0},
"ServoP180V5": {"X": 100.0, "Y": 0.0}
};
int j = 1;
Map<String, dynamic> newMap2 = oldMap
.map((key, value) => MapEntry('ServoP180V${j++}',value);
);
print(newMap2); // {ServoP180V1: {X: 100, Y: 0}, ServoP180V2: {X: 100, Y: 0}, ServoP180V3: {X: 100, Y: 0}}

nvd3 line chart xaxis with month names not working

I'm trying a line chart for several years with month names in the xaxis. xaxis seems to be an integer so I used the function from this answer: https://stackoverflow.com/a/30325918
The problem is that the chart is cut off by number 9 (which stands for "Okt") and I don't know why. So the 10 and 11 are missing. Also the tooltips are not shown, maybe that's a subsequent fault.
Style for the svg:
#chart svg {
height: 600px;
width: 1456px;
}
Code for the Chart:
nv.addGraph(function() {
var chart = nv.models.lineChart()
.margin({left: 50}) //Adjust chart margins to give the x-axis some breathing room.
.useInteractiveGuideline(true) //We want nice looking tooltips and a guideline!
.showLegend(true) //Show the legend, allowing users to turn on/off line series.
.showYAxis(true) //Show the y-axis
.showXAxis(true) //Show the x-axis
;
var Monate = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez']
chart.xAxis //Chart x-axis settings
.axisLabel('Monat')
.tickValues([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).tickFormat(function(d){
return Monate[d]
});
chart.forceY([0, 55000]);
d3.select('#chart svg').datum([{ values: [{ x:'0', y: '0'},{ x:'1', y: '0'},{ x:'2', y: '7147.75'},{ x:'3', y: '5567.75'},{ x:'4', y: '8307.75'},{ x:'5', y: '7680.75'},{ x:'6', y: '10416.75'},{ x:'7', y: '15119.25'},{ x:'8', y: '6654.75'},{ x:'9', y: '7670.75'},{ x:'10', y: '8535.75'},{ x:'11', y: '25978.5'}], key: '2012'},
{ values: [{ x:'0', y: '4397.5'},{ x:'1', y: '14542.5'},{ x:'2', y: '18652.5'},{ x:'3', y: '18027.5'},{ x:'4', y: '10123.5'},{ x:'5', y: '11686.25'},{ x:'6', y: '10451.5'},{ x:'7', y: '17918'},{ x:'8', y: '11013.25'},{ x:'9', y: '11598.25'},{ x:'10', y: '12179'},{ x:'11', y: '54450.75'}], key: '2013'},
{ values: [{ x:'0', y: '13257.75'},{ x:'1', y: '10050.25'},{ x:'2', y: '17530'},{ x:'3', y: '16684'},{ x:'4', y: '10437.25'},{ x:'5', y: '31956.5'},{ x:'6', y: '24343.75'},{ x:'7', y: '24026.5'},{ x:'8', y: '24754.5'},{ x:'9', y: '17423'},{ x:'10', y: '27284.75'},{ x:'11', y: '50121.25'}], key: '2014'},
...and so on...
{ values: [{ x:'0', y: '954'},{ x:'1', y: '0'},{ x:'2', y: '0'},{ x:'3', y: '0'},{ x:'4', y: '0'},{ x:'5', y: '0'},{ x:'6', y: '0'},{ x:'7', y: '0'},{ x:'8', y: '0'},{ x:'9', y: '0'},{ x:'10', y: '0'},{ x:'11', y: '0'}], key: '2019'}]).transition().duration(500).call(chart);
//Update the chart when window resizes.
nv.utils.windowResize(function() { chart.update() });
return chart;
});
All your x and y are strings.
Remove all the ' characters.
I have set the size style on the div#chart because nvd3.css has a style for the svg with width and height set to 100%.

How to allow Duplicate Tick Labels in Plotly?

I observed that while providing duplicate ticklabels (with an aim to mention the class of categorical variable), plotly creates a unique set and displays only the non-repeated tick labels. Is there a way to by-pass this feature and allow duplicate tick-labels?
var data = [
{
z: [[1, 20, 30, 50, 1], [20, 1, 60, 80, 30], [30, 60, 1, -10, 20]],
x: ['Healthy', 'Healthy', 'Moderate', 'Diseased', 'Diseased'],
y: ['Morning', 'Afternoon', 'Evening'],
type: 'heatmap'
}
];
Plotly.newPlot('myDiv', data);
Following is the jsfiddle depicting the issue:
https://jsfiddle.net/mam8sgwx/
Set the layout with ticktext:
var layout = {
xaxis: {
tickmode: "array",
ticktext: ['Healthy', 'Healthy', 'Moderate', 'Diseased', 'Diseased'],
tickvals: [0, 1, 2, 3, 4]
}
}
Here is the demo:
var data = [{
z: [
[1, 20, 30, 50, 1],
[20, 1, 60, 80, 30],
[30, 60, 1, -10, 20]
],
y: ['Morning', 'Afternoon', 'Evening'],
type: 'heatmap'
}];
var layout = {
xaxis: {
tickmode: "array",
ticktext: ['Healthy', 'Healthy', 'Moderate', 'Diseased', 'Diseased'],
tickvals: [0, 1, 2, 3, 4]
}
}
Plotly.newPlot('myDiv', data, layout);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<body>
<div id="myDiv"></div>

How should I modify my dataset and D3.JS code to add a group name?

Here is my dataset
var dataset = [{ x: 0, y: 100 }, { x: 1, y: 833 }, { x: 2, y: 1312 },
{ x: 3, y: 1222 }, { x: 4, y: 1611 },
{ x: 0, y: 200 }, { x: 1, y: 933 }, { x: 2, y: 1412 },
{ x: 3, y: 1322 }, { x: 4, y: 1711 },]
I'm not sure where to specify the name of each dataset, How should this be modified to allow for a Group Name to be passed and what changes should be made in my D3.JS code ?
Fiddle https://jsfiddle.net/qvrL3ey5/
Using http://jsfiddle.net/2N2rt/15/ I was able to resolve this issue.
Example
var data = [
{name: 'John', values: [0,1,3,9, 8, 7]},
{name: 'Harry', values: [0, -100, 7, 1, 1, 111]},
{name: 'Steve', values: [3, 1, 4, 4, 4, 107]},
{name: 'Adam', values: [4, 77, 2, 13, 11, 130]}
];

nvd3 multiBarChart not showing x axis

Hi i tried to create a nvd3 multibarchart but i can't get x-axis display.
I searched online but cannot find a solution to my issue.
here is complete code please help. i also attached a pix for output. Thanks!
<script>
function data0() {
var a0 = [],
a1 = [];
a0.push({ x: 1, y: 5065});
a0.push({ x: 2, y: 11464});
a0.push({ x: 3, y: 200});
a0.push({ x: 4, y: 100});
a0.push({ x: 5, y: 0});
a0.push({ x: 6, y: 0});
a0.push({ x: 7, y: 100});
a0.push({ x: 8, y: 2200});
a0.push({ x: 9, y: 2200});
a0.push({ x: 10, y: 0});
a0.push({ x: 11, y: 0});
a0.push({ x: 12, y: 0});
a0.push({ x: 13, y: 0});
a0.push({ x: 14, y: 0});
a1.push({ x: 1, y: 148});
a1.push({ x: 2, y: 2966});
a1.push({ x: 3, y: 1200});
a1.push({ x: 4, y: 79});
a1.push({ x: 5, y: 0});
a1.push({ x: 6, y: 0});
a1.push({ x: 7, y: 0});
a1.push({ x: 8, y: 2185});
a1.push({ x: 9, y: 2185});
a1.push({ x: 10, y: 0});
a1.push({ x: 11, y: 0});
a1.push({ x: 12, y: 0});
a1.push({ x: 13, y: 0});
a1.push({ x: 14, y: 0});
return [
{
values: a1,
key: 'Queue',
color: '#04B45F'
},
{
values: a0,
key: 'In Progress',
color: '#DBA901',
area: true
}
];
}
nv.addGraph(function () {
var chart = nv.models.multiBarChart()
.margin({ left: 65, right: 40, bottom: 45 })
.stacked(true)
.showControls(false)
;
chart.xAxis
.axisLabel("Daily")
.rotateLabels(0)
.tickValues([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
.tickSize(12)
.showMaxMin(false)
.tickFormat(function (d) {
var _tools = ["", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"]
return _tools[d]
});
chart.yAxis
.axisLabel("Number of[enter image description here][1]")
.tickFormat(d3.format(',.0f'));
d3.select('#BarChart-Cell svg')
.datum(data0)
.transition()
.duration(500)
.call(chart)
;
nv.utils.windowResize(chart.update);
return chart;
});
</script>
[ http://i.stack.imgur.com/HNNJf.png ]

Resources