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.IAxis;
00013 import org.swtchart.IBarSeries;
00014 import org.swtchart.IAxis.Direction;
00015 import org.swtchart.ISeries.SeriesType;
00016
00020 public class AxisTickBoundsExample {
00021
00022 private static final double[] ySeries = { 0.1, 0.1, 0.2, 0.2, 0.3 };
00023
00030 public static void main(String[] args) {
00031 Display display = new Display();
00032 Shell shell = new Shell(display);
00033 shell.setText("Axis Tick 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("Axis Tick Bounds");
00060
00061
00062 IBarSeries series1 = (IBarSeries) chart.getSeriesSet().createSeries(
00063 SeriesType.BAR, "series");
00064 series1.setYSeries(ySeries);
00065
00066
00067 chart.getAxisSet().adjustRange();
00068
00069
00070 chart.addMouseMoveListener(new MouseMoveListener() {
00071 public void mouseMove(MouseEvent e) {
00072 for (IAxis axis : chart.getAxisSet().getAxes()) {
00073 Rectangle r = axis.getTick().getBounds();
00074
00075
00076 if (r.x < e.x && e.x < r.x + r.width && r.y < e.y
00077 && e.y < r.y + r.height) {
00078
00079
00080 int pixelCoord;
00081 if (axis.getDirection() == Direction.X) {
00082 pixelCoord = e.x - r.x;
00083 } else {
00084 pixelCoord = e.y - r.y;
00085 }
00086
00087
00088 double dataCoord = axis.getDataCoordinate(pixelCoord);
00089
00090
00091 chart.setToolTipText(String.valueOf(dataCoord));
00092 return;
00093 }
00094 }
00095 chart.setToolTipText(null);
00096 }
00097 });
00098
00099 return chart;
00100 }
00101 }