Highcharts;
I'm trying to change categories dynamically in a column chart with a json. Something like this...
[{y:1,"other":"other","category":"namecategory"}....]
..
xAxis: {
categories: this.category,
maxZoom: 1
},
Is it possible?
Thanks. :D
You can dynamically load your categoreis i.e by json or other and then use setCategories which allows to change categoreis, dynamically.
Ok, maybe you can try it something like this:
var jsonData = [{
"y": "1",
"other": "other",
"category": "namecategory1"
}, {
"y": "2",
"other": "another",
"category": "namecategory2"
}];
// var jsObject = JSON.parse(jsonData);
var categories = new Array();
for (var p in jsonData) {
categories.push(jsonData[p].category);
}
chart.xAxis[0].setCategories(categories);
I think you need to set categories as an Array of primitive js types such as number or string.
Example:
categories: ['Apples', 'Bananas', 'Oranges']
As it is mentioned here: http://api.highcharts.com/highcharts#xAxis.categories :
"If categories are present for the xAxis, names are used instead of numbers for that axis."
Maybe, when you mention JSON are you talking about Object Literals? (because "y" key in your sample is not quoted.)
In fact , you CAN use an Array of Object Literals. But they will be displayed as "[Object]" in the xAxis.
An object literal is a pair of curly braces surrounding zero or more name/value
pairs. An object literal can appear anywhere an expression can appear:
var empty_object = {};
var stooge = {
first_name: "Jerome",
last_name: "Howard"
};
The quotes around a property’s name in an object literal are optional if the name would be a legal
JavaScript name and not a reserved word.
Maybe you can pre-process your JSON data an build an Array to be used in categories.
Related
This is the json data structure used in flutter
flutter: {
"members": [
{
"firstName": "Michael"
},
{
"firstName": "Jennifer"
},
{
"firstName": "Lisa"
}
]
}
To access the above data I used the below code
var parsedData = json.decode(state.successResponse);
var name = parsedData['members'];
I am not able to access the inner field of firstName in the above json.
How should I do that?
Since members is JSON Array you can convert it to dart List type and then you can operate on the list (adding, removing, accessing elements by index).
You can do something like this -
List members = parsedData['members'];
//You can access members by index
print(members[1]); //prints {firstName: Jennifer}
print(members[1]['firstName']); //prints Jennifer
//Or you can iterate over the list
members.forEach((member){
print(member['firstName']);
});
Hope this helps!
{
"result": [
{
"id": "a258377906705d889422fd0b41c324b8",
"coordinate": {
"London": {
"x": 65.565709,
"y": 98.931235
},
"New_York": {
"x": 37.59751,
"y": 47.448718
}
}
}
]
}
If I have a json like the above one,
how can I loop to get the x,y coordinate and if i get more data to get, how can I get it?
If I want to also get the London and New York to add to an array list, how can I do it (cannot directly add the name because there are more than two data)?
You can parse it and then use it using JSON.parse( insert json here ).
Basically what it does is that it takes your JSON string and converts it into a usable JSON.
For adding new objects into another object, I recommend using Object.assign().
You can refer the details here: Link
Here is an example given from the site:
var o1 = { a: 1 };
var o2 = { b: 2 };
var o3 = { c: 3 };
var obj = Object.assign(o1, o2, o3);
console.log(obj); // { a: 1, b: 2, c: 3 }
console.log(o1); // { a: 1, b: 2, c: 3 }, target object itself is changed.
Object.assign() takes a target object as the first parameter and modifies it with the additional parameters that we're provided.
In your case, you can update your object like this:
var countryCoord = JSON.parse(yourjsondata); // parse the json that you included
Object.assign(countryCoord, newCoord, newCoord2,...etc); //this would update countryCoord alone
Online parser: Link
You have to handle the json and understand where you are getting the JSONObject and where you are getting the JSONArray, On basis of that you can parse the JSON accordingly.
Please refer the link for more information on parsing json object with nested items in Java:
Retrieving nested arrays values with java
How do i convert json text file to..
var nested_obj = { pic: "jaedongImage", name: "ananth", team: "evil geniuses", server: "NA" };
My text file as below..
{
"data": [
{
"pic": "jaedongImage,"
"name": "ananth",
"team": "evil geniuses",
"server": "N/A"
}
]
}
It's unclear what you're asking, but: The JSON depicts an object with one property, data, which refers to an array. The array has one entry, which is the object you wanted. From your initial code sample, it looks like you're using JavaScript, so: Assuming you're receiving text (a string) in JSON format, you would parse it using JSON.parse, and then get at that object via data[0]:
var obj = JSON.parse(text);
var pic = obj.data[0];
[
{
"type": "spline",
"name": "W dor\u0119czeniu",
"color": "rgba(128,179,236,1)",
"mystring": 599,
"data": ...
}
]
I am trying to access this json as json['W doręczeniu']['mysting'], and I get no value why is that?
You're trying to access the index "W doręczeniu" but that's not an index it's a value. Also, what you seem to have is an array of JSON objects.
The [ at the start marks the array, the first element of which is your JSON object. The JSON obj begins with the {
You're also trying to use a [ ], but JSON values are accessed with the dot operator.
I'm not sure which index you're actually trying to access, but try something like this:
var x = json[0].mystring;
The value of "W doręczeniu" is not a key, so you cannot use it to get a value. Since your json string is an array you'll have to do json[0].nameto access the first (and only) element in the array, which happens to be the object. Of course, this is assuming json is the variable you store the array into.
var json = [{"type":"spline","name":"W dor\u0119czeniu","color":"rgba(128,179,236,1)","mystring":599}];
console.log(json[0].mystring); //should give you what you want.
EDIT:
To get the last element in a js array, you can simply do this:
console.log( json[json.length -1].mystring ); // same output as the previous example
'length - 1' because js arrays are indexed at 0. There's probably a million and one ways to dynamically get the array element you want, which are out of the scope of this question.
I have json data in the following format
{"updates":
{"message" :"[[student:123]] is present."},
"references":[{"type":"student","full_name":"XYZ","id":123}]
}
How can I map the student name to the message using the id present over the message?
I am relatively new to JSON parsing. I am currently using EJS template to manipulate the JSON into HTML.
In that just using
<%alert(updates.message.student)%>
returns "undefined". Please help.
updates.message is a string, not a JavaScript object. You can tell by the quotes around the whole attribute. JavaScript strings don't have a student property, so you are getting undefined. You can parse out the JSON part from the string with regular expressions and then use JSON.parse() to get the JSON object. However, the student id is also in updates.references[0].id in your example.
To get the student ID, do this:
<% alert(updates.references[0].id) %>
edit: If you are really want to get the id out of the message, you need to parse it out somehow. If the message format will always be the same, you can try a regular expression or string splitting to get the part containing the id.
var id_part = json.updates.message.split(" ")[0];
//parse out just the ID number in a group
var re = /\[\[[^:]+:(\d+)\]\]/;
var matches = re.exec(id_part);
var id = matches[1];
To then get the corresponding data out of the references part, you need to loop through until you find one with the id from the message. This would work.
//Ghetto old for loop for browser compatibility
for (var i = 0; i < updates.references.length; i++) {
if (updates.references[i].id == id) {
//We have found the reference we want.
//Do stuff with that reference.
break;
}
}
try
var json = {
"updates": {
"message": "[[student:123]] is present."
},
"references": [
{
"type": "student",
"full_name": "XYZ",
"id": 123
}
]
};
alert(json.references[0].full_name);