#755 Bug in chartcomposite class

closed
None
5
2007-05-19
2007-05-15
raghz
No

Hi ,

I am getting a very strange problem , When I maximize my chart and try to click and drag as I have kept mouse events in my code I am unable to drag at all .

The problem is the chart gets resized or Flickered when I try to click on the chart area . Has any one other than me faced this problem ? If yes How to fix it .I am using the SWT-chartComposite class to display my chart.

Thanks in advance
raghavan

Discussion

  • Henry Proudhon

    Henry Proudhon - 2007-05-16
    • assigned_to: nobody --> nenry
     
  • Henry Proudhon

    Henry Proudhon - 2007-05-16

    Logged In: YES
    user_id=1379312
    Originator: NO

    I will have a look during the week end, can you post a minimal example ?

    Regards, Henry

     
  • raghz

    raghz - 2007-05-17

    Logged In: YES
    user_id=1694072
    Originator: YES

    Hi nenry ,
    I have made some changes in chart composite and got the mouse events in my class . I have kept setter and getter methods for private variables thats it no other change I did .

    When U run this code the shell is displayed and when U maximize it to full screen and click on the chart the chart moves a bit or flicker's . I dont know Y .
    My code for the class is as follows.....

    public class Spectrum {
    public Spectrum() {
    }

    /\*\*
     \* Creates the demo chart.
     \*
     \* @return The chart.
     \*/
    XYDataset dataset = createDataset\(\);
    
    XYDataset dataset1 = null;
    
    int dataFlag = 0;
    
    static XYPlot plot;
    
    private static float lineAnnotationX0 = 15.505293f; // x coord - set from drag
    
    private static float lineAnnotationY0 = 0.0f; // y coord - set from drag
    
    private static float lineAnnotationX01 = 15.505293f; // x coord - set from drag
    
    private static float lineAnnotationY01 = 20.0f; // y coord - set from drag
    
    private static float boxAnnotationX0 = 4.374289f; // x coord - set from drag
    
    private static float boxAnnotationY0 = 3.784317f; // y coord - set from drag
    
    private static float boxAnnotationX01 = 5.819367f; // x coord - set from drag
    
    private static float boxAnnotationY01 = 6.263697f; // y coord - set from drag
    
    /\*\* Position in ball of mouse press to make dragging look better. \*/
    private static float mouseLocationX0 = 0; // pressed this far inside ball's
    
    private static float mouseLocationX1 = 0; // pressed this far inside ball's
    
    private static float mouseLocationX2 = 0; // pressed this far inside ball's
    
    private static float mouseLocationY0 = 0; // pressed this far inside ball's
    
    private static float mouseLocationY1 = 0; // pressed this far inside ball's
    
    private static float mouseLocationY2 = 0; // pressed this far inside ball's
    
    /\*\* true means mouse was pressed in ball and still in panel. \*/
    private static boolean \_canDrag = false;
    
    private static boolean \_canDragb = false;
    
    private static boolean \_canDragp = false;
    
    private static boolean \_canDrage = false;
    
    static XYLineAnnotation x1;
    
    static XYAnnotation boxannotation;
    
    static XYPolygonAnnotation ploy;
    
    static double\[\] rolyvalues = \{ 1.5, 2.5, 3.5, 4.5, 5.5, 6.5 \};
    
    static Shape shape;
    
    static XYShapeAnnotation ellipse;
    
    static double ellipseX=3.5;
    
    static double ellipseY=4.0;
    
    static double ellipsewidth =7.0;
    
    static double ellipseheight =6.5;
    
    static XYItemRenderer renderer;
    private JFreeChart createChart\(\) \{
    
        XYDataset dataset1 = createDataset\(\);
        JFreeChart chart = ChartFactory.createXYLineChart\("Spectrum Sample",
                "X Axis", "Y Axis", dataset1, PlotOrientation.HORIZONTAL, true,
                true, false\);
    

    // XYItemRenderer renderer = (XYItemRenderer) new StandardXYToolTipGenerator();

        plot = chart.getXYPlot\(\);
        renderer = plot.getRenderer\(\);
        plot.setOrientation\(PlotOrientation.VERTICAL\);
        plot.setBackgroundPaint\(Color.white\);
        plot.setDomainGridlinesVisible\(true\);
        plot.setRangeGridlinesVisible\(false\);
    
        plot
                .handleClick\(2, 10, new PlotRenderingInfo\(
                        new ChartRenderingInfo\(\)\)\);
        plot.setDomainGridlinePaint\(Color.lightGray\);
        plot.setRangeGridlinePaint\(Color.lightGray\);
    
        plot.setRangeCrosshairVisible\(false\);
        plot.setDomainCrosshairVisible\(false\);
    
        plot.setRangeCrosshairPaint\(Color.black\);
        plot.setDomainCrosshairPaint\(Color.black\);
    
        boxannotation = new XYBoxAnnotation\(boxAnnotationX0, boxAnnotationY0,
                boxAnnotationX01, boxAnnotationY01\);
        XYTextAnnotation annotat = new XYTextAnnotation\("annotation",15.544349,11.74591\);
        renderer.addAnnotation\(annotat\);
        x1 = new XYLineAnnotation\(lineAnnotationX0, lineAnnotationY0,
                lineAnnotationX01, lineAnnotationY01\);
        // x1.addMouseMotionListener\(this\);
        renderer.addAnnotation\(boxannotation\);
    
        ploy = new XYPolygonAnnotation\(rolyvalues\);
        renderer.addAnnotation\(ploy\);
    
        shape = new Ellipse2D.Double\(ellipseX, ellipseX, ellipsewidth, ellipseheight\);
         ellipse = new XYShapeAnnotation\(shape\);
        System.out.println\("  shape  " + shape.getBounds2D\(\).getMaxX\(\)
                \+ "   maxmx  " + shape.getBounds2D\(\).getMaxY\(\) + "   maxmY  "
                \+ shape.getBounds2D\(\).getWidth\(\) + "   width  "
                \+ shape.getBounds2D\(\).getHeight\(\) + "   height  "
                \+ shape.getBounds2D\(\).getX\(\) + "   getx  "
                \+ shape.getBounds2D\(\).getY\(\) + "   getY  "+ "    bounds    " +shape.getBounds2D\(\).getCenterY\(\)+ "  center xx    "+shape.getBounds2D\(\).getCenterY\(\)\);
        renderer.addAnnotation\(ellipse\);
        renderer.addAnnotation\(x1\);
        renderer.setPaint\(Color.red\);
        //     renderer.setToolTipGenerator\(tooltip\);
        plot.setDataset\(1, dataset\);
        renderer.setSeriesPaint\(1, Color.BLACK\);
        plot.setRenderer\(renderer\);
        return chart;
    \}
    
    /\*\*
     \* Creates a sample dataset.
     \*
     \* @param name  the dataset name.
     \* @param base  the starting value.
     \* @param start  the starting period.
     \* @param count  the number of values to generate.
     \*
     \* @return The dataset.
     \*/
    private XYDataset createDataset\(\) \{
        float i = 0.0f;
        int count = 0;
    
        XYSeries series = new XYSeries\("Sample"\);
        if \(dataFlag % 2 == 0\) \{
    
            while \(i < 20\) \{
                series.add\(i, java.lang.Math.random\(\) + 10\);
                i = i + 0.1f;
                count++;
            \}
            if \(dataset == null\)
                dataset = \(XYDataset\) new XYSeriesCollection\(series\);
    
        \}
    
        XYSeriesCollection dataset = new XYSeriesCollection\(\);
        dataset.addSeries\(series\);
        return dataset;
    
    \}
    
    /\*\*
     \* Starting point for the demonstration application.
     \*
     \* @param args  ignored.
     \*/
    public static void main\(String\[\] args\) \{
        Spectrum obj = new Spectrum\(\);
    
        final JFreeChart chart = obj.createChart\(\);
        final Display display = new Display\(\);
        Shell shell = new Shell\(display\);
        shell.setSize\(600, 300\);
        shell.setLayout\(new FillLayout\(\)\);
        shell.setText\("Spectrum Test"\);
    
        final ChartComposite frame = new ChartComposite\(shell, SWT.NONE, chart,
                true\);
        //Canvas canvas = ;//new Canvas\(frame,SWT.NO\_BACKGROUND\);
    
        frame.getCanvas\(\).addMouseMoveListener\(new MouseMoveListener\(\) \{
    
            // current state of coordinates
    
            public void mouseMove\(MouseEvent e\) \{
                int mouseX = e.x;
                int mouseY = e.y;
    
                Point p = frame.translateScreenToJavaSWT\(new Point\(mouseX,
                        mouseY\)\);
                XYPlot plot = \(XYPlot\) chart.getPlot\(\);
                org.eclipse.swt.graphics.Rectangle plotArea = frame
                        .getScreenDataArea\(\);
                ValueAxis domainAxis = plot.getDomainAxis\(\);
                RectangleEdge domainAxisEdge = plot.getDomainAxisEdge\(\);
                ValueAxis rangeAxis = plot.getRangeAxis\(\);
                RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge\(\);
                float chartX = \(float\) domainAxis.java2DToValue\(p.getX\(\),
                        SWTUtils.toAwtRectangle\(plotArea\), domainAxisEdge\);
                float chartY = \(float\) rangeAxis.java2DToValue\(p.getY\(\),
                        SWTUtils.toAwtRectangle\(plotArea\), rangeAxisEdge\);
    
                System.out.println\("Chart: x = " + chartX + ", y = " + chartY\);
    
                if \(\_canDrag\) \{
                    lineAnnotationX0 = \(chartX - mouseLocationX0\);
    
                    lineAnnotationX01 = chartX - mouseLocationX1;
    
                \} else if \(\_canDragb\) \{
                    boxAnnotationX0 = \(chartX - mouseLocationX0\);
    
                    boxAnnotationX01 = chartX - mouseLocationX1;
    
                    boxAnnotationY0 = \(chartY - mouseLocationY0\);
    
                    boxAnnotationY01 = chartY - mouseLocationY1;
    
                \} else if \(\_canDragp\) \{
                    rolyvalues\[0\] = \(chartX - mouseLocationX0\);
    
                    rolyvalues\[2\] = chartX - mouseLocationX1;
    
                    rolyvalues\[4\] = chartX - mouseLocationX1;
    
                    rolyvalues\[1\] = \(chartY - mouseLocationY0\);
    
                    rolyvalues\[3\] = chartY - mouseLocationY1;
    
                    rolyvalues\[5\] = chartY - mouseLocationY1;
    
                \} else if \(\_canDrage\) \{
                    ellipseX = \(chartX - mouseLocationX0\);
    
                    ellipseY = chartY - mouseLocationY0;
    
                \}
    
            \}
    
        \}\);
        frame.getCanvas\(\).addMouseListener\(new MouseListener\(\) \{
    
            public void mouseDoubleClick\(MouseEvent e\) \{
    
            \}
    
            public void mouseDown\(MouseEvent e\) \{
                int mouseX = e.x;
                int mouseY = e.y;
                System.out.println\("in mouse down"\);
                Point p = frame.translateScreenToJavaSWT\(new Point\(mouseX,
                        mouseY\)\);
    
                org.eclipse.swt.graphics.Rectangle plotArea = frame
                        .getScreenDataArea\(\);
                ValueAxis domainAxis = plot.getDomainAxis\(\);
                RectangleEdge domainAxisEdge = plot.getDomainAxisEdge\(\);
                ValueAxis rangeAxis = plot.getRangeAxis\(\);
                RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge\(\);
                float chartX = \(float\) domainAxis.java2DToValue\(p.getX\(\),
                        SWTUtils.toAwtRectangle\(plotArea\), domainAxisEdge\);
                float chartY = \(float\) rangeAxis.java2DToValue\(p.getY\(\),
                        SWTUtils.toAwtRectangle\(plotArea\), rangeAxisEdge\);
    
                float x = chartX; // Save the x coord of the click
                float y = chartY; // Save the y coord of the click
    
                if \(x >= lineAnnotationX0 && x <= lineAnnotationX01
                        && y >= lineAnnotationY0 && y <= lineAnnotationY01\) \{
    
                    \_canDrag = true;
                    mouseLocationX0 = x - lineAnnotationX0; // how far from left
    
                    mouseLocationX1 = x - lineAnnotationX01; // how far from left
    
                \} else if \(x >= boxAnnotationX0 && x <= boxAnnotationX01
                        && y >= boxAnnotationY0 && y <= boxAnnotationY01\) \{
    
                    \_canDragb = true;
                    mouseLocationX0 = x - boxAnnotationX0; // how far from left
    
                    mouseLocationX1 = x - boxAnnotationX01; // how far from left
    
                    mouseLocationY0 = y - boxAnnotationY0; // how far from left
    
                    mouseLocationY1 = y - boxAnnotationY01; // how far from left
    
                \} else if \(x >= rolyvalues\[0\] && x <= rolyvalues\[2\]
                        && y >= rolyvalues\[1\] && y <= rolyvalues\[3\]\) \{
    
                    \_canDragp = true;
                    mouseLocationX0 = \(float\) \(x - rolyvalues\[0\]\); // how far from left
    
                    mouseLocationX1 = \(float\) \(x - rolyvalues\[2\]\); // how far from left
    
                    mouseLocationX2 = \(float\) \(x - rolyvalues\[4\]\); // how far from left
    
                    mouseLocationY0 = \(float\) \(y - rolyvalues\[1\]\); // how far from left
    
                    mouseLocationY1 = \(float\) \(y - rolyvalues\[3\]\); // how far from left
    
                    mouseLocationY2 = \(float\) \(y - rolyvalues\[5\]\); // how far from left
    
                \} else if \(x >= shape.getBounds2D\(\).getX\(\) && x <= shape.getBounds2D\(\).getMaxX\(\)
                        && y >= shape.getBounds2D\(\).getY\(\) && y <= shape.getBounds2D\(\).getMaxY\(\)\) \{
    
                    \_canDrage = true;
                    mouseLocationX0 = \(float\) \(x - shape.getBounds2D\(\).getX\(\)\); // how far from left
    
                    mouseLocationY0 = \(float\) \(y - shape.getBounds2D\(\).getY\(\)\); // how far from left
    
                \}
    
                else \{
                    \_canDrag = false;
                    \_canDragb = false;
                    \_canDragp = false;
                    \_canDrage = false;
                \}
    
            \}
    
            public void mouseUp\(MouseEvent e\) \{
                int mouseX = e.x;
                int mouseY = e.y;
    
                System.out.println\("in mouse up"\);
                Point p = frame.translateScreenToJavaSWT\(new Point\(mouseX,
                        mouseY\)\);
    
                org.eclipse.swt.graphics.Rectangle plotArea = frame
                        .getScreenDataArea\(\);
                ValueAxis domainAxis = plot.getDomainAxis\(\);
                RectangleEdge domainAxisEdge = plot.getDomainAxisEdge\(\);
                ValueAxis rangeAxis = plot.getRangeAxis\(\);
                RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge\(\);
                float chartX = \(float\) domainAxis.java2DToValue\(p.getX\(\),
                        SWTUtils.toAwtRectangle\(plotArea\), domainAxisEdge\);
                float chartY = \(float\) rangeAxis.java2DToValue\(p.getY\(\),
                        SWTUtils.toAwtRectangle\(plotArea\), rangeAxisEdge\);
    
                float x = chartX; // Save the x coord of the click
                float y = chartY; // Save the y coord of the clic
    
                if \(x >= lineAnnotationX0 && x <= lineAnnotationX01
                        && y >= lineAnnotationY0 && y <= lineAnnotationY01\) \{
    
                 renderer = plot.getRenderer\(\);
                    renderer.removeAnnotation\(x1\);
                    x1 = new XYLineAnnotation\(lineAnnotationX0,
                            lineAnnotationY0, lineAnnotationX01,
                            lineAnnotationY01\);
                    renderer.addAnnotation\(x1\);
                    mouseLocationX0 = 0;
                    mouseLocationX1 = 0;
    
                    \_canDrag = false;
    
                \} else if \(x >= boxAnnotationX0 && x <= boxAnnotationX01
                        && y >= boxAnnotationY0 && y <= boxAnnotationY01\) \{
    
                     renderer = plot.getRenderer\(\);
                    renderer.removeAnnotation\(boxannotation\);
                    boxannotation = new XYBoxAnnotation\(boxAnnotationX0,
                            boxAnnotationY0, boxAnnotationX01, boxAnnotationY01\);
                    renderer.addAnnotation\(boxannotation\);
                    mouseLocationX0 = 0;
                    mouseLocationX1 = 0;
                    mouseLocationY0 = 0;
                    mouseLocationY1 = 0;
    
                    \_canDragb = false;
    
                \} else if \(x >= rolyvalues\[0\] && x <= rolyvalues\[2\]
                        && y >= rolyvalues\[1\] && y <= rolyvalues\[3\]\) \{
    
                     renderer = plot.getRenderer\(\);
                    renderer.removeAnnotation\(ploy\);
                    ploy = new XYPolygonAnnotation\(rolyvalues\);
                    renderer.addAnnotation\(ploy\);
                    mouseLocationX0 = 0;
                    mouseLocationX1 = 0;
                    mouseLocationY2 = 0;
                    mouseLocationY0 = 0;
                    mouseLocationY1 = 0;
                    mouseLocationY2 = 0;
                    \_canDragb = false;
    
                \} else if \(x >= shape.getBounds2D\(\).getX\(\) && x <= shape.getBounds2D\(\).getMaxX\(\)
                        && y >= shape.getBounds2D\(\).getY\(\) && y <= shape.getBounds2D\(\).getMaxY\(\)\) \{
    
                     renderer = plot.getRenderer\(\);
                    renderer.removeAnnotation\(ellipse\);
    
                    shape = new Ellipse2D.Double\(ellipseX, ellipseY,shape.getBounds2D\(\).getWidth\(\) , shape.getBounds2D\(\).getHeight\(\)\);
                    ellipse = new XYShapeAnnotation\(shape\);
                    renderer.addAnnotation\(ellipse\);
                    mouseLocationX0 = 0;
    
                    mouseLocationY0 = 0;
    
                    \_canDrage = false;
    
                \}
    
                else \{
                    \_canDrag = false;
                    \_canDragb = false;
                    \_canDragp = false;
                    \_canDrage = false;
                \}
    
            \}
    
        \}\);
    
        frame.setHorizontalAxisTrace\(false\);
        frame.setVerticalAxisTrace\(false\);
        frame.setChart\(chart\);
    
        shell.open\(\);
        while \(\!shell.isDisposed\(\)\) \{
            if \(\!display.readAndDispatch\(\)\)
                display.sleep\(\);
        \}
    \}
    

    }

    Thanks in advance
    raghavan

     
  • Henry Proudhon

    Henry Proudhon - 2007-05-19

    Logged In: YES
    user_id=1379312
    Originator: NO

    Hi,

    I'm trying to fix this problem, this has been annoying me for a while, so let's get rid of it!
    Your code does not have anything specific, the problem seems to be when the canvas is larger than the maximum draw width/heigh, the chart buffer is not filled properly. A workaround is to increase both DEFAULT_MAXIMUM_DRAW_WIDTH and DEFAULT_MAXIMUM_DRAW_HEIGHT to more than your screen resolution. you can also wait a few hours, hopefully this will be fixed :)

     
  • Henry Proudhon

    Henry Proudhon - 2007-05-19
    • status: open --> closed
     
  • raghz

    raghz - 2007-05-20

    Logged In: YES
    user_id=1694072
    Originator: YES

    Thanx....

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks