Table data plotted along with chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
anjanas
Posts: 5
Joined: Fri Mar 28, 2008 5:41 am

Table data plotted along with chart

Post by anjanas » Fri Mar 28, 2008 6:42 am

Hi

Is there any way to display the data plotted shown along with the chart. I need it as a separate table . Not as the item label in each plot in chart .

X Y Value
10 10 100
20 20 200
30 30 300
40 40 400
50 50 500

Is there any way to do it.. Please help

anjanas
Posts: 5
Joined: Fri Mar 28, 2008 5:41 am

Post by anjanas » Fri Mar 28, 2008 10:27 am

Just tell me if it is supported with jfree? Or anything like displaying a long text along with graph ?

s12kalpana
Posts: 5
Joined: Wed Apr 02, 2008 10:55 am

re:

Post by s12kalpana » Wed Apr 02, 2008 12:17 pm

Hi

As far as I knew ,we cannot get the data display beside the chart other than the labels and titles and subtitles.I also with the same issue.I dont know whether jfreecharts supports this feature.

jpmaia
Posts: 53
Joined: Fri Feb 22, 2008 10:44 pm

Post by jpmaia » Wed Apr 02, 2008 6:41 pm

You have to create a separate JPanel and display the data in there, probably using a JTable.

A couple of the demo programs display data in a separate table. I don't remember the names of the applicable demo, but if you run the demo program you'll find them there.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Post by paradoxoff » Wed Apr 02, 2008 11:10 pm

If you need the table to be part of the chart, you can use a nested BlockContainer with LabelBlocks. The example here uses the following approach:
The x values and y values are stored as LabelBlocks in two separate BlockContainers each having a ColumnArrangement.
These two BlockContainers are managed by a further main BlockContainer that has a FlowArrangement.
it is probably not the best approach. I tried to use a GridArrangement but that threw a "Not yet implemented" Exception :cry: .
Enjoy.

Code: Select all

package demo;

import java.awt.Font;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.block.Arrangement;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.ColumnArrangement;
import org.jfree.chart.block.FlowArrangement;
import org.jfree.chart.block.GridArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.CompositeTitle;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.ui.RectangleEdge;

public class ChartComponentDemo2{
	private static Font smallfont;
	private static Font bigfont;
	public static void main(String[] args){
		smallfont = new Font("Arial",0,12);
		bigfont = new Font("Arial",0,16);
		DefaultXYDataset set = new DefaultXYDataset();
		double[][] data = new double[][]{{10.0d,20.0d,30.0d,40.0d},{1.0d,2.0d,3.0d,4.0d}};
		set.addSeries("One",data);
		NumberAxis xAxis = new NumberAxis("x axis");
		xAxis.setLabelFont(smallfont);
		NumberAxis yAxis = new NumberAxis("yaxis");
		yAxis.setLabelFont(smallfont);
		XYPlot xyplot = new XYPlot(set,xAxis,yAxis,new XYLineAndShapeRenderer(true,true));
		JFreeChart chart = new JFreeChart(" The Chart",bigfont,xyplot,true);
		String[][] stringtable = createStringTableData(set,0);
		BlockContainer xValues = new BlockContainer(new ColumnArrangement());
		BlockContainer yValues = new BlockContainer(new ColumnArrangement());
		for(int i = 0; i < stringtable.length; i++){
			xValues.add(new LabelBlock(stringtable[i][0]));
			yValues.add(new LabelBlock(stringtable[i][1]));
		}
		BlockContainer mainBlock = new BlockContainer(new FlowArrangement());
		mainBlock.add(xValues);
		mainBlock.add(yValues);
		CompositeTitle cTitle = new CompositeTitle(mainBlock);
		cTitle.setPosition(RectangleEdge.RIGHT);
		chart.addSubtitle(cTitle);
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(new ChartPanel(chart));
		frame.pack();
		frame.setVisible(true);
 		
	}
	private static String[][] createStringTableData(XYDataset set,int seriesIndex){
		String[][] data = new String[set.getItemCount(seriesIndex)][2];
		for(int count = 0;count < set.getItemCount(seriesIndex);count++ ){
			data[count][0] = String.valueOf(set.getXValue(seriesIndex,count));
			data[count][1] = String.valueOf(set.getYValue(seriesIndex,count));
		}
		return data;
	}
}


anjanas
Posts: 5
Joined: Fri Mar 28, 2008 5:41 am

Post by anjanas » Fri Apr 04, 2008 11:45 am

Thanks so much for the info.

Locked