Stacked Area color does not match with Legend

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

Stacked Area color does not match with Legend

Post by HighWay » Wed Aug 01, 2007 10:43 am

Hi Everyone,

Pasted below is the self-contained source for stacked chart. If you run it, you will see stacked area chart color is not set same as legend.

I will appreciate if you can point me to right direction

Thanks
-Zack

p.s - don`t forget to change output directory path for chart :)

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


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 Aug 01, 2007 5:25 pm

The problem here is that the plot is using alpha transparency while the legend is not, so the colours look different. I'll see if I can add alpha transparency support to the legend items in a future release.

Note also that your chart isn't really a stacked chart...it is a regular area chart, where the values appear to be stacked since you add series values together in the dataset. Nothing wrong with that, if it suits your purposes, of course...
David Gilbert
JFreeChart Project Leader

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

redbaron
Posts: 1
Joined: Thu Jul 24, 2008 3:30 pm

Post by redbaron » Thu Jul 24, 2008 3:31 pm

Hi,

Am facing the same issue with 1.0.10.

Is there a workaround?

thanks,
james.

chechaquo
Posts: 10
Joined: Tue Oct 28, 2008 12:42 pm

Re: Stacked Area color does not match with Legend

Post by chechaquo » Wed Jun 16, 2010 10:25 am

Does 1.0.13 support alpha transparency for legend? I couldn't find this in documentations :(

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

Re: Stacked Area color does not match with Legend

Post by james.matheson » Mon Jun 25, 2012 7:04 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