Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SWTChart鼠标事件处理_Java_Swt_Mouseevent_Swtchart - Fatal编程技术网

Java SWTChart鼠标事件处理

Java SWTChart鼠标事件处理,java,swt,mouseevent,swtchart,Java,Swt,Mouseevent,Swtchart,我发现并只是想知道如何用鼠标在x轴上选择一个范围,例如1到3,然后我会得到属于所选x值的所有y轴值 (来源:) 在上面的示例中,如何包含用于选择范围的鼠标事件 提前感谢您。下面的代码应该会让您了解如何做到这一点。基本上,您必须获取图表的图表区域,添加SWT.MouseDown和SWT.MouseUp的侦听器,并记住这两个事件的位置。回答您的评论:是的,您可以使用SWT.Paint侦听器添加“突出显示”功能 import org.eclipse.swt.SWT; import org.eclip

我发现并只是想知道如何用鼠标在x轴上选择一个范围,例如1到3,然后我会得到属于所选x值的所有y轴值


(来源:)

在上面的示例中,如何包含用于选择范围的鼠标事件


提前感谢您。

下面的代码应该会让您了解如何做到这一点。基本上,您必须获取
图表的
图表区域
,添加
SWT.MouseDown
SWT.MouseUp
的侦听器,并记住这两个事件的位置。回答您的评论:是的,您可以使用
SWT.Paint
侦听器添加“突出显示”功能

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.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 location point of mouse down */
        private static double startX;
        private static double startY;

        private static int startXPos;
        private static int startYPos;

        private static int currentX;
        private static int currentY;

        private static boolean drag = false;

        /**
        * 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
            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.MouseDown, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    IAxis xAxis = chart.getAxisSet().getXAxis(0);
                    IAxis yAxis = chart.getAxisSet().getYAxis(0);

                    startX = xAxis.getDataCoordinate(event.x);
                    startY = yAxis.getDataCoordinate(event.y);

                    startXPos = event.x;
                    startYPos = event.y;

                    drag = true;
                }
            });

            plotArea.addListener(SWT.MouseUp, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    IAxis xAxis = chart.getAxisSet().getXAxis(0);
                    IAxis yAxis = chart.getAxisSet().getYAxis(0);

                    double endX = xAxis.getDataCoordinate(event.x);
                    double endY = yAxis.getDataCoordinate(event.y);

                    System.out.println(startX + " " + endX);
                    System.out.println(startY + " " + endY);

                    drag = false;

                    plotArea.redraw();
                }
            });

            plotArea.addListener(SWT.MouseMove, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    if(drag)
                    {
                        currentX = event.x;
                        currentY = event.y;

                        plotArea.redraw();
                    }
                }
            });

            plotArea.addListener(SWT.Paint, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    if(drag)
                    {
                        GC gc = event.gc;

                        gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
                        gc.setAlpha(128);

                        int minX = Math.min(startXPos, currentX);
                        int minY = Math.min(startYPos, currentY);

                        int maxX = Math.max(startXPos, currentX);
                        int maxY = Math.max(startYPos, currentY);

                        int width = maxX - minX;
                        int height = maxY - minY;

                        gc.fillRectangle(minX, minY, width, height);
                    }
                }
            });

            return chart;
        }
    }

下面的代码应该让您了解如何做到这一点。基本上,您必须获取
图表的
图表区域
,添加
SWT.MouseDown
SWT.MouseUp
的侦听器,并记住这两个事件的位置。回答您的评论:是的,您可以使用
SWT.Paint
侦听器添加“突出显示”功能

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.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 location point of mouse down */
        private static double startX;
        private static double startY;

        private static int startXPos;
        private static int startYPos;

        private static int currentX;
        private static int currentY;

        private static boolean drag = false;

        /**
        * 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
            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.MouseDown, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    IAxis xAxis = chart.getAxisSet().getXAxis(0);
                    IAxis yAxis = chart.getAxisSet().getYAxis(0);

                    startX = xAxis.getDataCoordinate(event.x);
                    startY = yAxis.getDataCoordinate(event.y);

                    startXPos = event.x;
                    startYPos = event.y;

                    drag = true;
                }
            });

            plotArea.addListener(SWT.MouseUp, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    IAxis xAxis = chart.getAxisSet().getXAxis(0);
                    IAxis yAxis = chart.getAxisSet().getYAxis(0);

                    double endX = xAxis.getDataCoordinate(event.x);
                    double endY = yAxis.getDataCoordinate(event.y);

                    System.out.println(startX + " " + endX);
                    System.out.println(startY + " " + endY);

                    drag = false;

                    plotArea.redraw();
                }
            });

            plotArea.addListener(SWT.MouseMove, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    if(drag)
                    {
                        currentX = event.x;
                        currentY = event.y;

                        plotArea.redraw();
                    }
                }
            });

            plotArea.addListener(SWT.Paint, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    if(drag)
                    {
                        GC gc = event.gc;

                        gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_RED));
                        gc.setAlpha(128);

                        int minX = Math.min(startXPos, currentX);
                        int minY = Math.min(startYPos, currentY);

                        int maxX = Math.max(startXPos, currentX);
                        int maxY = Math.max(startYPos, currentY);

                        int width = maxX - minX;
                        int height = maxY - minY;

                        gc.fillRectangle(minX, minY, width, height);
                    }
                }
            });

            return chart;
        }
    }

谢谢,它正在工作。是否可以使突出显示可见!?谢谢,它正在工作。是否可以使突出显示可见!?