| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MultiChartPanel |
|
| 2.857142857142857;2.857 |
| 1 | /* ======================================================================= | |
| 2 | * A visualisation library extension for JFreeChart. Please see JFreeChart | |
| 3 | * for further information. | |
| 4 | * ======================================================================= | |
| 5 | * Copyright (C) 2006 University of Helsinki, Department of Computer Science | |
| 6 | * | |
| 7 | * This library is free software; you can redistribute it and/or | |
| 8 | * modify it under the terms of the GNU Lesser General Public | |
| 9 | * License as published by the Free Software Foundation; either | |
| 10 | * version 2.1 of the License, or (at your option) any later version. | |
| 11 | * | |
| 12 | * This library is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 | * Lesser General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU Lesser General Public | |
| 18 | * License along with this library; if not, write to the Free Software | |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 20 | * ----------------------------- | |
| 21 | * Contact: ohtu@cs.helsinki.fi | |
| 22 | * ----------------------------- | |
| 23 | * | |
| 24 | */ | |
| 25 | ||
| 26 | ||
| 27 | package org.jfree.chart; | |
| 28 | ||
| 29 | import java.util.*; | |
| 30 | import java.math.BigDecimal; | |
| 31 | ||
| 32 | import java.awt.AWTEvent; | |
| 33 | import java.awt.Dimension; | |
| 34 | import java.awt.Graphics; | |
| 35 | import java.awt.Graphics2D; | |
| 36 | import java.awt.Image; | |
| 37 | import java.awt.Insets; | |
| 38 | import java.awt.Point; | |
| 39 | import java.awt.event.ActionEvent; | |
| 40 | import java.awt.event.ActionListener; | |
| 41 | import java.awt.event.MouseEvent; | |
| 42 | import java.awt.event.MouseListener; | |
| 43 | import java.awt.event.MouseMotionListener; | |
| 44 | import java.awt.geom.AffineTransform; | |
| 45 | import java.awt.geom.Line2D; | |
| 46 | import java.awt.geom.Point2D; | |
| 47 | import java.awt.geom.Rectangle2D; | |
| 48 | import java.awt.print.PageFormat; | |
| 49 | import java.awt.print.Printable; | |
| 50 | import java.awt.print.PrinterException; | |
| 51 | import java.awt.print.PrinterJob; | |
| 52 | import java.io.File; | |
| 53 | import java.io.IOException; | |
| 54 | import java.io.Serializable; | |
| 55 | import java.util.EventListener; | |
| 56 | import java.util.ResourceBundle; | |
| 57 | ||
| 58 | import javax.swing.JFileChooser; | |
| 59 | import javax.swing.JMenu; | |
| 60 | import javax.swing.JMenuItem; | |
| 61 | import javax.swing.JOptionPane; | |
| 62 | import javax.swing.JPanel; | |
| 63 | import javax.swing.JPopupMenu; | |
| 64 | import javax.swing.ToolTipManager; | |
| 65 | import javax.swing.event.EventListenerList; | |
| 66 | ||
| 67 | import org.jfree.chart.editor.ChartEditor; | |
| 68 | import org.jfree.chart.editor.ChartEditorManager; | |
| 69 | import org.jfree.chart.entity.ChartEntity; | |
| 70 | import org.jfree.chart.entity.EntityCollection; | |
| 71 | import org.jfree.chart.event.ChartChangeEvent; | |
| 72 | import org.jfree.chart.event.ChartChangeListener; | |
| 73 | import org.jfree.chart.event.ChartProgressEvent; | |
| 74 | import org.jfree.chart.event.ChartProgressListener; | |
| 75 | import org.jfree.chart.plot.Plot; | |
| 76 | import org.jfree.chart.plot.PlotOrientation; | |
| 77 | import org.jfree.chart.plot.PlotRenderingInfo; | |
| 78 | import org.jfree.chart.plot.ValueAxisPlot; | |
| 79 | import org.jfree.chart.plot.Zoomable; | |
| 80 | import org.jfree.ui.ExtensionFileFilter; | |
| 81 | ||
| 82 | /** | |
| 83 | * A class for linking two or more {@link ChartPanel} instances | |
| 84 | * together for printing and saving. | |
| 85 | * | |
| 86 | * @author viski-project, Univ. of Helsinki | |
| 87 | */ | |
| 88 | public class MultiChartPanel extends ChartPanel { | |
| 89 | ||
| 90 | /** List of panels of which contents will be used in save/print */ | |
| 91 | private List panels; | |
| 92 | ||
| 93 | /** | |
| 94 | * Constructs a panel that displays the specified chart. | |
| 95 | * | |
| 96 | * @param chart the chart. | |
| 97 | * @param panels the list of chartpanels. | |
| 98 | */ | |
| 99 | public MultiChartPanel(JFreeChart chart, | |
| 100 | List panels) { | |
| 101 | ||
| 102 | 3 | super(chart, |
| 103 | DEFAULT_WIDTH, | |
| 104 | DEFAULT_HEIGHT, | |
| 105 | DEFAULT_MINIMUM_DRAW_WIDTH, | |
| 106 | DEFAULT_MINIMUM_DRAW_HEIGHT, | |
| 107 | DEFAULT_MAXIMUM_DRAW_WIDTH, | |
| 108 | DEFAULT_MAXIMUM_DRAW_HEIGHT, | |
| 109 | DEFAULT_BUFFER_USED, | |
| 110 | true, // properties | |
| 111 | true, // save | |
| 112 | true, // print | |
| 113 | true, // zoom | |
| 114 | true // tooltips | |
| 115 | ); | |
| 116 | 3 | this.panels=panels; |
| 117 | ||
| 118 | 3 | } |
| 119 | ||
| 120 | /** | |
| 121 | * Constructs a panel containing a chart. | |
| 122 | * | |
| 123 | * @param chart the chart. | |
| 124 | * @param useBuffer a flag controlling whether or not an off-screen buffer | |
| 125 | * is used. | |
| 126 | * @param panels the list of chartpanels. | |
| 127 | */ | |
| 128 | public MultiChartPanel(JFreeChart chart, | |
| 129 | boolean useBuffer, | |
| 130 | List panels) { | |
| 131 | ||
| 132 | 1 | super(chart, |
| 133 | DEFAULT_WIDTH, | |
| 134 | DEFAULT_HEIGHT, | |
| 135 | DEFAULT_MINIMUM_DRAW_WIDTH, | |
| 136 | DEFAULT_MINIMUM_DRAW_HEIGHT, | |
| 137 | DEFAULT_MAXIMUM_DRAW_WIDTH, | |
| 138 | DEFAULT_MAXIMUM_DRAW_HEIGHT, | |
| 139 | useBuffer, | |
| 140 | true, // properties | |
| 141 | true, // save | |
| 142 | true, // print | |
| 143 | true, // zoom | |
| 144 | true // tooltips | |
| 145 | ); | |
| 146 | 1 | this.panels=panels; |
| 147 | ||
| 148 | 1 | } |
| 149 | ||
| 150 | /** | |
| 151 | * Constructs a JFreeChart panel. | |
| 152 | * | |
| 153 | * @param chart the chart. | |
| 154 | * @param properties a flag indicating whether or not the chart property | |
| 155 | * editor should be available via the popup menu. | |
| 156 | * @param save a flag indicating whether or not save options should be | |
| 157 | * available via the popup menu. | |
| 158 | * @param print a flag indicating whether or not the print option | |
| 159 | * should be available via the popup menu. | |
| 160 | * @param zoom a flag indicating whether or not zoom options should | |
| 161 | * be added to the popup menu. | |
| 162 | * @param tooltips a flag indicating whether or not tooltips should be | |
| 163 | * enabled for the chart. | |
| 164 | * @param panels the list of chartpanels. | |
| 165 | */ | |
| 166 | public MultiChartPanel(JFreeChart chart, | |
| 167 | boolean properties, | |
| 168 | boolean save, | |
| 169 | boolean print, | |
| 170 | boolean zoom, | |
| 171 | boolean tooltips, | |
| 172 | List panels) { | |
| 173 | ||
| 174 | 1 | super(chart, |
| 175 | DEFAULT_WIDTH, | |
| 176 | DEFAULT_HEIGHT, | |
| 177 | DEFAULT_MINIMUM_DRAW_WIDTH, | |
| 178 | DEFAULT_MINIMUM_DRAW_HEIGHT, | |
| 179 | DEFAULT_MAXIMUM_DRAW_WIDTH, | |
| 180 | DEFAULT_MAXIMUM_DRAW_HEIGHT, | |
| 181 | DEFAULT_BUFFER_USED, | |
| 182 | properties, | |
| 183 | save, | |
| 184 | print, | |
| 185 | zoom, | |
| 186 | tooltips | |
| 187 | ); | |
| 188 | 1 | this.panels=panels; |
| 189 | 1 | } |
| 190 | /** | |
| 191 | * Constructs a JFreeChart panel. | |
| 192 | * | |
| 193 | * @param chart the chart. | |
| 194 | * @param width the preferred width of the panel. | |
| 195 | * @param height the preferred height of the panel. | |
| 196 | * @param minimumDrawWidth the minimum drawing width. | |
| 197 | * @param minimumDrawHeight the minimum drawing height. | |
| 198 | * @param maximumDrawWidth the maximum drawing width. | |
| 199 | * @param maximumDrawHeight the maximum drawing height. | |
| 200 | * @param useBuffer a flag that indicates whether to use the | |
| 201 | * buffer to improve performance (at the | |
| 202 | * memory). | |
| 203 | * @param properties a flag indicating whether or not the chart property | |
| 204 | * editor should be available via the popup | |
| 205 | * @param save a flag indicating whether or not save options should be | |
| 206 | * available via the popup menu. | |
| 207 | * @param print a flag indicating whether or not the print option | |
| 208 | * should be available via the popup menu. | |
| 209 | * @param zoom a flag indicating whether or not zoom options should be | |
| 210 | * added to the popup menu. | |
| 211 | * @param tooltips a flag indicating whether or not tooltips should be | |
| 212 | * enabled for the chart. | |
| 213 | * @param panels the list of chartpanels. | |
| 214 | */ | |
| 215 | public MultiChartPanel(JFreeChart chart, | |
| 216 | int width, | |
| 217 | int height, | |
| 218 | int minimumDrawWidth, | |
| 219 | int minimumDrawHeight, | |
| 220 | int maximumDrawWidth, | |
| 221 | int maximumDrawHeight, | |
| 222 | boolean useBuffer, | |
| 223 | boolean properties, | |
| 224 | boolean save, | |
| 225 | boolean print, | |
| 226 | boolean zoom, | |
| 227 | boolean tooltips, | |
| 228 | List panels) { | |
| 229 | 1 | super(chart, |
| 230 | width, | |
| 231 | height, | |
| 232 | minimumDrawWidth, | |
| 233 | minimumDrawHeight, | |
| 234 | maximumDrawWidth, | |
| 235 | maximumDrawHeight, | |
| 236 | useBuffer, | |
| 237 | properties, | |
| 238 | save, | |
| 239 | print, | |
| 240 | zoom, | |
| 241 | tooltips | |
| 242 | ); | |
| 243 | 1 | this.panels=panels; |
| 244 | 1 | } |
| 245 | ||
| 246 | /** | |
| 247 | * Constructs a JFreeChart panel capable of multiprint/save | |
| 248 | * from existing ChartPanel | |
| 249 | * | |
| 250 | * @param panels the list of chartpanels. | |
| 251 | * @param chartpanel the existing ChartPanel. | |
| 252 | */ | |
| 253 | public MultiChartPanel(List panels, ChartPanel chartpanel) | |
| 254 | { | |
| 255 | 1 | super(chartpanel.getChart()); |
| 256 | 1 | this.panels=panels; |
| 257 | 1 | } |
| 258 | ||
| 259 | /** | |
| 260 | * Prints the chart on a single page. | |
| 261 | * | |
| 262 | * @param g the graphics context. | |
| 263 | * @param pf the page format to use. | |
| 264 | * @param pageIndex the index of the page. If not <code>0</code>, nothing | |
| 265 | * gets print. | |
| 266 | * | |
| 267 | * @return The result of printing. | |
| 268 | */ | |
| 269 | public int print(Graphics g, PageFormat pf, int pageIndex) { | |
| 270 | ||
| 271 | // if panels is null, size 0 or 1, use ChartPanel's printing method | |
| 272 | 2 | if(panels == null || panels.size() == 0 || panels.size() == 1) { |
| 273 | 2 | return super.print(g, pf, pageIndex); |
| 274 | } | |
| 275 | ||
| 276 | 0 | if (pageIndex != 0) { |
| 277 | 0 | return NO_SUCH_PAGE; |
| 278 | } | |
| 279 | ||
| 280 | 0 | Graphics2D g2 = (Graphics2D) g; |
| 281 | 0 | double x = pf.getImageableX(); |
| 282 | 0 | double y = pf.getImageableY(); |
| 283 | 0 | double w = pf.getImageableWidth(); |
| 284 | 0 | double h = pf.getImageableHeight(); |
| 285 | ||
| 286 | //number of columns in "printing grid" (a changing method could be added) | |
| 287 | 0 | int saveColumns = 2; |
| 288 | //number of rows in "printing grid" (number of charts / saveColumns, rounded up) | |
| 289 | int saveRows; | |
| 290 | ||
| 291 | //special case bubblegum fix, so images won't scale "wrong" | |
| 292 | 0 | if (panels.size()==2 && pf.getOrientation() == PageFormat.PORTRAIT) { |
| 293 | 0 | saveRows = 2; |
| 294 | 0 | } |
| 295 | else { | |
| 296 | 0 | saveRows = new BigDecimal((double)panels.size()).divide( |
| 297 | new BigDecimal((double)saveColumns), BigDecimal.ROUND_UP).intValue(); | |
| 298 | } | |
| 299 | ||
| 300 | 0 | Iterator i = panels.iterator(); |
| 301 | 0 | while(i.hasNext()) { |
| 302 | 0 | for(int j = 0; j < saveColumns; ++j) { |
| 303 | 0 | if (i.hasNext()) { |
| 304 | 0 | ((MultiChartPanel)i.next()).getChart().draw( |
| 305 | g2, new Rectangle2D.Double(x, y, w/saveColumns, h/saveRows), | |
| 306 | this.getAnchor(), null | |
| 307 | ); | |
| 308 | 0 | x = x + w/saveColumns; |
| 309 | } | |
| 310 | } | |
| 311 | 0 | x = pf.getImageableX(); |
| 312 | 0 | y = y + h/saveRows; |
| 313 | ||
| 314 | 0 | } |
| 315 | 0 | return PAGE_EXISTS; |
| 316 | } | |
| 317 | ||
| 318 | /** | |
| 319 | * Opens a file chooser and gives the user an opportunity to save the | |
| 320 | * charts in same file in PNG format. | |
| 321 | * | |
| 322 | * @throws IOException if there is an I/O error. | |
| 323 | */ | |
| 324 | public void doSaveAs() throws IOException { | |
| 325 | ||
| 326 | //if panels is null, use ChartPanel's saving method | |
| 327 | 0 | if (panels == null || panels.size() == 0 || panels.size() == 1) { |
| 328 | 0 | super.doSaveAs(); |
| 329 | 0 | } |
| 330 | ||
| 331 | else { | |
| 332 | 0 | JFileChooser fileChooser = new JFileChooser(); |
| 333 | 0 | ExtensionFileFilter filter = new ExtensionFileFilter( |
| 334 | localizationResources.getString("PNG_Image_Files"), ".png" | |
| 335 | ); | |
| 336 | 0 | fileChooser.addChoosableFileFilter(filter); |
| 337 | ||
| 338 | 0 | int option = fileChooser.showSaveDialog(this); |
| 339 | 0 | if (option == JFileChooser.APPROVE_OPTION) { |
| 340 | 0 | String filename = fileChooser.getSelectedFile().getPath(); |
| 341 | 0 | if (isEnforceFileExtensions()) { |
| 342 | 0 | if (!filename.endsWith(".png")) { |
| 343 | 0 | filename = filename + ".png"; |
| 344 | } | |
| 345 | } | |
| 346 | 0 | ArrayList charts = new ArrayList(); |
| 347 | 0 | Iterator i = panels.iterator(); |
| 348 | 0 | while(i.hasNext()) { |
| 349 | 0 | MultiChartPanel panel = (MultiChartPanel)i.next(); |
| 350 | 0 | JFreeChart chart = panel.getChart(); |
| 351 | 0 | charts.add(chart); |
| 352 | 0 | } |
| 353 | 0 | MultiChartUtilities.saveChartsAsPNG( |
| 354 | new File(filename), charts, getWidth(), getHeight() | |
| 355 | ); | |
| 356 | } | |
| 357 | } | |
| 358 | 0 | } |
| 359 | } |