00001
00002
00003
00004
00005
00006
00007 package org.swtchart;
00008
00009 import org.eclipse.swt.SWT;
00010 import org.eclipse.swt.graphics.Color;
00011 import org.eclipse.swt.graphics.GC;
00012 import org.eclipse.swt.graphics.Image;
00013 import org.eclipse.swt.graphics.ImageData;
00014 import org.eclipse.swt.graphics.ImageLoader;
00015 import org.eclipse.swt.widgets.Composite;
00016 import org.eclipse.swt.widgets.Control;
00017 import org.eclipse.swt.widgets.Display;
00018 import org.eclipse.swt.widgets.Event;
00019 import org.eclipse.swt.widgets.Listener;
00020 import org.swtchart.internal.ChartLayout;
00021 import org.swtchart.internal.ChartLayoutData;
00022 import org.swtchart.internal.ChartTitle;
00023 import org.swtchart.internal.Legend;
00024 import org.swtchart.internal.PlotArea;
00025 import org.swtchart.internal.Title;
00026 import org.swtchart.internal.axis.AxisSet;
00027 import org.swtchart.internal.series.SeriesSet;
00028
00032 public class Chart extends Composite implements Listener {
00033
00035 private Title title;
00036
00038 private Legend legend;
00039
00041 private AxisSet axisSet;
00042
00044 private PlotArea plotArea;
00045
00047 private int orientation;
00048
00050 private boolean compressEnabled;
00051
00053 private boolean updateSuspended;
00054
00063 public Chart(Composite parent, int style) {
00064 super(parent, style | SWT.DOUBLE_BUFFERED);
00065
00066 orientation = SWT.HORIZONTAL;
00067 compressEnabled = true;
00068 updateSuspended = false;
00069
00070 parent.layout();
00071
00072 setLayout(new ChartLayout());
00073
00074 title = new ChartTitle(this);
00075 title.setLayoutData(new ChartLayoutData(SWT.DEFAULT, 100));
00076 legend = new Legend(this, SWT.NONE);
00077 legend.setLayoutData(new ChartLayoutData(200, SWT.DEFAULT));
00078 plotArea = new PlotArea(this, SWT.NONE);
00079 axisSet = new AxisSet(this);
00080
00081 updateLayout();
00082
00083 addListener(SWT.Resize, this);
00084 }
00085
00091 public ITitle getTitle() {
00092 return title;
00093 }
00094
00100 public ILegend getLegend() {
00101 return legend;
00102 }
00103
00109 public IAxisSet getAxisSet() {
00110 return axisSet;
00111 }
00112
00118 public Composite getPlotArea() {
00119 return plotArea;
00120 }
00121
00127 public ISeriesSet getSeriesSet() {
00128 return plotArea.getSeriesSet();
00129 }
00130
00131
00132
00133
00134 @Override
00135 public void setBackground(Color color) {
00136 super.setBackground(color);
00137
00138 for (Control child : getChildren()) {
00139 if (!(child instanceof PlotArea) && !(child instanceof Legend)) {
00140 child.setBackground(color);
00141 }
00142 }
00143 }
00144
00151 public Color getBackgroundInPlotArea() {
00152 return plotArea.getBackground();
00153 }
00154
00164 public void setBackgroundInPlotArea(Color color) {
00165 if (color != null && color.isDisposed()) {
00166 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
00167 }
00168 plotArea.setBackground(color);
00169 }
00170
00179 @Override
00180 public void setOrientation(int orientation) {
00181 if (orientation == SWT.HORIZONTAL || orientation == SWT.VERTICAL) {
00182 this.orientation = orientation;
00183 }
00184 updateLayout();
00185 }
00186
00194 @Override
00195 public int getOrientation() {
00196 return orientation;
00197 }
00198
00208 public void enableCompress(boolean enabled) {
00209 compressEnabled = enabled;
00210 }
00211
00217 public boolean isCompressEnabled() {
00218 return compressEnabled;
00219 }
00220
00257 public void suspendUpdate(boolean suspend) {
00258 if (updateSuspended == suspend) {
00259 return;
00260 }
00261 updateSuspended = suspend;
00262
00263
00264 if (!suspend) {
00265 updateLayout();
00266 ((SeriesSet) getSeriesSet()).updateStackAndRiserData();
00267 }
00268 }
00269
00275 public boolean isUpdateSuspended() {
00276 return updateSuspended;
00277 }
00278
00279
00280
00281
00282 public void handleEvent(Event event) {
00283 switch (event.type) {
00284 case SWT.Resize:
00285 updateLayout();
00286 redraw();
00287 break;
00288 default:
00289 break;
00290 }
00291 }
00292
00296 public void updateLayout() {
00297 if (updateSuspended) {
00298 return;
00299 }
00300
00301 if (legend != null) {
00302 legend.updateLayoutData();
00303 }
00304
00305 if (title != null) {
00306 title.updateLayoutData();
00307 }
00308
00309 if (axisSet != null) {
00310 axisSet.updateLayoutData();
00311 }
00312
00313 layout();
00314
00315 if (axisSet != null) {
00316 axisSet.refresh();
00317 }
00318 }
00319
00320
00321
00322
00323 @Override
00324 public void update() {
00325 super.update();
00326 for (Control child : getChildren()) {
00327 child.update();
00328 }
00329 }
00330
00331
00332
00333
00334 @Override
00335 public void dispose() {
00336 title.dispose();
00337 legend.dispose();
00338 axisSet.dispose();
00339 plotArea.dispose();
00340 super.dispose();
00341 }
00342
00343
00344
00345
00346 @Override
00347 public void redraw() {
00348 super.redraw();
00349 for (Control child : getChildren()) {
00350 child.redraw();
00351 }
00352 }
00353
00362 public void save(String filename, int format) {
00363 Image image = new Image(Display.getDefault(), getBounds());
00364
00365 GC gc = new GC(image);
00366 print(gc);
00367 gc.dispose();
00368
00369 ImageData data = image.getImageData();
00370 ImageLoader loader = new ImageLoader();
00371 loader.data = new ImageData[] { data };
00372 loader.save(filename, format);
00373 image.dispose();
00374 }
00375
00382 public void renderOffscreenImage(Image image) {
00383 GC gc = new GC(image);
00384 print(gc);
00385 gc.dispose();
00386 }
00387 }