在图表中绘制几行

时间:2017-06-28 21:04:42

标签: c# mschart

我有4个单选按钮。它们中的每一个都对应于我想要使用的输入温度的一种类型(DB中的列)。

我有什么: 如果我选择一个单选按钮+按下加载 - 它会绘制图形。如果我再次选择此按钮(或任何其他Radiobuttons),则会在原始图形的序列中进行绘制。

我需要什么帮助: 我希望每次按下按钮" Load"该线将被添加"到现有的图表。换句话说,我可能在同一个图表中有4个不同的行,每个行代表我选择并按下的单选按钮"加载。"

代码:

private void BtnLoadDataToGraph_Click(object sender, EventArgs e)
{
    string column_to_use = "";
    double column_percentage_XX = 0;
    double column_percentage_XX = 0;
    string ReceiveNameFile = CboxReceiveNameFile.Text;


    if (RadioButtonStartXXTemp.Checked)
        column_to_use = "START_XX_TEMP";
    else if (RadioButtonStartXXTemp.Checked)
        column_to_use = "START_XX_TEMP";
    if (RadioButtonAvgTemp.Checked)
        column_to_use = "(START_XX_TEMP + START_XX_TEMP)/2";
    else
    {
        column_percentage_XX = Int32.Parse(TextBoxXXPercentage.Text);
        column_percentage_XX = (Convert.ToDouble(column_percentage_XX) / 100);

        column_percentage_XX = Int32.Parse(TextBoxXXPercentage.Text);
        column_percentage_XX = (Convert.ToDouble(column_percentage_XX) / 100);

        column_to_use = "(START_XX_TEMP*" + column_percentage_XX + ")+(START_XX_TEMP*" + column_percentage_XX + ")";
    }

    SqlConnection conDatabase = new SqlConnection("XXXXXX");
    SqlCommand cmdDatabase = new SqlCommand("select " + column_to_use + " AS temp,  SUBSTRING (header.TIME,CHARINDEX(' ',header.TIME,1),len(header.TIME)) as time,CONVERT(datetime,header.TIME,101) as new_time, REVERSE(SUBSTRING(REVERSE(fnames.PATH_NAME),0,CHARINDEX('\\\',REVERSE(fnames.PATH_NAME)))) as folder_name from TBL_DATA_TYPE_RO_HEADER header,TBL_FILE_NAMES fnames,TBL_PROGRAM program where program.PK_ID_TBL_PROGRAM = fnames.FK_ID_TBL_PROGRAM and fnames.PK_ID_TBL_FILE_NAMES = header.FK_ID_TBL_FILE_NAMES and REVERSE(SUBSTRING(REVERSE(fnames.PATH_NAME),0,CHARINDEX('\\\',REVERSE(fnames.PATH_NAME))))='" + ReceiveNameFile + "'  order by new_time", conDatabase);

    SqlDataReader myReader;

    try
    {
        conDatabase.Open();
        myReader = cmdDatabase.ExecuteReader();

        while (myReader.Read())
        {
            this.ChartTempVsTime.Series["TimeVsTemp"].Points.AddXY(myReader["time"].ToString(), myReader["temp"].ToString());
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我怎么能这样做的想法?我在考虑使用" Points.Aggregate"而不是" Points.AddXY"。但我不认为这是正确的道路,特别是因为每个单选按钮应该有不同的颜色线。

欢迎任何帮助或提示!

2 个答案:

答案 0 :(得分:0)

this.ChartTempVsTime.Series["TimeVsTemp"].Points.AddXY(myReader["time"].ToString(), myReader["temp"].ToString());

此行始终在同一系列中绘制数据。每次按下加载按钮时都需要创建一个新系列。类似的东西:

private void BtnLoadDataToGraph_Click(object sender, EventArgs e)
{
    try
    {
        string column_to_use = "";
        double column_percentage_XX = 0;
        double column_percentage_XX = 0;
        string ReceiveNameFile = CboxReceiveNameFile.Text;


        if (RadioButtonStartXXTemp.Checked)
            column_to_use = "START_XX_TEMP";
        else if (RadioButtonStartXXTemp.Checked)
            column_to_use = "START_XX_TEMP";
        if (RadioButtonAvgTemp.Checked)
            column_to_use = "(START_XX_TEMP + START_XX_TEMP)/2";
        else
        {
            column_percentage_XX = Int32.Parse(TextBoxXXPercentage.Text);
            column_percentage_XX = (Convert.ToDouble(column_percentage_XX) / 100);

            column_percentage_XX = Int32.Parse(TextBoxXXPercentage.Text);
            column_percentage_XX = (Convert.ToDouble(column_percentage_XX) / 100);

            column_to_use = "(START_XX_TEMP*" + column_percentage_XX + ")+(START_XX_TEMP*" + column_percentage_XX + ")";
        }

        SqlConnection conDatabase = new SqlConnection("XXXXXX");
        SqlCommand cmdDatabase = new SqlCommand("select " + column_to_use + " AS temp,  SUBSTRING (header.TIME,CHARINDEX(' ',header.TIME,1),len(header.TIME)) as time,CONVERT(datetime,header.TIME,101) as new_time, REVERSE(SUBSTRING(REVERSE(fnames.PATH_NAME),0,CHARINDEX('\\\',REVERSE(fnames.PATH_NAME)))) as folder_name from TBL_DATA_TYPE_RO_HEADER header,TBL_FILE_NAMES fnames,TBL_PROGRAM program where program.PK_ID_TBL_PROGRAM = fnames.FK_ID_TBL_PROGRAM and fnames.PK_ID_TBL_FILE_NAMES = header.FK_ID_TBL_FILE_NAMES and REVERSE(SUBSTRING(REVERSE(fnames.PATH_NAME),0,CHARINDEX('\\\',REVERSE(fnames.PATH_NAME))))='" + ReceiveNameFile + "'  order by new_time", conDatabase);

        SqlDataReader myReader;

        conDatabase.Open();
        myReader = cmdDatabase.ExecuteReader();

        Series s = new Series();

        while (myReader.Read())
        {
            s.Points.AddXY(myReader["time"].ToString(), myReader["temp"].ToString());
        }

        chart1.Series.Add(s);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

答案 1 :(得分:0)

只需在图表中创建一个新系列并将新点添加到其中