read the json file to output to an highcharts - json

I came to read the json file to output to an highcharts.
I have a highcharts areagraph whose values are received from json whose format is as follows:
scope.jsondata = [
{
"Name": "A",
"Categories": "03.01",
"Locate": "1",
"Value": 30
},
{
"Name": "A",
"Categories": "03.02",
"Locate": "1",
"Value": 50
},
{
"Name": "A",
"Categories": "03.03",
"Locate": "1",
"Value": 60
},
{
"Name": "A",
"Categories": "03.04",
"Locate": "1",
"Value": 40
},
{
"Name": "A",
"Categories": "03.05",
"Locate": "1",
"Value": 70
}
];
How can I embed those values for my jsondata in angularJS?
scope.render = function (data) {
var target = element.find('#detail-usage-chart'),
firstDate = {
name: scope.jsondata.Name,
data: scope.jsondata.Vaule,
color: '#f48d7f',
type: 'area'
},
tempOption = {
data: [10, 13, 17, 8, 11, 5, 11, 13 ,16, 18, 20, 13, 16, 21, 19],
type: 'spline',
yAxis: 1
}
};
Please provide a suitable way to embed the data from json.

Area chart is expecting a name and an array contaning values in its series:
See here
So all you have to do is just a quick function in order to prepare your data that way. For example:
var scope.readyValues = [];
for(var i=0;i<scope.jsondata.length;i++)
{
scope.readyValues.push(scope.jsondata[i].Value);
}
Next, just configure the series this way:
// .....chart options
series: [{
name: scope.jsondata[0].Name,
data: scope.readyValues
}
If you have multiple Names in your scope.jsondata then you can use jquery map function or you can make an array for each name.
And since you're using angular I recommend you use Highcharts-ng it's easier ;)

Related

How to retrieve nested JSON values

I have the fallowing JSON object and I want to take the value of Microsoft.VSTS.Scheduling.RemainingWork
[
{
"id": 13,
"rev": 12,
"fields": {
"System.Id": 13,
"Microsoft.VSTS.Scheduling.RemainingWork": 32,
"Microsoft.VSTS.Scheduling.CompletedWork": 20
},
"url": "https://dev.azure.com/.../_apis/wit/workItems/13"
}
]
I am able retrieve data until some point:
console.log("object of json : ",result);
console.log("result[0] : ", result[0])
console.log("result[0].fields : ", result[0].fields)
The console output is,
But I this is not working result[0].fields.Microsoft.VSTS.Scheduling.RemainingWork
You can access data like an associative array :
result[0].fields['Microsoft.VSTS.Scheduling.RemainingWork']
You need to use
result[0].fields["Microsoft.VSTS.Scheduling.RemainingWork"]
Basically when you use
result[0].fields.Microsoft.VSTS.Scheduling.RemainingWork
each time you use a ".", you are trying to get the value from a nested object, like this -
[
{
"id": 13,
"rev": 12,
"fields": {
"System.Id": 13,
"Microsoft": {
"VSTS": {
"Scheduling": {
"RemainingWork": 32
}
}
},
"Microsoft.VSTS.Scheduling.CompletedWork": 20
},
"url": "https://dev.azure.com/.../_apis/wit/workItems/13"
}
]
which is not correct since that is not the way your data is structured.

Normalizr usage for deep nested obj

I am trying to use Normalizr for normalize and denormalize object that I am using within my angular ngrx app.
I found almost the solution for it here:https://www.snip2code.com/Snippet/1028240/Deep-nested-tree-normalize-with-normaliz
import {normalize, Schema, arrayOf} from 'normalizr';
var data = [
{
"id": 1,
"name": "О компании",
"children": [
{
"id": 5,
"name": "Руководство",
"children": [
{
"id": 6,
"name": "Генеральный директор",
"children": [
{
"id": 20,
"name": "Зам гендира"
},
{
"id": 8,
"name": "Секретарша"
}
]
},
{
"id": 7,
"name": "Главный бухгалтер",
"children": [
{
"id": 21,
"name": "Зам главбуха"
}
]
}
]
}
]
},
{
"id": 2,
"name": "Вакансии",
"children": [
{
"id": 9,
"name": "Фронтенд-разработчик (JS)"
},
{
"id": 10,
"name": "Бэкэнд-разработчик (Java)"
},
{
"id": 11,
"name": "Оператор ЭВМ"
}
]
}
];
The thing is that many functions from solution below changed and it is little bit hard for me to use same solution with newer release of normalizr. Maybe some of you know how to rewrite below solution into new normalizr?
var node = new Schema('nodes');
node.define({
children: arrayOf(node)
});
var treeSchema = arrayOf(node);
var normalizedData = normalize(data, treeSchema);
console.log(normalizedData);
You're looking at an example using an outdated version of Normalizr. Try reading the docs for new APIs
I did it :)
const node = new schema.Entity('nodes');
node.define({
children: [node]
})
const treeSchema = [node];
const normalizedData = normalize(this.form,treeSchema);

merge 2 JSON objects in GAS (no jquery)

How can I merge 2 (structurally identical) JSON objects like this (in Google Spreadsheet script)?
{"records":[{"id":28100988,"work_text_reviews_count":13,"average_rating":"3.10"},{"id":10280687,"work_text_reviews_count":80,"average_rating":"3.87"}]}
{"records":[{"id":16135639,"work_text_reviews_count":0,"average_rating":"0.00"},{"id":17978337,"work_text_reviews_count":2414,"average_rating":"3.76"{"id":360721218,"work_text_reviews_count":4924,"average_rating":"3.98"}]}
After merge the result should be:
{"records":[{"id":28100988,"work_text_reviews_count":13,"average_rating":"3.10"},{"id":10280687,"work_text_reviews_count":80,"average_rating":"3.87"},{"id":16135639,"work_text_reviews_count":0,"average_rating":"0.00"},{"id":360721218,"work_text_reviews_count":4924,"average_rating":"3.98"}]}
Since this in Google Spreadsheets I can't use JQuery, and I've spent all day trying javascript solutions which I couldn't get working without errors.
You could use concat:
var json1 = {
"records": [{
"id": 28100988,
"work_text_reviews_count": 13,
"average_rating": "3.10"
}, {
"id": 10280687,
"work_text_reviews_count": 80,
"average_rating": "3.87"
}]
}
var json2 = {
"records": [{
"id": 16135639,
"work_text_reviews_count": 0,
"average_rating": "0.00"
}, {
"id": 17978337,
"work_text_reviews_count": 2414,
"average_rating": "3.76"
}, {
"id": 360721218,
"work_text_reviews_count": 4924,
"average_rating": "3.98"
}]
}
var json3 = json1.records.concat(json2.records);

Gatling: JsonPath extract multiple values

I'm building a gatling 2.1.3 scenario and I need to extract data from a json body.
Example of the body:
[
{
"objectId": "FirstFoo",
"pvrId": "413"
"type": "foo",
"name": "the first name",
"fooabilities": {
"foo1": true,
"foo2": true
},
"versions": [23, 23, 23, 23, 23, 23, 24, 23, 23],
"logo": [
{
"type": "firstlogo",
"width": 780,
"height": 490,
"url": "firstlogos/HD/{resolution}.png"
}
]
},
{
"objectId": "SecondFoo",
"pvrId": "414"
"type": "foo",
"name": "the second name",
"fooabilities": {
"foo1": true,
"foo2": false
},
"versions": [23, 23, 23, 23, 23, 23, 24, 23, 23],
"logo": [
{
"type": "secondlogo",
"width": 780,
"height": 490,
"url": "secondlogos/HD/{resolution}.png"
}
]
}
]
and I have this code trying to extract de data:
exec(
http("get object")
.get(commons.base_url_ws + "/my-resource/2.0/object/")
.headers(commons.headers_ws_session).asJSON
.check(jsonPath("$..*").findAll.saveAs("MY_RESULT"))) (1)
.exec(session => {
foreach("${MY_RESULT}", "result") { (2)
exec(session => {
val result= session("result").as[Map[String, Any]]
val objectId = result("objectId")
val version = result("version")
session.set("MY_RESULT_INFO", session("MY_RESULT_INFO").as[List[(String,Int)]] :+ Tuple2(objectId, version))
})
}
session
})
My goal is:
To extract the objectId and the 9th value from the version array.
I want it to look as Vector -> [(id1, version1),(id2, version2)] in the session to reuse later in another call to the API.
My concerns are:
(1) Is this going to create entries in the session with the complete sub objects? Because in other answers I was that is was always a map that was saved ("id" = [{...}]) and here I do not have ids.
(2) In the logs, I see that the session is loaded with a lot of data, but this foreach is never called. What could cause this ?
My experience in Scala is of a beginner - there may be issues I did not see.
I have looked into this issue: Gatling - Looping through JSON array and it is not exactly answering my case.
I found a way to do it with a regex.
.check(regex("""(?:"objectId"|"version"):"(.*?)",.*?(?:"objectId"|"version"):\[(?:.*?,){9}([0-9]*?),.*?\]""").ofType[(String, String)].findAll saveAs ("OBJECTS")))
I can then use this
foreach("${OBJECTS}", "object") {
exec(
http("Next API call")
.get(commons.base_url_ws + "/my-resource/2.0/foo/${object._1}/${object._2}")
[...]
}

Highcharts - Passing in Color with JSON

Currently, I'm using a back end process to create a JSON file for a 3d bar chart with HighCharts.
[{
"name": "Town",
"data": ["NorthCentral","NorthEast","SouthCentral","SouthEast","West"]
}, {
"name": "Population",
"data": [99.47,82,89,82,82]
}]
What I would like know is - can you pass in colors with each data point? for example:
[{
"name": "Town",
"data": ["NorthCentral","NorthEast","SouthCentral","SouthEast","West"]
}, {
"name": "Population",
"data": [{color: 'red',99.47},82,89,82,82]
}]
I have tried already with no luck. This might not be supported but I thought I would ask.
Try something like
data: [{
name: 'Point 1',
color: '#00FF00',
y: 0
}, {
name: 'Point 2',
color: '#FF00FF',
y: 5
}]
A sample with line chart