C# Dundas Charts Auto-Scale Y Axis with no values over 100% - c#

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.

Related

Get the X axis value of the maximum point in ChartArea C#

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;

How do I force the chart X axis labels to intervals of 100? (C#, .NET)

I'm using a chart control in a C# .NET application (Visual Studio 2013). How do I force the X-axis grid lines and labels to multiples of 100? I've set every "Interval" property I can find to 100, but at runtime it always puts grid lines and labels at 198, 398, 598, etc. This is for a data set with 2048 points.
I'd prefer do to it in the designer, but I'll do it in code if I must.
I'm new to C#/.NET, so please let me know what crucial pieces of information I've omitted...
First set chart.ChartArea[0].AxisX.Minimum and chart.ChartArea[0].AxisX.Maximum.
And then chart.ChartArea[0].AxisX.Interval = 100; and also adjust the IntervalOffset.
However, if Minimum is > 0: the grid will offset from the Minimum value. Which means the correct offset will be axis.IntervalOffset = axis.Interval - axis.Minimum;
But, if Minimum is < 0:
axis.IntervalOffset = axis.Interval - axis.Minimum
will be evaluate to something like (with Minimum = -4 as example) axis.IntervalOffset = 100 - (-4) = 104 which is problematic because if IntervalOffset > Interval the interval will start at IntervalOffset and skip any gridlines between Minimum and IntervalOffset. (With above example gridline at 0 will be skipped)
In another words the correct offset is: IntervalOffset = (axis.Interval - axis.Minimum) % axis.Interval.
Which gives the correct offset:
axis.IntervalOffset = (-axis.Minimum) % axis.Interval;
For instance:
var axis = chart.ChartAreas[0].AxisX;
var points = chart.Series[0].Points;
axis.Minimum = points.Min(p => p.XValue);
axis.Maximum = points.Max(p => p.XValue);
axis.Interval = 100;
axis.IntervalOffset = (-axis.Minimum) % axis.Interval;
Will give you a grid that intersects X = 0.

How do I set the charts Y axis to be dynamic with maximum value set at 125?

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;

mschart y-axis min / max setting issue (possible glitch?) C#

I have a problem with setting the maximum and minimum values for the y-axis of my mschart.
When setting the max or min to a value, say 10.025, the value the chart sets as max is 10.024999618530273.
mainChart.ChartAreas[selectedChartArea].AxisY.Maximum = GetRoundYAxisMax(newYMax, newYRange);
mainChart.ChartAreas[selectedChartArea].AxisY.Minimum = GetRoundYAxisMin(newYMin, newYRange);
The GetRoundYAxisMax method just returns a "round" value. Code below.
private float GetRoundYAxisMax(double calculatedMax, double yAxisRange)
{
double rangeFactor = 0;
if (yAxisRange > 10)
rangeFactor = 1;
else if (yAxisRange > 1)
rangeFactor = 4;
else if (yAxisRange > 0.1)
rangeFactor = 40;
else if (yAxisRange > 0.01)
rangeFactor = 400;
else if (yAxisRange > 0.001)
rangeFactor = 4000;
else if (yAxisRange > 0.0001)
rangeFactor = 40000;
else
rangeFactor = 400000;
float returnValue = (float)(Math.Round(calculatedMax * rangeFactor, MidpointRounding.ToEven) / rangeFactor);
return returnValue;
}
The rounding code evaluates properly and returns a correctly rounded value, but when setting this value to the max or min value on the y-axis it sets a value very close to it, but not rounded.
Any ideas why this is happening?
You can set the axis label style to round the un-rounded values:
mainChart.ChartAreas[selectedChartArea].AxisY.LabelStyle.Format = "#.###";
Just answering why 10.024999618530273 rather than 10.025.
Nothing on a computer is infinite in precision. You have ( IIRC ) 80 bits to represent your number. Part is exponent part is mantissa. What is happening is that 10.025 is not a number that has perfect representation in that number of bits. You dont get the whole number line, you get integral marks along it. ( take a random 80 bit double. That is a point on the number line. Now, add the smallest increment to that 80 bit double that is possible. Another point on the line. Now, in the "real" ( pun intended ) world, there are an infinite number of real points. Any point on the line you try to represent on a real world number line will be represented as one of the two points that bound that point in the 80 bit world ).

Can't read maximum Y axis value on MS chart

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();

Categories

Resources