c# chart change max and min values on the x axis - c#

Can someone tell me how to change the min and max values on the x axis on a C# chart? I want to go from 0 to 13, but it goes from -1 to 14 right now.

If you are using System.Windows.Forms.DataVizualisation.Charting you can set the Axis range by manipulating the chart's ChartAreas property. So something like
myChart.ChartAreas[0].AxisX.Maximum = 13;
myChart.ChartAreas[0].AxisX.Minimum = 0;

This will work better :
myChart.ChartAreas[0].AxisX.IsMarginVisible = false;

You might try myChart.ChartAreas[0].AxisX.RoundAxisValues(). It is on by default for the Y axis and I find it generally works better for the X Axis as well.

New Chart().SetYAxis("GPA",0,4);
If You using The chart Class
you could set the Y or X axis using the SetYAxis Method

chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 10;
chart1.ChartAreas[0].AxisY.Minimum = -5;
chart1.ChartAreas[0].AxisY.Maximum = 5;

Related

WinForms Chart: Set minimum Y Axis display range

I have a Winforms chart in which I have temperature readings arriving and displaying every second. I like the way the chart works automatically handling the display of the values, but I want to change one simple thing.
I want to increase the minimum displayed y axis range, so it displays a range of 20. At the moment it only displays around 5. I have tried a few things:
//(when new data arrives...)
//Does not work, I think because by default, Size is always NaN?
if (chart1.ChartAreas[0].AxisY.ScaleView.Size < 20)
{
chart1.ChartAreas[0].AxisY.ScaleView.Size = 20;
}
None of these work either:
chart1.ChartAreas[0].AxisY.ScaleView.SmallScrollMinSize = 20;
chart1.ChartAreas[0].AxisY.ScaleView.SmallScrollSize = 20;
chart1.ChartAreas[0].AxisY.ScaleView.MinSize = 20;
chart1.ChartAreas[0].AxisY.Minimum //doesn't seem to have any effect
chart1.ChartAreas[0].AxisY.Maximum //doesn't seem to have any effect
I'm sure I've missed something simple. I hope I have anyway.
The 'minimum display range' is not something built-in in the MSChart control.
But you can easily fake it:
Add a dummy Series which contains only two points to make sure the display range will not go below the range of their y-values..:
int rangeMin = -10;
int rangeMax = 20;
sDummy = chart.Series.Add("dummy");
sDummy.Color = Color.Transparent;
sDummy.IsVisibleInLegend = false;
sDummy.ChartType = SeriesChartType.Point;
sDummy.Points.AddXY(0, rangeMin + 1);
sDummy.Points.AddXY(0, rangeMax - 1);
Style your y-axis as you like:
Axis ay = chart.ChartAreas[0].AxisY;
ay.MajorGrid.Interval = 5;
And add one or more data Series:
sData = chart.Series.Add("data");
sData.LegendText = "Temperature";
sData.ChartType = SeriesChartType.Line;
Now as you add data points with a larger range of values the y-axis will grow its display range to accommodate them. And if you remove the larger points it will shrink back, but not below the range needed for the dummy series..:
Note that since the Chart automatically adds some slack I reduce the range on both sides by 1; with other Intervals etc other numbers are needed..
The code to remove the larger values, btw:
var toRemove = sData.Points.Cast<DataPoint>()
.Where(x => x.YValues[0] >= rangeMax).ToList();
foreach (var dp in toRemove) sData.Points.Remove(dp);

C# Windows form Chart fixed axis?

I am very new in all this of programming and I am beginning with C#
now I have an application which should show some points in a chart, but i would like to fix the x and y axis in a way that the graph area always has as minimum 50 and maximum 50, but the graphic area doesn't auto-fit the graph, I don't know if I explain it well, I mean I want the points for x and y axis always display from 0 to 50. does some one know how to set this?
you can easily set limits like this:
chart1.ChartAreas[0].AxisX.Maximum = 50;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 50;
chart1.ChartAreas[0].AxisY.Minimum = 0;

How to displace the origin of the Y axis on a polar Mschart?

Im working on a polar chart with mschart, and I need to make this "hole" on the middle of the chart. I mean, i need the "0" of the Y axis not to be on the center. I saw this plot somewhere on stackoverflow. It´s exactly what i need, how can I do that?
In order to create a radar plot that look like the provided example
// disable grid a long X
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chart1.ChartAreas[0].AxisX.MajorTickMark.Enabled = false;
// set Y axis
chart1.ChartAreas[0].AxisY.Minimum = -20; // creates the 'hole' by setting min Y
chart1.ChartAreas[0].AxisY.MajorGrid.IntervalOffset = 20; // so the major grid does not fill the 'hole'
chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 5;
chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

How to disable Chart axis autosize

I need to stop autosizing (or autoscaling) of Y Axis in c# chart and set it manually.
Any advice? Thanks
You can set the minimum and maximum of the Y axis like so:
chart.ChartAreas[0].AxisY.Minimum = 0;
chart.ChartAreas[0].AxisY.Maximum = 10;

Formatting main chart's axis in C#

I'm trying to make chart in C# which should be readable easily. I managed to draw sin() graph, but it's not really readable, as X and Y axis don't stand out, I feel like it's the middle of nowhere. I tried to set line width using following code:
area.AxisX.LineWidth = 3;
But it only made line in the bottom of chart fatter, not main axis (Y = 0) like I need.
Anyone know how I would accomplish that? Is Y axis (X = 0) same? If no, could you please specify how to bold it too?
I think what you are looking for is the MajorGridLines:
area.AxisY.MajorGrid.LineWidth = 3
Or maybe this:
area.AxisX.LineWidth = 3;
area.AxisX.Crossing = 0;
area.AxisY.LineWidth = 3;
area.AxisY.Crossing = 0;
Crossing will put the axis at the value you set it to.

Categories