private void saveChart(final DefaultCategoryDataset dataset) throws IOException { final JFreeChart chart = ChartFactory.createBarChart( "HtmlUnit implemented properties and methods for " + browserVersion_.getNickname(), "Objects", "Count", dataset, PlotOrientation.HORIZONTAL, true, true, false); final CategoryPlot plot = (CategoryPlot) chart.getPlot(); final NumberAxis axis = (NumberAxis) plot.getRangeAxis(); axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); final LayeredBarRenderer renderer = new LayeredBarRenderer(); plot.setRenderer(renderer); plot.setRowRenderingOrder(SortOrder.DESCENDING); renderer.setSeriesPaint(0, new GradientPaint(0, 0, Color.green, 0, 0, new Color(0, 64, 0))); renderer.setSeriesPaint(1, new GradientPaint(0, 0, Color.blue, 0, 0, new Color(0, 0, 64))); renderer.setSeriesPaint(2, new GradientPaint(0, 0, Color.red, 0, 0, new Color(64, 0, 0))); ImageIO.write( chart.createBufferedImage(1200, 2400), "png", new File( getArtifactsDirectory() + "/properties-" + browserVersion_.getNickname() + ".png")); }
/** * Returns a sequence plot as a ChartPanel. * * @param aProteinSequencePanelParent the protein sequence panel parent * @param sparklineDataset the dataset * @param proteinAnnotations the protein annotations * @param addReferenceLine if true, a reference line is added * @param allowZooming if true, the user can zoom in the created plot/chart * @return a sequence plot */ public ChartPanel getSequencePlot( ProteinSequencePanelParent aProteinSequencePanelParent, JSparklinesDataset sparklineDataset, HashMap<Integer, ArrayList<ResidueAnnotation>> proteinAnnotations, boolean addReferenceLine, boolean allowZooming) { this.proteinSequencePanelParent = aProteinSequencePanelParent; DefaultCategoryDataset barChartDataset = new DefaultCategoryDataset(); StackedBarRenderer renderer = new StackedBarRenderer(); renderer.setShadowVisible(false); CategoryToolTipGenerator myTooltips = new ProteinAnnotations(proteinAnnotations); // add the data for (int i = 0; i < sparklineDataset.getData().size(); i++) { JSparklinesDataSeries sparklineDataSeries = sparklineDataset.getData().get(i); for (int j = 0; j < sparklineDataSeries.getData().size(); j++) { barChartDataset.addValue(sparklineDataSeries.getData().get(j), "" + i, "" + j); renderer.setSeriesPaint(i, sparklineDataSeries.getSeriesColor()); renderer.setSeriesToolTipGenerator(i, myTooltips); } } // create the chart JFreeChart chart = ChartFactory.createStackedBarChart( null, null, null, barChartDataset, PlotOrientation.HORIZONTAL, false, false, false); // fine tune the chart properites CategoryPlot plot = chart.getCategoryPlot(); // remove space before/after the domain axis plot.getDomainAxis().setUpperMargin(0); plot.getDomainAxis().setLowerMargin(0); // remove space before/after the range axis plot.getRangeAxis().setUpperMargin(0); plot.getRangeAxis().setLowerMargin(0); renderer.setRenderAsPercentages(true); renderer.setBaseToolTipGenerator(new IntervalCategoryToolTipGenerator()); // add the dataset to the plot plot.setDataset(barChartDataset); // hide unwanted chart details plot.getRangeAxis().setVisible(false); plot.getDomainAxis().setVisible(false); plot.setRangeGridlinesVisible(false); plot.setDomainGridlinesVisible(false); // add a reference line in the middle of the dataset if (addReferenceLine) { DefaultCategoryDataset referenceLineDataset = new DefaultCategoryDataset(); referenceLineDataset.addValue(1.0, "A", "B"); plot.setDataset(1, referenceLineDataset); LayeredBarRenderer referenceLineRenderer = new LayeredBarRenderer(); referenceLineRenderer.setSeriesBarWidth(0, referenceLineWidth); referenceLineRenderer.setSeriesFillPaint(0, referenceLineColor); referenceLineRenderer.setSeriesPaint(0, referenceLineColor); plot.setRenderer(1, referenceLineRenderer); } // set up the chart renderer plot.setRenderer(0, renderer); // hide the outline chart.getPlot().setOutlineVisible(false); // make sure the background is the same as the panel chart.getPlot().setBackgroundPaint(backgroundColor); chart.setBackgroundPaint(backgroundColor); final HashMap<Integer, ArrayList<ResidueAnnotation>> blockTooltips = proteinAnnotations; // create the chart panel ChartPanel chartPanel = new ChartPanel(chart); chartPanel.addChartMouseListener( new ChartMouseListener() { @Override public void chartMouseClicked(ChartMouseEvent cme) { if (cme.getEntity() != null && cme.getTrigger().getButton() == MouseEvent.BUTTON1) { ((CategoryItemEntity) cme.getEntity()).getDataset(); Integer blockNumber = new Integer((String) ((CategoryItemEntity) cme.getEntity()).getRowKey()); ArrayList<ResidueAnnotation> annotation = blockTooltips.get(blockNumber); if (annotation != null) { proteinSequencePanelParent.annotationClicked(annotation, cme); } } } @Override public void chartMouseMoved(ChartMouseEvent cme) { cme.getTrigger() .getComponent() .setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); if (cme.getEntity() != null && cme.getEntity() instanceof CategoryItemEntity) { ((CategoryItemEntity) cme.getEntity()).getDataset(); Integer blockNumber = new Integer((String) ((CategoryItemEntity) cme.getEntity()).getRowKey()); ArrayList<ResidueAnnotation> annotation = blockTooltips.get(blockNumber); if (annotation != null && !annotation.isEmpty()) { if (blockTooltips.get(blockNumber).get(0).isClickable()) { cme.getTrigger() .getComponent() .setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); } } } } }); if (!allowZooming) { chartPanel.setPopupMenu(null); chartPanel.setRangeZoomable(false); } chartPanel.setBackground(Color.WHITE); return chartPanel; }