首页 > 解决方案 > C#图表系列如何连接屏幕上缺失的点?

问题描述

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

在此处输入图像描述

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

在此处输入图像描述

请注意,有很多点是空白的,这没关系,因为在这些时间间隔之间没有任何内存使用注册表。我在这里抱怨的唯一问题是,在该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;
    }

标签: c#charts

解决方案


您从未排序的数据填充图表。您的 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
}

推荐阅读