-1

I have a Chart. There is a ChartArea, I need to find Max and Min values on a Chart. How can I do it?

P.S. There is a Series and AxisX has time [not numbers, see a screenshot].

My problem is i can't get PointIndex [sure, I can do it, but when I don't click to DataPoint, but I click to just ChartArea that I can't get PointIndex, because is was -1

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
Bezgodov
  • 11
  • 1
  • 7

1 Answers1

0

Given a Series s you can find the minimum and maximum values like this:

double minX = s.Points.Select(v => v.XValue).Min();
double maxX = s.Points.Select(v => v.XValue).Max();
double minY = s.Points.Select(v => v.YValues[0]).Min();
double maxY = s.Points.Select(v => v.YValues[0]).Max();

Note that they do not directly map into the Points as each value may occur multiple times. So to find the first and last matching DataPoint we can use this:

// find datapoints from left..
DataPoint minXpt = s.Points.Select(p => p)
                    .Where(p => p.XValue == minX)
                    .DefaultIfEmpty(s.Points.First()).First();
DataPoint minYpt = s.Points.Select(p => p)
                    .Where(p => p.YValues[0] == minY)
                    .DefaultIfEmpty(s.Points.First()).First();
//..or from right
DataPoint maxXpt = s.Points.Select(p => p)
                    .Where(p => p.XValue == maxX)
                    .DefaultIfEmpty(s.Points.Last()).Last();
DataPoint maxYpt = s.Points.Select(p => p)
                    .Where(p => p.YValues[0] == maxY)
                    .DefaultIfEmpty(s.Points.Last()).Last();

Now after marking the found points, maybe like this:

Color c = Color.Green;

minXpt.MarkerColor = c;
minYpt.MarkerColor = c;
maxXpt.MarkerColor = c;
maxYpt.MarkerColor = c;

minXpt.MarkerSize = 12;
minYpt.MarkerSize = 12;
maxXpt.MarkerSize = 12;
maxYpt.MarkerSize = 12;

I get this result:

enter image description here

See here for an example how to restrict the search to a zoomed interval!

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • maybe you didn't understand what i wanted or i didn't ask as good as possible. `double minX = s.Points.Select(v => v.XValue).Min();` you wrote. But it founds only min of **FULL** series. And when i pick out my interval that my min value doesn't change. – Bezgodov Dec 11 '16 at 01:15
  • Actually i can say what i need. When i pick my interval then i get **first point X** and **second point X**, then i use `zoom(first, second)`. And i need one feature. When i pick out my interval, then i need to finds **max Y** and **min Y** on the interval and after i need to set `AxisY.Maximum = maxY`. This is my task, but i can't understand how i can do it. I hope you can understand my English :) – Bezgodov Dec 11 '16 at 01:23
  • Did you notice the link at the bottom of the answer? – TaW Dec 11 '16 at 03:21
  • Yeah, sure. I saw it, i even tried it. But id didn't work with my ChartArea. I'm sure it was my mistake :) Your code was working, i think. But i did my problem else. Thank you for answer. – Bezgodov Dec 12 '16 at 01:13