Parse key- value as weighted bubble output - javascript

I want to show key/value pairs (of social relationship data count) in bubble using D3.js or something similar.
Here goes my output from REST client:
{"data":{"sister":3,"General":130,"mother":3,"guest":1,"brother":10,"general":1,"father":4},"messages":[]}
And here goes the Jquery code for the same. I am getting keys in 'arr' and values in 'values' in the following code. All I need to output the data in visual form as bubble as per weight, example, 'General' count is 130, so it should have biggest bubble. like that.
$(document).on("click", "#socialGraph", function(){
var id="4501905311187190264";
$.ajax({
url: "/getSocialGraph/"+id,
type: "GET",
contentType: "application/json",
dataType: "json",
success: function(data){
var arr=Object.keys(data.data);
var value=[];
for(var i in arr){
value.push(data.data[arr[i]]);
}

Related

Adding JSON results within quotation marks in a dataset

I am trying to the JSON values from a JQuery Ajax request into a dataset. The problem is that the dataset is in quotation marks and will not accept my parsed JSON values.
This is the original dataset:
{"value":"B", "number":"1492"},
{"value":"C", "number":"2782"},
{"value":"D", "number":"4253"},
{"value":"E", "number":"2702"}
I need for it to be accept the parsed JSON from the JQuery call.
$(document).ready(function(){
$("#ButtonClick").click(function(){
$.ajax({
url: "https://myUrl.com",
type: "get",
dataType: "json",
success: function(data) {
{"value":data.name, "number": data.currency},
{"value":data.nameA, "number":data.currencyA},
{"value":data.nameB, "number":data.currencyB},
{"value":data.nameC, "number":data.CurrencyC}
How can I go about doing this?

Trouble retrieving list values in SharePoint

So I have a list with the default title field and a custom number field I named "Hours". I want to be able to display the value of both fields on a page. Displaying them is the easy part, but I am having trouble retrieving them.
I am using this code:
var json = $.ajax({
url: "[URL]/_api/web/lists/getbytitle([LIST NAME)/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
})
I can then navigate the JSON to get to the results where it shows the information for each item in the list, but I can only find the "Title" field, not the "Hours" field. Also, I tried it on a separate site and was able to get the "Hours" data, but it showed up with an odd name (d.results[i].o0ci). I am not really sure what is going on here, so any help would be appreciated. Thanks!
First you should check the internal name of your column, let say that i have this structure on sharepoint:
Title | Hours | Has Free Time
well internally sharepoint tends to change the name of those columns to
Title | Hours | Has_x0020_Free_x0020_Time
or some times if you create the column but you delete it, this might happend:
Title | Hours1 | 0o13
So try this code to see your internal names:
$.ajax({
url: "[URL]/_api/web/lists/getbytitle([LIST NAME)/items",
type: 'GET',
headers: { "Accept": "application/json; odata=verbose" },
dataType: 'JSON',
success: function (data){
$.each(data.d.results, function (index, value) {
console.info(value) // You should see all sharepoint fields so you can check the correct fieldname of hours
console.info("Title: ", value.Title);
console.info("Hours: ", value.Hours);
}
}
});

Pass Dictionary<String,List<String>> to java script

I would like to pass a Dictionary> to client using JavaScript.
I did look at this post and I didn't understand exactly how to proceed.
In case I'm doing something wrong I'll explain what I want to do.
The dictionary contains the 'name' key of all worksheets in the Excel file, and the 'value' is the column value of the first row in that worksheet.
The UI of the client should have two "drop list", the first will contain the key which is all the names of the worksheet in the Excel file.
The second contain all the column value of the first row of the worksheets that will choose in the first drop list – which is actually a List as the value in the dictionary.
So all the back end C# code is working fine. Now I need help in the front end JavaScript.
How do I parse the data to a key value so I can do a "search" on the keys as the client chooses some "key" in the first drop list so I can get back the relevant values as a list?
Thanx!
var ajaxRequest = $.ajax({
type: "POST",
url: "http://localhost:1894/api/Values",
contentType: false,
processData: false,
data: data,
success: function(dataTest) {
}
});
This is the JSON that I get back from the server:
{"First":["Name","Link","User","Pass"],"Sec":["test01"]}
How would I perform a search on this like in C#? I want to be able to do something like this: "dict.TryGetValue(key, out value); and the out value would return as an array of string or as a List.
Try this(you don't need var ajaxRequest variable you can directly call like this:
$.ajax({
type: "POST",
url: "http://localhost:1894/api/Values",
dataType: "json",
data: data,
success: function(dataTest) {
//dataTest should contain your data in a javascript object
for(var i = 0; i < dataTest.First.length; i++)
{
window.alert("" + dataTest.First[i]);
}
for(var i = 0; i < dataTest.Sec.length; i++)
{
window.alert("" + dataTest.Sec[i]);
}
//etc...
//this should print the values returned if you showed me exactly how your JSON is...
}
});
The javascript object will contain properties with an array as the value for each property. Think of it like a map of <String, String[]>. So your returned object dataTest will have properties First and Sec and for First the value associated with the key First will be ["Name","Link","User","Pass"] which is just an array. Same for Sec. So `dataTest.First[0] will equal "Name" and dataTest.First[1] will equal "Link" etc...
*****************************************UPDATE**************************************
You can save your dataTest to a global variable in this example (myObject) then you can access like this:
var key = "First";
// Or if you want to get your key from a dropdown (select) element then you could do like this:
var key = document.getElementById("myselect").options[document.getElementById("myselect").selectedIndex].innerHTML;
if(myObject[key] != undefined)
{
//That means there is values for this key.
//Loop through values or do whatever you want with myObject[key].
for(var i = 0; i < myObject[key].length; i++)
{
window.alert("" + myObject[key][i]);
}
}

Split Json data into multiple data using Jquery

$.ajax({
type: 'POST',
url: 'url',
data: val,
async: false,
dataType: 'json',
success: function (max) {
console.log(max.origin);
}
});
Then i am getting an output which is
["test,origin"]
I want to split it like .
test
origin
Please suggest me .
Much appreciated
If your returned value of max.origin really is ["test,origin"], you have to do something like this
var data = max.origin[0].split(',');
so that data will contain a list of the elements that are comma-separated. However, if your max.origin is in the form of ["test", "origin"], you can simply select each/all items by going through a for-loop and (optionally) print them out:
for (var i = 0; i <= max.origin.length; i++) {
console.log(max.origin[i]);
}
If you know that you only get the two elements (test and origin) each time, #void's answer is a good approach.
max.origin[0].split(",")[0] will give you test
max.origin[0].split(",")[1] will give you origin

javascript convert array to object for send data by ajax

I would like to send a list of data by ajax. So I push all data in a 2d array
But the ajax of jquery not accept the array data, the data must be object or query string
since the Object of javascript is no push function, I must use array to build the list of data.
Is there any function in jquery or javascript, let me
var countLine=$("line").length;
var lines=$("line");
var lineArr=new Array();
var linesArr=new Array();
var x1, y1, x2, y2;
for(i=0; i<countLine; i++)
{
lineArr['x1']=lines[i].getAttributeNS(null, "x1");
lineArr['y1']=lines[i].getAttributeNS(null, "y1");
lineArr['x2']=lines[i].getAttributeNS(null, "x2");
lineArr['y2']=lines[i].getAttributeNS(null, "y2");
linesArr.push(lineArr);
}
$.ajax({
type: "POST",
url: "test.php",
data: linesArr,
async: true,
cache: false,
success: function(data){
$("#txt").text(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("fail");
}
});
Change
data: linesArr
to
data: { linesArr: linesArr }
This will send an object with a single property, "linesArr", which contains the value of your array.
EDIT: you have a bigger problem. You are trying to store non-numeric properties in an array, as shown in this line:
lineArr['x1']=lines[i].getAttributeNS(null, "x1");
You are also reusing the same lineArr "array" each time through the loop. The following code would work much better:
var lines = $("line");
var linesArray = [];
for (var i = 0; i < lines.length; ++i) {
var line = lines[i];
linesArray.push({
x1: line.getAttributeNS(null, "x1"),
y1: line.getAttributeNS(null, "y1"),
x2: line.getAttributeNS(null, "x2"),
y2: line.getAttributeNS(null, "y2")
});
}
Then you would access your data like $_POST["linesArr"][0]["x1"].
You need to convert the object to a JSON string using JSON.stringify().
http://json.org/

Categories