d3js, apply force to array of objects - d3.js

Im currently trying to apply a force with d3.js to an array of objects. But the force only gets applied to the last object. The array contains 5 svg elements.
The code for applying the force looks like this.
for (i = 0; i < arr.length; i++) {
var force = d3.layout.force()
.nodes(arr)
.links([])
.size([svgWidth, svgHeight]);
arr[i] = force;
}

Related

Sum of separate years of image collection data within newly defined grid cells in GEE

I am working in the Google Earth Engine Javascript API and have defined grid cells covering my region of interest. It is:
var lat_start = 32.31644;
var lat_end = 37.31914;
var lon_start = 35.61394;
var lon_end = 42.38504;
// 2) Decide no. of (in this case: equally sized) cells
var num_cells = 10;
var lon_edge = (lon_end-lon_start)/Math.sqrt(num_cells);
var lat_edge = (lat_end-lat_start)/Math.sqrt(num_cells);
// 3) Create the grid
var polys = [];
var polys_line = [];
var cell_id = 0;
for (var lon = lon_start; lon < lon_end; lon += lon_edge) {
var x1 = lon;
var x2 = lon + lon_edge;
for (var lat = lat_start; lat < lat_end; lat += lat_edge) {
cell_id = cell_id + 1;
var y1 = lat;
var y2 = lat + lat_edge;
polys.push(ee.Feature(ee.Geometry.Rectangle(x1, y1, x2, y2), {label: cell_id}));
}
}
var grid = ee.FeatureCollection(polys);
I am using Worldpop population estimates data, an Image Collection with years as layers (2000-2016).
var pop = ee.ImageCollection("WorldPop/GP/100m/pop").filterBounds(grid);
My goal is to obtain a feature collection at the grid cell level that identifies the total population in each grid for a given year. I don't think I can use the "reduce" function over the image collection, since that would sum population across years for the cells defined by the image collection (my grid cells are larger).
My approach so far has been to isolate image layers with the hope of piecing them back together in a dataframe. Which so far, for each year, looks like this:
pop_01 = pop.filterDate('2001');
var pop_01_img = pop_01.reduce(ee.Reducer.sum());
var pop_grid_01 = pop_01_img.reduceRegions({collection: grid,reducer:
ee.Reducer.sum(), scale: 6000,});
This results in a series of feature collections and I can use inner joins to merge two feature collections. But how do I merge more? I was working off of the following basic inner join function:
var toyFilter = ee.Filter.equals({
leftField: 'system:index',
rightField: 'system:index'
});
var innerJoin = ee.Join.inner('primary', 'secondary');
var toyJoin = innerJoin.apply(pop_grid_00, pop_grid_01, toyFilter);
If you were trying to complete the task I described above, how would you approach it? Do you think it's most efficient to create the separate images and corresponding feature collections and put them back together, and if yes, how do we conduct inner joins for what would be 10 separate feature collections? Or is there a way for me to calculate the sum within my defined grid cells for each year layer in the image collection? If yes, how?
Thank you!!
I think I figured it out, using information from this blog. The key is to transform the image collection into a stack of images.

Creating Grid in as3

I am trying to make to grid to squares using as3.
I am using nested for loops to create the grid.
import flash.display.Shape;
import flash.display.MovieClip;
var n=10;
var myClip = new MovieClip;
stage.addChild(myClip);
for (var i = 0; i < n * 10; i += 10) {
for (var j = 0; j < n * 10; j += 10) {
var _shape = new Shape;
myClip.addChild(_shape);
_shape.graphics.lineStyle(1);
_shape.graphics.beginFill(0xcccccc);
_shape.graphics.drawRect(i, j, 10, 10);
_shape.graphics.endFill();
}
}
I am applying Zooming and Panning Gestures on myClip. For this Grid size everything works fine. As soon as I increase the value of n, it starts lagging
My game requires a bigger grid. Please help
I'll show some ideas as code, my AS3 is a bit rusty but perhaps it helps.
You can reduce the number of Shape instances or draw the vector based grid graphic to a Bitmap. Since you are zooming and panning it you'll want to smooth it. Depending on performance and how much you zoom you also may want to draw the bitmap at a higher resolution than initially used. See comments in the below.
import flash.display.Shape;
import flash.display.MovieClip;
var n=10;
// do you actually need dynamic binding or a timeline in this?
// if not (also if you don't know what it means) do use the Sprite class instead
var myClip = new MovieClip;
stage.addChild(myClip);
// if you don't need individual shapes move this out of the for loop
// your logic with all of the shapes having the same origin suggests this might be so
var _shape = new Shape;
myClip.addChild(_shape);
for (var i = 0; i < n * 10; i += 10) {
for (var j = 0; j < n * 10; j += 10) {
_shape.graphics.lineStyle(1);
_shape.graphics.beginFill(0xcccccc);
_shape.graphics.drawRect(i, j, 10, 10);
_shape.graphics.endFill();
}
}
// then we can draw the grid to a bitmap
// since you are scaling it I'm drawing it a 300% resolution
var _bmd:BitmapData = new BitmapData(_shape.width*3, _shape.height*3, true, 0);
var _bit:Bitmap = new Bitmap(_bmd);
myClip.addChild(_bit);
_shape.width*=3;
_shape.height*=3;
_bmd.draw(stage);
_bit.smoothing = true;
_bit.width/=3;
_bit.height/=3;
// and remove the vector shape so it won't be rendered
myClip.removeChild(_shape);
// if need be it can be redrawn later

How to use the transform tool to rotate multiple layers?

I have multiple layers that I used for animation.
Lets say I have "1111". Each 1 is a layer. I want each layer rotate 10 degrees.
Is there any way to do these automatically without selecting each one independently.
Thanks.
you can write a script to loop through all the layers in your document and rotate using something similar to below.
var j = 10;
for (var i = 0; i < app.activeDocument.layers.length; i++) {
var lyr = app.activeDocument.layers[i];
lyr.rotate(j)
j = j + 10;
}

jqPlot. Show a subset of a series

I have an array, fullset(24), containing plot data for the last 24h, by the hour. This array i feed to jqPlot to create a bar graph. Works fine. But I want to show only a subset of the data, say the business hours (8-17). I do this, rather clumsily, by creating a new array containing a subset and some additional trickery with the ticks, like so:
var ticks = [];
var subset = [];
for (i = 8; i < 17; i++)
{
subset[i - 8] = fullset[i][1];
ticks.push(sprintf("%d-%d", i, i + 1));
}
But is there a better way? Is it possible to somehow tell jqPlot to show only a subset of the full set?
On the axes settings, I have set a minimum and maximum, but not sure if this will do the same as you are looking for.
axes: {
xaxis: {
min:8,
max:16,
},
},

Flot - display labels on top of stacked bars

I am using the code snippet from this stackoverflow question to label my flot data points. So far this has served me well, but now I have to label the overall values of stacked bars. There are two data series and so far I've managed to calculate the sums, but I can't seem to work out a proper positioning for my labels. I'd like to place them on top of the stacks, but pointOffset only gives me the offsets based on non-stacked bars.
This is the code I am currently using, it places the labels where the second series' data points would be, if the bars weren't stacked, which puts them somewhere in the top bars.
$.each(p.getData()[1].data, function(i, el){
var series0 = p.getData()[0].data;
sum = el[1] + series0[i][2]
var o = p.pointOffset({x: el[0], y: el[1]});
$('<div class="data-point-label">' + sum + '</div>').css( {
position: 'absolute',
left: o.left - 5,
top: o.top ,
display: 'none'
}).appendTo(p.getPlaceholder()).fadeIn('slow');
});
Edit #1: So far I've tried using c2p/p2c, calculating the top value using the single data points' top values and finding more documentation on the stack plugin. I am afraid none of this has helped me much.
Edit #2: I've also tried the code given in this stackoverflow answer but it doesn't work for me. I suspect the author is using some label plugin ...
The solution to put the labels in the top of the bars in stack usinjg canvas is that you have to calculate the xPoint in base of the sum of the values in the complete stack.
Here is a example of code
var sumaArr = [];
for (var u = 0; u < p.getData().length; u++) {
$.each(p.getData()[u].data, function (i, el) {
sumaArr[i] > 0 ? sumaArr[i] = sumaArr[i] + el[1] : sumaArr[i] = el[1];
});
}
var ctx = p.getCanvas().getContext("2d");
var data = p.getData()[p.getData().length - 1].data;
var xaxis = p.getXAxes()[0];
var yaxis = p.getYAxes()[0];
var offset = p.getPlotOffset();
ctx.font = "12px 'Segoe UI'";
ctx.fillStyle = "gray";
for (var i = 0; i < data.length; i++) {
var text = sumaArr[i];
var metrics = ctx.measureText(text);
var xPos = (xaxis.p2c(data[i][0]) + offset.left) - metrics.width / 2;
var yPos = yaxis.p2c(sumaArr[i]) + offset.top - 5;
ctx.fillText(text, xPos, yPos);
}
The var yPos use the sume of the values that make the complete stack.

Resources