将JChart添加到面板 [英] Adding a JChart to a panel

查看:37
本文介绍了将JChart添加到面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用API​​是新手.我正在尝试在面板上显示图表.我已经设置了库,它们正在工作.我使用的API是JFreeCharts.

Im new to using APIs. I am trying to get a chart showing on a panel. I have already set up the libs and they are working. The API im using is JFreeCharts.

我制作了一个名为JChart的课程:

I made a class called JChart:

 public class JChart extends JFrame
 {

private static final long serialVersionUID = 1L;

  public JChart(String applicationTitle, String chartTitle) 
  {

        // This will create the dataset 
        PieDataset dataset = createDataset();
        // based on the dataset we create the chart
        JFreeChart chart = createChart(dataset, chartTitle);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(200, 150));



    }



/**
     * Creates a sample dataset 
     */

    private  PieDataset createDataset() {
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;

    }


/**
     * Creates a chart
     */

    private JFreeChart createChart(PieDataset dataset, String title) {

        JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
            dataset,                // data
            true,                   // include legend
            true,
            false);

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;

    }

}

在我的表单中,我有一个方法:

In my form I have a method:

  private void LoadMyGraphs()
    {     
        JChart chart = new JChart("name", "title");
        myGraphPanel.add(chart); 

    }

我没有收到任何错误,但是面板没有变化.我可以更改面板的背景颜色,因此我知道这方面没有任何问题.任何信息都将是惊人的,谢谢!

I am getting no errors, but the panel does not change. I can changed the background color of the panel so I know nothing is wring in that respect. Any info would be amazing, thanks!

推荐答案

有时,您必须将 ChartPanel 添加到封闭框架中的容器中,例如

At some point you must add the ChartPanel to a container in the enclosing frame, e.g.

this.add(chartPanel);

顺便说一句,也请考虑覆盖 getPreferredSize(),如此处所示并建议此处.另请参见 初始线程 .此处.

As an aside, also consider overriding getPreferredSize(), as shown here and suggested here. See also Initial Threads. More examples are cited here.

这篇关于将JChart添加到面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆