【问题标题】:Histogram using Chart2D使用 Chart2D 的直方图
【发布时间】:2012-04-01 13:48:52
【问题描述】:

我想画JPEG的系数直方图。我在 Google 上搜索了几个小时以了解如何使用 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)); 

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

BufferedImage lbImage = lbChart2D.getImage();

怎么了?

【问题讨论】:

    标签: java swing jpeg histogram jchart2d


    【解决方案1】:

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

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

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

    【讨论】:

    • 有什么教程可以帮助我吗?
    • 我会从The Chart2D Tutorial开始。
    • 它们在源代码分发中。
    • Chart2D/LBChart2DFrameDemo.java 有什么问题?它显示了十几个条形图变化。另见faq;这不是免费的编码服务。
    • 好吧,我问如何将数组设置为数据集? ,谁知道告诉我,提前谢谢
    【解决方案2】:

    感谢@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));
    ///
    

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 2014-02-15
      • 2020-10-27
      • 1970-01-01
      • 2013-12-04
      • 2012-11-18
      • 2016-06-02
      相关资源
      最近更新 更多