Example usage for org.jfree.chart.entity XYItemEntity getShapeCoords

List of usage examples for org.jfree.chart.entity XYItemEntity getShapeCoords

Introduction

In this page you can find the example usage for org.jfree.chart.entity XYItemEntity getShapeCoords.

Prototype

public String getShapeCoords() 

Source Link

Document

Returns the shape coordinates as a string.

Usage

From source file:probe.com.view.body.quantdatasetsoverview.diseasegroupsfilters.ComparisonsSelectionOverviewBubbleChart.java

private String saveToFile(final JFreeChart chart, final double width, final double height) {
    chart.getXYPlot().setNoDataMessage((int) width + "," + (int) height);
    isNewImge = true;//from   w  ww  .  j a v a 2s  . c  o m
    Set<SquaredDot> set = new TreeSet<SquaredDot>();
    Set<SquaredDot> updatedselectedComponents = new HashSet<SquaredDot>();

    try {
        if (width < 1 || height < 1) {
            return "";
        }

        imageData = ChartUtilities
                .encodeAsPNG(chart.createBufferedImage((int) width, (int) height, chartRenderingInfo));
        chartLayout.removeAllComponents();

        for (int i = 0; i < chartRenderingInfo.getEntityCollection().getEntityCount(); i++) {
            ChartEntity entity = chartRenderingInfo.getEntityCollection().getEntity(i);
            if (entity instanceof XYItemEntity) {
                XYItemEntity catEnt = (XYItemEntity) entity;
                SquaredDot square = new SquaredDot("cycle");
                String[] coords = catEnt.getShapeCoords().split(",");
                int smallX = Integer.MAX_VALUE;
                int largeX = Integer.MIN_VALUE;
                int smallY = Integer.MAX_VALUE;
                int largeY = Integer.MIN_VALUE;
                for (int x = 0; x < coords.length; x++) {
                    String coorX = coords[x++];
                    if (Integer.valueOf(coorX) < smallX) {
                        smallX = Integer.valueOf(coorX);
                    }
                    if (Integer.valueOf(coorX) > largeX) {
                        largeX = Integer.valueOf(coorX);
                    }

                    String coorY = coords[x];
                    if (Integer.valueOf(coorY) < smallY) {
                        smallY = Integer.valueOf(coorY);

                    }
                    if (Integer.valueOf(coorY) > largeY) {
                        largeY = Integer.valueOf(coorY);
                    }

                }
                int sqheight = (largeY - smallY);
                if (sqheight < 2) {
                    continue;
                } else if (sqheight < 14) {
                    smallY = smallY - (14 - sqheight);
                }

                int sqwidth = (largeX - smallX);
                int finalWidth;
                if (sqwidth < 20) {
                    finalWidth = 20;
                    smallX = smallX - ((finalWidth - sqwidth) / 2);

                } else {
                    finalWidth = sqwidth;
                }
                int finalHeight;

                if (sqheight < 20) {
                    finalHeight = 20;
                    if (sqheight < 14) {
                        smallY = smallY - (((finalHeight - sqheight) / 2) - (14 - sqheight));
                    } else {
                        smallY = smallY - ((finalHeight - sqheight) / 2);
                    }

                } else {
                    finalHeight = sqheight;
                }
                square.setWidth((finalWidth + 2) + "px");
                square.setHeight((finalHeight + 2) + "px");
                if (selectedComparisonList == null || selectedComparisonList.isEmpty()) {
                    return "";
                }
                QuantDiseaseGroupsComparison comparison;
                if (userCustomizedComparison != null && catEnt.getSeriesIndex() == 0) {
                    continue;
                } else if (userCustomizedComparison != null && catEnt.getSeriesIndex() == 1) {
                    comparison = userCustomizedComparison;
                } else {
                    comparison = ((QuantDiseaseGroupsComparison) selectedComparisonList
                            .toArray()[catEnt.getSeriesIndex() - 1 - userDataCounter]);
                }

                String header = comparison.getComparisonHeader();
                String updatedHeader = comparison.getComparisonFullName();//header.split(" / ")[0].split("\n")[0] + " / " + header.split(" / ")[1].split("\n")[0] + " - " + header.split(" / ")[1].split("\n")[1].replace("_", " ").replace("-", "'").replace("Disease", "") + "";
                int itemNumber = (int) ((XYItemEntity) entity).getDataset()
                        .getYValue(((XYItemEntity) entity).getSeriesIndex(), ((XYItemEntity) entity).getItem());

                square.setDescription(
                        updatedHeader + "<br/>#Proteins " + (int) tooltipsProtNumberMap.get(header)[itemNumber]
                                + " " + tooltipLabels[itemNumber]);
                double categIndex = (double) itemNumber;
                int seriesIndex = ((XYItemEntity) entity).getSeriesIndex();
                square.setParam("seriesIndex", seriesIndex);
                square.setParam("categIndex", categIndex);

                if (!lastselectedComponents.isEmpty()) {
                    square.unselect();
                    for (SquaredDot lastselectedComponent : lastselectedComponents) {
                        if (lastselectedComponent != null
                                && categIndex == (Double) lastselectedComponent.getParam("categIndex")
                                && seriesIndex == (Integer) lastselectedComponent.getParam("seriesIndex")) {
                            square.select();
                            updatedselectedComponents.add(square);
                            break;
                        }
                    }

                }

                square.setParam("position", "left: " + (smallX - 1) + "px; top: " + (smallY - 1) + "px;");
                set.add(square);
            }

        }
        lastselectedComponents.clear();
        lastselectedComponents.addAll(updatedselectedComponents);
        for (SquaredDot square : set) {
            chartLayout.addComponent(square, square.getParam("position").toString());
        }
        String base64 = Base64.encodeBase64String(imageData);
        base64 = "data:image/png;base64," + base64;
        return base64;
    } catch (IOException e) {
        System.err.println("at error " + e.getMessage());
    }
    return "";
}