different line colors

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Ivoryn
Posts: 8
Joined: Tue Apr 04, 2006 2:01 pm

different line colors

Post by Ivoryn » Wed Jun 21, 2006 8:45 am

hi,

in my program, several different datasets are displayed in one chartpanel with a range axis for each dataset. when i have two different datasets (or even more) it always paints the first line red, the second blue, etc, so i have two or more red lines.

so what can i do to make sure that one specific color is only shown one time in one chart? i know i can change colors with setSeriesPaint, but i need to do that dynamically, because the number of datasets varies with every file i load into my program.

what would be the best way to solve this problem?

and additionally, is it possible to color the range axis the same color as the color of the according line?

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 Jun 21, 2006 11:04 am

The plot should allocate unique colors to all the series by default. Can you post a small demo program that reproduces this problem?

There are three methods for changing the color of an axis:

setAxisLinePaint(Paint);
setLabelPaint(Paint);
setTickLabelPaint(Paint);

...but there is no mechanism to link these to the color of a series that is plotted against the axis, so you'll need to set the colors in your own code.
David Gilbert
JFreeChart Project Leader

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

Ivoryn
Posts: 8
Joined: Tue Apr 04, 2006 2:01 pm

Post by Ivoryn » Wed Jun 21, 2006 12:18 pm

i found the same problem in another topic:

http://www.jfree.org/phpBB2/viewtopic.p ... eriespaint

my picture looks the same. here is my sample code:

Code: Select all

import java.awt.Color;
import java.text.DecimalFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
     

	public class TimeSeriesChart implements Constants
	{
    	DatasetView datasetView = null;

    	ChartPanel chartPanel;
    	
        public TimeSeriesChart(Dataset d, DatasetView dv, boolean useScroll) 
        {
            datasetView = dv;
            JFreeChart chart = createChart(d, datasetView, useScroll);
            chartPanel = new ChartPanel(chart, false);
            chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
            chartPanel.setMouseZoomable(true, false);
            datasetView.setChart(chart);
            datasetView.setChartPanel(chartPanel);
        }   

        private static JFreeChart createChart(Dataset d, DatasetView dv, boolean useScroll) 
        {
        	XYDataset xydatasetLONG = createDataset(d, dv, Dataset.DOUBLE_FLOAT, useScroll);
        	XYDataset xydatasetINT = createDataset(d, dv, Dataset.INTEGER, useScroll);
        	XYDataset xydatasetFLOAT = createDataset(d, dv, Dataset.FLOAT, useScroll);
        	XYDataset xydatasetUNSIGNED = createDataset(d, dv, Dataset.UNSIGNED_INTEGER, useScroll);
        	JFreeChart chart = ChartFactory.createTimeSeriesChart(
                    d.name,                          // title
                    "Time",                          // x-axis label
                    null,                            // y-axis label
                    xydatasetINT,                    // data
                    include_legend,                  // create legend?
                    generate_tooltips,               // generate tooltips?
                    generate_URLs                    // generate URLs?
                );

            chart.setBackgroundPaint(new Color(233,233,233));
            XYPlot plot = (XYPlot) chart.getPlot();
            plot.setBackgroundPaint(Color.white);
            plot.setDomainGridlinePaint(Color.lightGray);
            plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
            plot.setDomainGridlinesVisible(true);
            plot.setRangeGridlinesVisible(true);
            plot.setDomainCrosshairVisible(true);
            plot.setRangeCrosshairVisible(true);      
            XYItemRenderer r = plot.getRenderer();
            if (r instanceof XYLineAndShapeRenderer) 
            {
                XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
                renderer.setShapesVisible(false); 
                //renderer.setLinesVisible(false);
                renderer.setShapesFilled(false);
                renderer.setSeriesPaint(0,plot.getRenderer().getItemLabelPaint()); 
            }
            int seriesnr = 0;
            if(xydatasetINT.getSeriesCount()!=0)
            {
            	NumberAxis rangeAxisINT = new NumberAxis("Integer");
            	DecimalFormat intFormat = new DecimalFormat("0");
            	rangeAxisINT.setNumberFormatOverride(intFormat);            	
                plot.setRangeAxis(seriesnr,rangeAxisINT);
                plot.setDataset(seriesnr,xydatasetINT);
                plot.mapDatasetToRangeAxis(seriesnr,seriesnr);
                seriesnr++;
            }
            if(xydatasetLONG.getSeriesCount()!=0)
            {
            	NumberAxis rangeAxisLONG = new NumberAxis("Double_float");
                plot.setRangeAxis(seriesnr,rangeAxisLONG);
                plot.setDataset(seriesnr,xydatasetLONG);
                plot.mapDatasetToRangeAxis(seriesnr,seriesnr);
//                XYItemRenderer r1 = plot.getRenderer(seriesnr);
//                if (r1 instanceof XYLineAndShapeRenderer) {
//                    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r1;
//                    renderer.setDefaultShapesVisible(false);
//                    renderer.setDefaultShapesFilled(false);
//                    renderer.setSeriesPaint(seriesnr,plot.getRenderer().getItemLabelPaint()); 
//                }
//                plot.setRenderer(seriesnr,r1);
                seriesnr++;
            }
            if(xydatasetFLOAT.getSeriesCount()!=0)
            {
            	NumberAxis rangeAxisFLOAT = new NumberAxis("Float");
                plot.setRangeAxis(seriesnr,rangeAxisFLOAT);
                
                plot.setDataset(seriesnr,xydatasetFLOAT);
                plot.mapDatasetToRangeAxis(seriesnr,seriesnr);
//                XYItemRenderer r1 = plot.getRenderer(seriesnr);
//                if (r1 instanceof XYLineAndShapeRenderer) {
//                    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r1;
//                    renderer.setDefaultShapesVisible(false);
//                    renderer.setDefaultShapesFilled(false);
//                    renderer.setSeriesPaint(seriesnr,plot.getRenderer().getItemLabelPaint()); 
//                }
//                plot.setRenderer(seriesnr,r1);
                seriesnr++;
            }
            if(xydatasetUNSIGNED.getSeriesCount()!=0)
            {
            	NumberAxis rangeAxisUNSIGNED = new NumberAxis("Unsigned_Int");
                plot.setRangeAxis(seriesnr,rangeAxisUNSIGNED);
                plot.setDataset(seriesnr,xydatasetUNSIGNED);
                plot.mapDatasetToRangeAxis(seriesnr,seriesnr);
//                XYItemRenderer r1 = plot.getRenderer(seriesnr);
//                if (r1 instanceof XYLineAndShapeRenderer) {
//                    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r1;
//                    renderer.setDefaultShapesVisible(false);
//                    renderer.setDefaultShapesFilled(false);
//                    renderer.setSeriesPaint(seriesnr,plot.getRenderer().getItemLabelPaint()); 
//                }
//                plot.setRenderer(seriesnr,r1);
                seriesnr++;
            }
            return chart;   
        }
        
        @SuppressWarnings("static-access")
		private static XYDataset createDataset(Dataset dataset, DatasetView dv, int type, boolean useScroll) 
        {
    		Dataset.ParameterList parameters = dataset.parameters;
    		TimeSeriesCollection timecoll = new TimeSeriesCollection();
            for (int s=0; s<parameters.size();s++)
            {
            	Dataset.Parameter parameter = parameters.get(s);
            	if (parameter.type == type)
            	{
            		String selString =dv.getViewTable().getModel().getValueAt(s,0).toString();
                    if(selString.equalsIgnoreCase("true"))
                    {
                    	if (useScroll)
                    	{
                    		long start = dataset.beginLT.getTime() + dv.slider.getValue();
                    		timecoll.addSeries(createTimeSeries(parameter, start, dv.getMilliSeconds(), dataset));
                    	}
                    	else
                    	{
                    		timecoll.addSeries(createTimeSeries(parameter));
                    	}
                    }
            	}
            }   
            return timecoll;
        }
        
        private static TimeSeries createTimeSeries(Dataset.Parameter p)
        {	
		    Dataset.SampleList sampleList3 =p.data;
		    String nickName = p.alias;
		    if (nickName == null) nickName = Dataset.shortPathname(p.pathname);
            TimeSeries timeSeries = new TimeSeries(nickName, Millisecond.class);
            for(int j=0; j<sampleList3.size();j++)
            {
			  Dataset.Sample sample = sampleList3.get(j);
			  switch(p.type)
			  {
			  case Dataset.INTEGER:
				  long i = ((Dataset.IntegerValue) sample.value).value;
				  //TODO if (timeSeries.getDataItem(new Millisecond(sample.localTime)) == null)
						  timeSeries.add(new Millisecond(sample.localTime),i);
				  timeSeries.setDescription("Integer");
				  break;
			  case Dataset.FLOAT:
				  float f = ((Dataset.FloatValue) sample.value).value;
				  timeSeries.add(new Millisecond(sample.localTime),f);
				  timeSeries.setDescription("Float");
				  break;
			  case Dataset.DOUBLE_FLOAT:
				  double lf = ((Dataset.DoubleFloatValue) sample.value).value;
				  timeSeries.add(new Millisecond(sample.localTime),lf);
				  break;
			  case Dataset.UNSIGNED_INTEGER:
				  long ui = ((Dataset.UnsignedIntegerValue) sample.value).value;
				  //TODO if (timeSeries.getDataItem(new Millisecond(sample.localTime)) == null)
						  timeSeries.add(new Millisecond(sample.localTime),ui);
				  timeSeries.setDescription("Unsigned_Integer");
				  break;
			  default:
				  System.out.println("Type is not defined: " + p.type);			  
			  }	  
		    }
        	return timeSeries;
        }
        
        private static TimeSeries createTimeSeries(Dataset.Parameter p, long start, long timeRange, Dataset d)
        {	
		    Dataset.SampleList sampleList3 =p.data;
		    String nickName = p.alias;
		    if (nickName == null) nickName = Dataset.shortPathname(p.pathname);
            TimeSeries timeSeries = new TimeSeries(nickName, Millisecond.class);
            for(int j=0; j<sampleList3.size();j++)
            {
			  Dataset.Sample sample = sampleList3.get(j);
			  switch(p.type)
			  {
			  case Dataset.INTEGER:
				  if (isInIntervall(start, timeRange, sample, d))
				  {
					  long i = ((Dataset.IntegerValue) sample.value).value;
					  if (timeSeries.getDataItem(new Millisecond(sample.localTime)) == null)
						  timeSeries.add(new Millisecond(sample.localTime),i);
					  timeSeries.setDescription("Integer");
				  }				  
				  break;
			  case Dataset.FLOAT:
				  if (isInIntervall(start, timeRange, sample, d)) {
				  float f = ((Dataset.FloatValue) sample.value).value;
				  timeSeries.add(new Millisecond(sample.localTime),f);
				  timeSeries.setDescription("Float");
				  }
				  break;
			  case Dataset.DOUBLE_FLOAT:
				  if (isInIntervall(start, timeRange, sample, d)) {
				  double lf = ((Dataset.DoubleFloatValue) sample.value).value;
				  timeSeries.add(new Millisecond(sample.localTime),lf);
				  }
				  break;
			  case Dataset.UNSIGNED_INTEGER:
				  if (isInIntervall(start, timeRange, sample, d)) {
				  long ui = ((Dataset.UnsignedIntegerValue) sample.value).value;
				  if (timeSeries.getDataItem(new Millisecond(sample.localTime)) == null)
						  timeSeries.add(new Millisecond(sample.localTime),ui);
				  timeSeries.setDescription("Unsigned_Integer");
				  }
				  break;
			  default:
				  System.out.println("Type is not defined: " + p.type);			  
			  }	  
		    }
        	return timeSeries;
        }

        public static boolean isInIntervall(long start, long timeRange, Dataset.Sample s, Dataset d)
        {
        	long timeMin = start; 
        	long timeMax = timeMin + timeRange;
        	if (timeMax > d.endLT.getTime()) timeMax = d.endLT.getTime();
        	long time = s.localTime.getTime();
        	return timeMin <= time && time <= timeMax;
        }
        
		public ChartPanel getChartPanel() 
		{
			return chartPanel;
		}
    
   }
about the colors for multiple axes. remember the MultipleAxisDemo1 in the JfreeChart Demo Collection? That is what I want :) I will try the three methods you suggested. Thank you.

Locked