嗨朋友我有一个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组件进行更改I CLick但是我没有看到任何变化。我该怎么办。我是初学者。我将欣赏示例和示例的链接。
答案 0 :(得分:2)
您的代码无效,因为虽然ListSelectionListener
中的代码执行createDataset()
但它没有对它执行任何操作,但您在第一次调用createDataset()
时创建的日期集未被实现/未更新。< / p>
不是每次列表框更改时创建新的DefaultPieDataset
,而是创建数据集一次(第一次调用方法说),然后在随后的chages中使用新查询调用JDBCPieDataset#executeQuery()
,将执行新查询,然后调用fireDatasetChanged()
。
让数据集成为属性
私有JDBCPieDataset数据集; 私人连接conn;
修改你的听众)
scheamaList.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
try {
dataset.executeQuery( (String) scheamaList.getSelectedItem());
} catch (Exception ae) {
ae.printStackTrace();
}
}
});
以便刷新数据集: