简体   繁体   中英

c# Winform MSChart reverse Y Axis

I am using the MSChart control on a Windows form.

I am trying to have a descending Y axis by using AxisY.IsReversed = true , but still keep the X axis on the bottom. By default, when I used AxisY.IsReversed = true , then the X axis goes up to the top. I then tried the setting the AxisY.Crossing = Max to flip the axis to the maximum end of the Y axis (which is at the bottom) but it won't go below the X axis, it only goes as far as just above it.

Please help me!!!!!

The behavior you describe is the same behavior as shown in Microsoft demo project. I believe there is no tweak to make labels of X axis simply appear under the X axis, as there is nothing similar to Axis.LabelPosition property, that would specify, that labels should be under the axis.

Either when you specify chart1.ChartAreas[0].AxisX.IsMarksNextToAxis = false; , then labels appear on top of the chart, not on the bottom as desired.

The only tweak you can use, is not to set AxisY.IsReversed = true; , but make all your Y values *-1 (make them negative). Then use LabelStyle.Format to format negative numbers as positive.

chart1.ChartAreas[0].AxisY.IsReversed = false;               
chart1.ChartAreas[0].AxisY.LabelStyle.Format = "##.##;##.##;##.##";

There is another possibility if you cannot or do not want to change the data source.

If the y-axis is inverted, the x-axis is drawn at the top instead of at the bottom. This cannot be changed, but it is possible to set for the chart series that the secondary x-axis should be used.

series.XAxisType = AxisType.Secondary;

Now the secondary axis is always drawn on the opposite side of the primary axis, ie in this case at the bottom - and this is exactly what you want in this case.

Note that the secondary axis must be configured in the same way as the primary axis (eg set minimum or maximum)

//Primary
chartArea.AxisX.IsReversed = chartDefinition.XAxis.InvertAxis;
chartArea.AxisX.Minimum = minimum;
chartArea.AxisX.Maximum = maximum;

//Secondary
chartArea.AxisX2.Minimum = minimum;
chartArea.AxisX2.Maximum = maximum;

The same applies to the y-axis.

Worked pretty good for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM