Hi
I was able to create Stacked Area chart from ChartFactory.createXYAreaChart(with TimeSeries dataset as Second.class). I faced problem while setting the color of stacked chart.
Note: the code below successfully sets the color for legend but failed to set the color for stacked area.
renderer.setSeriesPaint(0, Color.GREEN);
Do I have to something special to set the color for stacked area?
Thanks,
Zack
How to set the color for stacked area
Hi,
I am not sure why it is not working for you... but here is a sample code for setting the color for the series
StackedXYAreaRenderer renderer = new StackedXYAreaRenderer(
XYAreaRenderer.AREA_AND_SHAPES, ttg, null );
renderer.setSeriesPaint(0, new Color(255, 255, 180));
renderer.setSeriesPaint(1, new Color(206, 230, 255));
renderer.setSeriesPaint(2, new Color(255, 230, 230));
renderer.setSeriesPaint(3, new Color(206, 255, 206));
Try creating an instance of a Color ...
Hope this helps u
Laks.
I am not sure why it is not working for you... but here is a sample code for setting the color for the series
StackedXYAreaRenderer renderer = new StackedXYAreaRenderer(
XYAreaRenderer.AREA_AND_SHAPES, ttg, null );
renderer.setSeriesPaint(0, new Color(255, 255, 180));
renderer.setSeriesPaint(1, new Color(206, 230, 255));
renderer.setSeriesPaint(2, new Color(255, 230, 230));
renderer.setSeriesPaint(3, new Color(206, 255, 206));
Try creating an instance of a Color ...
Hope this helps u
Laks.
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: How to set the color for stacked area
It's hard to say what's going wrong here. Can you post a small self-contained demo of the problem?HighWay wrote:Note: the code below successfully sets the color for legend but failed to set the color for stacked area.
renderer.setSeriesPaint(0, Color.GREEN);
Do I have to something special to set the color for stacked area?
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


Hi David,
Still have the problem displaying same color as in legend. Below is the self contained Demo.
Note: Its does not matter if I select Color.Blue over new Color(255,244,222)
-Zack.
Still have the problem displaying same color as in legend. Below is the self contained Demo.
Note: Its does not matter if I select Color.Blue over new Color(255,244,222)
-Zack.
Code: Select all
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class StackedAreaChartMain {
SimpleDateFormat formatter = new SimpleDateFormat("MMM.dd.HH.m");
SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yyyy");
DecimalFormat df = new DecimalFormat("#,##0.00");
public static void main(String [] args) {
StackedAreaChartMain sacm = new StackedAreaChartMain();
JFreeChart chart = sacm.getChart();
File file = null;
try {
// specify your path todo
file = new File("C:\\tmp\\chart\\" + "TEST_STACKED_CHART_" + System.currentTimeMillis() + ".png");
OutputStream out = new FileOutputStream(file);
ChartUtilities.writeChartAsPNG(out, chart, 400, 400);
out.flush();
out.close();
} catch (IOException exce) {
exce.printStackTrace();
}
}
public JFreeChart getChart() {
IntervalXYDataset dataset = createDataset();
//XYArearChart
JFreeChart chart = ChartFactory.createXYAreaChart(
"Stacked Chart",
"X", //X
"Y", //Y
dataset,
PlotOrientation.VERTICAL,
true, // legend
true, // tool tips
false // URLs
);
XYPlot plot = (XYPlot) chart.getPlot();
ValueAxis domainAxis = new DateAxis();
domainAxis.setLowerMargin(0.0);
domainAxis.setUpperMargin(0.0);
plot.setDomainAxis(domainAxis);
plot.setForegroundAlpha(0.5f);
XYItemRenderer renderer = plot.getRenderer();
renderer.setToolTipGenerator(new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, sdf, df));
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesPaint(1, Color.YELLOW);
renderer.setSeriesPaint(2, Color.BLUE);
plot.setRenderer(renderer);
//set bg color
chart.setBackgroundPaint(Color.white);
return chart;
}
/**
* Create data set
*/
private IntervalXYDataset createDataset() {
TimeSeries abcSeries = new TimeSeries("A_PLUS_B_PLUS_C", Second.class);
TimeSeries abSeries = new TimeSeries("A_PLUS_B", Second.class);
TimeSeries aSeries = new TimeSeries("A", Second.class);
Second day = new Second(new Date(Long.parseLong("1185231632") * 1000));
abcSeries.add(day, Double.parseDouble("347"));
abSeries.add(day, Double.parseDouble("234"));
aSeries.add(day, Double.parseDouble("0"));
day = new Second(new Date(Long.parseLong("1185231719") * 1000));
abcSeries.add(day, Double.parseDouble("1865"));
abSeries.add(day, Double.parseDouble("1560"));
aSeries.add(day, Double.parseDouble("0"));
day = new Second(new Date(Long.parseLong("1185231778") * 1000));
abcSeries.add(day, Double.parseDouble("1585"));
abSeries.add(day, Double.parseDouble("1280"));
aSeries.add(day, Double.parseDouble("0"));
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(abcSeries);
dataset.addSeries(abSeries);
dataset.addSeries(aSeries);
return dataset;
}
}
-
- Posts: 2
- Joined: Mon Jun 25, 2012 7:00 am
- antibot: No, of course not.
Re: How to set the color for stacked area
I found a similar problem where the legend didn't match the data points. I fixed it by changing from StackedXYAreaRenderer to StackedXYAreaRenderer2.