I'd like to get the x axis value of the maximum point from the chart bellow.
You can see that the value I get is around 67, even though it should be around 30. I thought it happened because of I also limit the chart maximum X axis into a certain value with the chart3.ChartAreas[0].AxisX.Maximum code, then I tried the following code to get the maximum value var dataPoint = chart3.Series[0].Points.FindMaxByValue();. So maybe this code is searching the maximum value from the whole series, not only from the visible chart area.
My question is, is there any idea to get the maximum value from the visible chart area only?? Or maybe it's better to erase the unwanted chart series, but I don't know how to do that.
Thanks
You could try checking out this stackoverflow answer.
The below code will search all DataPoint in the Series.
var xAxisValue = chart3.Series[0].Points.FindMaxByValue();
So, from the link above you can get the value you require by doing something like this:
var scaleView = chart3.ChartAreas[0].AxisX.ScaleView;
var min = scaleView.ViewMinimum;
var max = scaleView.ViewMaximum;
var yourAnswerMaybe = chart3.Series[0].Points
.Where(p => p.XValue > min && p.XValue < max)
.OrderBy(p => p.XValue)
.Last()
.XValue;
Related
I draw my plot using Oxyplot and i don't have any problem about drawing the plot.
My Y axis is a 'LinearAxis' and X axis is a 'TimeSpanAxis'.
What i want to do is get Y value from given X value.
For example i want to get Y value from TimeSpan(0,0,0,1).
I can't use mouse position or any other events. X value will be given by user as a timespan.
This should be easy but I couldn't find anything.
I am not quite sure this is the most efficient way to do this, but here is one suggestion on how to find Y value from X. Please note this is not 100% tested code, but you would get the Gist of the idea from it.
The idea is to calculate the Slope based on the "defined point" before and after the point to search. Then, you can use the slope to find the missing cordinate of point in question.
Slope = (y2-y2)/(x2-x1)
Given you have one axis as LinearAxis and other as TimeSpanAxis, and you are give the TimeSpanAxis for which you need to find Y.
var timeSpanToSearch = new TimeSpan(8, 30, 0);
var timeSpanToSearchAsDouble = TimeSpanAxis.ToDouble(timeSpanToSearch);
var series = MyModel.Series.First() as LineSeries;
double result = 0;
// If the point is a defined point, you can find Y easily
if (series.Points.Any(x => x.X.Equals(timeSpanToSearchAsDouble)))
{
result = series.Points.First(x => x.X.Equals(timeSpanToSearchAsDouble)).Y;
}
else
{
var floorValue = series.Points.Where(x => x.X < timeSpanToSearchAsDouble).Last();
var ceilingValue = series.Points.Where(x => x.X > timeSpanToSearchAsDouble).First();
var slope = (ceilingValue.Y - floorValue.Y) / (ceilingValue.X - floorValue.X);
result = slope * (timeSpanToSearchAsDouble - floorValue.X) + floorValue.Y;
}
For example I have chart with 2 points - 0,0 and 10,10 and chart type is FastLine.
I want to know what Y value will be in chart in choosen X value.
For example when X is 5 I want to know that Y is 5.
My charts more complicated and have tons of points, I need to get Y value through X.
How can I do this?
The problem boils down to two tasks:
Finding the neighbouring points for an x-value
Interpolating their y-values for the given x-value.
If the x-values are indeed steadily increasing this should solve both:
double interpolatedY(Series s, double xval)
{
DataPoint pPrev = s.Points.Last(x => x.XValue <= xval);
DataPoint pNext = s.Points.First(x => x.XValue >= xval);
if (pPrev == pNext) return pPrev.YValues[0];
return pPrev.YValues[0] + (pNext.YValues[0] - pPrev.YValues[0])
* (xval - pPrev.XValue)/ (pNext.XValue - pPrev.XValue);
}
It uses Linq to find the previous and next datapoint and then uses simple math to find the interpolated value.
Note that most checks are omitted!
Here I have added an identical point series and a third one to add the interpolated values:
To convert between chart pixels and values there are Axis functions ValueToPixelPosition and PixelPositionToValue, btw.
I am using Dundas Charts in Visual Studio using C#.
My charts are displaying percentage values, if I do not use a maximum y axis value the chart will automatically scale the axis - however, sometimes it will display values over 100% (even though none of the values actually exceed 100%).
How can I allow the axis to change in size depending on chart values, but never allow it to exceed 100%?
For anyonewho is interested, I decided to programmatically set the maximum value for the Y Axis for each chart.
The following code is used to get the max value from the given array and return 100 or the max value rounded up to the next multiple of 10:
int GetYAxisMaxValue(decimal[] yValues)
{
var max = yValues.Max();
var output = 0m;
if (yValues.Max() % 10 == 0) output = max;
else output = max + (10 - max % 10);
if (output > 100) return 100;
else return Convert.ToInt32(output);
}
This is then used to create the chart, setting:
chart.ChartAreas["Default"].AxisY.Maximum = GetYAxisMaxValue(...);
Hope this helps.
I have a charts populated in asp.net C#. I have multiple charts and each can range from 0 - 125.
which look like this if I do not set the maximum which one of it, the chart maximum will go to 140 if my series range max is 125.:
Thus I would like to set the maximum of Y axis to 125 however not staticly setting it for all charts. which mean I would like to make it dynamic still like the second chart image with the maximum set as 125.
Please advice thanks.
// Maximum value in Series, you may have to change YValues[0] to the appropriate
// index for your type of chart (so that it measures the top of your markers)
double maxValue = chart.Series[0].Points.Max(x => x.YValues[0])
// yAxisMax calculated as max value in series + 10 then rounded up to nearest 10
// but capped at 125 below. You could change this to perhaps
// maxValue * 1.1 to always get a 10 % extra above, anything goes
double yAxisMax = Math.Ceiling((maxValue + 10) / 10) * 10;
if (yAxisMax > 125)
yAxisMax = 125;
chart.ChartAreas[0].AxisY.Minimum = 0;
chart.ChartAreas[0].AxisY.Maximum = yAxisMax;
chart.ChartAreas[0].AxisY.Interval = 5;
I'm using MS Chart generated image on a MVC3 view.
The chart works but the maximum value is so high on the top of the chart that I can't read the values.
Shouldn't the chart have a margin from the max value?
I don't really know if this is a real problem but I can't make this look nice unless I define a AxisYMaximum value that I think should not be used on dynamic values.
Yes, the chart control should calculate the margin necessary to clearly display the data, but in my experience, it doesn't.
Since your y-values are dynamic, you can dynamically set the AxisYMaximum to a value just above the greatest displayed y-value. Something like this could set it:
double greatestYValue = double.MinValue;
foreach (var pt in Chart1.Series[0].Points)
{
if (greatestYValue < pt.YValues[0]) greatestYValue = pt.YValues[0];
}
Chart1.ChartAreas[0].AxisY.Maximum = greatestYValue * 1.2;
// or
Chart1.ChartAreas[0].AxisY.Maximum = greatestYValue + 20;
I just looped through all the points in the first series to find the greatest y-value, then set the y-axis maximum to 120% of that value, or some absolute amount above that value, or whatever you need.
You could also get the greatest y-value in a one-liner using LINQ:
double greatestYValue = Chart1.Series[0].Points.Select(p => p.YValues[0]).Max();