Google chart Showing zero in case of missing date in bar graph - google-visualization

I have an object which contains "Date" and "Amount".The object will contain the data for last seven days.If any one date is missing in the object I want to show the bar graph as 0 for that date.
Can someone help me with this issue?

Found the answer .Incase if any one require you can have a look at below code
var orders = _orderService.GetAll(c => c.RestaurantId == restaurantId && (c.Date > DateTime.Now.AddDays(-7))).OrderBy(x => x.Date).GroupBy(item => item.Date.Date).OrderBy(g => g.Key).
Select(i => new Order { Date = i.Key.Date, GrossAmount = i.Sum(w => w.GrossAmount) }).ToList();
var from = DateTime.Now.AddDays(-7);
var to = DateTime.Now.AddDays(-1);
var days = Enumerable.Range(0, 1 + to.Subtract(from).Days)
.Select(offset => from.AddDays(offset))
.ToArray();
var data = days.Select(i =>new Order{ Date=i.Date,GrossAmount=orders.Where(p=>p.Date==i.Date).Sum(w=>w.GrossAmount)}).ToList();

Related

How to subtract the value of another row depends on Category column in PowerBI

Here is the photo explanation
I want to show the NewLinePrice in "Other Service", the value will subtract the value of "Graphic Design", so the first column will show ‭1152245‬ and so on.
So far i've tried to defined the new column "NewLinePrice", the following is the formula but not work
NewLinePrice =
Var category = 'Group-dtl'[ProductCategoryName]
var graphic_line_price = CALCULATE(SUM('Group-dtl'[LinePrice]),FILTER('Group-dtl', 'Group-dtl'[ProductCategoryName] = "Graphic Design"))
var graphic_line_price_temp = IF(category = "Graphic Design", 'Group-dtl'[LinePrice], 0)
//var graphic_line_price = 1
Var pre_result = IF(category = "Other Service", 'Group-dtl'[LinePrice] - graphic_line_price, BLANK())
Var result = IF(pre_result > 0, pre_result, BLANK())
return result
Anyone have ideas how to do that?
I spend some time to find the answer for your question, at the end I discover that to achiever your outcome, you cannot perform the calculation within the original but to create a new table, the reason is unknown, however at least it is achievable, see my answer below and accept if help, appreciate my hardworking :)
This is my original table name [Sheet1]
First I create a new table based on the original table
Table =
ADDCOLUMNS(VALUES(Sheet1[Product Name]),
"Sales",CALCULATE(SUM(Sheet1[Amount]),
FILTER(ALL(Sheet1),Sheet1[Product Name]=EARLIER(Sheet1[Product Name]))))
From the new table, I add new column using the following formula to return different value only for "Other Service"
New Line1 =
Var ServiceValue = CALCULATE(MAX(Sheet1[Amount]),Sheet1[Product Name] = "Other Service")
Var graphicValue = CALCULATE(MAX(Sheet1[Amount]),Sheet1[Product Name] = "Graphic Design")
Var charge = ServiceValue - graphicValue
return
if('Table'[Product Name] = "Other Service", charge,'Table'[Sales])
Here is new table with updated value:

Apex Interactive Grid how to retrieve a specific record

I am retrieving my grid data using:
var ig$ = apex.region("myGrid1").widget(),
view = ig$.interactiveGrid("getCurrentView");
Now I want to check for a specific record based on 2 columns: id1 and id2 where id1 = 1 and id2 = 7
How can I do that with javascript?
You can iterate for each record like this:
//"myGrid1" should be the static id of the IG region
var widget = apex.region('myGrid1').widget();
var grid = widget.interactiveGrid('getViews','grid');
var model = grid.model;
var results = [];
model.forEach(function(r) {
var record = r;
//the name of the columns should be ID1 and ID2, if not
//make the necessary changes using "_" to represent "space"
var value1 = model.getValue(record,'ID1');
var value2 = model.getValue(record,'ID2');
if(value1 == 1 && value2 == 7) {
results.push(record);
}
})
console.log(results);
To test this code, execute it on console.
To start the console on chrome just press F12
good luck.

How to do a MaxBy in RavenDb MapReduce

Using the Northwind database from RavenDB tutorial I'm trying to group orders by employee and get the most resent order for every employee.
Map:
from order in docs.Orders
select new {
Employee = order.Employee,
Count = 1,
MostRecent = order.OrderedAt,
MostRecentOrderId = order.Id
}
Reduce with nonexisting MaxBy:
from result in results
group result by result.Employee into grp
select new {
Employee = grp.Key,
Count = grp.Sum(result => result.Count),
MostRecent = grp.Max(result => result.MostRecent),
MostRecentOrderId = grp.MaxBy(result => result.MostRecent).MostRecentOrderId,
}
Reduce attempt:
from result in results
group result by result.Employee into grp
let TempMostRecent = grp.Max(result => result.MostRecent)
select new {
Employee = grp.Key,
Count = grp.Sum(result => result.Count),
MostRecent = TempMostRecent,
MostRecentOrderId = grp.First(result => result.MostRecent == TempMostRecent).MostRecentOrderId
}
However my reduce attempt returns 0 results.
Also: will RavenDB treat the Order.OrderetAt as a proper DateTime value and order them correctly?
You need to do it like
from order in docs.Orders
select new {
Employee = order.Employee,
Count = 1,
MostRecent = order.OrderedAt,
MostRecentOrderId = order.Id
}
from result in results
group result by result.Employee into grp
let maxOrder = grp.OrderByDescending(x=>x.MostRecent).First()
select new {
Employee = grp.Key,
Count = grp.Sum(result => result.Count),
MostRecent = maxOrder.MostRecent,
MostRecentOrderId = maxOrder.MostRecentOrderId,
}

DRYing up template code in Meteor?

I'm noticing significant code duplication while building my first Meteor project, and I'm wondering if there's a way to DRY it up.
My database model has stores, each of which have a number of products, and a field with the amount currently in inventory.
var store_id = Store.insert({name: 'Store 1', max_items: 50});
var p1 = Product.insert({name: 'General', store_id: store_id, item_count: 20});
var p2 = Product.insert({name: 'Special', store_id: store_id, item_count: 10});
I have a template to display a store, and statistics on how many products and items it has.
<template name="store">
<div class="store">
<b>{{name}}</b>
<p>Current items: {{current_items}}</p>
<p>Maximum # of items allowed in inventory: {{max_items}}</p>
<p>% Full: {{percent_full}}%</p>
</div>
</template>
Calculating the number of current items seems fairly straightforward, I pull all the products, sum the item counts (using d3), return the result.
Template.store.current_items = function () {
var store_id = this._id;
var items = Product.find({store_id: store_id}).fetch();
if ( items.length > 0 ) {
var item_cnt = d3.sum(_.pluck(items, 'item_count'));
return item_cnt;
}
else {
return 'N/A';
}
};
To calculate a percentage comparing the total # of allowed items, and the current items, it seems like I have to reduplicate everything. Is there a better way to do this?
Template.store.percent_full = function () {
var store_id = this._id;
var items = Product.find({store_id: store_id}).fetch();
if ( items.length > 0 ) {
var item_cnt = d3.sum(_.pluck(items, 'item_count'));
return item_cnt / max_items * 100;
}
else {
return 'N/A';
}
};
Extract the duplicated logic to a separate function and just call it appropriately from different helpers. This is not really different from DRYing any other JS code.

RavenDB Map/Reduce with grouping by date

I have to create a query to get a statistic by post per year/month, e.g. group by date. I created an index:
public class Posts_Count : AbstractIndexCreationTask<Post, ArchiveItem>
{
public Posts_Count()
{
Map = posts => from post in posts
select new
{
Year = post.PublishedOn.Year,
Month = post.PublishedOn.Month,
Count = 1
};
Reduce = results => from result in results
group result by new {
result.Year,
result.Month
}
into agg
select new
{
Year = agg.Key.Year,
Month = agg.Key.Month,
Count = agg.Sum(x => x.Count)
};
}
}
In studio I have next map and reduce functions:
Map:
docs.Posts.Select(post => new {Year = post.PublishedOn.Year, Month = post.PublishedOn.Month, Count = 1})
Reduce:
results
.GroupBy(result => new {Year = result.Year, Month = result.Month})
.Select(agg => new {Year = agg.Key.Year, Month = agg.Key.Month, Count = agg.Sum(x => ((System.Int32)(x.Count)))})
But the problem is I alway get a null values of Year and Month properties:
{
"Year": null,
"Month": null,
"Count": "1"
}
Can anybody help me to resolve the issue with my code? Thank You!
Your code looks fine. I tested it and it works in the current unstable build 1.2.2096. There have been some discussion around this lately on the RavenDB google group, so perhaps it was broken previously. Try again with the current build and see if it works for you now.