I would like to use dynamic data to inject into the pie. I have been doing this so far : https://codepen.io/Surunairdopera/pen/gOPgKrv
I have before this code a CSV file which I transform into a JSON file by using Papa parse. I create a json object composed of the 5 data I want to use in my pie. But highcharts doesn't accept this data.
$(document).ready(function() {
let file = "X.csv";
Papa.parse(file, {
download: true,
header: true,
delimiter:";",
complete: function(results) {
let dataDetail = results.data;
objJSONDetail = dataDetail.find(element => element.ticket == ticket);
let objJSONX = {
Donnee1: objJSONDetail.donnee1,
Donne2: objJSONDetail.donnee2,
Donnee3: objJSONDetail.donnee3,
Donnee4: objJSONDetail.donnee4,
Donnee5: objJSONDetail.donne5
}
console.log(objJSONX['Donnee1']);//return the data, i would like to use for the pie
}
I'm new at using Highcharts so, be nice ;)
UPDATE
I found the reason why :
I wrote variable as STRING. Highcharts expects numerical values. So it works with a parseFloat :
parseFloat(objJSONDetail.donnee1);
Related
I have a database with lots of document and i am using field type to define it as a table. I want to populate angularjs ui-grid with the JSON data coming in value. So i have created a view:
function(doc) {
if(doc.type === 'userTable'){
emit(doc._id, {userName:doc.userName,fName:doc.fName});
}
}
When i hit the url http://127.0.0.1:5984/rpt_db/_design/Dsgn_userjson/_view/Vw_userjson/ it gives me :
{"total_rows":1,"offset":0,"rows":[
{"id":"f43a147cd5c961fcfe4e1da1b800013a",
"key":"f43a147cd5c961fcfe4e1da1b800013a",
"value":{"userName":"rp670249","fName":"Ranjeeth"}}
]}
Now in my client code i need to do a loop and make data as JSON.
result.rows.forEach(function(item) {
var temp = { "id": item.value.userName, usrName: item.value.fName};
console.log(temp);
//Update data of grid using temp variable.
});
Is there an easy way so that i can directly use what's coming as part of couchdb JSON output which can be used as is in angular ui-grid.
You can refer column values using json dot notation in the columnDefs. Since your actual values are inside "value". you can create column definition like this,
$scope.myData = {"total_rows":1,"offset":0,"rows":[
{"id":"f43a147cd5c961fcfe4e1da1b800013a",
"key":"f43a147cd5c961fcfe4e1da1b800013a",
"value":{"userName":"rp670249","fName":"Ranjeeth"}}
]}
$scope.gridOptions = {
data : $scope.myData.rows,
columnDefs : [
{name:"id",field:"value.userName"},
{name:"usrName",field:"value.fName"}
]
};
Here's a working plnkr.
http://plnkr.co/edit/juLZeT6I0LOlek4QnMSP?p=preview
I have a rest service for which I am sending the Json data as ["1","2","3"](list of strings) which is working fine in firefox rest client plugin, but while sending the data in application the structure is {"0":"1","1":"2","2":"3"} format, and I am not able to pass the data, how to convert the {"0":"1","1":"2","2":"3"} to ["1","2","3"] so that I can send the data through application, any help would be greatly appreciated.
If the format of the json is { "index" : "value" }, is what I'm seeing in {"0":"1","1":"2","2":"3"}, then we can take advantage of that information and you can do this:
var myObj = {"0":"1","1":"2","2":"3"};
var convertToList = function(object) {
var i = 0;
var list = [];
while(object.hasOwnProperty(i)) { // check if value exists for index i
list.push(object[i]); // add value into list
i++; // increment index
}
return list;
};
var result = convertToList(myObj); // result: ["1", "2", "3"]
See fiddle: http://jsfiddle.net/amyamy86/NzudC/
Use a fake index to "iterate" through the list. Keep in mind that this won't work if there is a break in the indices, can't be this: {"0":"1","2":"3"}
You need to parse out the json back into a javascript object. There are parsing tools in the later iterations of dojo as one of the other contributors already pointed out, however most browsers support JSON.parse(), which is defined in ECMA-262 5th Edition (the specification that JS is based on). Its usage is:
var str = your_incoming_json_string,
// here is the line ...
obj = JSON.parse(string);
// DEBUG: pump it out to console to see what it looks like
a.forEach(function(entry) {
console.log(entry);
});
For the browsers that don't support JSON.parse() you can implement it using json2.js, but since you are actually using dojo, then dojo.fromJson() is your way to go. Dojo takes care of browser independence for you.
var str = your_incoming_json_string,
// here is the line ...
obj = dojo.fromJson(str);
// DEBUG: pump it out to console to see what it looks like
a.forEach(function(entry) {
console.log(entry);
});
If you're using an AMD version of Dojo then you will need to go back to the Dojo documentation and look at dojo/_base/json examples on the dojo.fromJson page.
I want use highstock to show my data in the web ,but i have someproblem with the json data,
the Example of basic-line :http://www.highcharts.com/stock/demo/basic-line
http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/stock/demo/basic-line/
i change the javascript file the
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data)
to the
$.getJSON('data.json', function(data)
i want use the json data in my web folder and i put data in the json file like :
[[1143072000000,60.16],
[1143158400000,59.96],
[1143417600000,59.51],
[1143504000000,58.71],
[1143590400000,62.33],
[1143676800000,62.75],
[1143763200000,62.72],
/* Apr 2006 */
[1144022400000,62.65],
[1144108800000,61.17],
[1144195200000,67.21],
[1144281600000,71.24]]
but i can see nothing in my web, what is wrong with me? is it the json data Format wrong?or others thanks for give me some help
In case when you use JSON, then you should get rid of all comments.
So JSON should looks like:
[[1143072000000,60.16],
[1143158400000,59.96],
[1143417600000,59.51],
[1143504000000,58.71],
[1143590400000,62.33],
[1143676800000,62.75],
[1143763200000,62.72],
[1144022400000,62.65],
[1144108800000,61.17],
[1144195200000,67.21],
[1144281600000,71.24]]
and chart code:
$.getJSON('data.json', function(data) {
window.chart = new Highcharts.StockChart({
chart : {
renderTo : 'container'
},
rangeSelector : {
selected : 1
},
series : [{
name : 'AAPL',
data : data
}]
});
});
Rename your file to data.js . For javascript file and not JSON. It's like that for me :)
I have the JSON data received from the server as:
[{"Text":"TRUCKLOAD","Spend":32323348.4},
{"Text":"NON-SYNDICATED:QUALITATIVE & QUANTITATIVE","Spend":23270306.54},
{"Text":"SAMPLING & EVENTERVICES","Spend":18924795.75},
{"Text":"OTHER LOGISTICS","Spend":18353919.01},
{"Text":"CONSTRUCTION AND INSTALLATION","Spend":13248733.26},
{"Text":"SECURITY SERVICES","Spend":9210534.97},
{"Text":"TAXES","Spend":8661964.99}]
Can anybody help me to bind this data dynamically to a piechart?
Another problem is that the highchart requires me to change the labels as Name and a y tag of JSON instead of the Text and Spend tag in my JSON. How do i change my JSON for that? I have scoured the net for the same but all examples only seem to show the binding of JSON from an external file.
You can see demos' source here and then click view options button.The best way to do it is format your json on your backend before you receive it.
But if you want to do it, you can use the following code:
var json = [{
"name":"TRUCKLOAD",
"y":32323348.4
}, {
"name":"NON-SYNDICATED:QUALITATIVE & QUANTITATIVE",
"y":23270306.54
}, {
"name":"SAMPLING & EVENTERVICES",
"y":18924795.75
}];
var data = [];
for(var i in json) {
var serie = new Array(json[i].name, json[i].y);
data.push(serie);
}
Demo
I just working with JSON data and am playing around with jQuery and Ajax requests. Pretty basic stuff, but here's my problem.
I have a basic data set which I was using for time tracking. I know how to parse the simple JSON data like this:
{
"end" : "1/18/2011",
"start" : "1/18/2011",
"task" : "Code Review",
},
It's the more complicated stuff I'm trying to parse like this where I'm trying to pull the "time" data out.
{
"end" : "1/17/2011",
"start" : "1/17/2011",
"task" : "Exclusive Brands",
"time" : {
"analysis" : 4,
"documentation" : 3,
"meetings" : 2
}
This is the code for the script I've been using to parse the simple data:
$(function() {
$('.load').click(function(){
$.getJSON("data.js",function(data){
$.each(data.timesheet, function(i,data){
var div_data ="<div class='box'>"+data.start+" "+data.task+"</div>";
$(div_data).appendTo("#time-tracking");
});
}
);
return false;
});
});
My question is what's the format to parse the time data, or what's the best way to parse the information nested inside the time element?
Any help will be greatly appreciated.
A JSON string will be parsed into an object. When parsed, the time is the key of one object. You could retrieve the value of this object through the dot operator (.).
data = JSON.parse('{"end":"1/17/2011", "start":"1/17/2011", "task":"Exclusive Brands", "time": {"analysis":4, "documentation":3, "meetings":2 } }')
// => obj
data.time.analysis
// => 4
In your case similarly you could use the data.time.meetings to access your data from remote server.
Unless I am terribly mistaken, since jquery already converted data into a javascript for you, you should be able to access time as if it was a javascript object like so:
var analysis = data.time.analysis;
var documentation = data.time.documentation;
var meetings = data.time.meetings;
etc...