Hi all,
I have observed that same colors are used for showing different series. i.e. first series is always shown in red, second in blue, third in green and so on.
I wanted to know if there is any provision for the programmer to specify the color of the series.
Also are there ways to specify more custom filling style for the series in Bar Charts. for e.g. diagonal filled, vertical filled etc.
If any body has added such feature, could you please share the code.
Thanks in advance
Nin
Changing the colors of different series in JFreeChart
Re: Changing the colors of different series in JFreeChart
Nin wrote:
> I have observed that same colors are used for showing
> different series. i.e. first series is always shown in red,
> second in blue, third in green and so on.
>
> I wanted to know if there is any provision for the programmer
> to specify the color of the series.
You can use one of the setSeriesPaint(...) methods in the Plot class.
> Also are there ways to specify more custom filling style for
> the series in Bar Charts. for e.g. diagonal filled, vertical
> filled etc.
You can use TexturePaint as a series paint, although I found that this didn't give great results. Apart from that, I don't know a way to achieve custom fill styles.
Regards,
DG
> I have observed that same colors are used for showing
> different series. i.e. first series is always shown in red,
> second in blue, third in green and so on.
>
> I wanted to know if there is any provision for the programmer
> to specify the color of the series.
You can use one of the setSeriesPaint(...) methods in the Plot class.
> Also are there ways to specify more custom filling style for
> the series in Bar Charts. for e.g. diagonal filled, vertical
> filled etc.
You can use TexturePaint as a series paint, although I found that this didn't give great results. Apart from that, I don't know a way to achieve custom fill styles.
Regards,
DG
Re: Changing the colors of different series in JFreeChart
Hi,
I am setting colors to OverlaidXYPlot as :
plot.setSeriesPaint(new Paint[] { Color.green, Color.orange, Color.red, Color.yellow});
This effects only to legend of the graph, but lines are in same red, blue etc.
I tried lot, but couldn't able to solve this.
Please help me.
The source of full function as follows :
private JFreeChart createOverlaidChart() {
// make an overlaid plot...
OverlaidXYPlot plot = new OverlaidXYPlot(strX_Label, strY_Label);
HorizontalDateAxis objHrzDtaxis =new HorizontalDateAxis(strX_Label);
objHrzDtaxis.configure();
plot.setDomainAxis(objHrzDtaxis);
// add subplot 1...
for (int intCtr=0; intCtr < strAValues.length; intCtr++){
XYDataset data2 = this.createDataset(intCtr);
XYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setToolTipGenerator(
new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
XYPlot subplot2 = new XYPlot(data2, null, null);//, renderer2);
plot.add(subplot2);
}
plot.setSeriesPaint(new Paint[] { Color.green, Color.orange, Color.red, Color.yellow});
// return a new chart containing the overlaid plot...
return new JFreeChart(strTitle, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}
I am setting colors to OverlaidXYPlot as :
plot.setSeriesPaint(new Paint[] { Color.green, Color.orange, Color.red, Color.yellow});
This effects only to legend of the graph, but lines are in same red, blue etc.
I tried lot, but couldn't able to solve this.
Please help me.
The source of full function as follows :
private JFreeChart createOverlaidChart() {
// make an overlaid plot...
OverlaidXYPlot plot = new OverlaidXYPlot(strX_Label, strY_Label);
HorizontalDateAxis objHrzDtaxis =new HorizontalDateAxis(strX_Label);
objHrzDtaxis.configure();
plot.setDomainAxis(objHrzDtaxis);
// add subplot 1...
for (int intCtr=0; intCtr < strAValues.length; intCtr++){
XYDataset data2 = this.createDataset(intCtr);
XYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setToolTipGenerator(
new TimeSeriesToolTipGenerator("d-MMM-yyyy", "0.00"));
XYPlot subplot2 = new XYPlot(data2, null, null);//, renderer2);
plot.add(subplot2);
}
plot.setSeriesPaint(new Paint[] { Color.green, Color.orange, Color.red, Color.yellow});
// return a new chart containing the overlaid plot...
return new JFreeChart(strTitle, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}
Re: Changing the colors of different series in JFreeChart
Set colors for each subplot like below :
XYPlot subplot1 = new XYPlot(courbes, null, null, renderer1);
subplot1.setSeriesPaint(0, Color.green);
subplot1.setSeriesPaint(1, Color.red);
// create subplot 2...
...
XYPlot subplot2 = new XYPlot(points, null, null, renderer2);
subplot2.setSeriesPaint(0, Color.cyan);
subplot2.setSeriesPaint(1, Color.blue);
subplot2.setSeriesPaint(2, Color.magenta);
XYPlot subplot1 = new XYPlot(courbes, null, null, renderer1);
subplot1.setSeriesPaint(0, Color.green);
subplot1.setSeriesPaint(1, Color.red);
// create subplot 2...
...
XYPlot subplot2 = new XYPlot(points, null, null, renderer2);
subplot2.setSeriesPaint(0, Color.cyan);
subplot2.setSeriesPaint(1, Color.blue);
subplot2.setSeriesPaint(2, Color.magenta);
Re: Changing the colors of different series in JFreeChart
I dont know whether this is right procedure to do or not.
I did something like this.
public class RTSSeries extends BasicTimeSeries implements java.io.Serializable {
private Color color;
/**
* Constructor for the RTSSeries object
*
*@param name Description of Parameter
*@param color Description of Parameter
*/
public RTSSeries(String name, Color color) {
this(name, color,
DEFAULT_DOMAIN_DESCRIPTION,
DEFAULT_RANGE_DESCRIPTION,
Day.class);
}
/**
* Constructor for the RTSSeries object
*
*@param name Description of Parameter
*@param color Description of Parameter
*@param timePeriodClass Description of Parameter
*/
public RTSSeries(String name, Color color, Class timePeriodClass) {
this(name, color,
DEFAULT_DOMAIN_DESCRIPTION,
DEFAULT_RANGE_DESCRIPTION,
timePeriodClass);
}
/**
* Constructor for the RTSSeries object
*
*@param name Description of Parameter
*@param color Description of Parameter
*@param domain Description of Parameter
*@param range Description of Parameter
*@param timePeriodClass Description of Parameter
*/
public RTSSeries(String name, Color color, String domain, String range, Class timePeriodClass) {
super(name, domain, range, timePeriodClass);
this.color = color;
}
/**
* Sets the Color attribute of the RTSSeries object
*
*@param color The new Color value
*/
public void setColor(Color color) {
Color old = this.color;
this.color = color;
propertyChangeSupport.firePropertyChange("Color", old, this.color);
}
/**
* Gets the Color attribute of the RTSSeries object
*
*@return The Color value
*/
public Color getColor() {
return color;
}
}
-------------------------------------------------------------------------------------------------
public class RTSPlot extends XYPlot {
/**
* Constructor for the RTSPlot object
*
*@param data Description of Parameter
*@param domainAxis Description of Parameter
*@param rangeAxis Description of Parameter
*/
public RTSPlot(XYDataset data, ValueAxis domainAxis, ValueAxis rangeAxis) {
super(data, domainAxis, rangeAxis);
}
/**
* Constructor for the RTSPlot object
*
*@param data Description of Parameter
*@param domainAxis Description of Parameter
*@param rangeAxis Description of Parameter
*@param renderer Description of Parameter
*/
public RTSPlot(XYDataset data,
ValueAxis domainAxis, ValueAxis rangeAxis, XYItemRenderer renderer) {
super(data, domainAxis, rangeAxis, renderer);
}
/**
* Constructor for the RTSPlot object
*
*@param data Description of Parameter
*@param domainAxis Description of Parameter
*@param rangeAxis Description of Parameter
*@param insets Description of Parameter
*@param backgroundPaint Description of Parameter
*@param backgroundImage Description of Parameter
*@param backgroundAlpha Description of Parameter
*@param outlineStroke Description of Parameter
*@param outlinePaint Description of Parameter
*@param alpha Description of Parameter
*@param renderer Description of Parameter
*/
public RTSPlot(XYDataset data,
ValueAxis domainAxis, ValueAxis rangeAxis,
Insets insets,
Paint backgroundPaint, Image backgroundImage, float backgroundAlpha,
Stroke outlineStroke, Paint outlinePaint, float alpha,
XYItemRenderer renderer) {
super(data, domainAxis, rangeAxis, insets, backgroundPaint, backgroundImage, backgroundAlpha,
outlineStroke, outlinePaint, alpha, renderer);
}
/**
* Sets the SeriesPaint attribute of the RTSPlot object
*
*@param index The new SeriesPaint value
*@param paint The new SeriesPaint value
*/
public void setSeriesPaint(int index, Paint paint) {
/*
* if (isSubplot()) {
* index = index + this.firstSeriesIndex;
* }
* this.seriesPaint[index] = paint;
*/
((RTSSeries) ((RTSSeriesCollection) getDataset()).getSeries(index)).setColor((Color) paint);
notifyListeners(new PlotChangeEvent(this));
}
/**
* Gets the SeriesPaint attribute of the RTSPlot object
*
*@param index Description of Parameter
*@return The SeriesPaint value
*/
public Paint getSeriesPaint(int index) {
/*
* if (isSubplot()) {
* index = index+this.firstSeriesIndex;
* }
* return seriesPaint[index % seriesPaint.length];
*/
return ((RTSSeries) ((RTSSeriesCollection) this.getDataset()).getSeries(index)).getColor();
}
/**
* Description of the Method
*
*@param g2 Description of Parameter
*@param dataArea Description of Parameter
*@param info Description of Parameter
*@param crosshairInfo Description of Parameter
*/
public void render(Graphics2D g2, Rectangle2D dataArea,
ChartRenderingInfo info, CrosshairInfo crosshairInfo) {
// now get the data and plot it (the visual representation will depend on the renderer
// that has been set)...
XYDataset data = this.getXYDataset();
if (data != null) {
Shape originalClip = g2.getClip();
Composite originalComposite = g2.getComposite();
g2.clip(dataArea);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
this.foregroundAlpha));
drawVerticalLines(g2, dataArea);
drawHorizontalLines(g2, dataArea);
renderer.initialise(g2, dataArea, this, data, info);
ValueAxis domainAxis = this.getDomainAxis();
ValueAxis rangeAxis = this.getRangeAxis();
int seriesCount = data.getSeriesCount();
for (int series = 0; series < seriesCount; series++) {
if (!((RTSSeries) ((RTSSeriesCollection) data).getSeries(series)).isDisplayable()) {
continue;
}
int itemCount = data.getItemCount(series);
for (int item = 0; item < itemCount; item++) {
renderer.drawItem(g2, dataArea, info, this,
domainAxis, rangeAxis,
data, series, item,
crosshairInfo);
}
}
// draw vertical crosshair if required...
domainAxis.setCrosshairValue(crosshairInfo.getCrosshairX());
if (domainAxis.isCrosshairVisible()) {
this.drawVerticalLine(g2, dataArea, domainAxis.getCrosshairValue(),
domainAxis.getCrosshairStroke(),
domainAxis.getCrosshairPaint());
}
// draw horizontal crosshair if required...
rangeAxis.setCrosshairValue(crosshairInfo.getCrosshairY());
if (rangeAxis.isCrosshairVisible()) {
this.drawHorizontalLine(g2, dataArea, rangeAxis.getCrosshairValue(),
rangeAxis.getCrosshairStroke(),
rangeAxis.getCrosshairPaint());
}
g2.setClip(originalClip);
g2.setComposite(originalComposite);
}
}
}
Hopefully if u guys can make out anything out of that fine. Otherwise if u have any doubts in the above program, please email me.
Siva.
I did something like this.
public class RTSSeries extends BasicTimeSeries implements java.io.Serializable {
private Color color;
/**
* Constructor for the RTSSeries object
*
*@param name Description of Parameter
*@param color Description of Parameter
*/
public RTSSeries(String name, Color color) {
this(name, color,
DEFAULT_DOMAIN_DESCRIPTION,
DEFAULT_RANGE_DESCRIPTION,
Day.class);
}
/**
* Constructor for the RTSSeries object
*
*@param name Description of Parameter
*@param color Description of Parameter
*@param timePeriodClass Description of Parameter
*/
public RTSSeries(String name, Color color, Class timePeriodClass) {
this(name, color,
DEFAULT_DOMAIN_DESCRIPTION,
DEFAULT_RANGE_DESCRIPTION,
timePeriodClass);
}
/**
* Constructor for the RTSSeries object
*
*@param name Description of Parameter
*@param color Description of Parameter
*@param domain Description of Parameter
*@param range Description of Parameter
*@param timePeriodClass Description of Parameter
*/
public RTSSeries(String name, Color color, String domain, String range, Class timePeriodClass) {
super(name, domain, range, timePeriodClass);
this.color = color;
}
/**
* Sets the Color attribute of the RTSSeries object
*
*@param color The new Color value
*/
public void setColor(Color color) {
Color old = this.color;
this.color = color;
propertyChangeSupport.firePropertyChange("Color", old, this.color);
}
/**
* Gets the Color attribute of the RTSSeries object
*
*@return The Color value
*/
public Color getColor() {
return color;
}
}
-------------------------------------------------------------------------------------------------
public class RTSPlot extends XYPlot {
/**
* Constructor for the RTSPlot object
*
*@param data Description of Parameter
*@param domainAxis Description of Parameter
*@param rangeAxis Description of Parameter
*/
public RTSPlot(XYDataset data, ValueAxis domainAxis, ValueAxis rangeAxis) {
super(data, domainAxis, rangeAxis);
}
/**
* Constructor for the RTSPlot object
*
*@param data Description of Parameter
*@param domainAxis Description of Parameter
*@param rangeAxis Description of Parameter
*@param renderer Description of Parameter
*/
public RTSPlot(XYDataset data,
ValueAxis domainAxis, ValueAxis rangeAxis, XYItemRenderer renderer) {
super(data, domainAxis, rangeAxis, renderer);
}
/**
* Constructor for the RTSPlot object
*
*@param data Description of Parameter
*@param domainAxis Description of Parameter
*@param rangeAxis Description of Parameter
*@param insets Description of Parameter
*@param backgroundPaint Description of Parameter
*@param backgroundImage Description of Parameter
*@param backgroundAlpha Description of Parameter
*@param outlineStroke Description of Parameter
*@param outlinePaint Description of Parameter
*@param alpha Description of Parameter
*@param renderer Description of Parameter
*/
public RTSPlot(XYDataset data,
ValueAxis domainAxis, ValueAxis rangeAxis,
Insets insets,
Paint backgroundPaint, Image backgroundImage, float backgroundAlpha,
Stroke outlineStroke, Paint outlinePaint, float alpha,
XYItemRenderer renderer) {
super(data, domainAxis, rangeAxis, insets, backgroundPaint, backgroundImage, backgroundAlpha,
outlineStroke, outlinePaint, alpha, renderer);
}
/**
* Sets the SeriesPaint attribute of the RTSPlot object
*
*@param index The new SeriesPaint value
*@param paint The new SeriesPaint value
*/
public void setSeriesPaint(int index, Paint paint) {
/*
* if (isSubplot()) {
* index = index + this.firstSeriesIndex;
* }
* this.seriesPaint[index] = paint;
*/
((RTSSeries) ((RTSSeriesCollection) getDataset()).getSeries(index)).setColor((Color) paint);
notifyListeners(new PlotChangeEvent(this));
}
/**
* Gets the SeriesPaint attribute of the RTSPlot object
*
*@param index Description of Parameter
*@return The SeriesPaint value
*/
public Paint getSeriesPaint(int index) {
/*
* if (isSubplot()) {
* index = index+this.firstSeriesIndex;
* }
* return seriesPaint[index % seriesPaint.length];
*/
return ((RTSSeries) ((RTSSeriesCollection) this.getDataset()).getSeries(index)).getColor();
}
/**
* Description of the Method
*
*@param g2 Description of Parameter
*@param dataArea Description of Parameter
*@param info Description of Parameter
*@param crosshairInfo Description of Parameter
*/
public void render(Graphics2D g2, Rectangle2D dataArea,
ChartRenderingInfo info, CrosshairInfo crosshairInfo) {
// now get the data and plot it (the visual representation will depend on the renderer
// that has been set)...
XYDataset data = this.getXYDataset();
if (data != null) {
Shape originalClip = g2.getClip();
Composite originalComposite = g2.getComposite();
g2.clip(dataArea);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
this.foregroundAlpha));
drawVerticalLines(g2, dataArea);
drawHorizontalLines(g2, dataArea);
renderer.initialise(g2, dataArea, this, data, info);
ValueAxis domainAxis = this.getDomainAxis();
ValueAxis rangeAxis = this.getRangeAxis();
int seriesCount = data.getSeriesCount();
for (int series = 0; series < seriesCount; series++) {
if (!((RTSSeries) ((RTSSeriesCollection) data).getSeries(series)).isDisplayable()) {
continue;
}
int itemCount = data.getItemCount(series);
for (int item = 0; item < itemCount; item++) {
renderer.drawItem(g2, dataArea, info, this,
domainAxis, rangeAxis,
data, series, item,
crosshairInfo);
}
}
// draw vertical crosshair if required...
domainAxis.setCrosshairValue(crosshairInfo.getCrosshairX());
if (domainAxis.isCrosshairVisible()) {
this.drawVerticalLine(g2, dataArea, domainAxis.getCrosshairValue(),
domainAxis.getCrosshairStroke(),
domainAxis.getCrosshairPaint());
}
// draw horizontal crosshair if required...
rangeAxis.setCrosshairValue(crosshairInfo.getCrosshairY());
if (rangeAxis.isCrosshairVisible()) {
this.drawHorizontalLine(g2, dataArea, rangeAxis.getCrosshairValue(),
rangeAxis.getCrosshairStroke(),
rangeAxis.getCrosshairPaint());
}
g2.setClip(originalClip);
g2.setComposite(originalComposite);
}
}
}
Hopefully if u guys can make out anything out of that fine. Otherwise if u have any doubts in the above program, please email me.
Siva.