2012-04-01 51 views
1

我想绘制JPEG的系数直方图。我正在Google上搜索几个小时,以了解如何使用Chart2D库,但没有示例教程。我想绘制的数组是hist[]。我创建了一个LBChart2D的对象,但我不知道如何将数组设置为数据集。使用Chart2D的直方图

//coeff[] is the coefficients array 
for(int i=0;i<coeff.length;i++) 
hist[coeff[i]]++; 

LBChart2D lbChart2D = new LBChart2D(); 

编辑:这是我尝试:

Object2DProperties object2DProps = new Object2DProperties(); 
object2DProps.setObjectTitleText ("Title "); 
Chart2DProperties chart2DProps = new Chart2DProperties(); 
chart2DProps.setChartBetweenChartAndLegendGapThicknessModel(5); 
LegendProperties legendProps = new LegendProperties(); 
legendProps .setLegendBorderThicknessModel(5); 
legendProps.setLegendBackgroundColor(Color.yellow); 
legendProps.setLegendExistence (false); 
GraphChart2DProperties graph2DProps = new GraphChart2DProperties(); 
GraphProperties graphProps = new GraphProperties(); 
object2DProps .setObjectTitleFontName("test"); 
Dataset dataset = new Dataset (1, hist.length, 1); 
for(int i=0;i<hist.length;i++) 
dataset.set (0, i, 0, hist[i]) ; 
LBChart2D lbChart2D = new LBChart2D(); 
lbChart2D.setObject2DProperties (object2DProps); 
lbChart2D.setChart2DProperties (chart2DProps); 
lbChart2D.setLegendProperties (legendProps); 
lbChart2D.setGraphChart2DProperties (graph2DProps); 
lbChart2D.addGraphProperties (graphProps); 
lbChart2D.addDataset (dataset); 
lbChart2D.setSize(width, height); 
BufferedImage lbImage = lbChart2D.getImage(); 
jLabel15.setIcon(new ImageIcon(lbImage)); 

现在,它在这条线产生异常java.lang.NullPointerException

BufferedImage lbImage = lbChart2D.getImage(); 

有什么不对?

回答

2

几个Chart2Ddemos包含在该分布中。您可以收集通过ImageIO获得的BufferedImage的数据。另请参阅

附录:如果没有完整的示例,可以使用validate()来获取调试消息。至少,请验证您是否使用hist.length标签调用setLabelsAxisLabelsTexts()

//Optional validation: Prints debug messages if invalid only. 
if (!chart2D.validate(false)) { 
    chart2D.validate(true); 
} 
+0

有没有可以帮助我的例子的任何教程? – muhannad 2012-04-01 14:09:08

+0

我会从[Chart2D教程](http://chart2d.sourceforge.net/Chart2D_1.9.6/Tutorial/Tutorial.htm)开始。 – trashgod 2012-04-01 14:12:37

+0

我看过了,但没有明显的例子!!!! – muhannad 2012-04-01 14:16:38

1

谢谢@trashgod试图帮助我。别担心,我已经知道了。 我用库来绘制直方图,这里是我使用的代码。

int hist[]=new int[11]; 
int val[]=new int[11]; 
for(int ii=0;ii<11;ii++) 
    hist[ii]=ii-5;//to get negative indeces I used an array to save them 
for(int kk=0;kk<coeff.length;kk++) 
if(coeff[kk]<=5 &coeff[kk]>=-5) val[coeff[kk]+5]++; 
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
for(int ii=0;ii<hist.length;ii++) 
dataset.setValue(val[ii], "Coefficient value",""+hist[ii]); 
JFreeChart chart = ChartFactory.createBarChart("Original Histogram", 
    "Coefficient value", "", dataset, 
    PlotOrientation.VERTICAL, false,true, false);  
//chart.setBackgroundPaint(Color.yellow); 
chart.getTitle().setPaint(Color.blue); 
CategoryPlot p = chart.getCategoryPlot(); 
p.setOutlinePaint(Color.BLUE); 
p.setRangeGridlinePaint(Color.blue); 
orgim=chart.createBufferedImage(400,400); 
Image im1= orgim.getScaledInstance(jLabel12.getWidth(),jLabel12.getHeight(),1); 
jLabel12.setIcon(new ImageIcon(im1)); 
/// 
+0

极好的选择! – trashgod 2012-04-14 06:31:38