I'm interested to visualize Meteor data (mongoDb) with dcJs. However, I can't find much information about this topic.
I recreated http://www.codeproject.com/Articles/697043/Making-Dashboards-with-Dc-js-Part-2-Graphing using Meteor.
Without calling mongodb,
var data = [
{_id: "iD1", date: "12/27/2012", http_404: 2, http_200: 190, http_302: 100},
{_id: "iD2", date: "12/28/2012", http_404: 2, http_200: 10, http_302: 100},
{_id: "iD3", date: "12/29/2012", http_404: 1, http_200: 300, http_302: 200},
{_id: "iD4", date: "12/30/2012", http_404: 2, http_200: 90, http_302: 0},
{_id: "iD5", date: "12/31/2012", http_404: 2, http_200: 90, http_302: 0},
{_id: "iD6", date: "01/01/2013", http_404: 2, http_200: 90, http_302: 0},
{_id: "iD7", date: "01/02/2013", http_404: 1, http_200: 10, http_302: 1},
{_id: "iD8", date: "01/03/2013", http_404: 2, http_200: 90, http_302: 0},
{_id: "iD9", date: "01/04/2013", http_404: 2, http_200: 90, http_302: 0},
{_id: "iD10", date: "01/05/2013", http_404: 2, http_200: 90, http_302: 0},
{_id: "iD11", date: "01/06/2013", http_404: 2, http_200: 200, http_302: 1},
{_id: "iD12", date: "01/07/2013", http_404: 1, http_200: 200, http_302: 100}
];
The result is working fine.
By inserting and calling data from mongodb, failed.
Not sure what's missing here.
Hope to get advice. Thanks!
I suspect the error is fired from the following code:
var minDate = dateDim.bottom(1)[0].date;
var maxDate = dateDim.top(1)[0].date;
It's probable that your template rendered callback is being fired before the data has been loaded from the database.
Probably the easiest way to fix it would be to put an if statement:
if (Dichas.find().count() !== 0) {
... all of the code that depends on there being data
}
Related
In my Flutter app, I have a database which keeps track of which items the user liked and which he disliked. I have the function
List finalFavoritesList;
void queryDb() async {
final db = await database;
final allRows = await db.query(TABLE_FAVORITE);
List finalFavoritesList = allRows.toList(growable: true);
print(finalFavoritesList);
}
which in my understanding creates a dart list from the sqflite database. Logcat prints:
[{id: 0, isFavorite: 0}, {id: 1, isFavorite: 1}, {id: 2, isFavorite: 0}, {id: 3, isFavorite: 1}, {id: 4, isFavorite: 0}, {id: 5, isFavorite: 1}]
Now I want to remove every entry, where isFavorite is equal to 0 but I don't know how. This new list should have another name.
I think your question itself has an answer!
Use removeWhere function.
List favorite = [{'id': 0, 'isFavorite': 0}, {'id': 1, 'isFavorite': 1}, {'id': 2, 'isFavorite': 0}, {'id': 3, 'isFavorite': 1}, {'id': 4, 'isFavorite': 0}, {'id': 5, 'isFavorite': 1}];
favorite.removeWhere((item) => item['isFavorite'] == 0);
print(favorite);
Output:
[{id: 1, isFavorite: 1}, {id: 3, isFavorite: 1}, {id: 5, isFavorite: 1}]
Refer: https://api.dart.dev/stable/2.9.3/dart-core/List/removeWhere.html
Hope that solves your case!
I have the below dictionary:
{'Closed': {'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6}, 'Impact Statement Pending': {'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0}, 'New': {'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2}, 'Remediation Plan Pending': {'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0}, 'Remedy in Progress': {'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1}}
How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?
The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:
trace_list = ['High', 'Medium', 'Critical', 'Low']
total_status_dict = {'Closed': {'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6}, 'Impact Statement Pending': {'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0}, 'New': {'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2}, 'Remediation Plan Pending': {'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0}, 'Remedy in Progress': {'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1}}
for item in trace_labels:
y_values = []
for key, val in total_status_dict.items():
for ke in total_status_dict[key]:
if item is ke:
y_values.append(total_status_dict[key][ke])
Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.
Your code is not efficient because you iterate over trace_labels twice:
for item in trace_labels:
for ke intotal_status_dict[key]: if item is ke:`
How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:
>>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
>>> total_status_dict = {'Closed': {'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6}, 'Impact Statement Pending': {'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0}, 'New': {'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2}, 'Remediation Plan Pending': {'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0}, 'Remedy in Progress': {'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1}}
>>> y_values_by_label = {}
>>> for key, value_by_label in total_status_dict.items():
... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
... y_values_by_label.setdefault(label, {})[key] = value
...
>>> y_values_by_label
{'High': {'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4}, 'Medium': {'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36}, 'Low': {'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18}, 'Critical': {'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1}}
setdefault(label, {}) creates a empty dict y_values_by_label[label] = {} if y_values_by_label does not have the key label.
If you want to turn this in a dict comprehension, you have to use your inefficient method:
>>> {label:{k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label} for label in trace_labels}
{'High': {'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4}, 'Medium': {'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36}, 'Critical': {'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1}, 'Low': {'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18}}
Using Laravel 5.4
I am reaching out as I am having a difficult time wrapping my head around this and after searching all day on the internet (and stackoverflow) I have not found a good solution to my problem that works.
Basically, I have a user object, that queries a child object which in turn includes a child object and I need to filter with where on that grandchild object.
It looks like this:
User =>
Pet(1) =>
PetServiceItem <= ServiceItem(1)
PetServiceItem <= ServiceItem(2)
PetServiceItem <= ServiceItem(3)
Pet(2) =>
PetServiceItem <= ServiceItem(1)
PetServiceItem <= ServiceItem(4)
PetServiceItem <= ServiceItem(5)
I'll post the relevant parts of the information so someone can tell me how this might be done.
User Model
class User extends Authenticatable
{
public function pets()
{
return $this->hasMany(Pet::class);
}
}
Pet Model
class Pet extends Model
{
protected $fillable = [
'id',
'user_id',
...];
public function user()
{
return $this->belongsTo(User::class);
}
public function petServiceItems(){
return $this->hasMany(PetServiceItem::class);
}
}
PetServiceItem model
class PetServiceItem extends Model
{
protected $fillable = [
'pet_id',
'service_item_id',
'approved'
];
protected $table = 'pet_service_item';
public function pet()
{
return $this->belongsTo(Pet::class);
}
public function serviceItem()
{
return $this->belongsTo(ServiceItem::class);
}
}
ServiceItem model
class ServiceItem extends Model
{
protected $fillable = [
'id',
...,
'start_date',
'end_date',
'...',
];
public function pets(){
return $this->hasMany(PetServiceItem::class);
}
}
Using Tinker I can do the following:
$user->pets()->with(['petServiceItems', 'petServiceItems.service'])->get()
And get this data:
=> Illuminate\Database\Eloquent\Collection {#1118
all: [
App\Pet {#1120
id: 1,
user_id: 6,
name: "Coco",
slug: "carol!coco",
image: "/dist/images/pets/carol/coco.jpg",
breed: null,
color: null,
gender: "Female",
birthdate: "2013-07-06 03:58:46",
fixed: 0,
weight: "48",
licensed: "",
tattoo: "",
microchip: "",
created_at: "2017-07-17 17:37:54",
updated_at: "2017-07-17 17:37:54",
petServiceItems: Illuminate\Database\Eloquent\Collection {#1126
all: [
App\PetServiceItem {#1132
id: 1,
provider_id: 2,
pet_id: 1,
service_item_id: 1,
approved: 1,
created_at: "2017-07-17 17:37:57",
updated_at: "2017-07-17 17:37:57",
serviceItem: App\ServiceItem {#1137
id: 1,
provider_id: 2,
type: "WALK",
subtype: "",
title: "7am 30min Walk between 7am and 10am",
desc: "Daily weekday walks between 7am and 10am",
day1: 0,
day2: 1,
day3: 1,
day4: 1,
day5: 1,
day6: 1,
day7: 0,
needs_approval: 0,
start_date: "2017-07-17 00:00:00",
end_date: "2017-10-17 00:00:00",
all_day: 0,
start_time: "07:00:00",
end_time: "10:00:00",
duration: 30,
pricing_one: 2000,
pricing_twoplus: 1800,
created_at: "2017-07-17 17:37:57",
updated_at: "2017-07-17 17:37:57",
deleted_at: null,
},
},
App\PetServiceItem {#1134
id: 3,
provider_id: 2,
pet_id: 1,
service_item_id: 4,
approved: 0,
created_at: "2017-07-17 17:37:57",
updated_at: "2017-07-17 17:37:57",
serviceItem: App\ServiceItem {#1139
id: 4,
provider_id: 2,
type: "AGILITY",
subtype: "",
title: "10am Agility Tu/Th",
desc: "Agility class # 10am Tuesdays and Thursdays for 90 minutes",
day1: 0,
day2: 0,
day3: 1,
day4: 0,
day5: 1,
day6: 0,
day7: 0,
needs_approval: 1,
start_date: "2017-07-17 00:00:00",
end_date: "2017-09-17 00:00:00",
all_day: 0,
start_time: "10:00:00",
end_time: "11:30:00",
duration: 90,
pricing_one: 5000,
pricing_twoplus: 4500,
created_at: "2017-07-17 17:37:57",
updated_at: "2017-07-17 17:37:57",
deleted_at: null,
},
},
],
},
},
App\Pet {#1123
id: 2,
user_id: 6,
name: "Ruby",
slug: "carol!ruby",
image: "/dist/images/pets/carol/ruby.jpg",
breed: null,
color: null,
gender: "Female",
birthdate: "2012-06-16 22:47:43",
fixed: 1,
weight: "53",
licensed: "",
tattoo: "",
microchip: "",
created_at: "2017-07-17 17:37:54",
updated_at: "2017-07-17 17:37:54",
petServiceItems: Illuminate\Database\Eloquent\Collection {#1119
all: [
App\PetServiceItem {#1133
id: 2,
provider_id: 2,
pet_id: 2,
service_item_id: 1,
approved: 1,
created_at: "2017-07-17 17:37:57",
updated_at: "2017-07-17 17:37:57",
serviceItem: App\ServiceItem {#1137},
},
App\PetServiceItem {#1135
id: 4,
provider_id: 2,
pet_id: 2,
service_item_id: 4,
approved: 0,
created_at: "2017-07-17 17:37:57",
updated_at: "2017-07-17 17:37:57",
serviceItem: App\ServiceItem {#1139},
},
],
},
},
],
}
Now I need to do a where clause on the ServiceItem for the start_date.
I tried:
$user->pets()->with(['petServiceItems', 'petServiceItems.serviceItem'])->where('service_items.start_date', '>=', '2017-01-01')->get()
But, I get this error:
Illuminate\Database\QueryException with message 'SQLSTATE[42S22]:
Column not found: 1054 Unknown column 'service_items.start_date' in
'where clause' (SQL: select * from pets where pets.user_id = 6
and pets.user_id is not null and service_items.start_date >=
2017-01-01)'
How can I use the where clause (or something else if needed) to filter the data I need?
edited:
I have figured out that this is the SQL that I want (or close enough approximation):
select * from users
join (select * from pets) pet on users.id = pet.user_id
join (select * from pet_service_item) psi on psi.pet_id = pet.id
join (select * from service_items) si on si.id = psi.service_item_id
join (select * from providers) prov on prov.id = si.provider_id
where si.start_date >= '2017-07-17'
AND si.end_date <= '2017-10-18'
AND prov.id = 2
AND users.id = 6
Where clause use the name of the table and not the model. Try this:
$user->pets()->with(['petServiceItems', 'petServiceItems.serviceItem'])
->where('service_items.start_date', '>=', '2017-01-01')->get();
So, I'm posting this as I never really did get an answer and I ended up with a solution, but not exactly the one I was looking for.
If anyone is interested, in the end, this is how I did it (note: this flattened the data, which for all intents and purposes, worked fine in my scenario).
$services = App\ServiceItem::where('provider_id', '=', $provider->id)
->where('user.user_id', '=', $user->id)
->where('start_date', '<=', $end_date)
->where('end_date', '>=', $start_date)
->orWhereNull('end_date')
->where('pet.pet_user_id', '=', $user->id)
->whereNull('service_items.deleted_at')
->whereNUll('pet.pet_deleted_at')
->join(DB::raw('(select pet_id as psi_pet_id, service_item_id as psi_service_item_id from pet_service_items) psi'), function($join) {
$join->on('psi.psi_service_item_id', '=', 'service_items.id');
})
->join(DB::raw('(select id as pet_id, user_id as pet_user_id, name as pet_name, deleted_at as pet_deleted_at from pets) pet'), function($join) {
$join->on('pet.pet_id', '=', 'psi.psi_pet_id');
})
->join(DB::raw('(select id as user_id, name as user_name, deleted_at as user_deleted_at from users) user'), function($join) {
$join->on('user.user_id', '=', 'pet.pet_user_id');
});
return ['status' => 200, 'services' => $services->get()];
Lets say I have this Object:
{town_id: 13, houses_data: [
{house_id: 5, price: 32, description: "thats a house"},
{house_id: 2, price: 12, description: "thats a house"}
]
}
And I want to update the desription of house with id 5 to "sold":
{town_id: 13, houses_data: [
{house_id: 5, price: 32, description: "sold"},
{house_id: 2, price: 12, description: "thats a house"}
]
}
What I tried:
town1 = town.findOne({town_id: 13});
Get the houses_data:
twon1.houses_data
And tried to update only the house_data where id = 5
twon1.houses_data.find({house_id: 5}).update(description: "sold");
But I get this error message:
[object Object],[object Object] has no method 'find'
What do I wrong? Thanks
You might use $ to update the first embedded document matching the given query:
db.test.town.update({town_id: 13, "houses_data.house_id":5},
{$set: { "houses_data.$.description": "sold"}})
We need to display a stacked column chart combined with a line chart and would like to stick to the VizFrame control offered by UI5. Is there a way to achieve this? It's not listed in the samples (https://sapui5.netweaver.ondemand.com/sdk/explored.html#/entity/sap.viz.ui5.controls.VizFrame/samples) but maybe there is a way to do it anyway.
EDIT
The data we need to display comes in the following format:
var data = [
{week: 1, stacked1: 10, stacked2: 20, stacked3: 30, line: 100},
{week: 2, stacked1: 12, stacked2: 13, stacked3: 14, line: 40},
{week: 3, stacked1: 14, stacked2: 25, stacked3: 26, line: 20},
{week: 4, stacked1: 15, stacked2: 24, stacked3: 33, line: 52}
];
So the idea is to have weeks on the x-axis, a stacked bar for the values stacked1, stacked2 and stacked3 as well as a value point for the line.
I think you want to use setVizType("stacked_combination") [or vizType: "stacked_combination"] on the VizFrame. You can see all the type on the getVizType() VizFrame doumentation. Here is a simple example where I extended the VizFrame and added two functions to display a Line Stacked Column Chart:
sap.viz.ui5.controls.VizFrame.extend("jonova.ui5.chart.JuVizFrame", {
renderer: { },
setLineStackedBar: function() {
var oModel = new sap.ui.model.json.JSONModel(
[{Product:"Total", Date: 2000, Available: 100},
{Product:"Total", Date: 2001, Available: 100},
{Product:"P1", Date: 2000, Utilized: 30},
{Product:"P1", Date: 2001, Utilized: 20},
{Product:"P2", Date: 2000, Utilized: 40},
{Product:"P2", Date: 2001, Utilized: 60}]);
var oDataset = new sap.viz.ui5.data.FlattenedDataset({
dimensions: [{name: 'Date', value: '{Date}'},
{name: 'Product', value: '{Product}'}],
measures: [{name: 'Available', value: '{Available}'},
{name: 'Utilized', value: '{Utilized}' }],
data: {path: "/"}});
var oFeeds = [new sap.viz.ui5.controls.common.feeds.FeedItem({uid: "valueAxis", type: "Measure", values: ["Utilized", "Available"]}),
new sap.viz.ui5.controls.common.feeds.FeedItem({uid: "categoryAxis", type: "Dimension", values: ["Date"]}),
new sap.viz.ui5.controls.common.feeds.FeedItem({uid: "color", type: "Dimension", values: ["Product"]})];
this.setChart("stacked_combination", oDataset, oModel, oFeeds);
},
setChart: function(aVizType, aDataset, aModel, aFeeds) {
this.setVizType(aVizType);
this.setDataset(aDataset);
this.setModel(aModel);
for( var i=0, len=aFeeds.length; i<len; i++) this.addFeed(aFeeds[i]);
},
});