A discussion forum for JFreeChart (a 2D chart library for the Java platform).
-
jesclaine
- Posts: 2
- Joined: Tue Dec 21, 2010 3:43 pm
- antibot: No, of course not.
Post
by jesclaine » Tue Dec 21, 2010 4:03 pm
Hi,
Is it possible to have a gradient line in XYlinechart ?
I tried this but is not good :
Code: Select all
XYItemRenderer renderer = plot.getRenderer();
Paint p = new GradientPaint(x1, y1, Color.blue, x2, y2, Color.red);
renderer.setSeriesPaint(0, p);
The problem is i don't know the absolute position of my first point (x1,y1) and of my last point (x2,y2).
Someone have an idea ?
Thanks
-
barbarius
- Posts: 74
- Joined: Tue Jul 27, 2010 9:33 am
- antibot: No, of course not.
Post
by barbarius » Fri Dec 24, 2010 12:47 am
in jfreechart, the lines are drawn one by one. so each line has its own color but jfc doesnt have a mechanism to set that colors seperately.
you can use this method with true parameter at the end for some different look but i dont think this is what you want..
Code: Select all
Paint p = new GradientPaint(x1, y1, Color.blue, x2, y2, Color.red, true);
maybe if you override that method in AbstractRenderer and make a big custom beautiful code, you might return proper gradient paint variables. I dont know if it will work for sure though..:
Code: Select all
public Paint getItemPaint(int row, int column)
-
lfkpoa
- Posts: 7
- Joined: Wed Dec 22, 2010 8:28 pm
- antibot: No, of course not.
Post
by lfkpoa » Fri Dec 24, 2010 2:35 pm
My suggestion is for creating a new GradientXYLineAndShapeRenderer like this:
Code: Select all
package org.jfree.chart.demo;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.geom.Rectangle2D;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRendererState;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.util.PaintList;
public class GradientXYLineRenderer extends XYLineAndShapeRenderer {
private PaintList paintList2;
private Rectangle2D dataArea;
public GradientXYLineRenderer() {
paintList2 = new PaintList();
}
public GradientXYLineRenderer(boolean lines, boolean shapes) {
super(lines, shapes);
paintList2 = new PaintList();
}
public void setSeriesPaint2(int series, Paint paint) {
paintList2.setPaint(series, paint);
}
@Override
public XYItemRendererState initialise(Graphics2D g2, Rectangle2D dataArea, XYPlot plot, XYDataset data, PlotRenderingInfo info) {
this.dataArea = dataArea;
return super.initialise(g2, dataArea, plot, data, info);
}
@Override
public Paint getSeriesPaint(int series) {
Paint c1 = super.getSeriesPaint(series);
if(! (c1 instanceof Color) || dataArea==null)
return c1;
Paint c2 = paintList2.getPaint(series);
if(c2==null || c1==c2 || ! (c2 instanceof Color))
return c1;
float y = 1;
GradientPaint gp = new GradientPaint((float)dataArea.getMinX(),y,(Color) c1,(float)dataArea.getMaxX(),y,(Color)c2);
return gp;
}
}
Create a new instance of this renderer, set the Colors you would like to use and set it as the plot renderer:
Code: Select all
GradientXYLineRenderer renderer = new GradientXYLineRenderer();
renderer.setSeriesPaint(0, Color.BLUE);
renderer.setSeriesPaint2(0, Color.WHITE);
plot.setRenderer(renderer);