Show nested values in chart with groups using c3.js - javascript

I have a block of JSON data looking like this:
{
date: '2014-01-01',
data: [
{
name: "Janos",
work: 3,
drive: 5
},
{
name: "Jelle",
work: 4,
drive: 2
}
]
}, {
date: '2014-01-02',
data: [
{
name: "Janos",
work: 3,
drive: 5
},
{
name: "Jelle",
work: 4,
drive: 2
}
]
}, {
date: '2014-01-03',
data: [
{
name: "Janos",
work: 3,
drive: 5
},
{
name: "Jelle",
work: 4,
drive: 2
}
]
}, {
date: '2014-01-04',
data: [
{
name: "Janos",
work: 3,
drive: 5
},
{
name: "Jelle",
work: 4,
drive: 2
}
]
}
And I want a line chart with an x axis based on the date key, and columns based on the given names in the data arrays for each item.
So for each person I want to show the work and drive value.
This is what I'm trying to achieve (in case I don't make any sense):
Achieved this screenshot by using inspect element
I hope my question makes sense, I can't seem to make it work with the available documentation.
Please leave a comment if I'm not clear enough.
Thanks in advance!

Related

Nested array of object data display on UI [duplicate]

This question already has answers here:
How to use nested Map in React component with the given data
(2 answers)
Closed 3 months ago.
I have following response from backend as an array of object,
const cloudData = [
{
dataCenter: "AWS-East",
availablechannels: [
{
channelName: "E-channel1",
id: 1,
},
{
channelName: "E-channel2",
id: 2,
},
{
channelName: "E-channel3",
id: 3,
},
],
},
{
dataCenter: "AWS-West",
availablechannels: [
{
channelName: "W-channel1",
id: 1,
},
{
channelName: "W-channel2",
id: 2,
},
{
channelName: "W-channel3",
id: 3,
},
],
},
];
I need to display on UI grid in following way ,
AWS East
E-channel1
E-channel2
E-channel3
I have tried using es6 map and filter
You can do this :
cloudData.forEach((datacenter)=> {
console.log(datacenter.dataCenter)
datacenter.availablechannels.forEach((channel)=>{
console.log(channel.channelName)
})
})

How to change the value of a field in an array mongodb nodejs

There are many array levels, that's why I can't choose the right field, I guess.
This is the Schema of the model:
{
season: 1,
finished: 0,
Games: [{
Game: [{
game: 1,
Players: [{
Id: 0,
name: "Peter",
jails: 3, //I want to change this value
pays: 1000,
gets: 2000,
points: 3
}]
}]
}]
}
I tried this query but it's not working:
Seasons.update({
season: 1,
game: 2,
Id: 0
}, {
jails: 4
},
(err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
}
}
)
I have used the $set propery but it didn't work as I want. It make changes all jails field of all players to same value in the same season. I mean, the query is selecting the most parent record, so it is the season record. But I want to change value of child element.

Creating a Object with same Structure than received json

Hola Developers im trying to create a product in my shopping card , but still im stacked in one issue :
Can't find the proper way to design a object just on the same way i receive it from my json (back-end).
Lets say the part is causing the issue is this:
JSON RECEIVED
"product_category": [
{
"categories_of_product": "Good"
},
{
"categories_of_product": "Danger"
},
{
"categories_of_product": "Homer"
}
],
"people_buying_this_product": "Jack Ripper"
},
]
then on my building-product-processs , on the data return is a section that have to do with this, where in then using checkboxes i get which checkbox is checked or not , in order to build a array of categories similar to the former one i have shown:
DATA RETURN
ProductAdded: {
description: "",
upload_image3: "",
upload_image2: "",
upload_image1: "",
unities: 0,
price: 0,
name: "",
Categories: [
{ id: 1, value: "Miscellaneous", selected: false },
{ id: 2, value: "Homer", selected: false },
{ id: 3, value: "Electronic", selected: false },
{ id: 4, value: "Internet", selected: true },
{ id: 5, value: "Kids", selected: false },
{ id: 6, value: "Donas", selected: true },
{ id: 7, value: "Sports", selected: true },
{ id: 8, value: "Horror", selected: false }
],
METHOD that dispatches de action in vuex
addProductOnSale(thisCurrent) {
this.$store.dispatch("addProductSale", this.ProductAdded);
}
then being already in the VUEX state management , on the action in fact , trying to build the new product for this product category, i set this:
addProductSale({commit,getters},currentProduct){
product_category: currentProduct.Categories.filter(option =>
option.selected).map(option => {categories_of_product: option.value})
}
------------------->this one gives me undefined like:
"product_category":
[
undefined
],
"product_category":
[
undefined
],
"product_category":
[
undefined
],
]
or tried this other :
addProductSale({commit,getters},currentProduct){
product_category:currentProduct.Categories.forEach( option.selected,() =>
option.value).map(option => option.selected),...
}
and gave me error , not even showing a result.
Basically cant find the way to design the same structure im receiving for json , nor even reaching to values.
Is weird ,but if i only set the query like this:
addProductSale({commit,getters},currentProduct){
product_category:currentProduct.Categories.filter(option => option.selected)
}
Indeed filters and gives back the objects inside Categories which commit the condition of selected true , but with its plain format just as the data returns , but that's not the idea.
Any advice or help?
Thanks in advance!!!!

ApostropheCMS: How do I populate the _children key under apostrophe-pages when there are in fact children?

This question is the same as asked here, but the only answer is something I had already tried by following the docs to no avail. I need to access 4 levels deep of pages. I currently have the following included in my lib/modules/apostrophe-pages/index.js:
addFields: [
{
name: '_pages',
type: 'joinByArray',
withType: 'apostrophe-page',
label: 'Pages',
idsField: 'pageIds',
filters: {
children: {
depth: 4,
areas: false
},
projection: {
title: 4,
slug: 4,
rank: 4,
level: 4,
path: 4
}
}
}
]
I even tried passing '1' as it says to do in the docs and still only returns an empty array under the "_children" key when logging out data.
and I have the following in my app.js file:
'apostrophe-pages': {
filters: {
ancestors: {
children: {
depth: 4
}
},
children: {
depth: 4
}
},
No matter what it's still empty. How can I get the _children key to populate accurately as my navigation tree shows?

Mongoose & getting average of ratings in a collection?

I have a NodeJS based application using Mongoose. I am wanting to create a response where there values are an average of ratings provided by people who have responded to a questionnaire, for each question asked.
My Schema looks as follows:
const questionnaireResultSchema = new Schema({
user: { type: ObjectId, ref: 'User' },
questionnaire: { type: ObjectId, ref: 'Questionnaire' },
rating: [{
id: Number,
question: String,
value: Number
}]
},{
timestamps: true
}).index({user: 1, questionnaire: 1}, {unique: true});
I have looked at the Mongoose aggregator operator, but I am not sure how I would apply it to my case. Pseudo code would look as follows:
Find all questionnaire results for questionnaire of id xyz
Provide a result where the ratings for each questionnaire result has be averaged
For example the response would look as follows:
[{
id: 1,
question: 'How strongly do you feel about candidate A?',
value: 12 // averaged value
},{
id: 2,
question: 'How strongly do you think we should change the sky color to green?',
value: 31 // averaged value
},{
id: 3,
question: 'How strongly do you think your answers count?',
value: 20 // averaged value
}]
I have tried:
QuestionnaireResultSchema.aggregate([{
$match: {
questionnaire: questionnaire
}},
{$project: {
scores: { $avg: '$scores'}
}}
]);
This just provides the JSON:
[{
"_id": "57bbd4b495407f6145b3ba9f",
"scores": null
}]
A sample document in a collection would like:
{
user: ObjectId("57bca30536e376c653f439bb")
questionnaire: ObjectId("37bca0feedb0bc470353ab")
scores: [{
id: 1,
question: 'How strongly do you feel about candidate A?',
value: 3
},{
id: 2,
question: 'How strongly do you think we should change the sky color to green?',
value: 4 // averaged value
},{
id: 3,
question: 'How strongly do you think your answers count?',
value: 5 // averaged value
}]
}
While I could calculate the averages myself, if Mongoose provides the functionality, I would rather leverage that.
Any help would be appreciated.
This should work for your aggregation pipeline:
[
{
$match: {
questionnaire: ObjectId("37bca0feedb0bc470353ab")
}
},
{
$unwind: "$rating"
},
{
$group:{
_id:{
"rating_id": "$rating.id",
"question": "$rating.question",
},
avg_rating: {$avg:"rating.value"}
}
},
{
$project:{
"id": "$_id.rating_id",
"question": "$_id.question",
"avg_rating": "$avg_rating"
}
}
]
Although your sample doc has "scores" instead of "rating" in which case you'd use:
[
{
$match: {
questionnaire: ObjectId("237bca0feedb0bc470353aba")
}
},
{
$unwind: "$scores"
},
{
$group:{
_id:{
"rating_id": "$scores.id",
"question": "$scores.question",
},
avg_rating: {$avg:"scores.value"}
}
},
{
$project:{
"id": "$_id.rating_id",
"question": "$_id.question",
"avg_rating": "$avg_rating"
}
}
]
Also, some of the ObjectId's that you are using are not valid. I'm assuming those are just stubbed.

Categories

Resources