Search in sources :

Example 1 with PieMapMarker

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);
    }
}
Also used : PieMapMarker(org.activityinfo.shared.report.content.PieMapMarker) Rectangle(com.google.code.appengine.awt.Rectangle)

Example 2 with PieMapMarker

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));
}
Also used : MapContent(org.activityinfo.shared.report.content.MapContent) NoClustering(org.activityinfo.shared.report.model.clustering.NoClustering) PieMapMarker(org.activityinfo.shared.report.content.PieMapMarker) AiLatLng(org.activityinfo.shared.report.content.AiLatLng) SiteDTO(org.activityinfo.shared.dto.SiteDTO) PiechartMapLayer(org.activityinfo.shared.report.model.layers.PiechartMapLayer) Indicator(org.activityinfo.server.database.hibernate.entity.Indicator) Test(org.junit.Test)

Example 3 with PieMapMarker

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);
}
Also used : IconOptions(org.discotools.gwt.leaflet.client.types.IconOptions) Options(org.discotools.gwt.leaflet.client.Options) MarkerOptions(org.discotools.gwt.leaflet.client.marker.MarkerOptions) MarkerOptions(org.discotools.gwt.leaflet.client.marker.MarkerOptions) SliceValue(org.activityinfo.shared.report.content.PieMapMarker.SliceValue) Point(org.discotools.gwt.leaflet.client.types.Point) MapIcon(org.activityinfo.shared.report.model.MapIcon) Icon(org.discotools.gwt.leaflet.client.types.Icon) MapMarker(org.activityinfo.shared.report.content.MapMarker) IconMapMarker(org.activityinfo.shared.report.content.IconMapMarker) PieMapMarker(org.activityinfo.shared.report.content.PieMapMarker) CircleMarker(org.discotools.gwt.leaflet.client.marker.CircleMarker) Marker(org.discotools.gwt.leaflet.client.marker.Marker) BubbleMapMarker(org.activityinfo.shared.report.content.BubbleMapMarker) Point(org.discotools.gwt.leaflet.client.types.Point) IconOptions(org.discotools.gwt.leaflet.client.types.IconOptions)

Example 4 with PieMapMarker

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());
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) Color(com.google.code.appengine.awt.Color) PieMapMarker(org.activityinfo.shared.report.content.PieMapMarker) SliceValue(org.activityinfo.shared.report.content.PieMapMarker.SliceValue) BufferedImage(com.google.code.appengine.awt.image.BufferedImage) Graphics2D(com.google.code.appengine.awt.Graphics2D)

Example 5 with PieMapMarker

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);
}
Also used : PointValue(org.activityinfo.shared.report.model.PointValue) ArrayList(java.util.ArrayList) Cluster(org.activityinfo.server.report.generator.map.cluster.Cluster) BubbleMapMarker(org.activityinfo.shared.report.content.BubbleMapMarker) Point(org.activityinfo.shared.report.content.Point) PieChartLegend(org.activityinfo.shared.report.content.PieChartLegend) Clusterer(org.activityinfo.server.report.generator.map.cluster.Clusterer) PieMapMarker(org.activityinfo.shared.report.content.PieMapMarker) AiLatLng(org.activityinfo.shared.report.content.AiLatLng)

Aggregations

PieMapMarker (org.activityinfo.shared.report.content.PieMapMarker)9 AiLatLng (org.activityinfo.shared.report.content.AiLatLng)3 SliceValue (org.activityinfo.shared.report.content.PieMapMarker.SliceValue)3 PiechartMapLayer (org.activityinfo.shared.report.model.layers.PiechartMapLayer)3 Color (com.google.code.appengine.awt.Color)2 Graphics2D (com.google.code.appengine.awt.Graphics2D)2 BufferedImage (com.google.code.appengine.awt.image.BufferedImage)2 ArrayList (java.util.ArrayList)2 Indicator (org.activityinfo.server.database.hibernate.entity.Indicator)2 SiteDTO (org.activityinfo.shared.dto.SiteDTO)2 BubbleMapMarker (org.activityinfo.shared.report.content.BubbleMapMarker)2 MapContent (org.activityinfo.shared.report.content.MapContent)2 PointValue (org.activityinfo.shared.report.model.PointValue)2 Test (org.junit.Test)2 Rectangle (com.google.code.appengine.awt.Rectangle)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 ServletException (javax.servlet.ServletException)1 Cluster (org.activityinfo.server.report.generator.map.cluster.Cluster)1