Note that there are some explanatory texts on larger screens.

plurals
  1. POBar Chart with MouseOver handling
    primarykey
    data
    text
    <p>I have found <a href="http://www.swtchart.org" rel="nofollow">SWTChart library</a> and I tried to implement a box with the info about the y-value when you go over the bar chart like in this <a href="http://teethgrinder.co.uk/open-flash-chart/gallery-bar.php" rel="nofollow">example</a>. </p> <p>However, the following code does not show any information about the y value.</p> <pre><code>package org.swtchart.examples; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.swtchart.Chart; import org.swtchart.IAxis; import org.swtchart.IBarSeries; import org.swtchart.ISeries; import org.swtchart.ISeries.SeriesType; /** * An example for bar chart. */ public class BarChartExampleMouseOver { private static final double[] ySeries = { 0.2, 1.1, 1.9, 2.3, 1.8, 1.5, 1.8, 2.6, 2.9, 3.2 }; /* Used to remember the location of the highlight point */ private static int highlightX; private static int highlightY; protected static boolean highlight; /** * The main method. * * @param args * the arguments */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Bar Chart"); shell.setSize(500, 400); shell.setLayout(new FillLayout()); createChart(shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } /** * create the chart. * * @param parent * The parent composite * @return The created chart */ static public Chart createChart(Composite parent) { // create a chart final Chart chart = new Chart(parent, SWT.NONE); // set titles chart.getTitle().setText("Bar Chart"); chart.getAxisSet().getXAxis(0).getTitle().setText("Data Points"); chart.getAxisSet().getYAxis(0).getTitle().setText("Amplitude"); // create bar series IBarSeries barSeries = (IBarSeries) chart.getSeriesSet().createSeries( SeriesType.BAR, "bar series"); barSeries.setYSeries(ySeries); // adjust the axis range chart.getAxisSet().adjustRange(); /* Get the plot area and add the mouse listeners */ final Composite plotArea = chart.getPlotArea(); plotArea.addListener(SWT.MouseHover, new Listener() { @Override public void handleEvent(Event event) { IAxis xAxis = chart.getAxisSet().getXAxis(0); IAxis yAxis = chart.getAxisSet().getYAxis(0); double x = xAxis.getDataCoordinate(event.x); double y = yAxis.getDataCoordinate(event.y); ISeries[] series = chart.getSeriesSet().getSeries(); double closestX = 0; double closestY = 0; double minDist = Double.MAX_VALUE; /* over all series */ for (ISeries serie : series) { double[] xS = serie.getXSeries(); double[] yS = serie.getYSeries(); /* check all data points */ for (int i = 0; i &lt; xS.length; i++) { /* compute distance to mouse position */ double newDist = Math.sqrt(Math.pow((x - xS[i]), 2) + Math.pow((y - yS[i]), 2)); /* if closer to mouse, remember */ if (newDist &lt; minDist) { minDist = newDist; closestX = xS[i]; closestY = yS[i]; } } } /* set tooltip of closest data point */ plotArea.setToolTipText(closestX + " " + closestY); /* remember closest data point */ highlightX = xAxis.getPixelCoordinate(closestX); highlightY = yAxis.getPixelCoordinate(closestY); highlight = true; /* trigger repaint (paint highlight) */ plotArea.redraw(); } }); plotArea.addListener(SWT.MouseMove, new Listener() { @Override public void handleEvent(Event arg0) { highlight = false; plotArea.redraw(); } }); plotArea.addListener(SWT.Paint, new Listener() { @Override public void handleEvent(Event event) { if (highlight) { GC gc = event.gc; gc.setBackground(Display.getDefault().getSystemColor( SWT.COLOR_RED)); gc.setAlpha(128); gc.fillOval(highlightX - 5, highlightY - 5, 10, 10); } } }); return chart; } } </code></pre> <p>Thank you in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload