Equal Axis by click

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Equal Axis by click

Post by Naxter » Mon May 23, 2016 12:00 pm

Hello everyone.

I have a normal XYPlot with a XYLineAndShapeRenderer and XYSeriesCollection. I am plotting different shapes. My clients want to have a button which generates "equal axis". Check out the images then you will get what I need. It is kind of squared axis. I found a old topic about it, but those solutions did not work for me.

For Example at first it is like this:
http://imgur.com/PNJKUeB

After clicking the button it has to be like this:
http://imgur.com/RnRGMzi

I tried to calculate a right scale with heigh and width, but I do not get it work:

Code: Select all

private void configureSquareAxis()
  {
    double height = getDrawingPanel().getHeight();
    double width = getDrawingPanel().getWidth();
    if((width != 0 && height != 0))
    {
      //RANGE
      ValueAxis rangeAx = getDrawingPanel().getXYPlot().getRangeAxis();
      double differenceRange = rangeAx.getUpperBound() - rangeAx.
          getLowerBound();
      //DOMAIN
      ValueAxis domainAx = getDrawingPanel().getXYPlot().getDomainAxis();
      double differenceDomain = domainAx.getUpperBound() - domainAx.
          getLowerBound();
      if(height <= width)
      {
        double percentage = width / height;
        double range = differenceDomain * percentage;
        double newUpperBound = domainAx.getLowerBound() + range;
        domainAx.setUpperBound(newUpperBound);
      }
      else
      {
        double percentage = height / width;
        double range = differenceRange * percentage;
        double newUpperBound = rangeAx.getLowerBound() + range;
        rangeAx.setUpperBound(newUpperBound);
      }
    }
  }
Does anyone see my mistake or has a better idea? Maybe a function of JFreeChart? Or a calculation idead?
Thank you for your help.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Equal Axis by click

Post by paradoxoff » Mon May 23, 2016 9:17 pm

In your code, you are changing the value range of the axis, i.e. its length in data space, but you rather want to change its length in Java2D space.
Naxter wrote:I found a old topic about it, but those solutions did not work for me.
Do you have a lok to this "old topic"?

Naxter
Posts: 45
Joined: Thu Jun 26, 2014 8:24 am
antibot: No, of course not.
Location: Germany, Aachen

Re: Equal Axis by click

Post by Naxter » Tue May 24, 2016 7:19 am

Yeah, you are absoluty right. So i tried a little bit more and found this solution: (It works great for me)

Code: Select all

ValueAxis rangeAx = getDrawingPanel().getXYPlot().getRangeAxis();
      double differenceY = rangeAx.getUpperBound() - rangeAx.
          getLowerBound();
      //DOMAIN
      ValueAxis domainAx = getDrawingPanel().getXYPlot().getDomainAxis();
      double differenceX = domainAx.getUpperBound() - domainAx.
          getLowerBound();
      double zoomY = height / differenceY;
      double zoomX = width / differenceX;
      if(zoomY < zoomX)
      {
        double range = (width / zoomY - differenceX) / 2;
        domainAx.setUpperBound(range + domainAx.getUpperBound());
        domainAx.setLowerBound(domainAx.getLowerBound() - range);
      }
      else
      {
        double range = (height / zoomX - differenceY) / 2;
        rangeAx.setUpperBound(range + rangeAx.getUpperBound());
        rangeAx.setLowerBound(rangeAx.getLowerBound() - range);
      }
The link to the topic is : http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=15613 it faces some other not complety same issues

Locked