c# - C#图表系列如何连接屏幕上缺失的点?
问题描述
我有一个 C# Windows Forms 应用程序,其中有一个屏幕设计用于显示Graph
使用DataVisualization.Charting.Chart
X 轴组成的类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;
}
解决方案
您从未排序的数据填充图表。您的 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
}
推荐阅读
- java - Android慢匹配器正则表达式
- html - 在 Rmarkdown html 页面中的 htmltools::tagList 元素之间添加新行空格
- javascript - HTML 浏览器数据库中的 count 函数返回的计数值不正确
- reactjs - React Redux,替换状态历史
- c++ - boost::gil 是否支持 10 位图像?
- angular - Angular:stopImmediatePropagation()在keydown上不起作用
- c++ - Stringstream编译错误只发生在linux环境下
- algorithm - 我的对称加密算法安全吗?
- javascript - 重新选择时,Kendo Scheduler 不会触发更改事件
- javascript - 计算通过 JS 检查并显示在页面上的复选框总数