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.Control;
00010 import org.eclipse.swt.widgets.Display;
00011 import org.eclipse.swt.widgets.Shell;
00012 import org.swtchart.Chart;
00013 import org.swtchart.IBarSeries;
00014 import org.swtchart.ISeries;
00015 import org.swtchart.ISeries.SeriesType;
00016
00020 public class LegendBoundsExample {
00021
00022 private static final double[] ySeries1 = { 0.1, 0.1, 0.2, 0.2, 0.3 };
00023 private static final double[] ySeries2 = { 0.5, 0.5, 0.4, 0.3, 0.2 };
00024 private static final double[] ySeries3 = { 0.3, 0.2, 0.3, 0.4, 0.4 };
00025
00032 public static void main(String[] args) {
00033 Display display = new Display();
00034 Shell shell = new Shell(display);
00035 shell.setText("Legend Bounds");
00036 shell.setSize(500, 400);
00037 shell.setLayout(new FillLayout());
00038
00039 createChart(shell);
00040
00041 shell.open();
00042 while (!shell.isDisposed()) {
00043 if (!display.readAndDispatch()) {
00044 display.sleep();
00045 }
00046 }
00047 display.dispose();
00048 }
00049
00057 static public Chart createChart(Composite parent) {
00058
00059
00060 final Chart chart = new Chart(parent, SWT.NONE);
00061 chart.getTitle().setText("Legend Bounds");
00062
00063
00064 IBarSeries series1 = (IBarSeries) chart.getSeriesSet().createSeries(
00065 SeriesType.BAR, "series 1");
00066 series1.setYSeries(ySeries1);
00067 series1.setBarColor(Display.getDefault()
00068 .getSystemColor(SWT.COLOR_GREEN));
00069 IBarSeries series2 = (IBarSeries) chart.getSeriesSet().createSeries(
00070 SeriesType.BAR, "series 2");
00071 series2.setYSeries(ySeries2);
00072 series2.setBarColor(Display.getDefault().getSystemColor(
00073 SWT.COLOR_MAGENTA));
00074 IBarSeries series3 = (IBarSeries) chart.getSeriesSet().createSeries(
00075 SeriesType.BAR, "series 3");
00076 series3.setYSeries(ySeries3);
00077
00078
00079 chart.getAxisSet().adjustRange();
00080
00081
00082 final Control legend = (Control) chart.getLegend();
00083 legend.addMouseMoveListener(new MouseMoveListener() {
00084 public void mouseMove(MouseEvent e) {
00085 for (ISeries series : chart.getSeriesSet().getSeries()) {
00086 Rectangle r = chart.getLegend().getBounds(series.getId());
00087 if (r.x < e.x && e.x < r.x + r.width && r.y < e.y
00088 && e.y < r.y + r.height) {
00089 legend.setToolTipText(series.getId());
00090 return;
00091 }
00092 }
00093 }
00094 });
00095
00096 return chart;
00097 }
00098 }