在回答我的问题时“ZedGraph 自定义图表”我有每秒插入数据的图表,现在我有其他问题:
如何用图表线平滑地向下移动Y2轴(DateTime类型)并在图表中始终只显示最后30分钟?
如何格式化 Y2Axis 标签“HH:mm”以获得 10:05、10:10、10:15、...、10:30?
感谢帮助!
UPD1:
Thanks kmp! I try your code - its good, but with issue: when I started I see this:
When after few minutes I see this picture:
我对图表区域进行了“压缩”,但我希望始终静态显示最后 30 分钟的数据,并缓慢向下移动旧数据,而无需缩放或用轴“包装”图表。我希望你能理解我。
UPD2:
Yet another issue - labels of Y2Axis haven't fixed values. For example now:
And after few seconds:
从最简单的开始 - 格式化标签可以像这样完成:
myPane.Y2Axis.Scale.Format = "HH:mm";
您可以执行此操作的一种方法(感觉有点不稳定,但我会让您决定)是在超出阈值(在本例中超过 30 分钟)时简单地将点从曲线中删除。这样,当图表重新绘制时,轴将相应更新。
我觉得采用比例最小值可能是比这更好的方法,但如果失败了,您可以简单地在添加点时维护一个队列,如下所示,然后在它们超出阈值时将其删除:
private Queue<DateTime> axisTimes;
private static readonly Random rnd = new Random();
private void button1_Click(object sender, EventArgs e)
{
GraphPane myPane = zg1.GraphPane;
myPane.XAxis.IsVisible = false;
myPane.X2Axis.IsVisible = true;
myPane.X2Axis.MajorGrid.IsVisible = true;
myPane.X2Axis.Scale.Min = 0;
myPane.X2Axis.Scale.Max = 600;
myPane.YAxis.IsVisible = false;
myPane.Y2Axis.IsVisible = true;
myPane.Y2Axis.Scale.MajorUnit = DateUnit.Minute;
myPane.Y2Axis.Scale.MinorUnit = DateUnit.Second;
myPane.Y2Axis.Scale.Format = "HH:mm";
myPane.Y2Axis.Type = AxisType.DateAsOrdinal;
LineItem myCurve = myPane.AddCurve("Alpha",
new PointPairList(),
Color.Red,
SymbolType.None);
myCurve.Symbol.Fill = new Fill(Color.White);
myCurve.IsX2Axis = true;
myCurve.IsY2Axis = true;
myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);
zg1.IsShowPointValues = true;
axisTimes = new Queue<DateTime>();
var t = new System.Windows.Forms.Timer();
t.Interval = 1000;
t.Tick += ShowData;
Thread.Sleep(100);
t.Start();
}
private void ShowData(object sender, EventArgs e)
{
var t = (System.Windows.Forms.Timer) sender;
t.Enabled = false;
int x = rnd.Next(500, 600);
var y = new XDate(DateTime.Now);
var myCurve = zg1.GraphPane.CurveList[0];
if (axisTimes.Any())
{
// Remove any points that go beyond our time threshold
while ((((DateTime)y) - axisTimes.Peek()).TotalMinutes > 30)
{
myCurve.RemovePoint(0);
axisTimes.Dequeue();
if (!axisTimes.Any())
{
break;
}
}
}
// Add the new point and store the datetime that it was added in
// our own queue
axisTimes.Enqueue(y);
myCurve.AddPoint(x, y);
zg1.AxisChange();
zg1.Invalidate();
t.Enabled = true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)