使用Chart2D的直方图 [英] Histogram using Chart2D
问题描述
我想绘制JPEG的系数直方图。我在Google上搜索了几个小时,知道如何使用 Chart2D 库,但是没有教程提供示例。我想要绘制的数组是 hist []
。我创建了一个 LBChart2D
的对象,但我不知道如何将数组设置为数据集。
I want to draw coefficient histogram of JPEG. I'm searching on Google for hours to know how to use Chart2D library, but there is no tutorial with examples. The array which I want to draw is hist[]
. I created an object of LBChart2D
, but I don't know how to set the array as data set to it.
//coeff[] is the coefficients array
for(int i=0;i<coeff.length;i++)
hist[coeff[i]]++;
LBChart2D lbChart2D = new LBChart2D();
编辑:这是我正在尝试的:
Here is what I'm trying :
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
在这一行:
Now It produces an Exception java.lang.NullPointerException
on this line:
BufferedImage lbImage = lbChart2D.getImage();
出了什么问题?
推荐答案
几个 Chart2D
演示包含在发行版中。您可以从 ImageIO
获得的 BufferedImage
中收集数据。另请参阅 jfreechart 。
Several Chart2D
demos are included in the distribution. You can collect the data from a BufferedImage
obtained via ImageIO
. See also jfreechart.
附录:如果没有完整的示例,您可以使用 validate()
来获取调试消息。至少,验证您使用 hist.length
标签调用 setLabelsAxisLabelsTexts()
。
Addendum: Absent a complete example, you can use validate()
to get debug messages. At a minimum, verify that you invoke setLabelsAxisLabelsTexts()
with hist.length
labels.
//Optional validation: Prints debug messages if invalid only.
if (!chart2D.validate(false)) {
chart2D.validate(true);
}
这篇关于使用Chart2D的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!