getting something from yahoo weather API - javascript

I am trying to create a simple app to get a City name and display the weather with yahoo weather API. I am able to make a json request and get the answer but I am completely lost on how to retrieve the information from this json.
I can see the response.query , but when I am trying response.query.something I get undefined. Can someone explain to me how I can get the response.query.results.city thing?
Thanks in advance!!
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22greenland%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
{
"query": {
"count": 1,
"created": "2015-11-23T16:18:30Z",
"lang": "vi",
"results": {
"channel": {
"title": "Yahoo! Weather - Greenland, GL",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
"description": "Yahoo! Weather for Greenland, GL",
"language": "en-us",
"lastBuildDate": "Mon, 23 Nov 2015 12:50 pm CGT",
"ttl": "60",
"location": {
"city": "Greenland",
"country": "Greenland",
"region": ""
},
"units": {
"distance": "mi",
"pressure": "in",
"speed": "mph",
"temperature": "F"
},
"wind": {
"chill": "15",
"direction": "220",
"speed": "15"
},
"atmosphere": {
"humidity": "63",
"pressure": "29.8",
"rising": "0",
"visibility": "6.21"
},
"astronomy": {
"sunrise": "10:45 am",
"sunset": "1:33 pm"
},
"image": {
"title": "Yahoo! Weather",
"width": "142",
"height": "18",
"link": "http://weather.yahoo.com",
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
},
"item": {
"title": "Conditions for Greenland, GL at 12:50 pm CGT",
"lat": "71.8",
"long": "-42.18",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
"pubDate": "Mon, 23 Nov 2015 12:50 pm CGT",
"condition": {
"code": "28",
"date": "Mon, 23 Nov 2015 12:50 pm CGT",
"temp": "27",
"text": "Mostly Cloudy"
},
"description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/><br />\n<b>Current Conditions:</b><br />\nMostly Cloudy, 27 F<BR />\n<BR /><b>Forecast:</b><BR />\nMon - AM Clouds/PM Sun. High: 29 Low: 17<br />\nTue - Mostly Cloudy. High: 19 Low: 8<br />\nWed - PM Snow Showers. High: 12 Low: 6<br />\nThu - Mostly Cloudy. High: 10 Low: 0<br />\nFri - Mostly Sunny. High: 1 Low: -8<br />\n<br />\nFull Forecast at Yahoo! Weather<BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n",
"forecast": [
{
"code": "30",
"date": "23 Nov 2015",
"day": "Mon",
"high": "29",
"low": "17",
"text": "AM Clouds/PM Sun"
},
{
"code": "28",
"date": "24 Nov 2015",
"day": "Tue",
"high": "19",
"low": "8",
"text": "Mostly Cloudy"
},
{
"code": "14",
"date": "25 Nov 2015",
"day": "Wed",
"high": "12",
"low": "6",
"text": "PM Snow Showers"
},
{
"code": "28",
"date": "26 Nov 2015",
"day": "Thu",
"high": "10",
"low": "0",
"text": "Mostly Cloudy"
},
{
"code": "34",
"date": "27 Nov 2015",
"day": "Fri",
"high": "1",
"low": "-8",
"text": "Mostly Sunny"
}
],
"guid": {
"isPermaLink": "false",
"content": "GLXX0012_2015_11_27_7_00_CGT"
}
}
}
}
}
}

The response you get is a String, not an Object, so you have to Parse it into an Object using JSON.parse(response).
I think you can try PostMan and JSONView, it will help you to test any API easier.
Hope this can help you.

Just found out what I have to do ( hated js list ).
I used dot notation to get my temps.
So just for the record
var response; // here i have the jsonparse thing
//I want to access something I have to do
response.query.results.channel.wind.speed //and i will get wind's speed
If anyone could use the [] notation it would be great.
Thanks for your time .

Related

How can we group multi-level grouping in Javascript?

can we group array of objects in multilevel. This is my array of objects with common date and places, but different timings.
[
{
"ID": 2104221,
"date": "2022-11-18T00:00:00",
"day": "18",
"weekDay": "Fri",
"month": "Nov",
"Placeid": 2293,
"Place": "AAAAAA",
"address": "25 SSSSSS",
"city": "RRRRR",
"state": "WWWW",
"Time": "8:00 PM"
},
{
"ID": 2104344,
"date": "2022-11-15T00:00:00",
"day": "15",
"weekDay": "Tue",
"month": "Nov",
"Placeid": 3478,
"Place": "BIG",
"address": "1433 The ADDDD,",
"city": "CA",
"state": "",
"zipCode": "95126",
"Time": "03:00 PM"
},
{
"ID": 2104345,
"date": "2022-11-15T00:00:00",
"day": "15",
"weekDay": "Tue",
"month": "Nov",
"Placeid": 3478,
"Place": "BIG",
"address": "1433 The ADDDD,",
"city": "CA",
"state": "",
"zipCode": "95126",
"Time": "06:00 PM"
},
{
"ID": 2104346,
"date": "2022-11-15T00:00:00",
"day": "15",
"weekDay": "Tue",
"month": "Nov",
"Placeid": 3478,
"Place": "BIG",
"address": "1433 The ADDDD,",
"city": "CA",
"state": "",
"zipCode": "95126",
"Time": "09:00 PM"
},
{
"ID": 2104347,
"date": "2022-11-15T00:00:00",
"day": "15",
"weekDay": "Tue",
"month": "Nov",
"Placeid": 2464,
"Place": "ATheaters",
"address": "2901 Capital",
"city": "C",
"state": "TT",
"Time": "06:00 PM"
},
{
"ID": 2104348,
"date": "2022-11-15T00:00:00",
"day": "15",
"weekDay": "Tue",
"month": "Nov",
"Placeid": 2464,
"Place": "ATheaters",
"address": "2901 Capital",
"city": "ANNNN",
"state": "TT",
"Time": "10:00 PM"
},
{
"ID": 2103857,
"date": "2022-11-05T16:30:00",
"day": "5",
"weekDay": "Sat",
"month": "Nov",
"Placeid": 34771,
"Place": "Playhouse",
"address": "525 Palace",
"city": "BBBB",
"state": "YYYYY",
"Time": " 4:30 PM"
},
{
"ID": 2103858,
"date": "2022-11-05T23:30:00",
"day": "5",
"weekDay": "Sat",
"month": "Nov",
"Placeid": 34771,
"Place": "Playhouse",
"address": "525 Palace",
"city": "BBBB",
"state": "YYYYY",
"Time": "11:30 PM"
},
{
"ID": 2103862,
"date": "2022-11-15T23:00:00",
"day": "15",
"weekDay": "Tue",
"month": "Nov",
"Placeid": 34771,
"Place": "Playhouse",
"address": "525 Palace",
"city": "BBBB",
"state": "YYYYY",
"Time": "11:00 PM"
}
]
I would like to group based on day and places. my desired output will be
[
{
"day": "18",
"date": "2022-11-18T00:00:00",
"weekDay": "Fri",
"month": "Nov",
"Places": [
{
"Placeid": 2293,
"Place": "AAAAAA",
"address": "25 SSSSSS",
"city": "RRRRR",
"state": "WWWW",
"Timings": [
{
"ID": 2104221,
"Time": "8:00 PM"
}
]
}
]
},
{
"day": "15",
"date": "2022-11-15T23:00:00",
"weekDay": "Tue",
"month": "Nov",
"Places": [
{
"Placeid": 3478,
"Place": "BIG",
"address": "1433 The ADDDD,",
"city": "CA",
"state": "",
"Timings": [
{
"ID": 2104344,
"Time": "03:00 PM"
},
{
"ID": 2104345,
"Time": "06:00 PM"
},
{
"ID": 2104346,
"Time": "09:00 PM"
}
]
},
{
"Placeid": 2464,
"Place": "ATheaters",
"address": "2901 Capital",
"city": "ANNNN",
"state": "TT",
"Timings": [
{
"ID": 2104347,
"Time": "06:00 PM"
},
{
"ID": 2104348,
"Time": "10:00 PM"
}
]
},
{
"Placeid": 34771,
"Place": "Playhouse",
"address": "525 Palace",
"city": "BBBB",
"Timings": [
{
"ID": 2103862,
"Time": "11:00 PM"
}
]
}
]
},
{
"day": "5",
"date": "2022-11-05T16:30:00",
"weekDay": "Sat",
"month": "Nov",
"Places": [
{
"Placeid": 34771,
"Place": "Playhouse",
"address": "525 Palace",
"city": "BBBB",
"state": "YYYYY",
"Timings": [
{
"ID": 2103857,
"Time": " 4:30 PM"
},
{
"ID": 2103858,
"Time": "11:30 PM"
}
]
}
]
}
]
Is it possible to group like this using javascript? i tried with below code to group. but i didnt get my desired output.
const MultiLevelGrouping=(data,['day','Placeid'])=>{
var getEmpty = () => ({ _: [] }),
result = data
.reduce((q, o) => {
groups
.reduce((r, k) => {
const v = o[k];
if (!v) return r;
if (!r[v]) r._.push({ [k]: v, [k + 'Detail']: (r[v] = getEmpty())._ });
return r[v];
}, q)
._
.push(o);
return q;
}, getEmpty())
._;
return result;
}
Can Someone help me out ?
You could use 2 mapper objects. One to group each day and another to group each day-place combination. Then create entries in each mapper object if the key doesn't exists yet. Also, push the dayPlaceMap[comboKey] reference to the dayMap[day].Places array o that when the dayPlaceMap is updated, the nested array is updated.
const input = [{ID:2104221,date:"2022-11-18T00:00:00",day:"18",weekDay:"Fri",month:"Nov",Placeid:2293,Place:"AAAAAA",address:"25 SSSSSS",city:"RRRRR",state:"WWWW",Time:"8:00 PM"},{ID:2104344,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:3478,Place:"BIG",address:"1433 The ADDDD,",city:"CA",state:"",zipCode:"95126",Time:"03:00 PM"},{ID:2104345,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:3478,Place:"BIG",address:"1433 The ADDDD,",city:"CA",state:"",zipCode:"95126",Time:"06:00 PM"},{ID:2104346,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:3478,Place:"BIG",address:"1433 The ADDDD,",city:"CA",state:"",zipCode:"95126",Time:"09:00 PM"},{ID:2104347,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:2464,Place:"ATheaters",address:"2901 Capital",city:"C",state:"TT",Time:"06:00 PM"},{ID:2104348,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:2464,Place:"ATheaters",address:"2901 Capital",city:"ANNNN",state:"TT",Time:"10:00 PM"},{ID:2103857,date:"2022-11-05T16:30:00",day:"5",weekDay:"Sat",month:"Nov",Placeid:34771,Place:"Playhouse",address:"525 Palace",city:"BBBB",state:"YYYYY",Time:" 4:30 PM"},{ID:2103858,date:"2022-11-05T23:30:00",day:"5",weekDay:"Sat",month:"Nov",Placeid:34771,Place:"Playhouse",address:"525 Palace",city:"BBBB",state:"YYYYY",Time:"11:30 PM"},{ID:2103862,date:"2022-11-15T23:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:34771,Place:"Playhouse",address:"525 Palace",city:"BBBB",state:"YYYYY",Time:"11:00 PM"}],
dayMap = {},
dayPlaceMap = {};
for (const { day, date, weekDay, month, Placeid, ID, Time, ...rest } of input) {
dayMap[day] ??= { day, date, weekDay, month, Places: [] };
const comboKey = `${day}|${Placeid}`
if(!dayPlaceMap[comboKey]) {
dayPlaceMap[comboKey] ??= { Placeid, ...rest, Timings: [] }
dayMap[day].Places.push( dayPlaceMap[comboKey] )
}
dayPlaceMap[comboKey].Timings.push({ ID, Time })
}
console.log(Object.values(dayMap))
Here is a one-liner to do it:
const data = [{ID:2104221,date:"2022-11-18T00:00:00",day:"18",weekDay:"Fri",month:"Nov",Placeid:2293,Place:"AAAAAA",address:"25 SSSSSS",city:"RRRRR",state:"WWWW",Time:"8:00 PM"},{ID:2104344,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:3478,Place:"BIG",address:"1433 The ADDDD,",city:"CA",state:"",zipCode:"95126",Time:"03:00 PM"},{ID:2104345,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:3478,Place:"BIG",address:"1433 The ADDDD,",city:"CA",state:"",zipCode:"95126",Time:"06:00 PM"},{ID:2104346,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:3478,Place:"BIG",address:"1433 The ADDDD,",city:"CA",state:"",zipCode:"95126",Time:"09:00 PM"},{ID:2104347,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:2464,Place:"ATheaters",address:"2901 Capital",city:"C",state:"TT",Time:"06:00 PM"},{ID:2104348,date:"2022-11-15T00:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:2464,Place:"ATheaters",address:"2901 Capital",city:"ANNNN",state:"TT",Time:"10:00 PM"},{ID:2103857,date:"2022-11-05T16:30:00",day:"5",weekDay:"Sat",month:"Nov",Placeid:34771,Place:"Playhouse",address:"525 Palace",city:"BBBB",state:"YYYYY",Time:" 4:30 PM"},{ID:2103858,date:"2022-11-05T23:30:00",day:"5",weekDay:"Sat",month:"Nov",Placeid:34771,Place:"Playhouse",address:"525 Palace",city:"BBBB",state:"YYYYY",Time:"11:30 PM"},{ID:2103862,date:"2022-11-15T23:00:00",day:"15",weekDay:"Tue",month:"Nov",Placeid:34771,Place:"Playhouse",address:"525 Palace",city:"BBBB",state:"YYYYY",Time:"11:00 PM"}]
let r = Object.values(data.reduce((a,
{day, date, weekDay, month})=>(a[`|${day}`]??={day, date, weekDay, month,
Places: Object.values(data.filter(i=>i.day===day).reduce((b, {
Placeid, Place, address, city, state})=>(b[`${day}|${Placeid}`]??={
Placeid, Place, address, city, state,
Timings: data.filter(j=>j.day===day && j.Placeid===Placeid)
.map(j=>({ID: j.ID, Time: j.Time}))}, b), {}))}, a), {}));
console.log(r);

Ngx-Charts show all x axis values but alternate x-axis labels

I am currently working on ngx charts, for examples i have 12 x-axis every values need to be plotted in graph, but only alternate x-axis label need to be displayed.
html code
<ngx-charts-area-chart [view]="view" [scheme]="colorScheme" [legend]="legend" [showXAxisLabel]="showXAxisLabel" [showYAxisLabel]="showYAxisLabel" [xAxis]="xAxis" [yAxis]="yAxis"
[xAxisLabel]="xAxisLabel" [yAxisLabel]="yAxisLabel" [results]="multi" (select)="onSelect($event)">
</ngx-charts-area-chart>
data.ts
var multi = [ {
"name": "Belarus",
"series": [
{
"value": 5776,
"name": "Jan 19"
},
{
"value": 4299,
"name": "Feb 19"
},
{
"value": 3995,
"name": "Mar 19"
},
{
"value": 6597,
"name": "Apr 19"
},
{
"value": 4954,
"name": "May 19"
},
{
"name": "June 19",
"value": 3865
},
{
"name": "July 19",
"value": 2349
},
{
"name": "Aug 19",
"value": 6910
},
{
"name": "Sept 19",
"value": 6224
},
{
"name": "Oct 19",
"value": 4161
},
{
"name": "Nov 19",
"value": 4825
},
{
"name": "Dec 19",
"value": 3835
}
]},{
"name": "Croatia",
"series": [
{
"value": 2576,
"name": "Jan 19"
},
{
"value": 5427,
"name": "Feb 19"
},
{
"value": 3097,
"name": "Mar 19"
},
{
"value": 4385,
"name": "Apr 19"
},
{
"value": 4849,
"name": "May 19"
},
{
"name": "June 19",
"value": 2867
},
{
"name": "July 19",
"value": 6241
},
{
"name": "Aug 19",
"value": 6517
},
{
"name": "Sept 19",
"value": 3808
},
{
"name": "Oct 19",
"value": 2068
},
{
"name": "Nov 19",
"value": 5121
},
{
"name": "Dec 19",
"value": 3876
}
]}]
In the above data set in x-axis I need to plot all values in the graph but only alternate labels need to displayed. This need to be applied for all types of ngx charts.

Javascript extract value [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I'm using weather-js with this code :
weather.find({search: 'San Francisco, CA', degreeType: 'F'}, function(err, result) {
if(err) console.log(err);
console.log(JSON.stringify(result, null, 2));
});
But I don't know how I could extract the result value from this
The result should be like this:
[
{
"location": {
"name": "San Francisco, CA",
"lat": "37.777",
"long": "-122.42",
"timezone": "-7",
"alert": "",
"degreetype": "F",
"imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
},
"current": {
"temperature": "70",
"skycode": "32",
"skytext": "Sunny",
"date": "2017-03-14",
"observationtime": "13:15:00",
"observationpoint": "San Francisco, California",
"feelslike": "70",
"humidity": "59",
"winddisplay": "3 mph West",
"day": "Tuesday",
"shortday": "Tue",
"windspeed": "3 mph",
"imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/32.gif"
},
"forecast": [
{
"low": "52",
"high": "69",
"skycodeday": "31",
"skytextday": "Clear",
"date": "2017-03-13",
"day": "Monday",
"shortday": "Mon",
"precip": ""
},
{
"low": "52",
"high": "70",
"skycodeday": "34",
"skytextday": "Mostly Sunny",
"date": "2017-03-14",
"day": "Tuesday",
"shortday": "Tue",
"precip": "10"
},
{
"low": "56",
"high": "63",
"skycodeday": "26",
"skytextday": "Cloudy",
"date": "2017-03-15",
"day": "Wednesday",
"shortday": "Wed",
"precip": "20"
},
{
"low": "50",
"high": "64",
"skycodeday": "28",
"skytextday": "Mostly Cloudy",
"date": "2017-03-16",
"day": "Thursday",
"shortday": "Thu",
"precip": "10"
},
{
"low": "53",
"high": "67",
"skycodeday": "32",
"skytextday": "Sunny",
"date": "2017-03-17",
"day": "Friday",
"shortday": "Fri",
"precip": "10"
}
]
}
]
In the result, there are many useful information:
latitude & longitude of the location
current temperature of the location
forecast information for next four days
All those information exist in the result JSON that you can parse and use it. Consider that the result is an JSON array with only one element in this case.
for example you can log current temperature like this:
console.log(result[0].current.temperature)
Declare a variable and write the result into this variable, if no error occured:
var res = null;
weather.find({search: 'San Francisco, CA', degreeType: 'F'}, function(err, result) {
if(err) {
console.log(err);
} else {
res = result;
}
console.log(JSON.stringify(result, null, 2));
});

Duplicate calls to http in Angular on Subscribe

I'm new to angular and trying to implement a dashboard application. The dashboard contains 50+ different charts so I decided to capture all the data of these charts user one API call, the json file is as follows
{
"site": "bje",
"date": "2018-03-09T00:00:00",
"charts": [
{
"code": "INDK-01",
"dataset": [
{
"name": "Actual",
"data": [
{
"label": "05 Jan 2018",
"value": 351,
"date": "2018-01-05T00:00:00"
},
{
"label": "12 Jan 2018",
"value": 373,
"date": "2018-01-12T00:00:00"
},
{
"label": "19 Jan 2018",
"value": 353,
"date": "2018-01-19T00:00:00"
},
{
"label": "26 Jan 2018",
"value": 379,
"date": "2018-01-26T00:00:00"
},
{
"label": "02 Feb 2018",
"value": 356,
"date": "2018-02-02T00:00:00"
},
{
"label": "09 Feb 2018",
"value": 371,
"date": "2018-02-09T00:00:00"
},
{
"label": "16 Feb 2018",
"value": 371,
"date": "2018-02-16T00:00:00"
},
{
"label": "23 Feb 2018",
"value": 368,
"date": "2018-02-23T00:00:00"
},
{
"label": "02 Mar 2018",
"value": 369,
"date": "2018-03-02T00:00:00"
},
{
"label": "09 Mar 2018",
"value": 371,
"date": "2018-03-09T00:00:00"
}
]
},
{
"name": "Budget",
"data": [
{
"label": "05 Jan 2018",
"value": 0,
"date": "2018-01-05T00:00:00"
},
{
"label": "12 Jan 2018",
"value": 0,
"date": "2018-01-12T00:00:00"
},
{
"label": "19 Jan 2018",
"value": 0,
"date": "2018-01-19T00:00:00"
},
{
"label": "26 Jan 2018",
"value": 0,
"date": "2018-01-26T00:00:00"
},
{
"label": "02 Feb 2018",
"value": 0,
"date": "2018-02-02T00:00:00"
},
{
"label": "09 Feb 2018",
"value": 0,
"date": "2018-02-09T00:00:00"
},
{
"label": "16 Feb 2018",
"value": 0,
"date": "2018-02-16T00:00:00"
},
{
"label": "23 Feb 2018",
"value": 0,
"date": "2018-02-23T00:00:00"
},
{
"label": "02 Mar 2018",
"value": 0,
"date": "2018-03-02T00:00:00"
},
{
"label": "09 Mar 2018",
"value": 331.02,
"date": "2018-03-09T00:00:00"
}
]
},
{
"name": "Target",
"data": [
{
"label": "05 Jan 2018",
"value": 0,
"date": "2018-01-05T00:00:00"
},
{
"label": "12 Jan 2018",
"value": 0,
"date": "2018-01-12T00:00:00"
},
{
"label": "19 Jan 2018",
"value": 0,
"date": "2018-01-19T00:00:00"
},
{
"label": "26 Jan 2018",
"value": 0,
"date": "2018-01-26T00:00:00"
},
{
"label": "02 Feb 2018",
"value": 0,
"date": "2018-02-02T00:00:00"
},
{
"label": "09 Feb 2018",
"value": 0,
"date": "2018-02-09T00:00:00"
},
{
"label": "16 Feb 2018",
"value": 0,
"date": "2018-02-16T00:00:00"
},
{
"label": "23 Feb 2018",
"value": 0,
"date": "2018-02-23T00:00:00"
},
{
"label": "02 Mar 2018",
"value": 0,
"date": "2018-03-02T00:00:00"
},
{
"label": "09 Mar 2018",
"value": 331.02,
"date": "2018-03-09T00:00:00"
}
]
}
]
},..............etc
] }
The service .ts file contain a function that return the data as follows:
getDashboardData(): Observable<ProcessedData>{
return this._http.get<ProcessedData>(this.baseUrl)
.map(res => res);
}
As well I have created one re-usable component which accept an input of chart code (ex. "INDK-01" in above json sample) an in ngOnInit I have this code
ngOnInit() {
this._service.getDashboardData(this.selectedSite, this.selectedDate)
.subscribe(res => {
this.BudgetData = res.charts.find(x => x.code == this.chartId)
.dataset.find(x => x.name == 'Budget')
.data
.sort();
this.TargetData = res.charts.find(x => x.code == this.chartId)
.dataset.find(x => x.name == 'Target')
.data
.sort();
.
.
.
});
in dashboard home component I add multiple
the problem that it makes multiple calls to the API everytime I load dashboard page, is there any way to avoid that? like for example store the data in a global object and from each chart will filter to get the proper data.
You need to first shareReplay(1) your observable something like following to cache the data:
const sharedOb = this._service.getDashboardData(this.selectedSite, this.selectedDate).shareReplay(1)
sharedOb.subscribe(x=>{
// do your work here
})
Now you can subscribe to sharedOb as many as times you want, there will only be one server round-trip.
You could use ".share".
this._service.getDashboardData(this.selectedSite, this.selectedDate).subscribe(res =>
{
this.BudgetData = res.charts.find(x => x.code == this.chartId)
.dataset.find(x => x.name == 'Budget')
.data
.sort();
this.TargetData = res.charts.find(x => x.code == this.chartId)
.dataset.find(x => x.name == 'Target')
.data
.sort();
}).share();

Process JSON data for Google org chart

I have a JSON data that looks like this
{
"name": "k11",
"id": "GOLD1501131955983380",
"data": {
"rank": "Agent",
"email": "k11#gmail.com",
"registrationDate": "13 Jan 2015 11:55:15 GMT",
"contact": "123123"
},
"children": [{
"name": "k11a",
"id": "GOLD1501131956858350",
"data": {
"registrationDate": "Jan 13, 2015 7:56:15 PM",
"rank": "Agent",
"email": "k11a#gmail.com",
"contact": "123123213123"
},
"children": [{
"name": "k11a2",
"id": "GOLD1501131959324173",
"data": {
"registrationDate": "Jan 13, 2015 7:59:25 PM",
"rank": "Agent",
"email": "k11a2#gmail.com",
"contact": "123123123"
},
"children": []
}, {
"name": "k11a1",
"id": "GOLD1501131958998436",
"data": {
"registrationDate": "Jan 13, 2015 7:58:27 PM",
"rank": "Agent",
"email": "k11a1#gmail.com",
"contact": "123123123"
},
"children": []
}]
}]
}
}
What I need to is to process this data to be compatible with google org chart that should look like this
{
"cols": [{
"id": "GOLD1501131955983380",
"name": "k11",
"data": {
"registrationDate": "Jan 13, 2015 7:59:25 PM",
"rank": "Agent",
"email": "k11a2#gmail.com",
"contact": "123123123"
}
}, {
}]
"rows": [{
"c": [{
"v": "k11"
}]
}, {
"c": [{
"v": "k11a2"
}, {
"v": "k11a"
}]
}]
}
Somewhat like the structured require by google charts.
Kindly please help me

Categories

Resources