Draggin Axes,scaling problem.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
merlushatrash
Posts: 19
Joined: Mon Sep 26, 2011 4:48 pm
antibot: No, of course not.

Draggin Axes,scaling problem.

Post by merlushatrash » Wed Mar 14, 2012 3:56 pm

I am trying to implement feature while by dragging mouse, chart will be move(axes are setting to new positions).
It works as follows.
1) While dragging mouse code memorizes start Drag point ;
2) When draggin more it gets end drag point;
3) Converts mouse coordinates in chart values
4) Calculates diplacement between end point and start point in doman and range coordinates
5) Using Range.shift method it shifts both axes by diplacement

However I encounter into several problems.
1) It drags on too huge dustance. I am easily out of scope. Is my method for transforming mouse coordinates into value coordinates correct?
Is Range.shift method is appopriate for moving axis?
2) While dragging left or right, up or down, axes are moved not so accodring to direction but soemhow different. It only more or less works correctly while draging mouse up?
Should I drag on both directions? Or maybe I need to somehow rescale it?


maybe there is better appoarch to implement it?


There is my code:

Code: Select all

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.noname.graphs;

import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import org.jfree.chart.*;
import org.jfree.data.time.*;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.date.SerialDate;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.ui.*;

/**
 *
 * @author zhudro
 */
public class DragAxisDemo extends ApplicationFrame
{   
      private static final int RIGHT_MOUSE_BUTTON=4;
      private static final Point ORIGIN = new Point(0, 0);

    public DragAxisDemo(final String title)
    {

        super(title);
        final JFreeChart chart = createChart();
        final ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);

        panel.setDomainZoomable(false);
        panel.setRangeZoomable(false);
        panel.setPopupMenu(null);
        panel.setPreferredSize(new java.awt.Dimension(500, 270));
        
        panel.addMouseMotionListener(new MouseDragActionListener(panel));
        setContentPane(panel);

    }

    private JFreeChart createChart()
    {
        return ChartFactory.createTimeSeriesChart("Just plot", "Date", "Price per Unit", createDataset(), true, true, false);


    }

    private IntervalXYDataset createDataset()
    {

        final TimeSeries series1 = new TimeSeries("Series 1", Day.class);
        series1.add(new Day(1, SerialDate.MARCH, 2002), 12353.3);
        series1.add(new Day(2, SerialDate.MARCH, 2002), 13734.4);
        series1.add(new Day(3, SerialDate.MARCH, 2002), 14525.3);
        series1.add(new Day(4, SerialDate.MARCH, 2002), 13984.3);
        series1.add(new Day(5, SerialDate.MARCH, 2002), 12999.4);
        series1.add(new Day(6, SerialDate.MARCH, 2002), 14274.3);
        series1.add(new Day(7, SerialDate.MARCH, 2002), 15943.5);
        series1.add(new Day(8, SerialDate.MARCH, 2002), 14845.3);
        series1.add(new Day(9, SerialDate.MARCH, 2002), 14645.4);
        series1.add(new Day(10, SerialDate.MARCH, 2002), 16234.6);
        series1.add(new Day(11, SerialDate.MARCH, 2002), 17232.3);
        series1.add(new Day(12, SerialDate.MARCH, 2002), 14232.2);
        series1.add(new Day(13, SerialDate.MARCH, 2002), 13102.2);
        series1.add(new Day(14, SerialDate.MARCH, 2002), 14230.2);
        series1.add(new Day(15, SerialDate.MARCH, 2002), 11235.2);

        final TimeSeriesCollection collection = new TimeSeriesCollection(series1);
        collection.setDomainIsPointsInTime(false);
        return collection;

    }

    public static void main(final String[] args)
    {

        final DragAxisDemo demo = new DragAxisDemo("Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

    private class MouseDragActionListener implements MouseMotionListener
    {

       ChartPanel panel;
       Point startPoint =ORIGIN;

        public MouseDragActionListener(ChartPanel panel)
        {
            this.panel=panel;
        }

        @Override
        public void mouseDragged(MouseEvent evt)
        {
            if (evt.getModifiers() != RIGHT_MOUSE_BUTTON)
            {
                return;
            } 
            
          if(startPoint.equals(ORIGIN))
          {
            startPoint=new Point(evt.getX(),evt.getY()); 
          }
          
         Point endPoint=new Point(evt.getX(),evt.getY()); 
         
         Point.Double startChartXPoint = mouse2ValuePoint(panel,startPoint);
         Point.Double endChartXPoint = mouse2ValuePoint(panel,endPoint);
         
         XYPlot plot=panel.getChart().getXYPlot();
         
          moveGraphHorizontally(plot,calculateHorizontalDiplacement(startChartXPoint,endChartXPoint));
            moveGraphVertically(plot,calculateVerticalDiplacement(startChartXPoint,endChartXPoint));
            
        }

        @Override
        public void mouseMoved(MouseEvent evt)
        {
        }
    }
    
    
     public static Point.Double mouse2ValuePoint(ChartPanel chartPanel,Point point)
    {                                  
        XYPlot plot=chartPanel.getChart().getXYPlot();
        
        Rectangle2D plotArea = chartPanel.getScreenDataArea();
        
        ValueAxis domainAxis = plot.getDomainAxis();
        ValueAxis rangeAxis = plot.getRangeAxis();

        RectangleEdge domainAxisEdge = plot.getDomainAxisEdge();
        
        RectangleEdge rangeAxisEdge = plot.getRangeAxisEdge();
                
        Point2D p = chartPanel.translateScreenToJava2D(point);

        double chartX = domainAxis.java2DToValue(p.getX(), plotArea,domainAxisEdge);                              
        
        double chartY = rangeAxis.java2DToValue(p.getY(), plotArea, rangeAxisEdge);
        
        return new Point.Double(chartX,chartY);        
    }
     
     
     private double calculateHorizontalDiplacement(Point.Double start,Point.Double end)
     {
         return end.x-start.x;
     }
     private double calculateVerticalDiplacement(Point.Double start,Point.Double end)
     {
         return end.y-start.y;
     }
     
      public static void moveGraphHorizontally(XYPlot plot,double move)    
    {           
        moveAxis(plot.getDomainAxis(),move);                       
    }
    
    public static void moveGraphVertically(XYPlot plot,double move)    
    {
        moveAxis(plot.getRangeAxis(),move);                               
    }
    
    
    private static void moveAxis(ValueAxis axis,double move)
    {           
        
        Range shiftedRange= Range.shift(axis.getRange(), move);
                                
        axis.setRange(shiftedRange);
    }
}

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Draggin Axes,scaling problem.

Post by matinh » Wed Mar 14, 2012 4:08 pm

Hi!

You probably should search this forum for "Pannable" and find a solution like this one.

hth,
- martin

Locked