Say that I have an unknown number of items (but not a large number - say maybe up to 30 or 40 max) that I want to place on a map. The items all belong on the same spot, because their location is vague (e.g. “Texas”) and so they go to the center point of that location. But I don’t want them to be on top of each other, so I want to offset them slightly.
What I want to do is to place them such that they are as close together in an orderly fashion, i.e a square grid. So if I had three they would look like this:
Whereas if there are 10, it would look like this:
Is there a way to calculate where each one should go if I only know how many I have already placed, not how many I will be placing total? Alternately, I could go with a spiral, or circles.
I could find out beforehand how many items I have, but that’s more overhead, and where would be the fun in that?
The typical way that’s handled in real mapping apps is to define some theshold where points which overlap beyond the threshold are combined and a single icon is placed on the centroid spot. Then the user can zoom in to expand the presentation until the points are no longer overlapped beyond the threshold. At which time teh points begin to render out as individual points.
The threshold is, or ought to be, scale sensitive. In effect it’s a pixel-based threshold, not a lat/long or mileage-based threshold. In real serious apps, we introduce some hysteresis into the thresholding function so we don’t get jittery behavior as folks run the zoom up & down.
Optionally, the app can create some kind of hover-over or click action on the combined icon which causes it to explode into some better representation of its constituents.
One thing you want to try to avoid when building a map application is simply placing dots at the centroid of a large area, e.g. Texas. The nature of a point is that it has an implied precision. e.g. It is at [+35.1234, -95.5432]. It is not at “Texas”.
If you have only have area data, use an area representation. For example, color each state a different hue, and use saturation to indicate hit count within the state. e.g. 40 hits for Texas makes all of Texas bright yellow, while 3 hits in in New Mexico makes it very pale green.
See http://www.esri.com/software/mapit/whats-new.html for a picture which shows the idea. Looking at their demos & showcases will give you an idea of how the pros do it. No, I don’t work for them. But my firm does use their stuff.
Using your example of 10 dots, where the dots populate in a somewhat triangular pattern, a formula to determine the number of columns and rows would look like this:
wolframalpha format:
[x=10, Ceiling[Sqrt[x],1], max(Ceiling[Sqrt[x-1],1],1)](http://www.wolframalpha.com/input/?i=x%3D10%2C+Ceiling%5BSqrt%5Bx%5D%2C1%5D%2C+max%28Ceiling%5BSqrt%5Bx-1%5D%2C1%5D%2C1%29)
Set **x** equal to **10** and the formulas yield **4** columns and **3** rows which matches your example.
You can experiment with different values of x to see how the rows and column counts change.
You can easily adapt the formula for MS Excel by replacing the square brackets with parentheses. You also need to put each formula separated by commas into separate cells.
If you want to know the exact number of dots for each row (how many dots are placed on row #1, and how many dots are placed on row #2, etc), you need another formula which lets you solve for a particular row #. I ran out of time this morning before doing this. However, the formula given so far will at least give you the maximum boundary of the dots – this might be good enough.
EDIT: Btw, Wolframalpha also has extensive graphing and visualization functions to actually plot the dots but I’m not knowledgeable enough to give examples.