BarChart & GradientPaintTransformType.CENTER_HORIZONTAL

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
harrisx
Posts: 4
Joined: Fri Mar 30, 2007 5:36 pm

BarChart & GradientPaintTransformType.CENTER_HORIZONTAL

Post by harrisx » Fri Mar 30, 2007 5:55 pm

Hi!

I am trying to get a horizontal gradient Fill for my Bars in the Barchart
that goes from color to white to color again in the same bar.

I tried the following:

Code: Select all

final BarRenderer renderer = new BarRenderer();
final CategoryPlot myPlot = new CategoryPlot();

myPlot.setRenderer(renderer);
myPlot.setDomainAxis(new CategoryAxis());
myPlot.setRangeAxis(new NumberAxis("y"));
myPlot.setOrientation(PlotOrientation.VERTICAL);

GradientPaint redGradient = new GradientPaint(
	0.0f, 0.0f, Color.red,
        0.0f, 0.0f, Color.white);
			
renderer.setGradientPaintTransformer( 
  new StandardGradientPaintTransformer( 
     GradientPaintTransformType.CENTER_HORIZONTAL)); 

renderer.setSeriesPaint(0,  redGradient);
renderer.setSeriesPaint(1,  java.awt.Color.lightGray );

renderer.setOutlinePaint(Color.black);
renderer.setOutlineStroke( new BasicStroke(0.7f) );
renderer.setMaximumBarWidth(0.10);
renderer.setItemMargin(0.05);

myPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
	
final JFreeChart chart = new JFreeChart(myPlot);
	
chart.setBackgroundPaint( Color.white );
chart.setBorderPaint( Color.black );
		 
LegendTitle legend = chart.getLegend();
legend.setPosition( RectangleEdge.TOP );
legend.setBorder(BlockBorder.NONE);
				 
CategoryAxis axis = (CategoryAxis) myPlot.getDomainAxis();
axis.setTickLabelFont(new java.awt.Font("SansSerif",java.awt.Font.PLAIN,8));
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
axis.setCategoryMargin(0.20f);
axis.setUpperMargin(0.02f);
axis.setLowerMargin(0.02f);

axis.setMaximumCategoryLabelLines(3);
axis.setMaximumCategoryLabelWidthRatio(15);
What i get is i a really nice plot with horizontal gradients for the bars in the first series but: it only goes from red to white and not back to white again (should by a kind of cylinder effect) and that was what I aspected GradientPaintTransformType.CENTER_HORIZONTAL to do ...

I'm sure it works this way but, I'm doing something wrong obviously!
Please help ?!

Thanks in advance!
Harris

RichardWest
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Post by RichardWest » Sat Mar 31, 2007 2:45 am

Looking at the GradientPaint api, I think you need to create a cyclic GradientPaint. The optional fifth parameter in the constructor you are using is a boolean which is true if the effect is cyclic. I do not work with GradientPaint seriously, so I am not sure how to exactly achieve what you are looking for off hand. Hopefully, this will point you in the right direction.
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

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

Post by david.gilbert » Mon Apr 02, 2007 5:24 pm

CENTER_HORIZONTAL will fill the bar with a cyclic GradientPaint, with the first colour appearing in the middle of the bar. That's what I got when I ran your code. Perhaps you need to reverse the red and white colours?
David Gilbert
JFreeChart Project Leader

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

harrisx
Posts: 4
Joined: Fri Mar 30, 2007 5:36 pm

Post by harrisx » Wed Apr 04, 2007 3:57 pm

Thank you for your answer!

Finally I managed to find out where the problem lies:

As I foud out using a cyclic GradientPaint doesn't help at all. The Center - Gradient Paint transformer should choose a cyclic Gradient for you ( and not only in theory as you will see in the next para.).

After Dave stated that he got the right result after he has run my code I played around a lot, updated to the latest versions of jfreechart and jcommons and so on. But finally a brilliant idea came to my mind: how about not writing the chart to a pdf but saving it as a png-file...and ... :shock: I also got the right result in that picture: a red-white-red gradient and not a red-white gradient as in the PDF.

So I guess we a have an iText problem here, not a jFreeChart one ... Stupid me, I apologize for the trouble caused. But anyway, if someone else stumbles on the same problem and also blames jFreeChart at first - here is the solution ;-)

Thank you very much for your help!
Harris

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

Post by david.gilbert » Wed Apr 04, 2007 5:46 pm

I've always wanted to contribute something to iText, so I look into this and see if I can fix it.
David Gilbert
JFreeChart Project Leader

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

FabianLange
Posts: 3
Joined: Tue Apr 24, 2007 10:41 am

Post by FabianLange » Tue Apr 24, 2007 10:44 am

Hi David,
just discovered that the AreaRenderer does not offer the set setGradientPaintTransformer(GradientPaintTransformer transformer)

When you pass a GradientPaint to it it just does a white chart.

Are you planning to enable the Area charts to support this?
Or also considering pulling setGradientPaintTransformer up to AbstractCategoryItemRenderer.

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

Post by david.gilbert » Tue Apr 24, 2007 2:05 pm

FabianLange wrote:Are you planning to enable the Area charts to support this?
I'd like to, but it isn't completely straightforward. The GradientPaint needs to be drawn relative to the highest peak on the area drawn, but the renderer is set up to draw the area in segments, so it doesn't have that information readily available at the time of drawing. I'll have to come up with some workaround to make it work...
David Gilbert
JFreeChart Project Leader

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

FabianLange
Posts: 3
Joined: Tue Apr 24, 2007 10:41 am

Post by FabianLange » Wed Apr 25, 2007 6:39 am

Hi David,
thanks for the fast reply. I have seen that after my positng when I found the source code on how your are doing that. At the same point of time my customer asked, "and hey whats about the pie chart?"
So I guess I will have a look how gradient colors (seems to be trendy right now) can be applied to nonrectangular shapes and see if i have time to contribute code on this.
Fabian

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

Post by david.gilbert » Wed Apr 25, 2007 8:08 am

For the non-rectangular shapes, you can just request the bounding box and set the gradient dimensions from that. Java2D takes care of the clipping.

I haven't really looked at gradient painting for pie charts - do people typically use a radial gradient paint, or just a linear gradient from the center of the plot to the outside?
David Gilbert
JFreeChart Project Leader

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

FabianLange
Posts: 3
Joined: Tue Apr 24, 2007 10:41 am

Post by FabianLange » Wed Apr 25, 2007 12:30 pm

i think what people do does not really matter. Customers would want every possible gradient. So we can tell them what we think is a nice gradient, or what we want to support...
I have not seen a gradient on a pie, i requested that from my customer as well and will let you know, i think one sensible thing would be from center to outside in a linar way.
Is it okay to misuse this thread further?

Locked