0

嗨朋友我有一个 DefaultPieDataset 正在以这种方式创建:

package business.intelligence.system;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JInternalFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.jdbc.JDBCPieDataset;


public class DepositBase extends JInternalFrame {

private JPanel jpAcc = new JPanel();
private JList checkBoxesJList;
private JLabel lScheme;
private String schm = "";

DepositBase() throws SQLException, ClassNotFoundException {
    super("Deposit base", false, true, false, true);
    setSize(1300, 600);
    jpAcc.setLayout(null);
    jpAcc.setBackground(Color.LIGHT_GRAY);
    JScrollPane scrollPane = new JScrollPane(checkBoxesJList);
    lScheme = new JLabel("<html><u>SCHEME CODE</u></html>");
    lScheme.setBounds(10, 5, 100, 25);
    lScheme.setForeground(Color.BLACK);
    Connection conn = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.80:1521:simba9i", "SYSTEM", "system123");
        Statement st = conn.createStatement();
        String combo = "select distinct SCHM_CODE from DLY_DEP_VIEW";
        ResultSet res = st.executeQuery(combo);
        String ids = "";
        ArrayList<String> v = new ArrayList<>();
        v.add("All");
        while (res.next()) {
            ids = res.getString("SCHM_CODE");
            v.add(ids);
            checkBoxesJList = new JList(createData(v));

        }



        checkBoxesJList.setBounds(10, 30, 80, 600);
        checkBoxesJList.setBackground(Color.LIGHT_GRAY);
        checkBoxesJList.setCellRenderer(new CheckListRenderer());
        checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    } catch (Exception as) {
    }


    checkBoxesJList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            if (evt.getValueIsAdjusting()) {
                return;
            }
            Object[] temp = checkBoxesJList.getSelectedValues();
            for (int i = 0; i < temp.length; i++) {
                // System.out.println(temp[i]);
                Connection conn = null;
                schm = temp[i].toString();
               // System.out.println(schm);
                try {

                    createDataset(schm);
                } catch (Exception ae) {
                }


            }
        }
    });

    checkBoxesJList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            int index = checkBoxesJList.locationToIndex(e.getPoint());
            CheckableItem item = (CheckableItem) checkBoxesJList.getModel().getElementAt(index);
            item.setSelected(!item.isSelected());
            Rectangle rect = checkBoxesJList.getCellBounds(index, index);
            checkBoxesJList.repaint(rect);
        }
    });



    final PieDataset dataset = createDataset(schm);
    final JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(900, 900));
    chartPanel.setBounds(550, 30, 700, 500);
    // setContentPane(chartPanel);
    jpAcc.add(chartPanel);
    jpAcc.add(checkBoxesJList);
    jpAcc.add(scrollPane);
    jpAcc.add(lScheme);


    getContentPane().add(jpAcc);
    setVisible(true);

}

private PieDataset createDataset(String schm) throws SQLException, ClassNotFoundException {


    // create the dataset...
    Connection conn = null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.80:1521:simba9i", "SYSTEM", "system123");
    final DefaultPieDataset dataset = new JDBCPieDataset(conn,
            "select distinct SCHM_CODE, sum(DEP_AMT) as AMOUNT from DLY_DEP_VIEW  where schm_code = '" + schm + "' group by schm_code");
    System.out.println(schm);
    try {
    } catch (Exception ad) {
        JOptionPane.showMessageDialog(this, ad,
                "Error", JOptionPane.ERROR_MESSAGE);
    }

    return dataset;

}

private CheckableItem[] createData(ArrayList<String> strs) {
    int n = strs.size();
    CheckableItem[] items = new CheckableItem[n];
    for (int i = 0; i < n; i++) {
        items[i] = new CheckableItem(strs.get(i));
    }
    return items;
}

private JFreeChart createChart(final PieDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createPieChart3D(
            "Deposit Base", dataset, true, true, false);



    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    final PiePlot3D plot = (PiePlot3D) chart.getPlot();     
    plot.setLabelGenerator(null);
    //plot.setLabelGenerator(new StandardPieSectionLabelGenerator(" {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()));

    plot.setForegroundAlpha(1f);
    plot.setNoDataMessage("No data to display");
    return chart;


}
}

class CheckableItem {

private String str;
private boolean isSelected;

public CheckableItem(String str) {
    this.str = str;
    isSelected = false;
}

public void setSelected(boolean b) {
    isSelected = b;
}

public boolean isSelected() {
    return isSelected;
}

@Override
public String toString() {
    return str;
}
}

class CheckListRenderer extends JCheckBox implements ListCellRenderer {

public CheckListRenderer() {
    setBackground(UIManager.getColor("List.textBackground"));
    setForeground(UIManager.getColor("List.textForeground"));
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean hasFocus) {
    setEnabled(list.isEnabled());
    setSelected(((CheckableItem) value).isSelected());
    setFont(list.getFont());
    setText(value.toString());
    return this;
}
}

我需要我的图表根据我单击的 JList 组件进行更改但我没有看到任何更改。我该怎么办。我是初学者。我将欣赏示例和示例的链接。

4

1 回答 1

2

您的代码不起作用,因为虽然里面的代码ListSelectionListener执行 createDataset()它什么都不做,但您第一次调用时创建的日期集createDataset()是未实际的/未更新的。

而不是DefaultPieDataset每次列表框更改时都创建一个新的数据集(第一次调用该方法时),然后在随后的更改中只调用JDBCPieDataset#executeQuery()您的新查询,这将执行新查询,然后调用fireDatasetChanged().

让你的数据集成为一个属性

私有 JDBCPieDataset 数据集;私人连接连接;

修改你的监听器)

  scheamaList.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
    try {
      dataset.executeQuery( (String) scheamaList.getSelectedItem());
    } catch (Exception ae) {
      ae.printStackTrace();
     }
  }
  });

以便刷新数据集:

在此处输入图像描述

于 2012-11-27T11:15:07.860 回答