Put two curves with different domains/ranges on one XYPlot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
lois.vanhee
Posts: 2
Joined: Wed Mar 09, 2016 9:16 am
antibot: No, of course not.

Put two curves with different domains/ranges on one XYPlot

Post by lois.vanhee » Wed Mar 09, 2016 4:57 pm

Dear community,

I want to create a single XYPlot based on two datasets and I want to have domain for each dataset. For the example, the first plot is an XYBarChart indicating the current planning (amount of money spend in the following weeks, domain [0,10]) and the second plot indicates some info from a related algorithm (range [0,100]).

What I want is to have both on the same plot. My point is that my second dataset is matched with the range of the first dataset. So, if I have the point in (-50,15) from the second dataset (the one in [-100,100]), it plots it as if it was a point of the first dataset (the one in [0,10]): rescaling the whole figure. Note that, for other aspects of the graph (one is based on lines and the other on bars), it works perfectly.

Do you know how I can solve this issue, please?

Thanks for any help!

Here is further detail about my code. The main function:

Code: Select all

private static JFreeChart consumptionAndCostCurveChart(Object cp)
	{
		XYPlot consumptionPlot = new XYPlot();
		addConsumptionBars(cp,consumptionPlot);
		
		addCostLines(cp,consumptionPlot);
		JFreeChart jfc = new JFreeChart(consumptionPlot);

		jfc.setTitle("hi");
		return jfc;
		
	}
The subfunction for the first graph, fitting the adequate ranges and datasets on index 0:

Code: Select all

	private static void addConsumptionBars(Object cp, XYPlot consumptionPlot) {
		IntervalXYDataset consumptionDataset = getConsumptionDataset(cp);
		consumptionPlot.setDataset(0,consumptionDataset);
		consumptionPlot.setDomainAxis(0,new NumberAxis("Timeslot"));
		
		NumberAxis rangeAxis = new NumberAxis("Energy");
		if(minConsumptionMargin!=maxConsumptionMargin)
			rangeAxis.setRange(minConsumptionMargin, maxConsumptionMargin);
		consumptionPlot.setRangeAxis(0,rangeAxis);
		
		XYBarRenderer renderer = new XYBarRenderer();
		renderer.setShadowVisible(false);
		renderer.setSeriesPaint(0, new Color(0.2f, 0.2f, 0.2f, 0.5f));
		renderer.setSeriesPaint(1, new Color(0, 0, 1f, 1f));
		renderer.setDrawBarOutline(false);
		renderer.setBarPainter(new StandardXYBarPainter());
		consumptionPlot.setRenderer(0,renderer);
	}
The plot for the second graph, fitting the adequate ranges and datasets at index 1 :

Code: Select all

private static void addCostLines(Object cp, XYPlot consumptionPlot) 
	{
		IntervalXYDataset costDataset = getCostDataSet(cp);
		consumptionPlot.setDataset(1,costDataset);
		
		NumberAxis domainAxis = new NumberAxis("Iteration");
		domainAxis.setRange(-100,100);
		consumptionPlot.setDomainAxis(1,domainAxis);
		
		NumberAxis rangeAxis = new NumberAxis("Cost");
		rangeAxis.setRange(-1000, 1000);
		consumptionPlot.setRangeAxis(1,rangeAxis);
		
		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesPaint(0, Color.black);
		consumptionPlot.setRenderer(1,renderer);
	}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Put two curves with different domains/ranges on one XYPl

Post by david.gilbert » Thu Mar 10, 2016 6:40 am

You need to tell the plot to map dataset 1 to domain axis 1:

http://www.jfree.org/jfreechart/api/jav ... s-int-int-
David Gilbert
JFreeChart Project Leader

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

lois.vanhee
Posts: 2
Joined: Wed Mar 09, 2016 9:16 am
antibot: No, of course not.

Re: Put two curves with different domains/ranges on one XYPl

Post by lois.vanhee » Fri Mar 11, 2016 10:41 am

Brilliant!
Thanks a lot david.gilbert for your quick and accurate reply!

For sake of completeness for future readers, I added to the "addCostLines" function, the following lines:

Code: Select all

List axisIndices = new LinkedList();
		axisIndices.add(1);
		consumptionPlot.mapDatasetToDomainAxes(1, axisIndices);
		consumptionPlot.mapDatasetToRangeAxes(1, axisIndices);

Locked