Closed curran closed 6 years ago
Related work by @fil http://thebulletin.org/global-nuclear-power-database
The relevant code for my case was:
var labels = series.map(function (d) {
var v = 4,
best = 0,
candidate = null;
for (var i = 0; i < d.length; i++) {
for (var j = d[i][0] + 2; j < d[i][1] - 2; j++) {
var score = 0;
for (var k = 1; k < d.length; k++) {
if (!d[i + k] || !d[i - k] || d[i + k][0] > j - v || d[i + k][1] < j + v || d[i - k][0] > j - v || d[i - k][1] < j + v)
break;
score++;
}
score *= Math.sqrt(1 + j);
if (score >= best) {
candidate = [i, j];
best = score;
}
}
}
if (best > 1) {
return {
year: d[candidate[0]].data.year,
height: candidate[1],
label: d.key
};
}
})
.filter(function (d) {
return !!d;
});
@Fil Thanks a ton for posting that! I will study it.
More related work, from New York Times (HT @1wheel)
I think I've strategized enough. The algorithm is working, and the remaining task is to use Bisection method to get more precision with fewer iterations https://en.wikipedia.org/wiki/Bisection_method
Related to https://github.com/leebyron/streamgraph/issues/3
The algorithm should find the maximum size possible, and position the label in the center of available space.
Prior art:
Stacked area label placement #2 by Noah Veltman
Streamgraph label positions by Noah Veltman
I'd describe the problem as something like this: Given an aspect ratio for a rectangle, and a polygon bounded on the top and bottom by X-monotone curves, find the rectangle of that aspect ratio of maximum size that fits inside of the polygon.
Ideally the solution would also center the rectangle in the available space, but I'm not quite sure how to phrase that bit, geometrically speaking.
Algorithm sketch:
Note that the algorithm should handle timeseries data where intervals are not consistant, such as the data in Syrian Refugees by Settlement Type.