从最简单的开始 - 格式化标签可以这样完成:
myPane.Y2Axis.Scale.Format = "HH:mm";
你可以做到这一点的一种方法(感觉有点不稳定,但我会让你决定)是一旦超过你的阈值(在这种情况下超过 30 分钟),就简单地从曲线中删除这些点。这样,当图表重绘时,轴将相应更新。
我觉得采用 scale min 值可能是比这更好的方法,但是如果您无法像这样添加点时简单地维护一个队列,然后在它们超出您的阈值时将它们删除:
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;
}