addAnnotation function of the ContourPlot class can't work

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
alsw-2000
Posts: 16
Joined: Thu May 22, 2003 12:53 pm

addAnnotation function of the ContourPlot class can't work

Post by alsw-2000 » Wed May 05, 2004 11:42 am

Dear all,

I have make a contour chart like this
Image
but my requirement is to make a contour chart on a category axis.
And I find that the contour chart only support value axis so I want to use the following work around (please tell me if there is a better way)
1. hidden the axis's tick label
2. add annotation at the position of the tick label to make the axis looks like a category axis

However, I found that the addAnnotation function of the ContourPlot class can't work !!!

It seems that the addAnnotation function only support XYPlot and CategoryPlot, but ContourPlot is not their subclass, so it can't work.

Here is my code for your reference:

Code: Select all

package charts.jfreechart;

import java.awt.Font;
import java.awt.Color;
import java.awt.GradientPaint;
import java.util.Date;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ColorBar;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.ContourPlot;
import org.jfree.data.ContourDataset;
import org.jfree.data.NonGridContourDataset;
import org.jfree.data.DefaultContourDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.ui.ColorPalette;
import org.jfree.chart.ui.RainbowPalette;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.ui.TextAnchor;



public class JFree_Contour extends ApplicationFrame
{
	public ChartPanel panel = null;

	public JFree_Contour(String title)
	{
		super(title);

		JFreeChart chart = createContourPlot();
		panel = new ChartPanel(chart, true, true, true, true, true);
		panel.setPreferredSize(new java.awt.Dimension(500, 270));
		panel.setMaximumDrawHeight(100000); //stop ChartPanel from scaling output
		panel.setMaximumDrawWidth(100000); //stop ChartPanel from scaling output
		panel.setHorizontalZoom(true);
		panel.setVerticalZoom(true);
		panel.setFillZoomRectangle(true);

	}

	/**
	 * Creates a ContourPlot chart.
	 *
	 * @return the chart.
	 */
	private JFreeChart createContourPlot()
	{
		String title = "Contour Plot";
		String xAxisLabel = "Option Delta";
		String yAxisLabel = "Maturity";
		String zAxisLabel = "";

		ValueAxis xAxis = null;
		NumberAxis yAxis = null;
		ColorBar zColorBar = null;

		xAxis = new NumberAxis(xAxisLabel);
		yAxis = new NumberAxis(yAxisLabel);
		zColorBar = new ColorBar(zAxisLabel);

		((NumberAxis) xAxis).setAutoRangeIncludesZero(false);
		xAxis.setTickLabelsVisible(false);
		xAxis.setTickMarksVisible(false);

		yAxis.setAutoRangeIncludesZero(false);
		yAxis.setTickLabelsVisible(false);
		yAxis.setTickMarksVisible(false);

		((NumberAxis) xAxis).setLowerMargin(0.0);
		((NumberAxis) xAxis).setUpperMargin(0.0);

		yAxis.setLowerMargin(0.0);
		yAxis.setUpperMargin(0.0);

		zColorBar.getAxis().setTickMarksVisible(true);
		zColorBar.setMinimumValue(-250);
		zColorBar.setMaximumValue(200);

		NonGridContourDataset data = createDataset();

		ColorPalette cp = new RainbowPalette();

		// stepping = 50
		double x[] = {-250,-200,-150,-100,-50, 0, 50, 100, 150, 200};

		cp.setTickValues(x);
		cp.setStepped(true);

		zColorBar.setColorPalette(cp);

		ContourPlot plot = new ContourPlot(data, xAxis, yAxis, zColorBar);
				
		plot.setDataAreaRatio(0);

		// Shove in the text
		XYTextAnnotation a1 = new XYTextAnnotation("10P", 20, 20);
		a1.setFont(new Font("SansSerif", Font.PLAIN, 12));
//		a1.setTextAnchor(TextAnchor.TOP_CENTER);
		plot.addAnnotation(a1);
		
		JFreeChart chart = new JFreeChart(title, null, plot, false);

		return chart;
	}

	private NonGridContourDataset createDataset()
	{
		NonGridContourDataset data = null;
		
// dataset of Andy Huson
 
		Double[] oDoubleX = new Double[25];
		Double[] oDoubleY = new Double[25];
		Double[] oDoubleZ = new Double[25];

		// 10P
		oDoubleX[0] = new Double(1);
		oDoubleY[0] = new Double(1);
		oDoubleZ[0] = new Double(-50);

		oDoubleX[1] = new Double(1);
		oDoubleY[1] = new Double(2);
		oDoubleZ[1] = new Double(-38.50);

		oDoubleX[2] = new Double(1);
		oDoubleY[2] = new Double(3);
		oDoubleZ[2] = new Double(-38.50);

		oDoubleX[3] = new Double(1);
		oDoubleY[3] = new Double(4);
		oDoubleZ[3] = new Double(-38.50);

		oDoubleX[4] = new Double(1);
		oDoubleY[4] = new Double(5);
		oDoubleZ[4] = new Double(-38.50);

		// 25P
		oDoubleX[5] = new Double(2);
		oDoubleY[5] = new Double(1);
		oDoubleZ[5] = new Double(-13.2);

		oDoubleX[6] = new Double(2);
		oDoubleY[6] = new Double(2);
		oDoubleZ[6] = new Double(-41.05);

		oDoubleX[7] = new Double(2);
		oDoubleY[7] = new Double(3);
		oDoubleZ[7] = new Double(-41.05);

		oDoubleX[8] = new Double(2);
		oDoubleY[8] = new Double(4);
		oDoubleZ[8] = new Double(-41.05);

		oDoubleX[9] = new Double(2);
		oDoubleY[9] = new Double(5);
		oDoubleZ[9] = new Double(-41.05);

		// ATM
		oDoubleX[10] = new Double(3);
		oDoubleY[10] = new Double(1);
		oDoubleZ[10] = new Double(-22.86);

		oDoubleX[11] = new Double(3);
		oDoubleY[11] = new Double(2);
		oDoubleZ[11] = new Double(-138);

		oDoubleX[12] = new Double(3);
		oDoubleY[12] = new Double(3);
		oDoubleZ[12] = new Double(-138);

		oDoubleX[13] = new Double(3);
		oDoubleY[13] = new Double(4);
		oDoubleZ[13] = new Double(-138);

		oDoubleX[14] = new Double(3);
		oDoubleY[14] = new Double(5);
		oDoubleZ[14] = new Double(-138);

		// 25C
		oDoubleX[15] = new Double(4);
		oDoubleY[15] = new Double(1);
		oDoubleZ[15] = new Double(-27.96);

		oDoubleX[16] = new Double(4);
		oDoubleY[16] = new Double(2);
		oDoubleZ[16] = new Double(-51.91);

		oDoubleX[17] = new Double(4);
		oDoubleY[17] = new Double(3);
		oDoubleZ[17] = new Double(-51.91);

		oDoubleX[18] = new Double(4);
		oDoubleY[18] = new Double(4);
		oDoubleZ[18] = new Double(-51.91);

		oDoubleX[19] = new Double(4);
		oDoubleY[19] = new Double(5);
		oDoubleZ[19] = new Double(-51.91);

		// 10C
		oDoubleX[20] = new Double(5);
		oDoubleY[20] = new Double(1);
		oDoubleZ[20] = new Double(-31.01);

		oDoubleX[21] = new Double(5);
		oDoubleY[21] = new Double(2);
		oDoubleZ[21] = new Double(-56.73);

		oDoubleX[22] = new Double(5);
		oDoubleY[22] = new Double(3);
		oDoubleZ[22] = new Double(-56.73);

		oDoubleX[23] = new Double(5);
		oDoubleY[23] = new Double(4);
		oDoubleZ[23] = new Double(-56.73);

		oDoubleX[24] = new Double(5);
		oDoubleY[24] = new Double(5);
		oDoubleZ[24] = new Double(-56.73);

		data = new NonGridContourDataset("Contouring", oDoubleX, oDoubleY, oDoubleZ);

		return data;

	}

	/**
	 * Starting point for the demonstration application.
	 *
	 * @param args  command line options, launch ContourDemoPlot -? for listing of options.
	 */
	public static void main(String[] args)
	{

		JFree_Contour demo = new JFree_Contour("ContourPlot Demo");
		demo.setContentPane(demo.panel);
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);

	}

}
Cheers,
Andy

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 May 05, 2004 1:28 pm

Why do you want a category axis? Your data values look numeric to me. Still, if you need to display categories, try the SymbolicAxis class which will convert numbers to labels.

The ContourPlot class needs to be changed so that it is a subclass of XYPlot, then annotations etc. will work. Not sure when that will get done. Someone else posted some code to get annotations working, but I forget where (search this forum and also the bugs, patches and RFEs at the SourceForge page.
David Gilbert
JFreeChart Project Leader

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

Anoop

Contour plot request

Post by Anoop » Tue Dec 06, 2005 6:21 am

Hi,

Im too looking for a functional "Response Surface Plot" which does smoothing and shows contour with gradient colors or just colored curves. I believe many developers are looking for it. Contour plot might be similar to it. We would appreciate if David puts some effort in this area soon.

Here is an example we are looling for :

Image

Locked