Yet another Pan/Shift class (source included)

A free public discussion forum for the JFreeChart class library.

Yet another Pan/Shift class (source included)

Postby gribas » Thu Jan 26, 2006 5:48 pm

Hi,

I was looking for some code to pan/shift a plot. I coudn't find anything
that suit my needs so I implemented this little class. Ideally it should be
incorporated to the ChartPanel :) .

It only responds to keyboard events but shouldn't be hard to add mouse
control, just multiply the number of pixels the mouse travelled by the default delta for 1 pixel.

I've only tested with a XYPlot but it should work with similar plots.

Have fun! ;)



Code: Select all
/*
* Copyright 2006 CPqD - Centro de Pesquisa e Desenvolvimento em Telecomunicações.
* http://www.cpqd.com.br/
*
* File ChartPanelShiftController.java created on 26/01/2006 10:49:42
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
* USA. 
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------------
* ChartPanelShiftController.java
* ------------------------
* Original Author:  Gustavo H. Sberze Ribas;
* Contributor(s):  ;
*
* Changes
* --------------------------
* 26-Jan-2006 : First Version (GR);
*
*/

package br.com.cpqd.chart.tools;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.ContourPlot;
import org.jfree.chart.plot.FastScatterPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;

/**
* This class provides ways to shift (aka pan/scroll) a plot. The shift is done
* through the arrow keys and its step can be configured to be a fixed amount,
* a percentual of the current axis or a range in pixels.
* <p>
* This class only supports plots of type {@link org.jfree.chart.plot.XYPlot XYPlot},
* {@link org.jfree.chart.plot.ContourPlot ContourPlot} and
* {@link org.jfree.chart.plot.FastScatterPlot FastScatterPlot}.
* <p>
* Use &larr; and &rarr; to shift the plot left and right; <br>
* Use &uarr; and &darr; to shift the plot up and down; <br>
* Press the SHIFT key to increase the shift by a factor of 10.
*
* @author  Gustavo H. Sberze Ribas (CPqD)
* @version 0.1
*/
public class ChartPanelShiftController implements KeyListener{

    /** PAN plot by a fixed percentual of the range (eg. 1%) */
    public static final int SHIFT_PERCENTUAL = 1;
    /** PAN plot by a fixed number of pixels (eg. 1px)  */
    public static final int SHIFT_PIXEL = 2;
    /** PAN plot by a fixed amout (eg. 5 range units) */
    public static final int SHIFT_FIXED = 3;


    /** The chart panel we're using */
    protected ChartPanel chartPanel;
   
    /** Does this plot supports shifting? Pie charts for example don't. */
    protected boolean plotSupported = false;
   
    /** The shift type. (default {@link #SHIFT_PIXEL} ) */
    protected int shiftType = SHIFT_PIXEL;
   
    /** Fixed shift amount for domain axis */
    protected double fixedDomainShiftUnits;
    /** Fixed shift amount for range axis */
    protected double fixedRangeShiftUnits;
   
    /** By default we assume that the range axis is the vertical one (ie,
     * PlotOrientation.VERTICAL (axesSwaped=false). If the range axis is the
     * horizontal one (ie, PlotOrientation.HORIZONTAL) this variable should
     * be set to true. */
    protected boolean axesSwaped = false;
   
    /**
     * Creates a new controller to handle plot shifts.
     * @param chartPanel The panel displaying the plot.
     */
    public ChartPanelShiftController(ChartPanel chartPanel) {
        super();
        this.chartPanel = chartPanel;
       
        // Check to see if plot is shiftable
        Plot plot = chartPanel.getChart().getPlot();
        if ((plot instanceof XYPlot)          ||
            (plot instanceof FastScatterPlot) ||
            (plot instanceof ContourPlot)     
           ) {
            plotSupported = true;           
            axesSwaped = isHorizontalPlot(plot);
        }
    }
   
   
    /**
     * Returns the plot orientation.
     * @return True = {@link org.jfree.chart.plot.PlotOrientation#VERTICAL VERTICAL};
     *         False = {@link org.jfree.chart.plot.PlotOrientation#HORIZONTAL HORIZONTAL}
     */
    protected boolean isHorizontalPlot(Plot plot) {
        if (plot instanceof XYPlot) {
            return ((XYPlot)plot).getOrientation()==PlotOrientation.HORIZONTAL;           
        }
        if (plot instanceof FastScatterPlot) {
            return ((FastScatterPlot)plot).getOrientation()==PlotOrientation.HORIZONTAL;           
        }
        return false;
    }


    /**
     * Returns the ValueAxis for the plot or <code>null</code> if the plot
     * doesn't have one.
     * @param chart     The chart
     * @param domain    True = get Domain axis. False = get Range axis.
     * @return The selected ValueAxis or <code>null</code> if the plot doesn't
     * have one.
     */
    protected ValueAxis getPlotAxis(JFreeChart chart, boolean domain) {
        // Where's the Shiftable interface when we need it ?? ;)
        Plot plot = chart.getPlot();
        if (plot instanceof XYPlot) {
            return domain?((XYPlot)plot).getDomainAxis():((XYPlot)plot).getRangeAxis();           
        }
        if (plot instanceof FastScatterPlot) {
            return domain?((FastScatterPlot)plot).getDomainAxis():((FastScatterPlot)plot).getRangeAxis();           
        }
        if (plot instanceof ContourPlot) {
            return domain?((ContourPlot)plot).getDomainAxis():((ContourPlot)plot).getRangeAxis();           
        }
        return null;
    }

   
    /**
     * Pan/Shifts a plot if the arrow keys are pressed.
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
     */
    public void keyPressed(KeyEvent e) {
        if (!plotSupported) return;
       
        int keyCode = e.getKeyCode();

        // we're only interested in arrows (code 37,38,39,40)
        if ((keyCode < 37) || (keyCode > 40)) return;

        // The axis we're gonna shift
        ValueAxis axis = null;
       
        // Delta is the amount we'll shift in axis units.
        double delta;
       
        boolean domainShift = false;  // used for PAN_FIXED
        // Calculations for the domain axis
        if ((keyCode == KeyEvent.VK_LEFT) || (keyCode == KeyEvent.VK_RIGHT)) {
            axis = getPlotAxis(chartPanel.getChart(),!axesSwaped);
            domainShift = true;
        }
        // Calculations for the range axis
        else {
            axis = getPlotAxis(chartPanel.getChart(),axesSwaped);
        }

        // Let's calculate 'delta', the amount by which we'll shift the plot
        switch (shiftType) {
            case SHIFT_PERCENTUAL:
                delta = (axis.getUpperBound()-axis.getLowerBound())/100.0;
                break;
            case SHIFT_FIXED:
                delta = (domainShift ? fixedDomainShiftUnits : fixedRangeShiftUnits);
                break;
            case SHIFT_PIXEL:  // also the default
            default: 
                // Let's find out what's the range for 1 pixel.
                final Rectangle2D scaledDataArea = chartPanel.getScreenDataArea();
                delta = axis.getRange().getLength() / (scaledDataArea.getWidth());
                break;
        }

        // Shift modifier multiplies delta by 10
        if (e.isShiftDown()) {
            delta *= 10;
        }
       
        switch (keyCode) {
            case KeyEvent.VK_LEFT:
            case KeyEvent.VK_DOWN:
                axis.setRange(axis.getLowerBound()-delta,axis.getUpperBound()-delta);
                break;
            case KeyEvent.VK_UP:
            case KeyEvent.VK_RIGHT:
                axis.setRange(axis.getLowerBound()+delta,axis.getUpperBound()+delta);
                break;
        }
    }
   
    /*
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
     */
    public void keyTyped(KeyEvent e) {}
    /*
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    public void keyReleased(KeyEvent e) {}


    /**
     * Returns the fixed shift step for the domain axis.
     * @return the fixed shift step for the domain axis.
     */
    public double getFixedDomainShiftUnits() {
        return fixedDomainShiftUnits;
    }
    /**
     * Sets the fixed shift step for the domain axis.
     * @param fixedDomainShiftUnits the fixed shift step for the domain axis.
     */
    public void setFixedDomainShiftUnits(double fixedDomainShiftUnits) {
        this.fixedDomainShiftUnits = fixedDomainShiftUnits;
    }
    /**
     * Returns the fixed shift step for the range axis.
     * @return the fixed shift step for the range axis.
     */
    public double getFixedRangeShiftUnits() {
        return fixedRangeShiftUnits;
    }
    /**
     * Sets the fixed shift step for the range axis.
     * @param fixedRangeShiftUnits the fixed shift step for the range axis.
     */
    public void setFixedRangeShiftUnits(double fixedRangeShiftUnits) {
        this.fixedRangeShiftUnits = fixedRangeShiftUnits;
    }
    /**
     * Returns the current shift type.
     * @return the current shift type.
     * @see #SHIFT_FIXED
     * @see #SHIFT_PERCENTUAL
     * @see #SHIFT_PIXEL
     */
    public int getShiftType() {
        return shiftType;
    }
    /**
     * Sets the shift type.
     * @param the new shift type.
     * @see #SHIFT_FIXED
     * @see #SHIFT_PERCENTUAL
     * @see #SHIFT_PIXEL
     */
    public void setShiftType(int shiftType) {
        this.shiftType = shiftType;
    }
    /**
     * Returns whether or not the plot supports shifting.
     * @return True if plot can be shifted.
     */
    public boolean isPlotSupported() {
        return plotSupported;
    }
}

:wink:
gribas
 
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Re: Yet another Pan/Shift class (source included)

Postby gribas » Thu Jan 26, 2006 5:52 pm

One example that uses the above class:

Code: Select all

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;
import org.jfree.ui.*;

import br.com.cpqd.chart.tools.ChartPanelShiftController;

public class ShiftTest extends JFrame{

    private ChartPanelShiftController controller;
   
    public ShiftTest() {
        ChartPanel panel = new ChartPanel(createChart(createDataset(100)));
        controller = new ChartPanelShiftController(panel);
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
        .addKeyEventDispatcher(new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED) {
                    controller.keyPressed(e);
                }
                return false;
            }
        });
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel,BorderLayout.CENTER);
        getContentPane().add(createOptionPanel(),BorderLayout.SOUTH);
    }
   
    private JPanel createOptionPanel() {
        JPanel optionPanel = new JPanel();
        ButtonGroup bg = new ButtonGroup();
        optionPanel.setLayout(new GridLayout(1,3));
        JRadioButton btn = new JRadioButton("Percentual (1%)");
        btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {   
                controller.setShiftType(ChartPanelShiftController.SHIFT_PERCENTUAL);
            }});
        bg.add(btn); optionPanel.add(btn,null);
        btn = new JRadioButton("Fixed (20u Domain, 50u Range)");
        btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {   
                controller.setShiftType(ChartPanelShiftController.SHIFT_FIXED);
                controller.setFixedDomainShiftUnits(20);
                controller.setFixedRangeShiftUnits(50);               
            }});
        bg.add(btn); optionPanel.add(btn,null);
        btn = new JRadioButton("Pixel (1px)");
        btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {   
                controller.setShiftType(ChartPanelShiftController.SHIFT_PIXEL);
                controller.setFixedDomainShiftUnits(20);
                controller.setFixedRangeShiftUnits(50);               
            }});
        btn.setSelected(true); bg.add(btn); optionPanel.add(btn,null);
        return optionPanel;
    }

    /**
     * Creates a sample dataset.
     * @return a sample dataset.
     */
    public XYDataset createDataset(int numPoints) {
        final XYSeries series1 = new XYSeries("Test");
        for(int i=0; i<numPoints; i++) {
            series1.add(i, Math.random()*100);           
        }
        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
        return dataset;
    }

    /**
     * Creates a chart.
     * @param dataset  the data for the chart.
     * @return a chart.
     */
    public JFreeChart createChart(final XYDataset dataset) {
        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Pan Test - use arrow keys (Press SHIFT to pan 10x faster)", // chart title
            "X",                      // x axis label
            "Y",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            false,                     // include legend
            false,                     // tooltips
            false                     // urls
        );     
        // get a reference to the plot for further customisation...
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
       
        return chart;
    }

   /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final ShiftTest demo = new ShiftTest();
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    } 
}
Last edited by gribas on Thu Jan 26, 2006 6:53 pm, edited 1 time in total.
gribas
 
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Postby david.gilbert » Thu Jan 26, 2006 5:56 pm

Thanks! I created an entry in the Patch Manager so I don't forget about this when the post rolls off the page:

https://sourceforge.net/tracker/index.p ... tid=315494

I will try out your code tomorrow I hope...meanwhile comments from others are most welcome!
Dave Gilbert
JFreeChart Project Leader

:idea: Buy the JFreeChart Developer Guide :idea:
david.gilbert
JFreeChart Project Leader
 
Posts: 10759
Joined: Fri Mar 14, 2003 10:29 am
Location: Europe

Postby Manu38 » Wed Feb 01, 2006 3:29 pm

According to you, it is not difficult to implement the use of a scrollbare instead of the keyboard ??


Have you got any feedback on your small application about performances with big charts, about usage with other charts than XYplot ???


Thanks,


Manu
Manu38
 
Posts: 14
Joined: Sun Jan 22, 2006 9:52 pm

Postby gribas » Wed Feb 01, 2006 6:21 pm

Manu38 wrote:According to you, it is not difficult to implement the use of a scrollbare instead of the keyboard ??


No, it's not. Take a look at the keyPressed() method to see how the shifting is done. In a nutshell you'll have to map the BoundedRangeModel of the scrollbar to your chart. The min and max of the rangeModel map to your axis desired lower and upper bounds. The value of the rangeModel maps to the current axis' lower value and the (extent+val) maps to the current axis' upper value. You'll probably wanna use some sort of correction factor since the BoundedRangeModel only accepts integer values. Should be fun... :D

Google for 'PanScrollZoomDemo.java' if you're in trouble... :wink:

Manu38 wrote:Have you got any feedback on your small application about performances with big charts


It's all about the renderers, since each shift/pan forces a repaint. Usually the performance decreases when you increase the chart dimensions.

Manu38 wrote:about usage with other charts than XYplot ???


Never tried, but should work with FastScatterPlot and CountourPlot since they use the same kind of axis.
gribas
 
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Postby Manu38 » Thu Feb 02, 2006 8:49 am

Thanks for your answer,

I have got a last question : your are not the first to tell me about 'PanScrollZoomDemo.java' but this class isn't in the sample sources i bought, where can i found it ??

Regards,


Manu
Manu38
 
Posts: 14
Joined: Sun Jan 22, 2006 9:52 pm

Postby gribas » Thu Feb 02, 2006 12:02 pm

Manu38 wrote:I have got a last question : your are not the first to tell me about 'PanScrollZoomDemo.java' but this class isn't in the sample sources i bought, where can i found it ??


Already told you... google for it. If you dislike google you can use Altavista or Yahoo... ;)

PanScrollZoomDemo was originally written by Matthias Rose and later received contributions from Eduardo Ramalho and David Gilbert. If I'm not mistaken it was included with previous versions of JFC.

It probably wasn't included with the sources you bought because it no longer compiles against JFC 1.0.x, you'll have to adapt the code to the new API.

The version modified by Eduardo Ramalho can be found here http://www.jfree.org/phpBB2/viewtopic.php?t=644&highlight=panscrollzoomdemo.

The version that was distributed with JFC can be found through search engines.
gribas
 
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Postby Manu38 » Fri Feb 03, 2006 9:28 am

Google ???? no never heared about it .... :D

Is one version of this demo working with the latest version of JFreeChart ??


Thanks,

Manu
Manu38
 
Posts: 14
Joined: Sun Jan 22, 2006 9:52 pm

Postby gribas » Fri Feb 03, 2006 12:58 pm

Manu38 wrote:Is one version of this demo working with the latest version of JFreeChart ??


Dunno. Anyway, I've quickly adapted my previous sample to include a ScrollBar above the chart. I didn't use a correction factor so it will only scroll by integer amounts. I've also taken advantage of the fact that the default values for the rangeModel matched the bounds of the chart.


One last thing, be sure to check the patch submissions on sourceForge. There's some cool stuff there:
https://sourceforge.net/tracker/?group_ ... tid=315494

Code: Select all

import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.*;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.event.ChartChangeEvent;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;
import org.jfree.ui.*;

import br.com.cpqd.chart.tools.ChartPanelShiftController;

public class ShiftTest extends JFrame{

    private ChartPanelShiftController controller;
    private JScrollBar bar = new JScrollBar(Adjustable.HORIZONTAL);
    ChangeListener cl;

    public ShiftTest() {
        final ChartPanel panel = new ChartPanel(createChart(createDataset(100)));
        controller = new ChartPanelShiftController(panel);
        KeyboardFocusManager.getCurrentKeyboardFocusManager()
        .addKeyEventDispatcher(new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(KeyEvent e) {
                if (e.getID() == KeyEvent.KEY_PRESSED) {
                    controller.keyPressed(e);
                }
                return false;
            }
        });
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel,BorderLayout.CENTER);
        getContentPane().add(createOptionPanel(),BorderLayout.SOUTH);

       
        /** CODE FOR ScrollBar */
        getContentPane().add(bar,BorderLayout.NORTH);
        final BoundedRangeModel model = bar.getModel();
        panel.getChart().getXYPlot().getDomainAxis().setUpperBound(40);
        model.setExtent(40);       
        panel.getChart().addChangeListener(new ChartChangeListener() {
            public void chartChanged(ChartChangeEvent event) {
                ValueAxis axis = panel.getChart().getXYPlot().getDomainAxis();
                model.removeChangeListener(cl); //avoids recursion
                model.setValue((int) axis.getLowerBound());
                model.setExtent((int) axis.getRange().getLength());
                model.addChangeListener(cl);
            }
        });
        model.addChangeListener(cl = new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                ValueAxis axis = panel.getChart().getXYPlot().getDomainAxis();
                axis.setRange(model.getValue(),model.getValue()+model.getExtent());
            }
        });
        /** END */
    }
   
    private JPanel createOptionPanel() {
        JPanel optionPanel = new JPanel();
        ButtonGroup bg = new ButtonGroup();
        optionPanel.setLayout(new GridLayout(1,3));
        JRadioButton btn = new JRadioButton("Percentual (1%)");
        btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {   
                controller.setShiftType(ChartPanelShiftController.SHIFT_PERCENTUAL);
            }});
        bg.add(btn); optionPanel.add(btn,null);
        btn = new JRadioButton("Fixed (20u Domain, 50u Range)");
        btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {   
                controller.setShiftType(ChartPanelShiftController.SHIFT_FIXED);
                controller.setFixedDomainShiftUnits(20);
                controller.setFixedRangeShiftUnits(50);               
            }});
        bg.add(btn); optionPanel.add(btn,null);
        btn = new JRadioButton("Pixel (1px)");
        btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {   
                controller.setShiftType(ChartPanelShiftController.SHIFT_PIXEL);
                controller.setFixedDomainShiftUnits(20);
                controller.setFixedRangeShiftUnits(50);               
            }});
        btn.setSelected(true); bg.add(btn); optionPanel.add(btn,null);
        return optionPanel;
    }

    /**
     * Creates a sample dataset.
     * @return a sample dataset.
     */
    public XYDataset createDataset(int numPoints) {
        final XYSeries series1 = new XYSeries("Test");
        for(int i=0; i<numPoints; i++) {
            series1.add(i, Math.random()*100);           
        }
        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
        return dataset;
    }

    /**
     * Creates a chart.
     * @param dataset  the data for the chart.
     * @return a chart.
     */
    public JFreeChart createChart(final XYDataset dataset) {
        // create the chart...
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Pan Test - use arrow keys (Press SHIFT to pan 10x faster)", // chart title
            "X",                      // x axis label
            "Y",                      // y axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            false,                     // include legend
            false,                     // tooltips
            false                     // urls
        );     
        // get a reference to the plot for further customisation...
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        return chart;
    }

   /**
     * Starting point for the demonstration application.
     * @param args  ignored.
     */
    public static void main(final String[] args) {
        final ShiftTest demo = new ShiftTest();
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}
gribas
 
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Postby Manu38 » Wed Feb 08, 2006 8:42 am

Thanks,


Regards

Manu
Manu38
 
Posts: 14
Joined: Sun Jan 22, 2006 9:52 pm

Postby Guest » Wed Feb 08, 2006 9:22 pm

You can remove the KeyboardFocusManager requirement by putting
the following code in the controller constructor (works for simple
charts at least):

// start code snippet

chartPanel.setFocusable(true );
chartPanel.addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
chartPanel.requestFocusInWindow();
}
});
chartPanel.addKeyListener( this );

// end code snippet
Guest
 

Postby jvolkar » Tue Mar 21, 2006 9:38 pm

The snippette above works quite well with one small change: The chartPanel parameter to the constructor must be changed to final so the anon-inner class can referenence it.

Thanks! Now to make it respond to shift+mouse-moves in addition to the key board. If I get something workable I'll post it back here.

Regards, John Volkar[/i]
jvolkar
 
Posts: 1
Joined: Tue Mar 21, 2006 9:26 pm

Postby knc » Tue Aug 22, 2006 5:01 pm

Thanks for posting this example. One question though - if I change the dataset to be, say 10,000 points instead of 100, the scrollbar does not scroll past 100. How do you fix that?

knc
knc
 
Posts: 19
Joined: Mon Aug 07, 2006 11:12 pm

Postby gribas » Tue Aug 22, 2006 6:21 pm

Hello,

100 is the default maximum value, just change it.

Code: Select all
public ShiftTest() {
   final ChartPanel panel = new ChartPanel(createChart(createDataset(1000)));
   bar.setMaximum(1000);
   controller = new ChartPanelShiftController(panel);
   ... 
}
gribas
 
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Postby knc » Wed Aug 23, 2006 1:04 am

Thanks for the tip.
knc
 
Posts: 19
Joined: Mon Aug 07, 2006 11:12 pm

Next

Return to JFreeChart - General

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 5 guests