how to display array of objects in High stock charts - highcharts

I have array of objects like this
{
"data": [{
"date": "11/25/2016 08:59:58",
"energy": 29940935080,
"power": 6815.7056798623,
"time": 217781943
}, {
"date": "11/25/2016 09:29:59",
"energy": 29940981851,
"power": 6803.7187250996,
"time": 217783743
}, {
"date": "11/25/2016 09:59:59",
"energy": 29941028913,
"power": 6841.5804195804,
"time": 217785544
}, {
"date": "11/25/2016 10:29:59",
"energy": 29941075952,
"power": 6845.9247648903,
"time": 217787343
}, {
"date": "11/25/2016 10:59:58",
"energy": 29941123228,
"power": 6877.2764478764,
"time": 217789143
}]
}
and i want display power in y-axis and date in x-axis.Once data is loaded am adding Y prop, so that Higchart can read y-axis values
for (var i = data.data.length - 1; i >= 0; i--) {
var item=data.data[i];
item.y=data.data[i].power;
data.data[i]=item;
var timestamp = new Date(data.data[i]['date']).getTime();
values.push(timestamp,item.y);
}
chart.series[0].setData(values);
But for some reason data is not show in charts, please guide me on this

I'm not sure why you're doing your loop the way you are, but it has a few problems.
You're reversing the array. Unless your dates are stored in reverse order, that is going to cause problems. Highcharts wants the X data sorted.
On the line values.push(timestamp, item.y); you are pushing 2 items onto the values array instead of pushing an array of 2 values or an object with x and y values. So, highcharts thinks you have double the points and that they are sequential.
A corrected loop:
var values = [];
for (var i = 0; i < data.length; i++) {
var item = {};
item.y = data[i].power;
item.x = new Date(data[i]['date']).getTime();
values.push(item);
}
http://jsfiddle.net/gq385b16/1/

Related

How to read multidimensional JSON data in google sheet appscript?

How can I read this type of json data like I want profession of ID:12311 in google spreadsheet?
{
"user": [
{
"id": 12311,
"name": "Deffy doof",
"profession": "Photographer"
},
{
"id": 18341,
"name": "John Smith",
"profession": "Developer"
}
]
}
Solution:
Since this is an array of objects you can reference elements using array notation, e.g., to pull "Photographer":
data.user[0].profession
Sample:
If you only know the unique ID and not the index, you can filter the array first, then get the element.
var array = data.user.filter(function b(e) { return e.id == 12311 });
console.log(array[0].profession);
References:
filter()

Configuring collectionView:cellForRowAt based on an array of strings

I've got this JSON data (not verbatim) that I get from the backend. It contains the actual data and an array of strings describing the sequence of cells to be shown:
{
"data": [
{
"name": "text",
"data": {
"text": "some text"
}
},
{
"name": "pic",
"data": {
"url": "https://somepic.jpg",
"text": "picture"
}
},
{
"name": "switcher",
"data": {
"id": 1,
"options": [
{
"id": 0,
"text": "option 1"
},
{
"id": 1,
"text": "option 2"
},
{
"id": 2,
"text": "option 3"
}
]
}
}
],
"view": [
"text",
"pic",
"switcher",
"text"
]
}
The problem is that I can't get my head around how to configure cellForRowAt: and get the right order of cells in one section. (i.e. text, pic, selector, text).
I tried a couple of things:
Looping through "view" array and switching on each individual view string to dequeue a specific cell but that doesn't seem to work since returning a cell from a switch case gives a "unexpected non-void return value in void function" error.
I was also thinking about turning a "view" array into a dictionary and then, based on keys in it, dequeue a specific cell but then again, a dictionary should have unique keys meaning that I will not have 2 "text" entries, one of them will be lost.
So, the questions is: how can I dequeue specific cells based on the array of strings? It's also important to understand that it should be done in one section. I'm feeling that it's somehow not that difficult to implement but I'm kinda lost right now. Thanks!
you need to transform your view list and data array into an array of cell contents that you can use inside the TableViewDelegate and TableViewSource method :
var cellsContents : [Int] = []
for aView in view {
var found = false
var index = 0
for aData in data {
if !found {
if let name = aData["name"] as? String {
if aView == name {
found = true
cellsContents.append(index)
continue
}
}
index = index + 1
}
}
}
Then :
number of rows : cellsContents.count
type and contents for a row : data[cellsContents[indexPath.row]]["name"] and data[cellsContents[indexPath.row]]["data"]

Issue on JSON nested Array objects into TableVIew

I've tried many ways but i'm unable get the data from Something Array and show it in UITableView based on indexPath.
something is like below response
{
"results": [
{
"Something": [
[
{
"airV": "LX",
"class": "Y"
}
]
],
},
{
"Something": [
[
{
"airV": "FZ",
"class": "Y"
}
],
[
{
"airV": "FZ",
"class": "Y"
}
]
],
},
],
}
Note : something array [] ->>> contains many arrays in it.Looping the something array i want to get "airV": "LX","class": "Y"
airv,class into TableView
You can store the NSArray of (something) and iterate over that array based on NSIndexPath.
NSArray * arrayofSomething /* Your Array */
Inside your cellForRowAtIndexPath you can get the objects from arrayofSomething based on index.
Obj *obj = [arrayofSomething objectAtIndex:indexPath.row];

Different Cells in same UITableView

I have got a json result, that looks like this:
[{
"EventDate": "2015-02-19",
"PubEvents": [{
"Title": "Ladies Night",
"Description": "Every thursday is Ladies Night at the Irish House.\nLadies: 2 cocktails for the price of 1 - 1 pint of Crowmoor Cider - 30 kr",
"EventType": "LadiesNight",
"Start": "2015-02-19",
"End": "2015-02-20",
"StartTime": "19:00",
"EndTime": "02:00",
"Image": "ladies.jpg"
}, {
"Title": "Pat Kelly",
"Description": "Pat Kelly from Ireland has extensive experience in entertaining all nationalities, as he has travelled around the world from New York to Amsterdam. He has a very wide repertoire maintaining a traditional approach, and is well received and appreciated for his ability to get his audience excited every night he hits the stage. A “sure thing” evening you will get with the talented Pat.",
"EventType": "Music",
"Start": "2015-02-19",
"End": "2015-02-20",
"StartTime": "21:30",
"EndTime": "01:00",
"Image": "http://domain.dk/Images/Musicians/kelly"
}],
"Matches": [{
"EventType": "Sports",
"Start": "2015-02-19",
"End": "2015-02-19",
"StartTime": "18:00",
"EndTime": "19:00",
"HomeTeam": {
"Id": 0,
"TeamName": "Hold",
"HomeImageUrl": "defaultHome.png",
"AwayImageUrl": "defaultAway.png",
"Badge": "defaultBadge.png"
},
"AwayTeam": {
"Id": 0,
"TeamName": "AndetHold",
"HomeImageUrl": "defaultHome.png",
"AwayImageUrl": "defaultAway.png",
"Badge": "couldn't get an away team"
}
}]
}, {
"EventDate": "2015-02-20",
"PubEvents": [{
"Title": "Pat Kelly",
"Description": "Pat Kelly from Ireland has extensive experience in entertaining all nationalities, as he has travelled around the world from New York to Amsterdam. He has a very wide repertoire maintaining a traditional approach, and is well received and appreciated for his ability to get his audience excited every night he hits the stage. A “sure thing” evening you will get with the talented Pat.",
"EventType": "Music",
"Start": "2015-02-20",
"End": "2015-02-21",
"StartTime": "22:30",
"EndTime": "02:00",
"Image": "http://domain.dk/Images/Musicians/kelly"
}],
"Matches": []
},
An Array of Events in the format:
var eventDate : String?
var pubEvents : Array<PubEvent>?
var matches : Array<Match>?
numberOfSectionsInTableView is eventarray.count, as every date is distinct
numberOfRowsInSection:
let event = self.eventarray[section]
let matchesCount = event.matches?.count ?? 0
let pubEventsCount = event.pubEvents?.count ?? 0
return matchesCount + pubEventsCount
Since every section has x number of matches and x number of pubEvents i need a combined count of the amount of each.
Here is my problem:
I would need to insert the pubevents (if any) and the matches (if any) in the tableview, but I can't figure it out with Swift.
Edit
Philip Mills helped: A very quick implementation:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
let event = self.eventarray[indexPath.section] as SortedEvent
let matchesCount = event.matches?.count ?? 0
let pubEventsCount = event.pubEvents?.count ?? 0
if indexPath.row < matchesCount{
cell.textLabel?.text = "\(event.matches![indexPath.row].homeTeam!.teamName!) v \(event.matches![indexPath.row].awayTeam!.teamName!)"
cell.detailTextLabel?.text = "\(event.matches![indexPath.row].startTime!) - \(event.matches![indexPath.row].endTime!)"
}
else{
cell.textLabel?.text = event.pubEvents![indexPath.row - matchesCount].title!
cell.detailTextLabel?.text = "\(event.pubEvents![indexPath.row - matchesCount].startTime!) - \(event.pubEvents![indexPath.row - matchesCount].endTime!)"
}
return cell
}
When you're given an index path to provide a cell, see if the row is less than the count of matches. If it is, use that match. If it's not less, subtract the count of matches from the row and use that as the index of the pub event you want.

Merging a complex data structure of NSArray and NSDictionary

I have the following data structure Snapshot which is a combination of nested arrays and dictionaries:
{"Snapshot": {
"ask": [
{
"price": 101.42,
"size": 7
},
{
"price": 101.85,
"size": 5
}
],
"bid": [
{
"price": 89.72,
"size": 79
},
{
"price": 89.71,
"size": 158
}
]
}}
The existing structure from above should be updated by the price and size entries in the following dictionary / array structure:
{"Refresh": {
"ask": [],
"bid": [
{
"price": 89.71,
"size": 666
}
],
}}
In this example, the value size for the bid item which has the price: 89.71 should be updated from 158 to 666. If the price does not exist yet, a new item with the price and size from the Refresh data structure should be added. If size = 0, the price and size item should be removed.
I have tried to do this with the methods addEntriesFromDictionary and addObjectsFromArray but did find a solution to my problem.
What would be the fastest way to do this?
How would you recommend to merge these two data structures?
Thank you!
Assuming the dictionaries are named snapshot and refresh...
snapshot[#"Snapshot][#"bid"] = [snapshot[#"Snapshot][#"bid"] arrayByAddingObjectsFromArray:refresh[#"Refresh"][#"bid]];
More complicated union of the arrays will just require more code. And if you want price to be a unique key, why not make it a key?

Resources