Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:34:00+00:00 2026-06-11T16:34:00+00:00

I have found SWTChart library and just wonder how it would be possible to

  • 0

I have found SWTChart library and just wonder how it would be possible to go over the below graph with the mouse and get box with the info about the y-value like in this example.

a busy cat
(source: swtchart.org)

package org.swtchart.examples;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.swtchart.Chart;
import org.swtchart.ILineSeries;
import org.swtchart.ISeries.SeriesType;

/**
* An example for area chart.
*/
public class AreaChartExample {

    private static final double[] ySeries1 = { 0.1, 0.38, 0.71, 0.92, 1.0 };

    private static final double[] ySeries2 = { 1.2, 3.53, 3.1, 0.1, 0.5 };

    /**
    * The main method.
    * 
    * @param args
    *            the arguments
    */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Area 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
        Chart chart = new Chart(parent, SWT.NONE);

        // set titles
        chart.getTitle().setText("Area Chart");

        // create line series
        ILineSeries lineSeries1 = (ILineSeries) chart.getSeriesSet()
                .createSeries(SeriesType.LINE, "line series 1");
        lineSeries1.setYSeries(ySeries1);
        lineSeries1.setLineColor(Display.getDefault().getSystemColor(
                SWT.COLOR_RED));
        lineSeries1.enableArea(true);

        ILineSeries lineSeries2 = (ILineSeries) chart.getSeriesSet()
                .createSeries(SeriesType.LINE, "line series 2");
        lineSeries2.setYSeries(ySeries2);
        lineSeries2.enableArea(true);

        // adjust the axis range
        chart.getAxisSet().adjustRange();

        return chart;
    }
}

How is it possible to include a mouse event to open a box with the y-value like in the above example?

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:34:02+00:00Added an answer on June 11, 2026 at 4:34 pm

    Got it working. My code will search for the closest data point to the mouse position, display its coordinates in the tooltip and highlight the data point in the plot:

    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.ILineSeries;
    import org.swtchart.ISeries;
    import org.swtchart.ISeries.SeriesType;
    
    /**
     * An example for area chart.
     */
    public class AreaChartExample {
    
        private static final double[] ySeries1 = { 0.1, 0.38, 0.71, 0.92, 1.0 };
    
        private static final double[] ySeries2 = { 1.2, 3.53, 3.1, 0.1, 0.5 };
    
        /* Used to remember the location of the highlight point */
        private static int highlightX;
        private static int highlightY;
    
        /**
         * The main method.
         * 
         * @param args
         *            the arguments
         */
        public static void main(String[] args) {
            Display display = new Display();
            Shell shell = new Shell(display);
            shell.setText("Area Chart");
            shell.setSize(500, 400);
            shell.setLayout(new FillLayout());
    
            createChart(shell);
    
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
            display.dispose();
        }
    
        protected static boolean highlight;
    
        /**
         * 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("Area Chart");
    
            // create line series
            ILineSeries lineSeries1 = (ILineSeries) chart.getSeriesSet()
                    .createSeries(SeriesType.LINE, "line series 1");
            lineSeries1.setYSeries(ySeries1);
            lineSeries1.setLineColor(Display.getDefault().getSystemColor(
                    SWT.COLOR_RED));
            lineSeries1.enableArea(true);
    
            ILineSeries lineSeries2 = (ILineSeries) chart.getSeriesSet()
                    .createSeries(SeriesType.LINE, "line series 2");
            lineSeries2.setYSeries(ySeries2);
            lineSeries2.enableArea(true);
    
            // 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 < 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 < 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;
        }
    }
    

    The result looks like this:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found this script that expand image when mouse is over, but when
I have found already some similar discussion on this, but would like to investigate
I have found location on google map and get custom iframe code with marked
HAVE FOUND THE SOLUTION FOR BELOW PROBLEM : here is the Js Fiddle special
I have found some info on the subject ( like this link) , but
I have found this code the internet to computer all possible Permutations for a
I have found many calculations here and some php examples and most are just
I have found lots of info online about how to use the initEvent and
I have found Apache's impelementation of Soundex and Metaphone in Java but I would
I have found Okapi Similarity measure can be used to calculated document similarity from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.