[PATCH] XYStepRenderer enhancement

A free public discussion forum for the JFreeChart class library.

[PATCH] XYStepRenderer enhancement

Postby matinh » Fri Oct 13, 2006 12:12 pm

Hi!

I coded an enhancement to the current XYStepRenderer class and thought it could be usefull to others as well. Let me explain first what the enhancement is all about:

The original XYStepRenderer class connected two datapoints by drawing a horizontal line from the X-value of the first data point to the X-value of the second datapoint and afterward drawing a vertical line to the endpoint (e.g.: first right, then up). What i needed was a renderer doing it the other way round (e.g. first up, then right).

My enhancement adds a getter and a setter method allowing one to get/set the step position: getStepPosition()/setStopPosition(). I also added two static fields STEP_BEGIN and STEP_END representing the two supported positions (There could be a STEP_MIDDLE if somebody needs it).

So here is my patch:
Code: Select all
--- XYStepRenderer.java.orig        2006-01-27 15:50:02.000000000 +0100
+++ XYStepRenderer.java     2006-10-13 13:06:55.000000000 +0200
@@ -94,6 +94,15 @@

     /** For serialization. */
     private static final long serialVersionUID = -8918141928884796108L;
+
+    /** Constant for drawing the step at the beginning (at the previous datapoint). */
+    public static final int STEP_BEGIN = 1;
+    /** Constant for making the step at the end (at the active datapoint). */
+    public static final int STEP_END   = 2;
+
+    /** At which position to perform the step. */
+    private int stepPosition = STEP_END;
+

     /**
      * Constructs a new renderer with no tooltip or URL generation.
@@ -116,6 +125,23 @@
         setShapesVisible(false);
     }

+
+    /**
+     * @return Returns the stepPosition.
+     */
+    public int getStepPosition()
+    {
+        return stepPosition;
+    }
+
+    /**
+     * @param stepPosition The stepPosition to set.
+     */
+    public void setStepPosition(int stepPosition)
+    {
+        this.stepPosition = stepPosition;
+    }
+
     /**
      * Draws the visual representation of a single data item.
      *
@@ -189,9 +215,11 @@
                         g2.draw(line);
                     }
                     else {  //this handles the need to perform a 'step'.
-                        line.setLine(transY0, transX0, transY1, transX0);
+                        double x = (this.stepPosition == STEP_END) ? transX0 : transX1;
+                        double y = (this.stepPosition == STEP_END) ? transY1 : transY0;
+                        line.setLine(transY0, transX0, y, x);
                         g2.draw(line);
-                        line.setLine(transY1, transX0, transY1, transX1);
+                        line.setLine(y, x, transY1, transX1);
                         g2.draw(line);
                     }
                 }
@@ -202,9 +230,11 @@
                         g2.draw(line);
                     }
                     else {  //this handles the need to perform a 'step'.
-                        line.setLine(transX0, transY0, transX1, transY0);
+                        double x = (this.stepPosition == STEP_END) ? transX1 : transX0;
+                        double y = (this.stepPosition == STEP_END) ? transY0 : transY1;
+                        line.setLine(transX0, transY0, x, y);
                         g2.draw(line);
-                        line.setLine(transX1, transY0, transX1, transY1);
+                        line.setLine(x, y, transX1, transY1);
                         g2.draw(line);
                     }
                 }



What do you thiink?

- martin
matinh
 
Posts: 467
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

[PATCH] XYStepRenderer enhancement

Postby matinh » Thu Jan 17, 2008 9:48 am

Hi!

I adapted my patch from above to work with JFreeChart 1.0.9. In addition one can now instruct the renderer to not draw the vertical lines (the steps).

Tha patch is available unter the ID 1873517 from sourceforge:
http://sourceforge.net/tracker/index.php?func=detail&aid=1873517&group_id=15494&atid=315494

Hope it will be included in JFreeChart some day.

- martin
matinh
 
Posts: 467
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Postby david.gilbert » Fri Jan 18, 2008 1:15 pm

Thanks. This looks like a good enhancement. I might change the integer constants to a class defining some constants (for example, like the DatasetRenderingOrder class) - it is more verbose, but prevents out-of-range constants, gives more info in the Javadocs for valid values, and allows code-completion in IDEs to suggest values (and, of course, still works in JDK1.3, otherwise I think an enumeration would be the solution).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11358
Joined: Fri Mar 14, 2003 10:29 am

Postby matinh » Fri Jan 18, 2008 2:19 pm

david.gilbert wrote:This looks like a good enhancement.

Thanks.
I might change the integer constants to a class defining some constants

Ok, adapt to what ever you like.

Could you please drop a note when it's in SVN.

Thanks,
- martin
matinh
 
Posts: 467
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Postby uvoigt » Fri Jan 18, 2008 2:39 pm

I did already implement this myself but did not post it. Sorry!!
My implementation is a bit more general. You can define a percentage at which x point the step should be rendered.

Example:
Step: 0% => Step is perfomed at x0
Step: 100% => Step is performed at x1
Step: 25% => Step is performed at x0+0.25*(x1-x0)

If you like I can also create a patch for this.
uvoigt
 
Posts: 166
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Postby david.gilbert » Fri Jan 18, 2008 3:43 pm

uvoigt wrote:My implementation is a bit more general. You can define a percentage at which x point the step should be rendered.


I like that idea. A patch would be great - thanks!
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11358
Joined: Fri Mar 14, 2003 10:29 am

Postby uvoigt » Fri Jan 18, 2008 6:14 pm

Mmmh, I have problems creating a fast patch because I don't have the current 1.0.x freechart sources available. So I am posting a class which implements the behaviour and subclasses the XYStepRenderer.

It should be easy to integrate it in the common XYStepRenderer sources.
I'll post it in the patch tracker (id 1873517).
uvoigt
 
Posts: 166
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Postby uvoigt » Fri Jan 18, 2008 6:25 pm

Uuups, cannot post patches to trackid 1873517. Therefore I have added another patch thread 1874890 :
http://sourceforge.net/tracker/index.ph ... tid=315494

I am sorry that the patch is not a 'real' patch but at the moment I am full with work and have no more time to make it nice. Anyway I hope you can use it :-)

Ulrich
uvoigt
 
Posts: 166
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Postby matinh » Mon Jan 21, 2008 10:35 am

Thanks for this. I also like the idea and think this is the way to go with the extension.

I didn't try your class yet, but what I think what's missing in your class is the adaption of the equals(), hash() and clone() methods, but this should be rather trivial to implement.

- martin
matinh
 
Posts: 467
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Postby uvoigt » Mon Jan 21, 2008 10:57 am

You are right. Feel free to add it :-)
uvoigt
 
Posts: 166
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Postby uvoigt » Mon Jan 28, 2008 2:19 pm

I found some time to create a patch against the 1.0.x branch. I have added the patch to the tracker https://sourceforge.net/tracker/index.p ... tid=315494.

Any comments are welcome!

David, can you check if you want to commit the patch to freechart?

Thanks
Ulrich
uvoigt
 
Posts: 166
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Postby matinh » Mon Jan 28, 2008 2:41 pm

Hi Ulrich!

Just had a quick look at your patch which looks good to me. One thing I noticed is that stepPoint has to be within [0,1] which is fine. However, this is not checked in the setStepPoint() method.

Just my 2¢,
- martin
matinh
 
Posts: 467
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Postby uvoigt » Wed Jan 30, 2008 1:08 pm

Hi Martin!

oh yeah, you are right. I have posted a corrected path with range checks.

Ulrich
uvoigt
 
Posts: 166
Joined: Mon Aug 23, 2004 10:50 am
Location: Germany

Postby david.gilbert » Thu Feb 14, 2008 4:55 am

Thanks very much to both of you for working on this. I've just committed the patch to Subversion for inclusion in the 1.0.10 release.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11358
Joined: Fri Mar 14, 2003 10:29 am

Postby matinh » Thu Feb 14, 2008 8:37 am

Aahhh, seems patch-month is starting up now :-).
That's good news!
matinh
 
Posts: 467
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Next

Return to JFreeChart - General

Who is online

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

cron