如何在JFreeChart条形图中更新数据集 [英] How to update dataset in JFreeChart Bar Chart

查看:96
本文介绍了如何在JFreeChart条形图中更新数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JFrame,在此框架中有一个JPanel,在JPanel中有一个JFreeChart(图表从方法中获取数据集),并将此图表添加到在JPanel中创建的ChartPanel,然后将ChartPanel添加到JPanel.另外,如果我更改了该JComboBox中的选项,那么ActionListener会更新数据集的值,我还会在JPanel中获得JComboBox.也是返回数据集的方法,它从JComboBox中获取一个字符串(数据集的数据取决于JComboBox输出).因此,如果我更改JComboBox中的选项,我想用新的数据集更新JFreeChart并将其显示在屏幕上.我知道我需要在该ActionListener中添加代码,但是我不需要在其中添加代码,是否有任何方法已经创建了JFreeChart更新?

I have got a JFrame, within this frame there is a JPanel, in a JPanel there is a JFreeChart (chart takes dataset from a method) and this chart is added to ChartPanel created in JPanel, then ChartPanel is added to JPanel. Also I have got JComboBox in JPanel, if I change option in that JComboBox an ActionListener updates value of dataset. Also method which return dataset, takes a string from JComboBox(data of dataset depends on JComboBox output). So if I change option in JComboBox I would want to update JFreeChart with new dataset and display it on screen. I know that I need to add code in that ActionListener but I don't what I should add there, is there any method which updates already created JFreeChart?

private class PanelChart extends JPanel { {
    this.setLayout(new BorderLayout());

    // Create Dataset
    //method GUIImplementation.GetDataForChart takes as an input value of combobox

    CategoryDataset dataset = GUIImplementation.GetDataForChart(comboBoxCrimeTypeChart.getSelectedItem().toString());


    //Create chart
    JFreeChart crimeNumberBarChart = ChartFactory.createBarChart(
        "Number of crimes by type", //Chart Title
        "Fallen within", // Category axis
        "Number of crimes", // Value axis
        dataset,
        PlotOrientation.VERTICAL,
        true,true,false
        );
    ChartPanel panelCrimeNumberBarChart = new ChartPanel(crimeNumberBarChart);
    this.add(panelCrimeNumberBarChart, BorderLayout.CENTER);

    PanelChartSouth panelChartSouth = new PanelChartSouth(); //there is a combobox
    this.add(panelChartSouth, BorderLayout.SOUTH);
}}

private class ChartButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == backChart) {
            SizeAndTitleSetter(0);
            SwapPanelChart(0);
        } else if(e.getSource() == comboBoxCrimeTypeChart) { //comboBoxCrimeTypeChart is a JComboBox from PanelChartSouth
            String crimeType = comboBoxCrimeTypeChart.getSelectedItem().toString();
            System.out.println(crimeType);
            dataset = GUIImplementation.GetDataForChart(crimeType); // dataset updated
        }
    }
}

到目前为止,我仅设法通过从JFrame中删除PanelChart,然后创建新的PanelChart并将其添加到JFrame并使用重绘和重新验证来更新该图表.如果我一次更改了JComboBox中的选项,它会起作用,第二次更改后,所有内容开始在屏幕上停止显示,旧图表不希望消失,新图表位于其下,如果我调整屏幕大小就可以看到它.

So far I only managed to update somehow that chart by removing PanelChart from JFrame then create new PanelChart and add it to JFrame and use repaint and revalidate. It worked if I changed option in JComboBox once, after second change everything started to brake on screen, old chart didn't want to dissapear and new chart was under it and I could see it if I resized screen.

推荐答案

您可以更新现有图表的数据集,如这些 CategoryDataset 的具体实现通常会提供合适的变体.

You can update an existing chart's dataset, as shown in these examples; when you update the model (dataset), the listening plot (view) will update itself accordingly; concrete implementations of CategoryDataset typically provide suitable mutators.

DefaultCategoryDataset model = …
…
@Override
public void actionPerformed(ActionEvent e) {
    model.setValue(…);
}

您可以使用相关图表的

You can replace an existing chart's dataset using the relevant plot's setDataset() method.

CategoryPlot plot = (CategoryPlot) chart.getPlot();
…
@Override
public void actionPerformed(ActionEvent e) {
    plot.setDataset(…);
}

尽管如此处所示,在技术上可以替换封闭的视图组件,但通常最好更新或替换模型,如此处所示.

While it's technically possible to replace the enclosing view component, as suggested here, it's generally better to update or replace the model, as suggested here.

这篇关于如何在JFreeChart条形图中更新数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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