123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- /* ===========================================================
- * JFreeChart : a free chart library for the Java(tm) platform
- * ===========================================================
- *
- * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
- *
- * Project Info: http://www.jfree.org/jfreechart/index.html
- *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation; either version 2.1 of the License, or
- * (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
- * License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
- * USA.
- *
- * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
- * Other names may be trademarks of their respective owners.]
- *
- * -------------------------
- * StackedAreaChartTest.java
- * -------------------------
- * (C) Copyright 2005-2013, by Object Refinery Limited.
- *
- * Original Author: David Gilbert (for Object Refinery Limited);
- * Contributor(s): -;
- *
- * Changes:
- * --------
- * 12-Apr-2005 : Version 1 (DG);
- *
- */
- package org.jfree.chart;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.fail;
- import java.awt.Graphics2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.image.BufferedImage;
- import org.jfree.chart.axis.ValueAxis;
- import org.jfree.chart.event.ChartChangeEvent;
- import org.jfree.chart.event.ChartChangeListener;
- import org.jfree.chart.labels.CategoryToolTipGenerator;
- import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
- import org.jfree.chart.plot.CategoryPlot;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.chart.renderer.category.CategoryItemRenderer;
- import org.jfree.chart.urls.CategoryURLGenerator;
- import org.jfree.chart.urls.StandardCategoryURLGenerator;
- import org.jfree.data.Range;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.general.DatasetUtilities;
- import org.junit.Before;
- import org.junit.Test;
- /**
- * Some tests for a stacked area chart.
- */
- public class StackedAreaChartTest {
- /** A chart. */
- private JFreeChart chart;
- /**
- * Common test setup.
- */
- @Before
- public void setUp() {
- this.chart = createChart();
- }
- /**
- * Draws the chart with a null info object to make sure that no exceptions
- * are thrown (a problem that was occurring at one point).
- */
- @Test
- public void testDrawWithNullInfo() {
- try {
- BufferedImage image = new BufferedImage(200 , 100,
- BufferedImage.TYPE_INT_RGB);
- Graphics2D g2 = image.createGraphics();
- this.chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null,
- null);
- g2.dispose();
- }
- catch (Exception e) {
- fail("There should be no exception.");
- }
- }
- /**
- * Replaces the dataset and checks that it has changed as expected.
- */
- @Test
- public void testReplaceDataset() {
- Number[][] data = new Integer[][]
- {{new Integer(-30), new Integer(-20)},
- {new Integer(-10), new Integer(10)},
- {new Integer(20), new Integer(30)}};
- CategoryDataset newData = DatasetUtilities.createCategoryDataset("S",
- "C", data);
- LocalListener l = new LocalListener();
- this.chart.addChangeListener(l);
- CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
- plot.setDataset(newData);
- assertEquals(true, l.flag);
- ValueAxis axis = plot.getRangeAxis();
- Range range = axis.getRange();
- assertTrue("Expecting the lower bound of the range to be around -30: "
- + range.getLowerBound(), range.getLowerBound() <= -30);
- assertTrue("Expecting the upper bound of the range to be around 30: "
- + range.getUpperBound(), range.getUpperBound() >= 30);
- }
- /**
- * Check that setting a tool tip generator for a series does override the
- * default generator.
- */
- @Test
- public void testSetSeriesToolTipGenerator() {
- CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
- CategoryItemRenderer renderer = plot.getRenderer();
- StandardCategoryToolTipGenerator tt
- = new StandardCategoryToolTipGenerator();
- renderer.setSeriesToolTipGenerator(0, tt);
- CategoryToolTipGenerator tt2 = renderer.getToolTipGenerator(0, 0);
- assertTrue(tt2 == tt);
- }
- /**
- * Check that setting a URL generator for a series does override the
- * default generator.
- */
- @Test
- public void testSetSeriesURLGenerator() {
- CategoryPlot plot = (CategoryPlot) this.chart.getPlot();
- CategoryItemRenderer renderer = plot.getRenderer();
- StandardCategoryURLGenerator url1
- = new StandardCategoryURLGenerator();
- renderer.setSeriesItemURLGenerator(0, url1);
- CategoryURLGenerator url2 = renderer.getItemURLGenerator(0, 0);
- assertTrue(url2 == url1);
- }
- /**
- * Create a stacked bar chart with sample data in the range -3 to +3.
- *
- * @return The chart.
- */
- private static JFreeChart createChart() {
- Number[][] data = new Integer[][]
- {{new Integer(-3), new Integer(-2)},
- {new Integer(-1), new Integer(1)},
- {new Integer(2), new Integer(3)}};
- CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S",
- "C", data);
- return ChartFactory.createStackedAreaChart(
- "Stacked Area Chart", // chart title
- "Domain", "Range",
- dataset, // data
- PlotOrientation.HORIZONTAL,
- true, // include legend
- true,
- true
- );
- }
- /**
- * A chart change listener.
- */
- static class LocalListener implements ChartChangeListener {
- /** A flag. */
- private boolean flag = false;
- /**
- * Event handler.
- *
- * @param event the event.
- */
- @Override
- public void chartChanged(ChartChangeEvent event) {
- this.flag = true;
- }
- }
- }
|