/** * Creates a stacked vertical bar chart with default settings. * * @param title the chart title. * @param domainAxisLabel the label for the category axis. * @param rangeAxisLabel the label for the value axis. * @param data the dataset for the chart. * @param legend a flag specifying whether or not a legend is required. * @param tooltips configure chart to generate tool tips? * @param urls configure chart to generate URLs? * @return The chart. */ public static JFreeChart createStackedBarChart( String title, java.awt.Font titleFont, String domainAxisLabel, String rangeAxisLabel, CategoryDataset data, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls, CategoryURLGenerator urlGenerator) { CategoryAxis categoryAxis = new CategoryAxis(domainAxisLabel); ValueAxis valueAxis = new NumberAxis(rangeAxisLabel); // create the renderer... StackedBarRenderer renderer = new StackedBarRenderer(); if (tooltips) { renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); } if (urls) { renderer.setItemURLGenerator(urlGenerator); } CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); plot.setOrientation(orientation); JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); return chart; }
/** Two objects that are equal are required to return the same hashCode. */ public void testHashCode() { StackedBarRenderer r1 = new StackedBarRenderer(); StackedBarRenderer r2 = new StackedBarRenderer(); assertTrue(r1.equals(r2)); int h1 = r1.hashCode(); int h2 = r2.hashCode(); assertEquals(h1, h2); }
private static JFreeChart createChart(CategoryDataset categorydataset, String title) { JFreeChart jfreechart = ChartFactory.createStackedBarChart( title, "分类:区域", "短信数", categorydataset, PlotOrientation.HORIZONTAL, true, true, false); jfreechart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setRangeGridlinePaint(Color.white); categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer(); stackedbarrenderer.setDrawBarOutline(false); stackedbarrenderer.setMaximumBarWidth(0.1); stackedbarrenderer.setItemLabelsVisible(true); return jfreechart; }
/** Check that the equals() method distinguishes all fields. */ public void testEquals() { StackedBarRenderer r1 = new StackedBarRenderer(); StackedBarRenderer r2 = new StackedBarRenderer(); assertTrue(r1.equals(r2)); assertTrue(r2.equals(r1)); r1.setRenderAsPercentages(true); assertFalse(r1.equals(r2)); r2.setRenderAsPercentages(true); assertTrue(r1.equals(r2)); }
/** Confirm that cloning works. */ public void testCloning() { StackedBarRenderer r1 = new StackedBarRenderer(); StackedBarRenderer r2 = null; try { r2 = (StackedBarRenderer) r1.clone(); } catch (CloneNotSupportedException e) { System.err.println("Failed to clone."); } assertTrue(r1 != r2); assertTrue(r1.getClass() == r2.getClass()); assertTrue(r1.equals(r2)); }
public void createChart() { CategoryDataset categorydataset = (CategoryDataset) this.datasetStrategy.getDataset(); report = ChartFactory.createStackedBarChart( this.datasetStrategy.getTitle(), this.datasetStrategy.getYAxisLabel(), this.datasetStrategy.getXAxisLabel(), categorydataset, PlotOrientation.HORIZONTAL, true, true, false); // report.setBackgroundPaint( Color.lightGray ); report.setAntiAlias(false); report.setPadding(new RectangleInsets(5.0d, 5.0d, 5.0d, 5.0d)); CategoryPlot categoryplot = (CategoryPlot) report.getPlot(); categoryplot.setBackgroundPaint(Color.white); categoryplot.setRangeGridlinePaint(Color.lightGray); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); if (datasetStrategy instanceof CloverBarChartStrategy || datasetStrategy instanceof MultiCloverBarChartStrategy) { numberaxis.setRange(0.0D, StackedBarChartRenderer.NUMBER_AXIS_RANGE); numberaxis.setNumberFormatOverride(NumberFormat.getPercentInstance()); } else { numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } numberaxis.setLowerMargin(0.0D); StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer(); stackedbarrenderer.setDrawBarOutline(false); stackedbarrenderer.setItemLabelsVisible(true); stackedbarrenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); stackedbarrenderer.setItemLabelFont( StackedBarRenderer.DEFAULT_VALUE_LABEL_FONT.deriveFont(Font.BOLD)); int height = (categorydataset.getColumnCount() * ChartUtils.STANDARD_BARCHART_ENTRY_HEIGHT * 2) + ChartUtils.STANDARD_BARCHART_ADDITIONAL_HEIGHT + 10; if (height > ChartUtils.MINIMUM_HEIGHT) { super.setHeight(height); } else { super.setHeight(ChartUtils.MINIMUM_HEIGHT); } Paint[] paints = this.datasetStrategy.getPaintColor(); for (int i = 0; i < categorydataset.getRowCount() && i < paints.length; i++) { stackedbarrenderer.setSeriesPaint(i, paints[i]); } }
/** Some checks for the findRangeBounds() method. */ public void testFindRangeBounds() { StackedBarRenderer r = new StackedBarRenderer(); assertNull(r.findRangeBounds(null)); // an empty dataset should return a null range DefaultCategoryDataset dataset = new DefaultCategoryDataset(); assertNull(r.findRangeBounds(dataset)); dataset.addValue(1.0, "R1", "C1"); assertEquals(new Range(0.0, 1.0), r.findRangeBounds(dataset)); dataset.addValue(-2.0, "R1", "C2"); assertEquals(new Range(-2.0, 1.0), r.findRangeBounds(dataset)); dataset.addValue(null, "R1", "C3"); assertEquals(new Range(-2.0, 1.0), r.findRangeBounds(dataset)); dataset.addValue(2.0, "R2", "C1"); assertEquals(new Range(-2.0, 3.0), r.findRangeBounds(dataset)); dataset.addValue(null, "R2", "C2"); assertEquals(new Range(-2.0, 3.0), r.findRangeBounds(dataset)); }
/** * 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; }