Suppose there is an document like:
{
"_id": "1cf2080c-ce9a-367b-93ba-dbf7b2bf8a2c",
"template_id" : "1cf2080c-ce9a-367b-93ba-dbf7b2bf8a2c",
"some_value" : 12
}
mapping to a class as Template, and given a templateid array array_id = ["aa", "bb", "cc"]
question: how can I get a Template List using mongodb C# driver2.1 up, which each item in list have largest some_value ?
for example there is a collection:
{"_id": "1", "template_id": "aa", "some_value":1}
{"_id": "2", "template_id": "aa", "some_value":2}
{"_id": "3", "template_id": "aa", "some_value":3}
{"_id": "4", "template_id": "bb", "some_value":4}
{"_id": "5", "template_id": "bb", "some_value":5}
and a template_id_array = ["aa", "bb"]
expect result is:
{"_id": "3", "template_id": "aa", "some_value":3}
{"_id": "5", "template_id": "bb", "some_value":5}
I think it would be some "aggregate" or "map-reduce" like procedure, first match result and then group by template id and then find max somevalue for each group.
I have not tested this but should work with some probable modifications
var result = new List<Template>();
foreach (var element in template_id_array)
{
var filter = builder.Eq("TemplateId", element);
var doc = await collection.Find(filter)
.SortByDescending(x => x.SomeValue)
.Limit(1)
.FirstOrDefaultAsync();
result.Add(doc);
}
Then you can serialize it to the final form:
var expectedResult = JsonConvert.SerializeObject(result);
This sample uses jqGrid 4.6:
http://jsfiddle.net/aUDHx/1218/
As one can see, regardless of the number of aggregates, the header names are displayed correctly ("A A", "A B", etc.)
However, when I switch to version 4.7, the pivoted columns aren't named correctly when more than one aggregate is used:
http://jsfiddle.net/aUDHx/1219/
If only one aggregate is used, the headers display correctly.
Does 4.7 have a different method of specifying the header names, or is this a bug? If the latter, does an appropriate workaround exist?
This is the code for the yDimension:
yDimension: [{
dataName: 'product',
converter: function (val) {return val.replace(/\s/g, ' ');}
}],
The converter function is used to correctly format the header name. This is not required in 4.7 if you only use one aggregate, but anything more than that causes it to break.
"Gurrido" is now the new name of jqGrid.
The problem is in spaces which you use in names. jqPivot don't support currently spaces in the names. You can fix the problem by replacing the spaces to _ (underscore) for example. I described the workaround here.
By the way Gurrido jqGrid is not the only successor of free open source jqGrid with MIT licence. After starting Gurrido jqGrid some other jqGrid forks of the last free jqGrid is developing. I post my results here. I plan to publish new version probably in the current month. Another fork you can find here. One apply in the fork many changes which I make in my repository, but one make some his own changes too.
UPDATED: The problem with the labels which you described is a bug in jqGrid 4.7. By the way you don't need to use the converter in case of usage spaces in the aggregation values.
I posted the bug fix here in my jqGrid repository. You can see the results on the demo http://jsfiddle.net/OlegKi/b47ocLd7/
The demo uses the following JavaScript code
var mydata = [
{ id: "1", product: "A A", sold: "8", sold2: "8", sold3: "8", emp: "Michelle" },
{ id: "2", product: "A A", sold: "3", sold2: "8", sold3: "8", emp: "Tania" },
{ id: "6", product: "A B", sold: "1", sold2: "8", sold3: "8", emp: "Mark" },
{ id: "3", product: "A B", sold: "5", sold2: "8", sold3: "8", emp: "Tommy" },
{ id: "4", product: "B B", sold: "2", sold2: "8", sold3: "8", emp: "Dave" },
{ id: "5", product: "B B", sold: "5", sold2: "8", sold3: "8", emp: "Carol" }
];
$("#grid").jqGrid("jqPivot", mydata, {
xDimension: [
{ isGroupField: false, width: 40, dataName: "id", label: "ID" },
{ isGroupField: false, width: 80, dataName: "emp", label: "Employee" }
],
yDimension: [
{ dataName: "product" }
],
aggregates: [
{ aggregator: "sum", width: 60, member: "sold", label: "Sold" },
{ aggregator: "sum", width: 60, member: "sold2", label: "Sold 2" }
],
colTotals: true
},
{
height: "auto",
pager: "#pager",
iconSet: "fontAwesome",
resizeStop: function () {
$(this).jqGrid("setGridWidth", this.grid.newWidth, false);
},
caption: "Daily Sales"
}
);
Hi I convert a PDF to a txt file in Ruby 1.9.3
Here is part of the txt file:
[["Rate", "Card", "February", "29,", "2012"]]
[["Termination", "Color", "Test", "No", "Rate", "Currency", "Notes"]]
[["x", "A", "CAMEL", "56731973573", "$", "0.1400", "USD", "30/45/100%"]]
["y", "A", "CARDINAL", "56731972501", "$", "0.1400", "USD", "30/45/100%"]]
[["z", "A", "CARNELIAN", "56731971654", "$", "0.1400", "USD", "30/45/100%"]]
.....
....
[["Rate", "Card", "February", "29,", "2012"]]
[["Termination", "Color", "Test", "No", "Rate", "Currency", "Notes"]]
I store every line in a different array, but the problem is that I don't want to read the two first lines which appears lots of times in my txt file, because those lines are the header in every page on the pdf. Any idea about how to do that? Thanks!
You can read file into array and reject lines you do not need:
rejected = [
'[["Rate", "Card", "February", "29,", "2012"]]',
'[["Termination", "Color", "Test", "No", "Rate", "Currency", "Notes"]]',
]
lines = File.readlines('/path/to/file').reject { |line| rejected.include? line }
I have seen a few questions on here (one was even by me) with respect to using R to plot an image.
The difference here is that I need to set the reference cooridnates for my image to match the data I am looking to plot on top of the image.
More specifically, I need R to understand that the coordinates for the background image are x = (-100,100) and y = (40,-40).
I have been able to to read in the image file and plot it using the ReadImages package, but when I overlay my data using points(), the data obviously do not line up appropriately.
Any help is much appreciated.
EDIT: here are some example data and I attached the image:
structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2",
"6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20",
"10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79",
"81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53",
"54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59",
"50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18",
"-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25",
"-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16",
"8", "8")), .Names = c("teamid", "xcoord", "ycoord"), class = "data.frame", row.names = c(74328L,
74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L,
74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L,
100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L,
100163L, 100167L))
You can create an empty plot with the correct dimensions, then use the rasterImage function to plot the image, then adding the points should work fine.
Another approach is to use the updateusr function from the TeachingDemos package after plotting the image to make sure that the coordinates match what you want them to before adding lines or points.
I saved your graphic above as ice.png and ran the following code:
library(EBImage)
ice <- readImage('My Pictures/ice.png')
pos <- structure(list(teamid = c("6", "6", "6", "6", "6", "6", "2",
"6", "6", "6", "2", "6", "10", "10", "10", "10", "20", "20",
"10", "10", "10", "20", "20", "20", "10", "10"), xcoord = c("79",
"81", "33", "34", "75", "52", "-67", "80", "44", "79", "-53",
"54", "-55", "-81", "-66", "-66", "45", "81", "-78", "-70", "-59",
"50", "53", "63", "-79", "-78"), ycoord = c("0", "0", "-18",
"-20", "6", "-11", "-7", "7", "-28", "-10", "35", "22", "25",
"-5", "25", "23", "-11", "13", "22", "16", "13", "23", "7", "16",
"8", "8")), .Names = c("teamid", "xcoord", "ycoord"),
class = "data.frame", row.names = c(74328L,
74331L, 74332L, 74334L, 74336L, 74338L, 74340L, 74341L, 74346L,
74347L, 74348L, 74349L, 100136L, 100137L, 100138L, 100139L, 100147L,
100148L, 100151L, 100154L, 100156L, 100158L, 100159L, 100161L,
100163L, 100167L))
pos$xcoord <- as.numeric(pos$xcoord)
pos$ycoord <- as.numeric(pos$ycoord)
ice2 <- as.raster(ice)
pin <- par('pin')
plot( c(-100,100), c(-40,40), type='n', xlab='', ylab='',
asp=pin[1]/pin[2], axes=FALSE, xaxs='i', yaxs='i')
rasterImage(ice2, -100, -40, 100, 40, interpolate=FALSE)
with(pos, text(xcoord, ycoord, teamid, col='green', cex=1.2) )
Does this do what you want?