Required help in JSON - javascript

I have this below json data.
[{"start_date":"2014-06-27","count":"21","value":"134"},{"start_date":"2014-06-28","count":"17","value":"120"},{"start_date":"2014-06-29","count":"21","value":"138"},{"start_date":"2014-06-30","count":"9","value":"121"},{"start_date":"2014-07-01","count":"12","value":"112"},{"start_date":"2014-07-02","count":"19","value":"132"}]
I trying to plot this a line chart using highcharts.
I wanted to convert my data to something like this.
[{
name: "count",
data: [21, 17 .......]
},{
name: "value",
data: [134, 120, .......]
}]
How can I format my data like that using jquery?
I use AJAX to get that data from a database.
Any help will be very helpful.
Thanks in advance.

USE
var res= YOUR JSON DATA
var data=$.parseJSON(res);
var count= new Array();
var val=new Array();
$(data).each(function(i,u){
count.push(u.count);
val.push(u.value);
});
now you can use
[{
name: "count",
data: count
},{
name: "value",
data: val
}]

var array = [{"start_date":"2014-06-27","count":"21","value":"134"},{"start_date":"2014-06-28","count":"17","value":"120"},{"start_date":"2014-06-29","count":"21","value":"138"},{"start_date":"2014-06-30","count":"9","value":"121"},{"start_date":"2014-07-01","count":"12","value":"112"},{"start_date":"2014-07-02","count":"19","value":"132"}]
var chartOptions = [];
var countData = {name: "count", data: []};
var valueData = {name: "value", data: []};
array.forEach(function(value, index){
countData.data.push(value.count);
valueData.data.push(value.value);
});
chartOptions.push(countData);
chartOptions.push(valueData);

I would build the JSON structure required by highcharts server side, before returning it in the AJAX call. There simply adding it directly to highcharts.
This will be more efficient then letting the users browser make the formating.

Related

How can I loop through an array with razor and javascript together ASP.net

I am currently using ASP.NET MVC, I am using javascript to create a graph that represents data in my models.
The Javascript is expecting the data in this format:
series: [{
name: 'Bench',
data: [
10, 20, 30
]
}],
However, I am trying to use razor to loop through my array in the data field as so:
series: [{
name: 'Bench',
data: [
#for (int j = 0; j < numArry.Length; j++)
{
numArry[j] + ",";
}
]
}],
My issue is that the comma that separates my integers is not accepted by razor syntax. I need to comma to be repeated after every array integer otherwise the graph will only contain the last integer in the array.
series: [{
name: 'Bench',
data: [
#numArry[0]
,
#numArry[1]
,
#numArry[2]
,
#numArry[3]
]
}],
This is my current solution that does work however, I can enter every element of the array manually.
Try it following ways..
Create Model with same structure and serialize it to JSON
Move the for loop outside of the series and declare a variable before the series code as below to get comma separated value.
# {
var str = String.Join(",", numArry);
}
series: [{
name: 'Bench',
data: [ { # {str } ]
}],

Javascript How to push item in object

var data = {items: [
{id: "1", name: "Snatch", type: "crime"}
]};
And I would like to add the mark's key.
So the result would be:
var data = {items: [
{id: "1", name: "Snatch", type: "crime", mark:"10"}
]};
How can I do ?
I tried to do data.items.push({"mark": "10"}) but it adds another object which is not what I want.
Thanks.
Access the correct index and simply set the property
data.items[0].mark = "10";
You may not need push here because you want to create a new key to n existig object. Here you need dot (.) to create a new key
var data = {
items: [{
id: "1",
name: "Snatch",
type: "crime"
}]
};
data.items[0].mark = "10";
console.log(data)
And, if you want add “mark” property to all the items:
data.items.forEach(function(item, index) {
data.items[index].mark = 10;
}

How to merge or push to an $.param array?

I have made a serialized array/ object using:
var data = $.param([{name: "commentID", value: commentID}, {name: "comment", value: comment}])
Then later on before my Ajax request I need to add another $.param array, how can I do this? I tried $.merge but it messes up the array?
You can do something like this,
var data = $.param([{name: "commentID", value: commentID}, {name: "comment", value: comment}]);
var newData = $.param([{name: "newID", value: newID}]);
data = data + "&" + newData;
Another way to do is have an array and use $.param before ajax call,
var data = [{name: "commentID", value: commentID}, {name: "comment", value: comment}];
data.push({name: "newID", value: newID});
and at the end
data = $.param(data);

How to map model to table when the structure is array based?

I have my data as following:
{
meta: {
format: "csv",
info: "desc",
columns: [
{
id: "Name",
type: "Text",
length: 32
},
{
id: "Text",
type: "Text",
length: 128
}]
},
rows: [
["John","xxxx"],
["Alpha","yyyy"],
["Beta","wwww"],
["Gamma","zzzz"]]
}
Now, I am struggling to map the records to a Table control as Columns and Rows. Column seems straight forward, straight map, but the rows since lacks a mapping to column I wonder what could be the simplest way.
Approach Steps:
Make a keys[] from column.id of each columns record.
Traverse the rows[]
Each loop, while keys.length create an object as {keys[j]:row[k]}
Push to an array
Recreate the original JSON but replace Rows arrays with Objects
I am really struggling to translate this into code specially at the rows[] parsing and creating Objects. Is there, I am sure there must be, an efficient way to achieve this.
Here is what you could do. using Array.map and forEach.
var input = {
meta: {
format: "csv",
info: "desc",
columns: [{
id: "Name",
type: "Text",
length: 32
}, {
id: "Text",
type: "Text",
length: 128
}]
},
rows: [
["John", "xxxx"],
["Alpha", "yyyy"],
["Beta", "wwww"],
["Gamma", "zzzz"]
]
};
var columns = input.meta.columns.map((column) => {
return column.id
});
var rows = input.rows.map((row) => {
var obj = {};
row.forEach((column, idx) => {
obj[columns[idx]] = column;
});
return obj;
});
input.rows = rows;
console.log(input);

Filtering column's collection

In my Webix datatable one column fetches the data from the DataColletion. The problem is in the column's filtering: seems like it works with the original data (that contains the ID) and ignores the value of the data collection. How can I change this behavior and filter datatable by the collection's value?
Data:
var mycollection = new webix.DataCollection({
data:[{
id: '12',
value: 'CollItem 1'
}]
});
var mydata = [{
id: 1,
name: 'Item 1',
troublesomeColumn: '12' // id of the CollItem 1
}];
Config:
columns:[{
id: 'troublesomeColumn',
collection: mycollection,
header:{
content:"textFilter"
}
}],
data:mydata
Code snippet. Thanks in advance.
Filters work with the dataset, not with the templates or values from the linked collections. Therefore, you need to create a custom filtering rule as described in the Webix Docs, i.e. define the needed pattern in the compareproperty of the filter:
{
content:"textFilter",
compare:function(value, filter, obj){
var colValue = mycollection.getItem(value).value;
toFilter = colValue.toString().toLowerCase();
filter = filter.toString().toLowerCase();
return toFilter.indexOf(filter) !== -1;
}
}
Updated snippet

Categories

Resources