How to set the color for stacked area

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
HighWay
Posts: 6
Joined: Tue Jul 17, 2007 12:37 am

How to set the color for stacked area

Post by HighWay » Thu Jul 19, 2007 6:29 pm

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

lukkumar
Posts: 17
Joined: Thu Jun 07, 2007 8:10 pm

Post by lukkumar » Thu Jul 19, 2007 10:43 pm

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.

david.gilbert
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

Post by david.gilbert » Fri Jul 20, 2007 9:58 am

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?
It's hard to say what's going wrong here. Can you post a small self-contained demo of the problem?
David Gilbert
JFreeChart Project Leader

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

HighWay
Posts: 6
Joined: Tue Jul 17, 2007 12:37 am

Post by HighWay » Wed Jul 25, 2007 10:29 pm

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.

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;
    }
}




HighWay
Posts: 6
Joined: Tue Jul 17, 2007 12:37 am

Post by HighWay » Mon Jul 30, 2007 8:28 am

Hi,

Wondering if I spotted a bug or there is a solution to my problem (code posted above)?

Thanks
-Zack

james.matheson
Posts: 2
Joined: Mon Jun 25, 2012 7:00 am
antibot: No, of course not.

Re: How to set the color for stacked area

Post by james.matheson » Mon Jun 25, 2012 7:22 am

I found a similar problem where the legend didn't match the data points. I fixed it by changing from StackedXYAreaRenderer to StackedXYAreaRenderer2.

Locked