我正在尝试为我的mschart图表创建一个缩小功能。我的想法是为图表提供一种单击方法,每次使用鼠标右键单击图表时,缩放值都应更改。因此,图表应能响应并具有更多值的空间。我已经完成了4个缩放步骤:240个值,3600个值,43'200个值,86'400。图表点击方法如下所示:
void chart_holder_Click(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (counter < 3)
{
counter++;
}
else
{
counter = 0;
}
switch (counter)
{
default:
//zoomsize -> 240s | 4min
zoomSize = 0;
graphSizing = 240;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 0:
//zoomsize -> 240s | 4min
zoomSize = 0;
graphSizing = 240;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 1:
//zoomsize -> 3'600s | 1h
zoomSize = 1;
graphSizing = 3600;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 2:
//zoomsize -> 43'200s | 12h
zoomSize = 2;
graphSizing = 43200;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
case 3:
//zoomsize -> 86'400s | 24h
zoomSize = 3;
graphSizing = 86400;
TempBuffer = new DataPoint[graphSizing];
file = new DataPoint[graphSizing];
TempbufferValuesTOZero();
FileValuesTOZero();
break;
}
}
}
使用属性public static int graphSizing { get; set; }
定义AxisX.Maximum。我得到的图表是实时图表,因此每秒钟都会添加新值,等等。TempbufferValuesTOZero,FileValuesTOZero用0/0填充整个数组。
在将值添加到图表之前,请使用以下方法准备它们: 我有一个switch语句,它检查zoomSize来知道如何准备数据
public void prepareData()
{
switch (zoomSize)
{
default:
prepData1();
break;
case 0:
prepData1();
break;
case 1:
prepData2();
break;
case 2:
prepData3();
break;
case 3:
prepData4();
break;
}
}
示例如何准备
public void prepData1()
{
graphSizing = 240;
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
for (double i = unixTimestamp; unixTimestamp - graphSizing < i; i--)
{
bool exists = true;
DateTime x = UnixTODateTime(i);
if (exists != (Array.Exists(file, element => element.XValue == i)))
{
TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(x.ToOADate(), 0) }).ToArray();
}
else
{
DataPoint point = Array.Find(file, element => element.XValue == i);
TempBuffer = TempBuffer.Skip(1).Concat(new DataPoint[] { new DataPoint(x.ToOADate(), point.YValues) }).ToArray();
}
}
}
此后,将调用此方法以在图表中显示值:
public void fileRead()
{
chart_holder.Series[0].Points.Clear();
foreach (DataPoint a in TempBuffer)
{
chart_holder.Series[0].Points.Add(a);
if (a.YValues[0] == 9999)
{
a.Color = Color.Red;
}
}
}
TempBuffer是一个DataPoint数组,我在其中存储值(public DataPoint[] TempBuffer = new DataPoint[graphSizing];
)。
但是,它并不是像预期的那样安静地工作。只有默认值/ 0的情况可以按计划正常运行。但是,只要我要缩小,它就无法工作了。在情况2中,AxisX值是完全错误的,而AxisY值甚至都没有显示。在情况3和情况4中,程序崩溃了。另外,CPU使用率也很高。
您知道为什么其他情况无法像default / 1一样解决吗? 以及如何提高性能并最大程度地减少高CPU使用率?
感谢帮助
问候C.User