ZedGraph smoothly moves Y2Axis with a graphic line

Following my question “Custom ZedGraph Chart” I have a chart with every second data insert, now I have other questions:

  • How to smoothly move Y2Axis (DateTime type) using a chart line and always show only the last 30 minutes in a chart?

  • How to format Y2Axis tags "HH: mm" to get 10:05, 10:10, 10:15, ..., 10:30?

Thanks for the help!

UPD1 : Thanks kmp! I try your code - this is good, but with the question: when I started, I see this: enter image description here When after a few minutes I see this picture: enter image description here

I have a “squeeze” area of ​​the chart, but I want to always show the last 30 minutes statically and slowly move down the old dates without scaling or “packing” with the axis. I hope you understand me.

UPD2 : Another problem is that Y2Axis labels do not have fixed values. For example, now: enter image description here

And after a few seconds: enter image description here

+1
source share
1 answer

Starting from the easiest - formatting shortcuts can be done like this:

myPane.Y2Axis.Scale.Format = "HH:mm"; 

One way to do this (and it peels off a bit, but I'll let you decide) is to simply remove the points from the curve when they go beyond your threshold (in this case, more than 30 minutes). Thus, when the chart redraws the axis, it will be updated accordingly.

It seems to me that getting the min min value may be better than that, but if you cannot just maintain the queue when adding such points, then remove them when they go beyond your threshold:

 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; } 
+2
source

Source: https://habr.com/ru/post/913310/


All Articles