00001 package org.swtchart.examples.advanced;
00002
00003 import org.eclipse.swt.SWT;
00004 import org.eclipse.swt.events.MouseEvent;
00005 import org.eclipse.swt.events.MouseMoveListener;
00006 import org.eclipse.swt.graphics.Rectangle;
00007 import org.eclipse.swt.layout.FillLayout;
00008 import org.eclipse.swt.widgets.Composite;
00009 import org.eclipse.swt.widgets.Display;
00010 import org.eclipse.swt.widgets.Shell;
00011 import org.swtchart.Chart;
00012 import org.swtchart.IBarSeries;
00013 import org.swtchart.ISeries;
00014 import org.swtchart.ISeries.SeriesType;
00015
00019 public class BarBoundsExample {
00020
00021 private static final double[] ySeries1 = { 3.0, 2.1, 1.9, 2.3, 3.2 };
00022 private static final double[] ySeries2 = { 2.0, 3.1, 0.9, 1.3, 2.2 };
00023
00030 public static void main(String[] args) {
00031 Display display = new Display();
00032 Shell shell = new Shell(display);
00033 shell.setText("Bar Bounds");
00034 shell.setSize(500, 400);
00035 shell.setLayout(new FillLayout());
00036
00037 createChart(shell);
00038
00039 shell.open();
00040 while (!shell.isDisposed()) {
00041 if (!display.readAndDispatch()) {
00042 display.sleep();
00043 }
00044 }
00045 display.dispose();
00046 }
00047
00055 static public Chart createChart(Composite parent) {
00056
00057
00058 final Chart chart = new Chart(parent, SWT.NONE);
00059 chart.getTitle().setText("Bar Bounds");
00060
00061
00062 IBarSeries series1 = (IBarSeries) chart.getSeriesSet().createSeries(
00063 SeriesType.BAR, "series 1");
00064 series1.setYSeries(ySeries1);
00065
00066 IBarSeries series2 = (IBarSeries) chart.getSeriesSet().createSeries(
00067 SeriesType.BAR, "series 2");
00068 series2.setYSeries(ySeries2);
00069 series2.setBarColor(Display.getDefault()
00070 .getSystemColor(SWT.COLOR_GREEN));
00071
00072
00073 chart.getAxisSet().adjustRange();
00074
00075
00076 chart.getPlotArea().addMouseMoveListener(new MouseMoveListener() {
00077 public void mouseMove(MouseEvent e) {
00078 for (ISeries series : chart.getSeriesSet().getSeries()) {
00079 Rectangle[] rs = ((IBarSeries) series).getBounds();
00080 for (int i = 0; i < rs.length; i++) {
00081 if (rs[i] != null) {
00082 if (rs[i].x < e.x && e.x < rs[i].x + rs[i].width
00083 && rs[i].y < e.y
00084 && e.y < rs[i].y + rs[i].height) {
00085 setToolTipText(series, i);
00086 return;
00087 }
00088 }
00089 }
00090 }
00091 chart.getPlotArea().setToolTipText(null);
00092 }
00093
00094 private void setToolTipText(ISeries series, int index) {
00095 chart.getPlotArea().setToolTipText(
00096 "Series: " + series.getId() + "\nValue: "
00097 + series.getYSeries()[index]);
00098 }
00099 });
00100
00101 return chart;
00102 }
00103 }