我使用Swing GUI为计算机商店设计了一个库存/销售信息系统(虚构)。其中一个功能是显示某些产品在一年内销售情况的基本条形图。
从名为dbsales
的MySQL数据库调用数据,并使用jComboBox(cboAnSales
)选择产品类别。
我有一个方法(public void graph()
),它具有生成图形的编码。
项目的数据显示在文本字段中,图表使用该信息进行显示。在每个类别的cboAnSales
if语句内调用该方法。
现在的问题是它在自己的图表上生成每个项目。例如,“电缆”类别中有7个项目,当我选择它时,它会生成7个图形(每个项目一个) 我需要在一张图表上显示所有7个项目。
我怎样才能做到这一点?
这是cboAnSales的电缆部分:
private void cboAnSalesActionPerformed(java.awt.event.ActionEvent evt) {
if (cboAnSales.getSelectedIndex() == 1)
{
try
{
String sql = "select * from dbsales where category ='" + "Cable" + "'";
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/salventri","root","password");
Statement stmt=con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
DefaultListModel model = new DefaultListModel();
while(rs.next())
{
String ID = rs.getString("pid");
txtpid.setText(ID);
String brand = rs.getString("pbrand");
txtBrand.setText(brand);
String name = rs.getString("pname");
txtName.setText(name);
String category = rs.getString("category");
txtCategory.setText(category);
String unitSold = rs.getString("usold");
txtUnitsSold.setText(unitSold);
graph();
}
}
catch (Exception e)
{
}
}
以下是public void graph()
方法中我的图表的编码:
String unitsSold = txtUnitsSold.getText();
String brand = txtBrand.getText();
String name = txtName.getText();
String cat = txtCategory.getText();
DefaultCategoryDataset ddataset = new DefaultCategoryDataset();
ddataset.setValue(new Double(unitsSold), cat, brand + " " + name);
JFreeChart chart = ChartFactory.createBarChart3D("Annual Sales Performance", cat, "Number of Units Sold", ddataset);
chart.getTitle().setPaint(Color.RED);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLUE);
ChartFrame frame2 = new ChartFrame("Annual Sales", chart);
frame2.setVisible(true);
frame2.setSize(450,350);
答案 0 :(得分:1)
归功于WillShackleford,我的问题的解决方案导致了public void graph()
中的以下代码:
public void graph()
{
String year = cboAnnYear.getSelectedItem().toString();
if(cboAnSales.getSelectedIndex() == 1)
{
try
{
//declaring the type of category in the column
String text = "Cable";
//Select statement getting the row count
String sql = "select count(category) from dbsales where category ='" + text + "'";
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/salventri","root","password");
PreparedStatement stmt = con.prepareStatement("select * from dbsales where category=?");
stmt.setString(1, text);
ResultSet rs = stmt.executeQuery();
DefaultCategoryDataset ddataset = new DefaultCategoryDataset();
while (rs.next())
{
ddataset.setValue(new Double(rs.getDouble("usold")),
rs.getString("pbrand") + " " + rs.getString("pname"),
rs.getString("syear"));
}
JFreeChart chart = ChartFactory.createBarChart3D("Annual Sales Performance", text, "Number of Units Sold", ddataset);
chart.getTitle().setPaint(Color.RED);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.BLUE);
ChartFrame frame2 = new ChartFrame("Annual Sales", chart);
frame2.setVisible(true);
frame2.setSize(900,700);
}