Add a line to CONNECT the Dual-Axis Line.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
bruce_infosys
Posts: 1
Joined: Tue Nov 09, 2010 11:03 am
antibot: No, of course not.

Add a line to CONNECT the Dual-Axis Line.

Post by bruce_infosys » Tue Nov 09, 2010 11:41 am

My JFreeChart is 1.0.13. I have created a dual axis line chart. The y axis are 'Sales' and 'Margin' As the business, I must add a line to connect the two different lines at the same domain axis value. Would you please explain me how to do. I have a demo as well, but I can't attach it here. If need, I would be glad to send the demo to you. My email address is : Bruce_Wang@infosys.com

Thank you for support. As below is my code.

Code: Select all

package example;

import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CyclicXYItemRenderer;
import org.jfree.data.xy.DefaultXYDataset;

public class DualAxisLine {

	public static void main(String[] args) {
		double[][] arrSales = { { 0.00, 0.10, 0.20, 0.30, 0.40, 0.50 },
				{ 158.79, 1524.83, 2845.63, 3931.16, 4466.86, 3950.87 } };
		double[][] arrMargin = { { 0.00, 0.10, 0.20, 0.30, 0.40, 0.50 },
				{ 115.91, 708.44, 991.69, 685.48, -645.64, -1582.87 } };

		DefaultXYDataset dataset1 = new DefaultXYDataset();
		DefaultXYDataset dataset2 = new DefaultXYDataset();

		dataset1.addSeries("Sales", arrSales);
		dataset2.addSeries("Margin", arrMargin);

		NumberAxis discountAxis = new NumberAxis("Product Discount");
		NumberAxis salesAxis = new NumberAxis("Sales");
		NumberAxis marginAxis = new NumberAxis("Margin");

		discountAxis.setRange(0.00, 0.50);

		CyclicXYItemRenderer renderer1 = new CyclicXYItemRenderer();
		CyclicXYItemRenderer renderer2 = new CyclicXYItemRenderer();

		XYPlot plot = new XYPlot(dataset1, discountAxis, salesAxis, renderer1);

		plot.setDataset(1, dataset2);
		plot.mapDatasetToRangeAxis(1, 1);
		plot.setRangeAxis(1, marginAxis);
		plot.setRenderer(1, renderer2);
		plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

		JFreeChart chart = new JFreeChart("Offer Discount Sensitivity", null,
				plot, true);

		ChartFrame frame = new ChartFrame("Sensitivity", chart);
		frame.pack();
		frame.setVisible(true);
	}
}

Locked