C# - How do you make a chart object start at 0 on the X axis?

南笙酒味 提交于 2019-12-12 02:27:20

问题


I've been trying to set the position on the X axis from which the data starts getting plotted with no luck. It always start at 1 no matter what. I've tried doing

chartArea.AxisX.Minimum = -1;
chartArea.AxisX.Maximum = 5;

But it doesn't work. Even if I do:

chartArea.AxisX.Minimum = 3;
chartArea.AxisX.Maximum = 6;

The bars will be invisible rather than start at 3.

manaCurveChart.ChartAreas[0].AxisX.IsStartedFromZero = true;

Doesn't seem to do anything either.

EDIT: Here's the code

            ChartArea chartArea = manaCurveChart.ChartAreas[0];
            chartArea.AxisX.IsStartedFromZero = true;
            chartArea.AxisX.Minimum = -1;
            chartArea.AxisX.Maximum = 5;

            string[] Pets = new string[] { "Dog", "Cat" };
            int[] PointArray = new int[] { 1, 2 };

            manaCurveChart.Titles.Add("Pets");
            for (int i = 0; i < Pets.Length; i++)
            {
                Series series = manaCurveChart.Series.Add(Pets[i]);
                series.Points.Add(PointArray[i]);
            }

回答1:


Instead of doing

series.Points.Add(PointArray[i]); 

(where the x value is determined automatically), use

series.Points.AddXY(i, PointArray[i]);

so the x value is specified explicitly. It forces the x axis to have a minimum of 0, and you can then set your min/max/intervals as desired (or leave them to be determined automatically).



来源:https://stackoverflow.com/questions/23912573/c-sharp-how-do-you-make-a-chart-object-start-at-0-on-the-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!