📄 pieplottests.java
字号:
plot1.setLabelGap(0.11); assertFalse(plot1.equals(plot2)); plot2.setLabelGap(0.11); assertTrue(plot1.equals(plot2)); // links visible plot1.setLabelLinksVisible(false); assertFalse(plot1.equals(plot2)); plot2.setLabelLinksVisible(false); assertTrue(plot1.equals(plot2)); // linkMargin plot1.setLabelLinkMargin(0.11); assertFalse(plot1.equals(plot2)); plot2.setLabelLinkMargin(0.11); assertTrue(plot1.equals(plot2)); // labelLinkPaint plot1.setLabelLinkPaint(new GradientPaint(1.0f, 2.0f, Color.magenta, 3.0f, 4.0f, Color.white)); assertFalse(plot1.equals(plot2)); plot2.setLabelLinkPaint(new GradientPaint(1.0f, 2.0f, Color.magenta, 3.0f, 4.0f, Color.white)); assertTrue(plot1.equals(plot2)); // labelLinkStroke plot1.setLabelLinkStroke(new BasicStroke(1.0f)); assertFalse(plot1.equals(plot2)); plot2.setLabelLinkStroke(new BasicStroke(1.0f)); assertTrue(plot1.equals(plot2)); // toolTipGenerator plot1.setToolTipGenerator( new StandardPieToolTipGenerator("{2}{1}{0}") ); assertFalse(plot1.equals(plot2)); plot2.setToolTipGenerator( new StandardPieToolTipGenerator("{2}{1}{0}") ); assertTrue(plot1.equals(plot2)); // urlGenerator plot1.setURLGenerator(new StandardPieURLGenerator("xx")); assertFalse(plot1.equals(plot2)); plot2.setURLGenerator(new StandardPieURLGenerator("xx")); assertTrue(plot1.equals(plot2)); // minimumArcAngleToDraw plot1.setMinimumArcAngleToDraw(1.0); assertFalse(plot1.equals(plot2)); plot2.setMinimumArcAngleToDraw(1.0); assertTrue(plot1.equals(plot2)); // legendItemShape plot1.setLegendItemShape(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0)); assertFalse(plot1.equals(plot2)); plot2.setLegendItemShape(new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0)); assertTrue(plot1.equals(plot2)); // legendLabelGenerator plot1.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( "{0} --> {1}")); assertFalse(plot1.equals(plot2)); plot2.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( "{0} --> {1}")); assertTrue(plot1.equals(plot2)); // legendLabelToolTipGenerator plot1.setLegendLabelToolTipGenerator( new StandardPieSectionLabelGenerator("{0} is {1}")); assertFalse(plot1.equals(plot2)); plot2.setLegendLabelToolTipGenerator( new StandardPieSectionLabelGenerator("{0} is {1}")); assertTrue(plot1.equals(plot2)); // legendLabelURLGenerator plot1.setLegendLabelURLGenerator(new StandardPieURLGenerator( "index.html")); assertFalse(plot1.equals(plot2)); plot2.setLegendLabelURLGenerator(new StandardPieURLGenerator( "index.html")); assertTrue(plot1.equals(plot2)); } /** * Some basic checks for the clone() method. */ public void testCloning() { PiePlot p1 = new PiePlot(); PiePlot p2 = null; try { p2 = (PiePlot) p1.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); } /** * Check cloning of the urlGenerator field. */ public void testCloning_URLGenerator() { CustomPieURLGenerator generator = new CustomPieURLGenerator(); PiePlot p1 = new PiePlot(); p1.setURLGenerator(generator); PiePlot p2 = null; try { p2 = (PiePlot) p1.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); // check that the URL generator has been cloned assertTrue(p1.getURLGenerator() != p2.getURLGenerator()); } /** * Check cloning of the legendItemShape field. */ public void testCloning_LegendItemShape() { Rectangle shape = new Rectangle(-4, -4, 8, 8); PiePlot p1 = new PiePlot(); p1.setLegendItemShape(shape); PiePlot p2 = null; try { p2 = (PiePlot) p1.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); // change the shape and make sure it only affects p1 shape.setRect(1.0, 2.0, 3.0, 4.0); assertFalse(p1.equals(p2)); } /** * Check cloning of the legendLabelGenerator field. */ public void testCloning_LegendLabelGenerator() { StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(); PiePlot p1 = new PiePlot(); p1.setLegendLabelGenerator(generator); PiePlot p2 = null; try { p2 = (PiePlot) p1.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); // change the generator and make sure it only affects p1 generator.getNumberFormat().setMinimumFractionDigits(2); assertFalse(p1.equals(p2)); } /** * Check cloning of the legendLabelToolTipGenerator field. */ public void testCloning_LegendLabelToolTipGenerator() { StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(); PiePlot p1 = new PiePlot(); p1.setLegendLabelToolTipGenerator(generator); PiePlot p2 = null; try { p2 = (PiePlot) p1.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); // change the generator and make sure it only affects p1 generator.getNumberFormat().setMinimumFractionDigits(2); assertFalse(p1.equals(p2)); } /** * Check cloning of the legendLabelURLGenerator field. */ public void testCloning_LegendLabelURLGenerator() { CustomPieURLGenerator generator = new CustomPieURLGenerator(); PiePlot p1 = new PiePlot(); p1.setLegendLabelURLGenerator(generator); PiePlot p2 = null; try { p2 = (PiePlot) p1.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } assertTrue(p1 != p2); assertTrue(p1.getClass() == p2.getClass()); assertTrue(p1.equals(p2)); // check that the URL generator has been cloned assertTrue(p1.getLegendLabelURLGenerator() != p2.getLegendLabelURLGenerator()); } /** * Serialize an instance, restore it, and check for equality. */ public void testSerialization() { PiePlot p1 = new PiePlot(null); PiePlot p2 = null; try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(buffer); out.writeObject(p1); out.close(); ObjectInput in = new ObjectInputStream(new ByteArrayInputStream( buffer.toByteArray())); p2 = (PiePlot) in.readObject(); in.close(); } catch (Exception e) { e.printStackTrace(); } assertEquals(p1, p2); } /** * Some checks for the getLegendItems() method. */ public void testGetLegendItems() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("Item 1", 1.0); dataset.setValue("Item 2", 2.0); dataset.setValue("Item 3", 0.0); dataset.setValue("Item 4", null); PiePlot plot = new PiePlot(dataset); plot.setIgnoreNullValues(false); plot.setIgnoreZeroValues(false); LegendItemCollection items = plot.getLegendItems(); assertEquals(4, items.getItemCount()); // check that null items are ignored if requested plot.setIgnoreNullValues(true); items = plot.getLegendItems(); assertEquals(3, items.getItemCount()); // check that zero items are ignored if requested plot.setIgnoreZeroValues(true); items = plot.getLegendItems(); assertEquals(2, items.getItemCount()); // check that negative items are always ignored dataset.setValue("Item 5", -1.0); items = plot.getLegendItems(); assertEquals(2, items.getItemCount()); } /** * Check that the default base section paint is not null, and that you * can never set it to null. */ public void testGetBaseSectionPaint() { PiePlot plot = new PiePlot(); assertNotNull(plot.getBaseSectionPaint()); boolean pass = false; try { plot.setBaseSectionPaint(null); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); } static class NullLegendLabelGenerator implements PieSectionLabelGenerator { public AttributedString generateAttributedSectionLabel( PieDataset dataset, Comparable key) { return null; } public String generateSectionLabel(PieDataset dataset, Comparable key) { return null; } } /** * Draws a pie chart where the label generator returns null. */ public void testDrawWithNullLegendLabels() { DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("L1", 12.0); dataset.setValue("L2", 11.0); JFreeChart chart = ChartFactory.createPieChart("Test", dataset, true, false, false); PiePlot plot = (PiePlot) chart.getPlot(); plot.setLegendLabelGenerator(new NullLegendLabelGenerator()); boolean success = false; try { BufferedImage image = new BufferedImage(200 , 100, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null); g2.dispose(); success = true; } catch (Exception e) { success = false; } assertTrue(success); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -