无法在Fastline图表中绘制多条线

时间:2017-04-05 08:35:21

标签: c# charts

我正在尝试使用快速线图表(选择类型,因为我有很多指向情节)。我想在我刚刚得到的数据中加上平均值加上同一图表的平均值。应该很容易,但我一直收到错误"未处理的类型' System.Reflection.TargetInvocationException'发生在mscorlib.dll"。

下面是函数,如果我注释掉第二个名为[AverageTime"]的系列,我编译并使用一行。

请注意该函数是async(.net 4.5),我用它来等待Task.Delay(Int32.Parse(txtbx_timing_delay.Text));"。不确定这是否与它有关,因为我认为我有一个我正在等待的线程。

关于如何解决问题的任何想法?

private async void btn_timing_send_Click(object sender, EventArgs e)
    {
        int recursive_counter = 0, num = 0, num2 = 0;
        double total_time = 0, average_time = 0, min_time = 0, max_time = 0, ave_plot = 0;

        num2 = Int32.Parse(txtbx_iterations.Text); // number of required iterations

        /*
        * Just a word on timings - Windows is bad at doing timings in lower Microseconds and below so we need to do a few things to try and get
        * accurate timings. 
        * 
        * As the general rule, when testing comms with completion time in the range of several microseconds it's recommended to implement 
        * the loop running 10,000...100,000 iterations in order to increase the accuracy of the measurement 
        */

        string str1 = "";

        Stream stm = tcpclnt.GetStream();
        byte[] payload;
        string ID = txtbx_timing_send_data.Text;
        str1 = "No Connection";
        ID = ID.ToCharArray().Aggregate("", (result, c) => result += ((!string.IsNullOrEmpty(result) && (result.Length + 1) % 3 == 0) ? " " : "") + c.ToString());//add space every two chars to make hex
        payload = ID.Split().Select(s => Convert.ToByte(s, 16)).ToArray(); //split the bytes into the array

        if (ckbx_plot.Checked)
        {
            chrt_timings.Series["ResponseTimes"].ChartType = SeriesChartType.FastLine; //set type
            chrt_timings.Series["ResponseTimes"].Color = Color.Blue; //set colour

            chrt_timings.Series["AverageTime"].ChartType = SeriesChartType.FastLine; //set type
       //  chrt_timings.Series["AverageTime"].Color = Color.Red; //set colour
       //  chrt_timings.Series["AverageTime"].BorderDashStyle = ChartDashStyle.Dash;

            chrt_timings.Legends.Clear(); // We do not need a legend
            chrt_timings.ChartAreas[0].AxisX.IsMarginVisible = false;
        }

        do
        {
            try
            {
                Stopwatch timer = Stopwatch.StartNew();
                long frequency = Stopwatch.Frequency;
                long nanosecPerTick = (1000L * 1000L * 1000L) / frequency;

                long startTick = timer.ElapsedTicks; //start of timed section
                stm.Write(payload, 0, payload.Length);


                byte[] input = new byte[tcpclnt.ReceiveBufferSize];
                int k = stm.Read(input, 0, tcpclnt.ReceiveBufferSize);
                Array.Resize(ref input, k); //crop the array to the amount of items it read in
                str1 = string.Join(" ", input.Select(b => string.Format("{0:X2} ", b))); //format as hex bytes

                long stopTick = timer.ElapsedTicks; //end of timed section
                var timestamp = Convert.ToDouble((stopTick - startTick) * nanosecPerTick) / 1000000;

                timer.Reset();

                rchtxbox_timings.SelectionColor = Color.LimeGreen;
                rchtxbox_timings.AppendText(str1 + "\r");
                rchtxbox_timings.SelectionColor = Color.Yellow;
                rchtxbox_timings.AppendText(timestamp + "ms\r\r");
                rchtxbox_timings.ScrollToCaret();

                if (num == 0) min_time = timestamp;

                if (num2 > 1)
                {
                    total_time = total_time + timestamp;

                    if (max_time < timestamp) max_time = timestamp;
                    if (min_time > timestamp) min_time = timestamp;
                }



                    if (chkBx_LogData.Checked)
                {
                    using (StreamWriter sw = new StreamWriter(timing_filename, true))
                    {
                        str1 = str1.Replace(" ", ""); //take out the spaces
                        sw.WriteLine(str1 + "," + timestamp.ToString() + "\r");
                    }
                }

                //Plot graph if required
                if (ckbx_plot.Checked)
                {
                    ave_plot = timestamp / num;

                    if (ckbx_restrict_graph.Checked)
                    {
                        if (chrt_timings.Series["ResponseTimes"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["ResponseTimes"].Points.RemoveAt(0);
                        chrt_timings.Series["ResponseTimes"].Points.Add(timestamp);
                        chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString();

                        //   if (chrt_timings.Series["AverageTime"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["Average Time"].Points.RemoveAt(0);
                        //  chrt_timings.Series["AverageTime"].Points.Add(ave_plot);

                        chrt_timings.ResetAutoValues();
                    }
                    else
                    {
                        recursive_counter++;
                        chrt_timings.Series["ResponseTimes"].Points.AddXY(recursive_counter, timestamp);
                        chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString();

                       // chrt_timings.Series["AverageTime"].Points.AddXY(ave_plot, timestamp);
                    }
                }

                num = num + 1;
                timestamp = 0;
                await Task.Delay(Int32.Parse(txtbx_timing_delay.Text));

            }
            catch (Exception d)
            {
                rchTxtBx_output.AppendText("red..... " + d.StackTrace);
            }
        } while (num2 > num);

        if (num2 > 1)
        {
            //write out min, max and ave times
            average_time = total_time / num;
            rchtxbox_timings.SelectionColor = Color.LightBlue;
            rchtxbox_timings.AppendText("\rMinimum Time = " + min_time + "\r");
            rchtxbox_timings.SelectionColor = Color.LightBlue;
            rchtxbox_timings.AppendText("Maximum Time = " + max_time + "\r");
            rchtxbox_timings.SelectionColor = Color.LightBlue;
            rchtxbox_timings.AppendText("Average Time = " + average_time + "\r\r");
            rchtxbox_timings.ScrollToCaret();
        }
    }

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,经过多次努力,我解决了这个问题。我错过了一个小点,那就是我没有第二个系列,系列[&#34; AverageTime&#34;],被宣布为图表画布的一员。

要解决这个问题,我去了属性,然后点击集合并添加成员,AverageTime。一旦完成,我可以将它的点绘制到图表画布。

所以现在我初始化下面的行

if (ckbx_plot.Checked)
    {
        chrt_timings.Series["ResponseTimes"].ChartType = SeriesChartType.FastLine; //set type
        chrt_timings.Series["ResponseTimes"].Color = Color.Blue; //set colour

        chrt_timings.Series["AverageTime"].ChartType = SeriesChartType.FastLine; //set type
        chrt_timings.Series["AverageTime"].Color = Color.Red; //set colour
        chrt_timings.Series["AverageTime"].BorderDashStyle = ChartDashStyle.Dash;

        chrt_timings.Legends.Clear(); // We do not need a legend
        chrt_timings.ChartAreas[0].AxisX.IsMarginVisible = false;
    }

现在我添加了数据

 if (ckbx_restrict_graph.Checked)
                { //shows just set number of points, add a new point remove an old point
                    if (chrt_timings.Series["ResponseTimes"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["ResponseTimes"].Points.RemoveAt(0);
                    chrt_timings.Series["ResponseTimes"].Points.Add(timestamp);
                    chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString();

                       if (chrt_timings.Series["AverageTime"].Points.Count() >= Convert.ToInt16(txtbx_axispoints.Text)) chrt_timings.Series["Average Time"].Points.RemoveAt(0);
                      chrt_timings.Series["AverageTime"].Points.Add(ave_plot);

                    chrt_timings.ResetAutoValues();
                }
                else
                { //squash all points onto the same canvas
                    recursive_counter++;
                    chrt_timings.Series["ResponseTimes"].Points.AddXY(recursive_counter, timestamp);
                    chrt_timings.Series["ResponseTimes"].ToolTip = timestamp.ToString();

                    chrt_timings.Series["AverageTime"].Points.AddXY(ave_plot, timestamp);
                }