Skip to main content
 首页 » 编程设计

C# Winform 图表,x 轴显示日期和时间的缩放和刻度

2024年02月13日15JeffreyZhao

我有一个带有图表的winform,我已经在我的代码中设置了:

//Enable range selection and zooming end user interface 
        this.chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true; 
        this.chart1.ChartAreas["ChartArea1"].CursorY.IsUserEnabled = true; 
        this.chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true; 
        this.chart1.ChartAreas["ChartArea1"].CursorY.IsUserSelectionEnabled = true; 
        this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true; 
        this.chart1.ChartAreas["ChartArea1"].AxisY.ScaleView.Zoomable = true; 
        this.chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.IsPositionedInside = true; 
        this.chart1.ChartAreas["ChartArea1"].AxisY.ScrollBar.IsPositionedInside = true; 

它会放大选择。但是,我的选择仅限于网格线。而且,在选择时,有一个红色的十字,十字只停留在一个固定的x轴值。我如何修改这样的选择不限于网格线/特定的固定 x 轴值?

我还将 x 轴类型设置为日期时间,即

this.chart1.Series[chanName].XValueType = ChartValueType.DateTime; 

但是,x 轴只显示日期,不显示时间。如何在 x 轴上同时显示日期和时间?谢谢!

请您参考如下方法:

您可以通过设置 LabelStyle.Format 属性在 x 轴上显示日期和时间。

this.chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "g"; 

您需要设置 CursorX.Interval 属性。它的默认值为 1.0。

this.chart1.ChartAreas["ChartArea1"].CursorX.Interval = 1 / 24.0 / 60.0; // 1 minute 

您还可以设置 AxisX.ScaleView.MinSize 属性来限制选择。它的默认值未设置。

this.chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.MinSize = 1 / 24.0 / 60.0;