C#图表系列如何连接屏幕上缺失的点?

时间:2018-10-08 12:58:10

标签: c# charts

我有一个C#Windows Forms应用程序,其中有一个屏幕设计为使用Graph类显示DataVisualization.Charting.Chart,其中X轴由DateTime组成,Y轴为由整数组成(目标是表示某些其他进程随时间变化的内存使用量,以MB为单位)。因此,我想以Continuous function的格式显示它。但是,当我将DataVisualization.Charting.Series对象类型设置为SeriesChartType.Line时,表单以一种非常奇怪的方式绘制图形,请参见下图:

enter image description here

,当我将对象系列类型设置为SeriesChartType.Point时,显示的图形为:

enter image description here

请注意,其中有很多空白,这没关系,因为在这些时间间隔之间没有任何内存使用注册表。我在这里抱怨的唯一问题是,在Line模式下,图形是以这种奇怪的方式绘制的。这些图的生成代码为:

private void CarregaSerieMemoria()
    {
        // this InvokeRequired is because it is called in a separeted Thread, the graph creation happens in the Else block
        if (this.InvokeRequired)
        {
            VoidVoidDelegate d = new VoidVoidDelegate(CarregaSerieMemoria);
            this.Invoke(d);
        }
        else
        {
            try {
                // Data table containing the Memory Usage history
                foreach (DataRow row in Dados.dsComponentes.Tables["MemoryHistory"].Rows)
                {
                    string proc = row["NomeProcesso"].ToString();
                    if (!string.IsNullOrEmpty(proc))
                    {
                        string dataStr = row["TimeStamp"].ToString();
                        string memoriaStr = row["Memoria"].ToString();
                        DateTime data;
                        int memoria;
                        try
                        {
                            data = DateTime.ParseExact(dataStr, "yyyyMMdd-HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
                            memoria = int.Parse(memoriaStr) / 1000;
                        }
                        catch (FormatException)
                        {
                            continue;
                        }
                        if (TemSerieProc(proc))
                        { // if there is already a Series object with proc name
                            Series s = this.chartMemory.Series.Where(x => x.Name.Equals(proc)).FirstOrDefault();
                            s.Points.AddXY(data, memoria);
                        }
                        else
                        {   // else creates a new Series object and add this current point (data,memoria)
                            Series s = DefineNovaSerie(proc);
                            s.XValueType = ChartValueType.DateTime;
                            s.Points.AddXY(data, memoria);
                            this.chartMemory.Series.Add(s);
                        }
                    }
                }

                chartMemory.ChartAreas[0].AxisX.LabelStyle.Format = "dd/MM/yyyy HH:mm:ss";
                chartMemory.ChartAreas[0].AxisX.Interval = 30;
                chartMemory.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
                chartMemory.ChartAreas[0].AxisX.IntervalOffset = 1;
                chartMemory.ChartAreas[0].AxisX.Minimum = graphDateBegin.ToOADate();
                chartMemory.ChartAreas[0].AxisX.Maximum = graphDateEnd.AddHours(24).ToOADate();

                chartMemory.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
                chartMemory.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
                chartMemory.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
                chartMemory.ChartAreas[0].AxisY.ScaleView.Zoomable = true;

                chartMemory.ChartAreas[0].AxisX.Title = "Horário";
                chartMemory.ChartAreas[0].AxisY.Title = "Memória (MegaBytes)";

                chartMemory.MouseWheel += chartMemory_MouseWheel;
                chartMemory.MouseClick += chartMemory_MouseClick;
                chartMemory.Visible = true;
                labelLoad.Visible = false;
                btnReload.Visible = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }

    private Series DefineNovaSerie(string proc)
    {

        Series temp = new Series(proc);

        temp.ChartType = SeriesChartType.Line;
        //temp.MarkerSize = 10;
        temp.Color = GetNextColor(nextColorInt++);

        return temp;
    }

1 个答案:

答案 0 :(得分:0)

您从未排序的数据中填充图表。您的x轴是日期,因此在将点添加到图表时应按日期对数据进行排序。 这是一个示例(未经测试),如何通过对TimeStamp字段中的数据表中的数据进行排序来解决此问题。

var dataTable = Dados.dsComponentes.Tables["MemoryHistory"];
var orderedDataView = new DataView(dataTable);
orderedDataView.Sort = "TimeStamp";
foreach (DataRow row in orderedDataView.ToTable().Rows)
{
    //rest of code
}