使用Chart2D的直方图

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

 //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)); 

现在它在这一行产生一个Exception java.lang.NullPointerException

 BufferedImage lbImage = lbChart2D.getImage(); 

怎么了?

分发中包含了几个Chart2D 演示 。 您可以从通过ImageIO获得的BufferedImage收集数据。 另见jfreechart 。

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

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

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

 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=-5) val[coeff[kk]+5]++; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for(int ii=0;ii