CrosshairOverlay enhancement

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

CrosshairOverlay enhancement

Post by John Matthews » Tue May 23, 2017 9:49 am

This recent Q&A concerning CrosshairOverlay suggests that the present implementation ignores the crosshair's outline and font properties . A possible enhancement might be to alter the drawHorizontalCrosshair and drawVerticalCrosshair methods as shown below. Altering CrosshairOverlayDemo1 as follows then produces the result shown.

Code: Select all

xCrosshair.setLabelFont(xCrosshair.getLabelFont().deriveFont(20f));
xCrosshair.setLabelBackgroundPaint(new Color(1f, 1f, 1f, 0f));
xCrosshair.setLabelOutlineVisible(false);
Image

Code: Select all

--- /opt/jfreechart/source/org/jfree/chart/panel/CrosshairOverlay.java	2014-07-31 14:02:10.000000000 -0400
+++ source/org/jfree/chart/panel/CrosshairOverlay.java	2017-05-23 03:46:56.000000000 -0400
@@ -42,6 +42,7 @@
 
 package org.jfree.chart.panel;
 
+import java.awt.Font;
 import java.awt.Graphics2D;
 import java.awt.Paint;
 import java.awt.Rectangle;
@@ -282,6 +283,7 @@
                     dataArea.getMaxX(), y);
             Paint savedPaint = g2.getPaint();
             Stroke savedStroke = g2.getStroke();
+            Font savedFont = g2.getFont();
             g2.setPaint(crosshair.getPaint());
             g2.setStroke(crosshair.getStroke());
             g2.draw(line);
@@ -308,11 +310,13 @@
                 g2.setPaint(crosshair.getLabelBackgroundPaint());
                 g2.fill(hotspot);
                 g2.setPaint(crosshair.getLabelOutlinePaint());
-                g2.draw(hotspot);
+				if (crosshair.isLabelOutlineVisible()) { g2.draw(hotspot); }
+				g2.setFont(crosshair.getLabelFont());
                 TextUtilities.drawAlignedString(label, g2, xx, yy, alignPt);
             }
             g2.setPaint(savedPaint);
             g2.setStroke(savedStroke);
+            g2.setFont(savedFont);
         }
     }
 
@@ -332,6 +336,7 @@
                     dataArea.getMaxY());
             Paint savedPaint = g2.getPaint();
             Stroke savedStroke = g2.getStroke();
+            Font savedFont = g2.getFont();
             g2.setPaint(crosshair.getPaint());
             g2.setStroke(crosshair.getStroke());
             g2.draw(line);
@@ -357,11 +362,13 @@
                 g2.setPaint(crosshair.getLabelBackgroundPaint());
                 g2.fill(hotspot);
                 g2.setPaint(crosshair.getLabelOutlinePaint());
-                g2.draw(hotspot);
+					if (crosshair.isLabelOutlineVisible()) { g2.draw(hotspot); }
+				g2.setFont(crosshair.getLabelFont());
                 TextUtilities.drawAlignedString(label, g2, xx, yy, alignPt);
             }
             g2.setPaint(savedPaint);
             g2.setStroke(savedStroke);
+            g2.setFont(savedFont);
         }
     }

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: CrosshairOverlay enhancement

Post by david.gilbert » Wed May 24, 2017 3:45 am

Thanks for the suggestion, it is committed here:

https://github.com/jfree/jfreechart/com ... 9b39270086
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked