1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/* ===========================================================
* 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.]
*
* -----------------------------------
* CombinedDomainCategoryPlotTest.java
* -----------------------------------
* (C) Copyright 2003-2013, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 19-Aug-2003 : Version 1 (DG);
* 03-Jan-2008 : Added testNotification() (DG);
*
*/
package org.jfree.chart.plot;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.List;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.TestUtilities;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.junit.Test;
/**
* Tests for the {@link CombinedDomainCategoryPlot} class.
*/
public class CombinedDomainCategoryPlotTest implements ChartChangeListener {
/** A list of the events received. */
private List events = new java.util.ArrayList();
/**
* Receives a chart change event.
*
* @param event the event.
*/
@Override
public void chartChanged(ChartChangeEvent event) {
this.events.add(event);
}
/**
* This is a test to replicate the bug report 987080.
*/
@Test
public void testRemoveSubplot() {
CombinedDomainCategoryPlot plot = new CombinedDomainCategoryPlot();
CategoryPlot plot1 = new CategoryPlot();
CategoryPlot plot2 = new CategoryPlot();
plot.add(plot1);
plot.add(plot2);
// remove plot2, but plot1 is removed instead
plot.remove(plot2);
List plots = plot.getSubplots();
assertTrue(plots.get(0) == plot1);
assertEquals(1, plots.size());
}
/**
* Some checks for the equals() method.
*/
@Test
public void testEquals() {
CombinedDomainCategoryPlot plot1 = createPlot();
CombinedDomainCategoryPlot plot2 = createPlot();
assertTrue(plot1.equals(plot2));
}
/**
* Some checks for cloning.
*/
@Test
public void testCloning() throws CloneNotSupportedException {
CombinedDomainCategoryPlot plot1 = createPlot();
CombinedDomainCategoryPlot plot2 = (CombinedDomainCategoryPlot)
plot1.clone();
assertTrue(plot1 != plot2);
assertTrue(plot1.getClass() == plot2.getClass());
assertTrue(plot1.equals(plot2));
}
/**
* Serialize an instance, restore it, and check for equality.
*/
@Test
public void testSerialization() {
CombinedDomainCategoryPlot plot1 = createPlot();
CombinedDomainCategoryPlot plot2 = (CombinedDomainCategoryPlot)
TestUtilities.serialised(plot1);
assertEquals(plot1, plot2);
}
/**
* Check that only one chart change event is generated by a change to a
* subplot.
*/
@Test
public void testNotification() {
CombinedDomainCategoryPlot plot = createPlot();
JFreeChart chart = new JFreeChart(plot);
chart.addChangeListener(this);
CategoryPlot subplot1 = (CategoryPlot) plot.getSubplots().get(0);
NumberAxis yAxis = (NumberAxis) subplot1.getRangeAxis();
yAxis.setAutoRangeIncludesZero(!yAxis.getAutoRangeIncludesZero());
assertEquals(1, this.events.size());
// a redraw should NOT trigger another change event
BufferedImage image = new BufferedImage(200, 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
this.events.clear();
chart.draw(g2, new Rectangle2D.Double(0.0, 0.0, 200.0, 100.0));
assertTrue(this.events.isEmpty());
}
/**
* Creates a dataset.
*
* @return A dataset.
*/
public CategoryDataset createDataset1() {
DefaultCategoryDataset result = new DefaultCategoryDataset();
// row keys...
String series1 = "First";
String series2 = "Second";
// column keys...
String type1 = "Type 1";
String type2 = "Type 2";
String type3 = "Type 3";
String type4 = "Type 4";
String type5 = "Type 5";
String type6 = "Type 6";
String type7 = "Type 7";
String type8 = "Type 8";
result.addValue(1.0, series1, type1);
result.addValue(4.0, series1, type2);
result.addValue(3.0, series1, type3);
result.addValue(5.0, series1, type4);
result.addValue(5.0, series1, type5);
result.addValue(7.0, series1, type6);
result.addValue(7.0, series1, type7);
result.addValue(8.0, series1, type8);
result.addValue(5.0, series2, type1);
result.addValue(7.0, series2, type2);
result.addValue(6.0, series2, type3);
result.addValue(8.0, series2, type4);
result.addValue(4.0, series2, type5);
result.addValue(4.0, series2, type6);
result.addValue(2.0, series2, type7);
result.addValue(1.0, series2, type8);
return result;
}
/**
* Creates a dataset.
*
* @return A dataset.
*/
public CategoryDataset createDataset2() {
DefaultCategoryDataset result = new DefaultCategoryDataset();
// row keys...
String series1 = "Third";
String series2 = "Fourth";
// column keys...
String type1 = "Type 1";
String type2 = "Type 2";
String type3 = "Type 3";
String type4 = "Type 4";
String type5 = "Type 5";
String type6 = "Type 6";
String type7 = "Type 7";
String type8 = "Type 8";
result.addValue(11.0, series1, type1);
result.addValue(14.0, series1, type2);
result.addValue(13.0, series1, type3);
result.addValue(15.0, series1, type4);
result.addValue(15.0, series1, type5);
result.addValue(17.0, series1, type6);
result.addValue(17.0, series1, type7);
result.addValue(18.0, series1, type8);
result.addValue(15.0, series2, type1);
result.addValue(17.0, series2, type2);
result.addValue(16.0, series2, type3);
result.addValue(18.0, series2, type4);
result.addValue(14.0, series2, type5);
result.addValue(14.0, series2, type6);
result.addValue(12.0, series2, type7);
result.addValue(11.0, series2, type8);
return result;
}
/**
* Creates a sample plot.
*
* @return A sample plot.
*/
private CombinedDomainCategoryPlot createPlot() {
CategoryDataset dataset1 = createDataset1();
NumberAxis rangeAxis1 = new NumberAxis("Value");
rangeAxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
LineAndShapeRenderer renderer1 = new LineAndShapeRenderer();
renderer1.setBaseToolTipGenerator(
new StandardCategoryToolTipGenerator()
);
CategoryPlot subplot1 = new CategoryPlot(
dataset1, null, rangeAxis1, renderer1
);
subplot1.setDomainGridlinesVisible(true);
CategoryDataset dataset2 = createDataset2();
NumberAxis rangeAxis2 = new NumberAxis("Value");
rangeAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer renderer2 = new BarRenderer();
renderer2.setBaseToolTipGenerator(
new StandardCategoryToolTipGenerator()
);
CategoryPlot subplot2 = new CategoryPlot(
dataset2, null, rangeAxis2, renderer2
);
subplot2.setDomainGridlinesVisible(true);
CategoryAxis domainAxis = new CategoryAxis("Category");
CombinedDomainCategoryPlot plot
= new CombinedDomainCategoryPlot(domainAxis);
plot.add(subplot1, 2);
plot.add(subplot2, 1);
return plot;
}
}
|