Javascript: Array overriding the same values after assinging to JSON object - json

I am working with jquery ajax & the ajax response is multidimensional json array, I am assinging the JSON values to coordinates array & then assinging coordinates array to new JSON coord_set, after assinging all values to coord_set, It takes last array values to all,
for e.g.
the JSON result contains following values
obj[0]={125, 343, 456, 453},
obj[1]={345, 657, 234, 787},
obj[2]={980, 677, 657, 568}
after assinging to new JSON the values are:
coord_set[0] = {
fillColor : 'rgba(255, 234, 111 ,0.5)',
data : [980, 677, 657, 568]
}
coord_set[1] = {
fillColor : 'rgba(255, 234, 111 ,0.5)',
data : [980, 677, 657, 568]
}
coord_set[2] = {
fillColor : 'rgba(255, 234, 111 ,0.5)',
data : [980, 677, 657, 568]
}
This is my code:
var obj = JSON.parse(data);
for(var j=0;j<obj.length;j++)
{
for (var i=0;i<obj[j].length;i++)
{
coordinates[i] = obj[j][i].how_many;
}
coord_set[j] = { fillColor : 'rgba(255, 234, 111 ,0.5)', data : coordinates };
}
alert(JSON.stringify(coord_set));
Please tell me, If I am doing Anything wrong in my code?

The problem is that you're using a single coordinates array. You keep setting and re-setting the values in that same array, and you keep storing that same array in new elements of coord_set. To fix this, you just need to use a new coordinates array on each pass through the outer loop:
for(var j=0;j<obj.length;j++)
{
coordinates = []; // <----- add this
for (var i=0;i<obj[j].length;i++)

I'm gonna assume that JSON.parse actually works... but since you said you are using jquery, i would use http://api.jquery.com/jQuery.parseJSON/ personally...
Few things about your code:
you never create a new object for coordinates, which is, by default, a global variable in JS. You must type:
var coordinates = []; // before the for (var i = 0; ...
where is this property "how_many" coming from? I don't see it in your first block of code... The code in your i loop should be:
coordinates[i] = obj[j][i];
finally, why do you need a copy of this transient object anyway? That should just do it:
coord_set[j] = { fillColor : 'rgba(255, 234, 111 ,0.5)', data : obj[j] };
// if you change your result set to give a code like that:
obj[0]= [ 125, 343, 456, 453 ] // and not: obj[0]={125, 343, 456, 453}

Related

Google Charts column chart how to change vertical axis scale?

I have a column chart which I am trying to set the vertical axis scale to whole numbers i.e. 0, 1, 2... instead of 0, 0.5, 1...
Using vAxis : {format : '#'} merely rounds up the decimals, leaving duplicates i.e. 0, 1, 1, 2, 2...
Using vAxis : { gridlines : { count : 5 // for example } } does not seem to have an effect.
Ticks look like a solution but my question is what if my chart is to be dynamic? If I don't know what the max number of jobs would be, so as to set the ticks?
The last resort seems to be putting a height constraint on the chart, forcing the v-axis unit to be larger.
Thank you in advance!
to use ticks and still be dynamic,
you can use data table method getComumnRange(columnIndex)
to determine the min and max values of the y-axis values
then use those values to build the ticks array,
see following working snippet...
google.charts.load('current', {
packages:['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['category', 'value'],
['a', 1],
['b', 10],
['c', 4],
['d', 3],
['e', 7]
]);
var yAxisRange = data.getColumnRange(1);
var ticks = [];
for (var i = 0; i <= yAxisRange.max; i++) {
ticks.push(i);
}
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, {
vAxis: {
format: '0',
ticks: ticks
}
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

Restore sectionBox in forge viewer

How to get bounding box info of intersection box in forge viewer?
i want to restore section extension box bounding value to be restored.for that for below method i get box value.
getSelectionbox
now i want to restore that to viewer/set that saved value to viewer for that is there any method is available?
i used setSectionBox(box) but it isn't worked for me.
Thank you
Here is code sample:
viewer.restoreState(currentState, null, false);
viewer.hide(Object.values(val.hiddenNodes));
if(Object.values(val.isolateNodes).length > 0){
viewer.isolate(Object.values(val.isolateNodes));
}
if(val.cutPlanes.length !== 0){
viewer.loadExtension("Autodesk.Section").then(function(sectionTool){
sectionTool.activate(val.sectionStyle);
var sectionTool = markupsExt.tool.getSectionBoxValues();
const sectionbboxmin = new THREE.Vector3(val.sectionBox[0], val.sectionBox[1], val.sectionBox[2]);
const sectionbboxmax = new THREE.Vector3(val.sectionBox[3], val.sectionBox[4], val.sectionBox[5]);
const box = new THREE.Box3(sectionbboxmin,sectionbboxmax);
box.transform = val.sectionBoxTransform;
sectionTool.setSectionBox(box);
});
}
I just tried applying a custom section box to a model in Forge Viewer using this code snippet:
let box = new THREE.Box3(new THREE.Vector3(-100,-100,-100), new THREE.Vector3(100,100,100));
let sectionExt = viewer.getExtension('Autodesk.Section');
sectionExt.setSectionBox(box);
And the result is this:
So this seems to be working ok. Could you provide more details about how you tried to set the section box?
EDIT:
Here's how you can restore the section box from a list of cutplanes (like the one you can obtain from viewer.getState()):
function restoreSectionBox(viewer, cutplanes) {
let box = new THREE.Box3();
for (const cutplane of cutplanes) {
const normal = new THREE.Vector3(cutplane[0], cutplane[1], cutplane[2]);
const offset = cutplane[3];
const pointOnPlane = normal.negate().multiplyScalar(offset);
box.expandByPoint(pointOnPlane);
}
const sectionExt = viewer.getExtension('Autodesk.Section');
sectionExt.setSectionBox(box);
}
// ...
let cutplanes = [
[1, 0, 0, 40],
[0, 1, 0, 50],
[0, 0, 1, 30],
[-1, 0, 0, 60],
[0, -1, 0, 20],
[0, 0, -1, 70]
];
restoreSectionBox(viewer, cutplanes);

Angular 2, split data from Json

Hi I have a json from http request, the server json response is this:
{
"map": {
"03/04": 13,
"05/04": 41,
"06/04": 1,
"12/04": 4,
"14/04": 7,
"18/04": 8,
"19/04": 2,
"22/04": 1,
"25/04": 4
},
"links": []
}
I want to split dates in 1 array and values in other array,
At the end I want :
Data[03/04,05/04,06/04....] and
Val[13,41,1....]
is it possible without difficult implementation?
This could be an approach:
private Data = [];
private Val = [];
for (let key in data) {
this.Data.push(key);
this.Val.push(data[key])
}
let date = Object.keys(JsonRespond.map) // get all keys in map object
let value = [];
date.forEach((key) => {
value.push(JsonRespond.map[key]);
})
you can use this
var a=`{
"map": {
"03/04": 13,
"05/04": 41,
"06/04": 1,
"12/04": 4,
"14/04": 7,
"18/04": 8,
"19/04": 2,
"22/04": 1,
"25/04": 4
},
"links": []
}`
var Data=[];
var val=[]
for(each in a.map){
Data.push(each);
val.push(a.map[each]);
}
Use Object.entries
var dates = [];
var values = [];
var data = Object.entries(yourObj.map);
for (var i in data.length) {
dates.push(data[i][0]);
values.push(data[i][1]);
}

Decode Xml to mxGraph .Net

I want to execute an operation that is an inversion of what i do when forming xmlString in mxGraph().
mxGraph graph = new mxGraph();
Object parent = graph.GetDefaultParent();
graph.Model.BeginUpdate();
try
{
Object v1 = graph.InsertVertex(parent, null, "Hello,", 20, 20, 80, 30);
Object v2 = graph.InsertVertex(parent, null, "World!", 200, 150, 80, 30);
Object e1 = graph.InsertEdge(parent, null, "Edge", v1, v2);
}
finally
{
graph.Model.EndUpdate();
}
mxCodec codec = new mxCodec();
Xml = mxUtils.GetXml(codec.Encode(graph.Model));
var xmlString = mxUtils.GetXml(xml);
I'm trying to execute inverse operation.
XmlDocument doc = mxUtils.ParseXml(xmlString);
mxGraph graphNew = new mxGraph();
var decoder = new mxCodec(doc);
decoder.Decode(doc, graphNew.Model);
Object parentNew = graphNew.GetDefaultParent();
But the object "parentNew" has no children.
this code:
decoder.Decode(doc, graphNew.Model);
should be replaced with this code below:
decoder.Decode(doc.DocumentElement, graphNew.Model);
I just encountered this myself and was able to solve it.
decoder.Decode(doc, graphNew.Model);
should be:
decoder.Decode(doc.DocumentElement, graphNew.getModel());
In addition to doc.DocumentElement, you also need to use graphNew.getModel(), not just graphNew.model.
As per this documentation
Also, you do not need to create a new graph. Insert your existing graph into the second parameter of Decode.

KineticJS object with added property to JSON

I am trying to serialize KineticJS object (with one added property) to JSON, but if I call the JSON.stringify(test);, it returns only JSON representation of KineticJS object without my added property. Does anybody know, where could be a problem, please?
test = new Kinetic.Line(
{
points : [ 29, 30, 31, 32 ],
stroke : 'black',
strokeWidth : 2,
lineJoin : 'round',
id : '#line'
});
test.obj = "my own property";
JSON.stringify(test);
returns
{"attrs":{"points":[{"x":29,"y":30},{"x":31,"y":32}],"stroke":"black","strokeWidth":2,"lineJoin":"round","id":"#line"},
"nodeType":"Shape","shapeType":"Line"}"
But I need also information about test.obj..
You can do this like that :
var test = new Object();
test.kinetic = new Kinetic.Line(
{
points : [ 29, 30, 31, 32 ],
stroke : 'black',
strokeWidth : 2,
lineJoin : 'round',
id : '#line'
});
test.obj = "my own property";
console.log(JSON.stringify(test));