C# 如何在图表控件中设置 X 值的间隔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8882039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Interval of X values in Chart control
提问by Chris
I have a line chart with some values as shown on the picture. I want X values to be 1 2 3 etc, but now I have data in series and on x i have 0,77 1,77 2,77 3,77. I set
我有一个带有一些值的折线图,如图所示。我希望 X 值是 1 2 3 等,但现在我有系列数据,在 xi 上有 0,77 1,77 2,77 3,77。我设置
IsStartedFromZero = true;
Interval = 1;
Maximum = 4;
Maximum = 4;
in chartarea properties
在图表区域属性中
How to force X values to be 1 2 3 4?
如何强制 X 值为 1 2 3 4?
CODE:
代码:
Series s = new Series();
s.Color = Color.Red;
s.ChartType = SeriesChartType.Line;
s.BorderWidth = 3;
s.Points.Add(new DataPoint(1.2, 0));
s.Points.Add(new DataPoint(1.2,50));
s.Points.Add(new DataPoint(2, 80));
s.Points.Add(new DataPoint(3.2, 100));
Series s1 = new Series();
s1.Color = Color.Blue;
s1.ChartType = SeriesChartType.Line;
s1.BorderWidth = 2;
s1.Points.Add(new DataPoint(0.8,3.2));
s1.Points.Add(new DataPoint(0.83,6.5));
s1.Points.Add(new DataPoint(0.9,12.9));
s1.Points.Add(new DataPoint(1,25.8));
s1.Points.Add(new DataPoint(1.1,29));
s1.Points.Add(new DataPoint(1.2,54.8));
s1.Points.Add(new DataPoint(1.4,58.1));
s1.Points.Add(new DataPoint(1.5,61.3));
s1.Points.Add(new DataPoint(1.6,67.7));
s1.Points.Add(new DataPoint(2,90.3));
s1.Points.Add(new DataPoint(2.5,100));
chart1.Series.Add(s);
chart1.Series.Add(s1);
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.White;
chart1.ChartAreas[0].AxisX.Maximum = 4;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IsStartedFromZero = true;
chart1.ChartAreas[0].AxisX.IntervalOffsetType = DateTimeIntervalType.Number;


回答by Brissles
I would think that the default behaviour is to the set the first X label to the lowest value contained within your data series. In your case, it seems the lowest value of your blue series is ~0.8, which is below 1.
我认为默认行为是将第一个 X 标签设置为数据系列中包含的最低值。在您的情况下,您的蓝色系列的最低值似乎是 ~0.8,低于 1。
Given that you're specifying an Intervalof 1, and a Maximumof 4 It makes sense that the the X labels would be roughly 0.77, 1.77, 2.77, 3.77.
鉴于您指定的Interval是 1 和Maximum4,X 标签大致为 0.77、1.77、2.77、3.77 是有道理的。
If you force the X labels to be 1,2,3,4 explicitly after the chart has been bound then your labels won't correspond to your data correctly, and if you align your data to begin at 1.0 then you'll be cropping some of your series data out of the chart.
如果在绑定图表后明确强制 X 标签为 1,2,3,4,那么您的标签将无法正确对应您的数据,如果您将数据对齐以从 1.0 开始,那么您将进行裁剪您的一些系列数据超出了图表。
Depends on what you want to achive, I'd just stick with the default values the chart spits out.
取决于您想要达到的目标,我只会坚持使用图表吐出的默认值。
回答by Chris
The answer is to set:
答案是设置:
chart1.ChartAreas[0].AxisX.Minimum = 0;
And that's all!
就这样!

