how to change gridColor in plots

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

how to change gridColor in plots

Aryeh Weiss
I amusing the Plot class to create plots, and I want to have a grid on
the plot, but I also want a lightGray background.

The problem is that the grid is also lightGray, and this appear to be
coded in a private variable in Plot.java
https://imagej.nih.gov/ij/source/ij/gui/Plot.java

private Color gridColor = new Color(0xc0c0c0); //light gray

Is there any way to change this color, either directly or through a suitable method?

Thanks in advance.
--aryeh

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: how to change gridColor in plots

Michael Schmid-3
Hi Aryeh,

yes, the grid color is hardcoded (at the times when this was coded,
plots were much more basic than now...).
The easiest way to change it would be in a macro using the changeValues
macro command.

E.g.
   changeValues(0xc0c0c0,0xc0c0c0,0x0000ff);
changes the grid to blue.

Unfortunately, this change will be gone if you change the plot in some
way, e.g. resize the window or zoom in on data.

If you can't live with that, in principle it should be possible to have
a Plot.setGridColor method and macro command. It would be a bit of work,
however. One would also have to decide once and for all whether one will
have two independent colors for the x&y grids, and one needs to think
about a good place how to store it in a way that it can be serialized
for saving&reading (the secondary color of the plot frame is already in
use for the background).

Michael
________________________________________________________________
On 18/07/2018 15:47, Aryeh Weiss wrote:
 > I amusing the Plot class to create plots, and I want to have a grid
on the plot, but I also want a lightGray background.
 >
 > The problem is that the grid is also lightGray, and this appear to be
coded in a private variable in Plot.java
 > https://imagej.nih.gov/ij/source/ij/gui/Plot.java
 >
 > private Color gridColor = new Color(0xc0c0c0);    //light gray
 >
 > Is there any way to change this color, either directly or through a
suitable method?
 >
 > Thanks in advance.
 > --aryeh

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: how to change gridColor in plots

Curtis Rueden
In reply to this post by Aryeh Weiss
Hi Aryeh,

You can override the grid color directly via reflection:

--snip--
import ij.gui.Plot

float[] xValues = [0, 1, 2, 3, 4, 5]
float[] yValues = [0, 1, 4, 9, 16, 25]
plot = new Plot("Cat Proximity", "<-- Far | Human Proximity to Cat | Near
-->", "Inanity of Statements", xValues, yValues)
plot.setPlotObjectStyles(0, "blue, green, 1")

// HACK: Override the grid color.
gridColorField = Plot.class.getDeclaredField("gridColor")
gridColorField.setAccessible(true)
gridColorField.set(plot, java.awt.Color.red.darker())

plot.show()
--snap--

But this is a hack relying on internal implementation details, so may not
continue working in all future versions of ImageJ. I agree with Michael
that a better long-term solution is for ij.gui.Plot to gain public API for
overriding this color.

Regards,
Curtis

--
Curtis Rueden
LOCI software architect - https://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
Did you know ImageJ has a forum? http://forum.imagej.net/


On Wed, Jul 18, 2018 at 8:47 AM, Aryeh Weiss <[hidden email]> wrote:

> I amusing the Plot class to create plots, and I want to have a grid on the
> plot, but I also want a lightGray background.
>
> The problem is that the grid is also lightGray, and this appear to be
> coded in a private variable in Plot.java
> https://imagej.nih.gov/ij/source/ij/gui/Plot.java
>
> private Color gridColor = new Color(0xc0c0c0);  //light gray
>
> Is there any way to change this color, either directly or through a
> suitable method?
>
> Thanks in advance.
> --aryeh
>
> --
> Aryeh Weiss
> Faculty of Engineering
> Bar Ilan University
> Ramat Gan 52900 Israel
>
> Ph:  972-3-5317638
> FAX: 972-3-7384051
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: how to change gridColor in plots

Aryeh Weiss
Thanks to Michael and Curtis for their quick and informative answers.
I prefer Curtis's hack. with the caveats understood.

Here is a pythonized version of the script:

--snip--
from ij.gui import Plot
from java.awt import Color

xValues = [0, 1, 2, 3, 4, 5]
yValues = [0, 1, 4, 9, 16, 25]
plot = Plot("Cat Proximity", "<-- Far | Human Proximity to Cat | Near
-->", "Inanity of Statements", xValues, yValues)
plot.setPlotObjectStyles(0, "blue, green, 1")

# HACK: Override the grid color.
gridColorField = Plot.getDeclaredField("gridColor")
gridColorField.setAccessible(True)
gridColorField.set(plot, Color.red.darker())

plot.show()
--snip--

getDeclaredField , set Accessible and set  is something I never thought
one could do. That is really neat.
I inserted it into my code and it works as advertised.

Best regards
--aryeh


On 18/07/2018 19:12, Curtis Rueden wrote:

> Hi Aryeh,
>
> You can override the grid color directly via reflection:
>
> --snip--
> import ij.gui.Plot
>
> float[] xValues = [0, 1, 2, 3, 4, 5]
> float[] yValues = [0, 1, 4, 9, 16, 25]
> plot = new Plot("Cat Proximity", "<-- Far | Human Proximity to Cat |
> Near -->", "Inanity of Statements", xValues, yValues)
> plot.setPlotObjectStyles(0, "blue, green, 1")
>
> // HACK: Override the grid color.
> gridColorField = Plot.class.getDeclaredField("gridColor")
> gridColorField.setAccessible(true)
> gridColorField.set(plot, java.awt.Color.red.darker())
>
> plot.show()
> --snap--
>
> But this is a hack relying on internal implementation details, so may
> not continue working in all future versions of ImageJ. I agree with
> Michael that a better long-term solution is for ij.gui.Plot to gain
> public API for overriding this color.
>
> Regards,
> Curtis
>
> --
> Curtis Rueden
> LOCI software architect - https://loci.wisc.edu/software
> ImageJ2 lead, Fiji maintainer - https://imagej.net/User:Rueden
> Did you know ImageJ has a forum? http://forum.imagej.net/
>
>
> On Wed, Jul 18, 2018 at 8:47 AM, Aryeh Weiss <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     I amusing the Plot class to create plots, and I want to have a
>     grid on the plot, but I also want a lightGray background.
>
>     The problem is that the grid is also lightGray, and this appear to
>     be coded in a private variable in Plot.java
>     https://imagej.nih.gov/ij/source/ij/gui/Plot.java
>     <https://imagej.nih.gov/ij/source/ij/gui/Plot.java>
>
>     private Color gridColor = new Color(0xc0c0c0);  //light gray
>
>     Is there any way to change this color, either directly or through
>     a suitable method?
>
>     Thanks in advance.
>     --aryeh
>
>     --
>     Aryeh Weiss
>     Faculty of Engineering
>     Bar Ilan University
>     Ramat Gan 52900 Israel
>
>     Ph:  972-3-5317638
>     FAX: 972-3-7384051
>
>
>     --
>     ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>     <http://imagej.nih.gov/ij/list.html>
>
>

--
Aryeh Weiss
Faculty of Engineering
Bar Ilan University
Ramat Gan 52900 Israel

Ph:  972-3-5317638
FAX: 972-3-7384051


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html