Built a JSON from string pieces - extjs

I work with a line chart in ExtJS4 now. The chart is based on the data of the store. The store change its data with help 'loadRawData()' function.
Familiar situation, isn't it?
AJAX sends strings every 10 seconds and I need to built JSON from this pieces of strings. I'm trying:
success: function(response) {
var requestMassive = JSON.parse(response.responseText);
var jArray = [];
for(var i=0;i<requestMassive.length;i++){
var firstPiece = JSON.parse(response.responseText)[i].date;
var secondPiece = JSON.parse(response.responseText)[i].connectCount;
var recording = "{'machinesPlayed':"+firstPiece+", 'machinesOnline':"+secondPiece+"}";
jArray.push(recording);
}
jArray = '['+jArray+']';
store.loadRawData(jArray);
}
But it is wrong way. How to do it properly?

You could use loadData() function instead of loadRawData(). loadData() only needs an array of objects.
success: function(response) {
var requestMassive = JSON.parse(response.responseText);
var jArray = [];
for(var i=0;i<requestMassive.length;i++){
jArray.push({ machinesPlayed: requestMassive[i].date, machinesOnline: requestMassive[i].connectCount});
}
store.loadData(jArray);
}

I didnt get what you are trying to achieve.But it can be formed this way.Try this out.
var recording = {
"machinesPlayed" : firstPiece,
"machinesOnline" : secondPiece
}
jArray.push(recording);
OR
jArray.push({
"machinesPlayed" : firstPiece,
"machinesOnline" : secondPiece
});

Related

Issues with AnyGantt

Im using AnyGantt, but Im having problems setting it up correctly.
Here is the full code:
var endpoint = '/api/chart/data/'
var label = []
var start = []
var end = []
var werk = []
$.ajax({
method: 'GET',
url: endpoint,
success: function(data){
labels = data.label
start = data.start
end = data.end
uplant = data.werk
var obj = {}
var finalArray = []
for (var i = 1; i <= start.length; i++) {
var first = { id: i, name: uplant[i] }
obj = { ...obj, ...first }
var periods = { id: labels[i], start: start[i - 1], end: end[i - 1] }
if (obj.periods) {
obj.periods.push(periods)
} else {
obj.periods = [periods]
}
finalArray.push(obj)
}
anychart.onDocumentReady(function () {
var data = finalArray;
var treeData = anychart.data.tree(data, "asTable");
chart = anychart.ganttResource();
chart.data(treeData);
chart.getTimeline().scale().minimum("2018-01-01");
chart.getTimeline().scale().maximum("2020-01-01");
var dataGrid = chart.dataGrid();
dataGrid.column(0)
.title('#')
.width(30)
.cellTextSettings({hAlign: 'center'});
dataGrid.column(1)
.title('Werk')
.width(60)
.cellTextSettings({hAlign: 'left'})
.format(function () {
return this.name;
});
chart.getTimeline().horizontalScrollBar().enabled(true);
/* chart.getTimeline().periods().edit(true); */
chart.getTimeline().edit(true);
chart.getTimeline().tooltip(false);
chart.getTimeline().elements().labels(false);
chart.container("containerx");
chart.draw();
chart.fitAll();
});
},
error:function(error_data){
console.log("error")
console.log(error_data)
}});
I can't click and move the periods (tasks) and I cant scroll.
Thank you very much for any suggestions
Please find below a screenshot of the chart:
Please find below a screenshot of the chart:
Scale range
You are applying the scale min/max correctly:
chart.getTimeline().scale().minimum("2018-01-01");
chart.getTimeline().scale().maximum("2020-01-01");
But then you override the default scale and it drops the min/max settings:
var dateTimeScale = anychart.scales.dateTime();
var dateTimeTicks = dateTimeScale.ticks();
chart.xScale(dateTimeScale);
There's no need to do this! The default Gantt Chart is already a dateTime type. Solution - simply remove those three lines above from your code.
LiveEdit
Your line chart.getTimeline().periods().edit(true); is correct, but due to a little bug in the current version 8.7.0 (Aug 2019) this setter doesn't work for types of elements. To avoid it replace this line with the following line chart.getTimeline().edit(true);.
Scrollers
The Gantt chart doesn't support x/yScrollers, only simple scroll bars.
The following methods the Gantt chart doesn't support:
chart.xScroller(true);
chart.yScroller(true);
For zooming, you can use one of the API functions. This subject is described in detail in the Gantt zooming article.

Adding Markers to Google map from json Array

This is my poor code
function loaddata() {
var url = "http://localhost/Geocording/api.php";
$.getJSON(url, function (data) {
var json = data
for (var i = 0, length = json.length; i < length; i++) {
var val = json[i],
var latLng = new google.maps.LatLng(val.lat, val.lng);
console.log(latLng)
}
});
}
Im trying to get details from my own api using json array.
but its not working.
{"location":[{"name":"Home 1","lat":"6.824367","lng":"80.034523","type":"1"},{"name":"Grid Tower 1","lat":"6.82371292","lng":"80.03451942","type":"1"},{"name":"Power Station A","lat":"6.82291793","lng":"80.03417451","type":"1"}],"success":1}
This is json response from my api.php
Try to make things clear first then apply it. First read JSON clearly then go on to apply it in your code. This is the working code.
function loaddata() {
var url = "http://localhost/Geocording/api.php";
$.getJSON(url, function (data) {
var json = data['location'];
for (var i = 0, length = json.length; i < length; i++) {
var val = json[i];
var latLng = new google.maps.LatLng(val['lat'], val['lng']);
console.log(latLng)
}
});
}
Hope this may help you!

iterating through json getting empty value

Not able to push values out. Please help!!
function pullJSON(data) {
var url="https://www.eventbriteapi.com/v3/subcategories/?event_status=live&token=XXXXXXXXXXX&page_count=4"; // Paste your JSON URL here
var response = UrlFetchApp.fetch(url); // get feed
var data = JSON.parse(response.getContentText()); //
var dict = JSON.stringify(data);
//Logger.log(dict);
var keys = [];
for(var k in dict) keys.push(k+':'+dict[k]);
Logger.log(keys);
}
Here is the result: [17-03-29 11:41:19:033 EDT] []
JSON data
When you use JSON.stringify(), it will return a string. It looks like you want to have an object, not a string.
Try using this instead of your for loop:
var keys = Object.keys(data).map(function(key){ return key + ':' + data[key]});

sending multiple array in multiple array in Angular js

How to send this data in AngularJS. (It's a multiple array in a multiple array which need to send on one Object)
[{
"working_day":"sunday",
"from_time":{"hour":"9","min":"30"},
"to_time":{"hour":"6","min":"30"}
},{
"working_day":"monday",
"from_time":{"hour":"9","min":"30"},
"to_time":{"hour":"6","min":"30"}... and so on for other week days
}]
I am trying to send data like this in an API and I am new to AngularJs, so please tell me how can I make this data through HTML at runtime?
$scope.schedule = []; // Hard coded value
$scope.week = {};
$scope.week.working_day = "Sunday";
$scope.week.from_time = {};
$scope.week.from_time.min = "10:00";
$scope.week.from_time.max = "5:00";
$scope.week.to_time = {};
$scope.week.to_time.min = "2:10";
$scope.week.to_time.max = "8:00";
var dataParam = {
"prefix":$scope.data1.prefix,
"first_name":$scope.data1.first_name,
"password":$scope.data1.password,
"last_name":$scope.data1.last_name,
"email_id":$scope.data1.email_id,
"mobile_number":$scope.data1.mobile_number,
"roleCode":[$scope.data1.roleCode],
"role":[$scope.data1.role],
"Schedule":angular.toJson($scope.schedule.push($scope.week))
}
console.log(angular.toJson(dataParam));
/* $http({
url: "/here",
method: "POST",
headers :{'Content-Type': 'application/json','Accept': 'application/json' },
data: dataParam
}) .success(function(response) {
if(response.status_code=="success")
{
$scope.successmsg = response.status_message;
console.log(angular.toJson(response));
$state.go('dashboard.setting.user', {'user': $scope.viewUser});
}
else {
$scope.successmsg = response.status_code;
}
}); */
};
for now i just need to send this data format with angular js, please suggest
this is what i am doing in angular by "hard code" i need to send this data in angular js, how can i do that?
now please help me with html code of angular to send that hard coded value into runtime value by user
Have an array
$scope.schedule = [];
Create an object with your Json keys
$scope.week = {};
$scope.week.working_day = "";
$scope.week.from_time = {};
$scope.week.from_time.min = "";
$scope.week.from_time.max = "";
$scope.week.to_time = {};
$scope.week.to_time.min = "";
$scope.week.to_time.max = "";
Push it into array
$scope.schedule.push($scope.week);

Multiple Queries with Parse Cloud Code Using Promises

I have two questions:
Is the below example the right way to execute multiple Parse queries in a single Cloud Code function?
Is the below example going to provide all the data I'm querying with one HTTP request (when I call logbookEntries) and then count as two Parse requests on my account because it's two Parse queries?
Here's the code:
Parse.Cloud.define("logbookEntries", function(request, response) {
//::: Query 1 :::
var firstQuery = new Parse.Query("Logbook");
var returnData = [];
firstQuery.find().then(function(firstResults) {
returnData[0] = firstResults;
}).then(function(result) {
//::: Query 2 :::
var secondQuery = new Parse.Query("Logbook");
secondQuery.find().then(function(secondResults))
returnData[1] = secondResults;
}).then(function(result) {
response.success(returnData);
}, function(error) {
response.error(error);
});
});
Thanks in advance.
It's one way, though not quite correct.
Yes
Your code should really be:
Parse.Cloud.define("logbookEntries", function(request, response) {
//::: Query 1 :::
var firstQuery = new Parse.Query("Logbook");
var returnData = [];
firstQuery.find().then(function(firstResults) {
returnData[0] = firstResults;
var secondQuery = new Parse.Query("Logbook");
return secondQuery.find();
}).then(function(result) {
returnData[1] = result;
response.success(returnData);
}, function(error) {
response.error(error);
});
});
Or, a better way to structure it would be:
Parse.Cloud.define("logbookEntries", function(request, response) {
var firstQuery = new Parse.Query("Logbook");
var secondQuery = new Parse.Query("Logbook");
var promises = [];
promises.push(firstQuery.find());
promises.push(secondQuery.find());
Parse.Promise.when(promises).then(function(result1, result2) {
var returnData = [];
returnData[1] = result1;
returnData[2] = result2;
response.success(returnData);
}, function(error) {
response.error(error);
});
}
Just to update Wain's structured code:
Promise.when returns array when supplied with an array, so the correct code would be
Parse.Promise.when(promises).then(function([result1, result2]) {
and since there is no need to repack the array, it would simply be
Parse.Promise.when(promises).then(function(result) {
response.success(result);
See here for more info.

Resources