use of org.activityinfo.shared.report.content.PieMapMarker in project activityinfo by bedatadriven.
the class ImageMapRenderer method drawPieMarker.
public static void drawPieMarker(Graphics2D g2d, PieMapMarker marker) {
// Determine the total area
Rectangle area = new Rectangle();
area.setRect(marker.getX() - marker.getRadius(), marker.getY() - marker.getRadius(), marker.getRadius() * 2, marker.getRadius() * 2);
// Get total value of all slices
double total = 0.0D;
for (PieMapMarker.SliceValue slice : marker.getSlices()) {
total += slice.getValue();
}
// Draw each pie slice
double curValue = 0.0D;
int startAngle = 0;
int i = 0;
for (PieMapMarker.SliceValue slice : marker.getSlices()) {
// Compute the start and stop angles
startAngle = (int) (curValue * 360 / total);
int arcAngle = (int) (slice.getValue() * 360 / total);
// and last slice
if (i++ == marker.getSlices().size() - 1) {
arcAngle = 360 - startAngle;
}
// Set the color and draw a filled arc
g2d.setColor(bubbleFillColor(ColorUtil.colorFromString(slice.getColor())));
g2d.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
curValue += slice.getValue();
}
if (marker.getLabel() != null) {
drawLabel(g2d, marker);
}
}
use of org.activityinfo.shared.report.content.PieMapMarker in project activityinfo by bedatadriven.
the class PiechartMapLayerGeneratorTest method testSomething.
@Test
public void testSomething() {
SiteDTO siteData = new SiteDTO();
siteData.setId(42);
siteData.setX(15.0);
siteData.setY(0.0);
siteData.setIndicatorValue(1, 50.0);
siteData.setIndicatorValue(2, 10.0);
siteData.setIndicatorValue(3, 20.0);
siteData.setIndicatorValue(4, 40.0);
PiechartMapLayer pcml = new PiechartMapLayer();
pcml.setMinRadius(10);
pcml.setMaxRadius(50);
pcml.addIndicatorId(1);
pcml.addIndicatorId(2);
pcml.addIndicatorId(3);
pcml.addIndicatorId(4);
pcml.setClustering(new NoClustering());
TiledMap map = new TiledMap(500, 600, new AiLatLng(15.0, 0.0), 6);
Map<Integer, Indicator> indicators = Maps.newHashMap();
indicators.put(1, new Indicator());
indicators.put(2, new Indicator());
indicators.put(3, new Indicator());
indicators.put(4, new Indicator());
PiechartLayerGenerator gen = new PiechartLayerGenerator(pcml, indicators);
gen.setSites(Arrays.asList(siteData));
MapContent mc = new MapContent();
gen.generate(map, mc);
assertThat(mc.getMarkers().size(), equalTo(1));
assertThat(((PieMapMarker) mc.getMarkers().get(0)).getSlices().size(), equalTo(4));
}
use of org.activityinfo.shared.report.content.PieMapMarker in project activityinfo by bedatadriven.
the class LeafletMarkerFactory method createPieMapMarker.
public static Marker createPieMapMarker(PieMapMarker marker) {
StringBuilder sb = new StringBuilder();
sb.append("icon?t=piechart&r=").append(marker.getRadius());
for (SliceValue slice : marker.getSlices()) {
sb.append("&value=").append(slice.getValue());
sb.append("&color=").append(slice.getColor());
}
String iconUrl = sb.toString();
int size = marker.getRadius() * 2;
IconOptions iconOptions = new IconOptions();
iconOptions.setIconUrl(iconUrl);
iconOptions.setIconAnchor(new Point(marker.getRadius(), marker.getRadius()));
iconOptions.setIconSize(new Point(size, size));
Options markerOptions = new MarkerOptions();
markerOptions.setProperty("icon", new Icon(iconOptions));
return new Marker(toLatLng(marker), markerOptions);
}
use of org.activityinfo.shared.report.content.PieMapMarker in project activityinfo by bedatadriven.
the class MapIconServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Cache forever
resp.setHeader("Cache-Control", "max-age=31556926, public");
if (req.getParameter("t").equals("bubble")) {
int radius = Integer.parseInt(req.getParameter("r"));
Color color = ColorUtil.colorFromString(req.getParameter("c"));
BufferedImage icon = new BufferedImage(radius * 2, radius * 2, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = icon.createGraphics();
g2d.setPaint(new Color(255, 255, 255, 0));
g2d.fillRect(0, 0, radius * 2, radius * 2);
renderer.drawBubble(g2d, color, radius, radius, radius);
resp.setContentType("image/png");
ImageIO.write(icon, "PNG", resp.getOutputStream());
} else {
if (req.getParameter("t").equals("piechart")) {
int radius = Integer.parseInt(req.getParameter("r"));
BufferedImage icon = new BufferedImage(radius * 2, radius * 2, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = icon.createGraphics();
PieMapMarker pmm = new PieMapMarker();
pmm.setRadius(radius);
pmm.setX(radius);
pmm.setY(radius);
String[] values = req.getParameterValues("value");
String[] colors = req.getParameterValues("color");
if (colors.length != values.length) {
String error = "Expected same amount of colors & values. Amount of Colors: [{0}]. Amount of values: [{1}].";
error = String.format(error, colors.length, values.length);
throw new ServletException(error);
}
for (int i = 0; i < colors.length; i++) {
String color;
double value = 0.0;
color = colors[i];
try {
value = Double.parseDouble(values[i]);
} catch (NumberFormatException e) {
// color = Color.decode(colors[i]).getRGB();
}
SliceValue slice = new SliceValue();
slice.setColor(color);
slice.setValue(value);
pmm.getSlices().add(slice);
}
g2d.setPaint(new Color(255, 255, 255, 0));
g2d.fillRect(0, 0, radius * 2, radius * 2);
renderer.drawPieMarker(g2d, pmm);
resp.setContentType("image/png");
ImageIO.write(icon, "PNG", resp.getOutputStream());
}
}
}
use of org.activityinfo.shared.report.content.PieMapMarker in project activityinfo by bedatadriven.
the class PiechartLayerGenerator method generate.
@Override
public void generate(TiledMap map, MapContent content) {
// create the list of input point values
List<PointValue> points = new ArrayList<PointValue>();
List<PointValue> unmapped = new ArrayList<PointValue>();
// define our symbol scaling
RadiiCalculator radiiCalculator;
if (layer.getScaling() == ScalingType.None || layer.getMinRadius() == layer.getMaxRadius()) {
radiiCalculator = new FixedRadiiCalculator(layer.getMinRadius());
} else if (layer.getScaling() == ScalingType.Graduated) {
radiiCalculator = new GsLogCalculator(layer.getMinRadius(), layer.getMaxRadius());
} else {
radiiCalculator = new FixedRadiiCalculator(layer.getMinRadius());
}
Clusterer clusterer = ClustererFactory.fromClustering(layer.getClustering(), radiiCalculator, new BubbleIntersectionCalculator(layer.getMaxRadius()));
generatePoints(map, layer, clusterer, points, unmapped);
// add unmapped sites
for (PointValue pv : unmapped) {
content.getUnmappedSites().add(pv.getSite().getId());
}
List<Cluster> clusters = clusterer.cluster(map, points);
// create the markers
List<BubbleMapMarker> markers = new ArrayList<BubbleMapMarker>();
for (Cluster cluster : clusters) {
Point px = cluster.getPoint();
AiLatLng latlng = map.fromPixelToLatLng(px);
BubbleMapMarker marker = new PieMapMarker();
sumSlices((PieMapMarker) marker, cluster.getPointValues());
for (PointValue pv : cluster.getPointValues()) {
marker.getSiteIds().add(pv.getSite().getId());
}
marker.setX(px.getX());
marker.setY(px.getY());
marker.setValue(cluster.sumValues());
marker.setRadius((int) cluster.getRadius());
marker.setLat(latlng.getLat());
marker.setLng(latlng.getLng());
marker.setAlpha(layer.getAlpha());
marker.setIndicatorIds(new HashSet<Integer>(layer.getIndicatorIds()));
marker.setClusterAmount(cluster.getPointValues().size());
marker.setClustering(layer.getClustering());
markers.add(marker);
}
// number markers if applicable
if (layer.getLabelSequence() != null) {
numberMarkers(markers);
}
PieChartLegend legend = new PieChartLegend();
legend.setDefinition(layer);
content.getMarkers().addAll(markers);
content.addLegend(legend);
}
Aggregations