winforms 从列表框中绘制多个图表

lawou6xi  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(145)

我做了一个代码如下。我希望有尽可能多的图表被选中的列表框。现在他们总是绘制所有他们。例如,我想选择listBox1是一个图形,例如listBox1,listBox2,listBox3是3个图形。
怎么改,加什么?

private void Form1_Load(object sender, EventArgs e)
{
   listBox1.Items.Add("value1");
   listBox1.Items.Add("value2");
   listBox1.Items.Add("value3");
   listBox1.SelectionMode = SelectionMode.MultiSimple;
}
private void button1_Click_1(object sender, EventArgs e)
{
   connect.Open();
   string mysql = "SELECT value1, value2, value3, valuedate FROM mydata";
   MySqlDataAdapter da = new MySqlDataAdapter(mysql, connect);
  
   DataSet ds = new DataSet();
   chart1.DataSource = ds.Tables[0];
  
   chart1.Series["value1"].XValueMember = "valuedate";
   chart1.Series["value1"].YValueMembers = "value1";
  
   chart1.Series["value2"].XValueMember = "valuedate";
   chart1.Series["value2"].YValueMembers = "value2";
  
   chart1.Series["value3"].XValueMember = "valuedate";
   chart1.Series["value3"].YValueMembers = "value3";
}
sqougxex

sqougxex1#

您应该根据列表视图中选定的项修改查询,然后以类似的方式添加系列。
下面的代码将给予您如何处理它。

string query = "SELECT valuedate ";

            for (int i = 0; i < listView1.SelectedItems.Count; i++)
            {
                string value = listView1.SelectedItems[i].Text;
                query += ", " + value;
                chart1.Series.Add(value);
                chart1.Series[value].XValueMember = "valuedate";
                chart1.Series[value].YValueMembers = value;
            }

            query += " FROM mydata";

相关问题