C# Chart图表X轴时间标签不能正常显示

问题遇到的现象和发生背景

C# Winform Chart Line折线图鼠标框选放大后不显示x轴标签;其次鼠标框选局部放大有没有触发对应的函数事件,怎样重写这个事件

遇到的现象和发生背景,请写出第一个错误信息

double size = chart.ChartAreas[0].AxisX.ScaleView.Size;
size = NaN;鼠标滚轮放大和框选局部放大会有冲突,局部放大后在用鼠标缩放会导致折线图不显示(已取消鼠标缩放)

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
//初始化图表控件,2个折线图,上下联动的。支持鼠标框选放大
  private void InitChat()
        {
            ChartLine.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; //未设置网格类型
            ChartLine.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Solid; //设置网格类型为实线
          
            // ChartLine.Series[0].IsValueShownAsLabel = true;//设置显示示数
            ChartLine.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
            ChartLine.ChartAreas[0].AxisX.ScaleView.Position = 1;
            ChartLine.ChartAreas[0].AxisX.ScaleView.Size = 100;
            ChartLine.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
            ChartLine.ChartAreas[0].AxisX.ScrollBar.ButtonColor = SystemColors.Control;
            ChartLine.ChartAreas[0].AxisX.ScrollBar.LineColor = SystemColors.Control;
            ChartLine.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;

            ChartLine.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.PaleGreen;
            ChartLine.ChartAreas[0].AxisX.MinorGrid.Enabled = true;
            ChartLine.ChartAreas[0].AxisX.MinorGrid.LineColor = Color.WhiteSmoke;
            ChartLine.ChartAreas[0].AxisX.MinorTickMark.Enabled = true;
            ChartLine.ChartAreas[0].AxisX.MinorTickMark.LineColor = Color.DimGray;
          
            ChartLine.ChartAreas[0].CursorX.SelectionColor = Color.SkyBlue;

            ChartLine.ChartAreas[0].CursorX.Interval = 0D;
            ChartLine.ChartAreas[0].CursorX.IntervalOffsetType = DateTimeIntervalType.Auto;
            ChartLine.ChartAreas[0].CursorX.IsUserEnabled = true;
            ChartLine.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            ChartLine.Cursor = System.Windows.Forms.Cursors.Cross;

            // X 时间轴 设置
            ChartLine.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Hours;//设置x轴间隔值单位:秒
            ChartLine.ChartAreas[0].AxisX.LabelStyle.Interval = 2;//设置X轴的值的间隔大小
            ChartLine.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = false;//是否在轴末尾显示标记
            ChartLine.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";//设置X轴的数据样式
            ChartLine.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Minutes;
            ChartLine.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Minutes; //度量单位
            ChartLine.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1D;
            ChartLine.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Minutes;
            ChartLine.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
            //ChartLine.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true;

            ChartPlcLine.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.NotSet; //未设置网格类型
            ChartPlcLine.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Solid; //设置网格类型为实线
            ChartPlcLine.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.PaleGreen;
            // ChartLine.Series[0].IsValueShownAsLabel = true;//设置显示示数
            ChartPlcLine.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
            ChartPlcLine.ChartAreas[0].AxisX.ScaleView.Position = 1;
            ChartPlcLine.ChartAreas[0].AxisX.ScaleView.Size = 100;
            ChartPlcLine.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
            ChartPlcLine.ChartAreas[0].AxisX.ScrollBar.ButtonColor = SystemColors.Control;
            ChartPlcLine.ChartAreas[0].AxisX.ScrollBar.LineColor = SystemColors.Control;
            ChartPlcLine.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
            // ChartPlcLine.ChartAreas[0].AxisX.Interval = 10;
            // X 时间轴 设置
            ChartPlcLine.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Hours;//设置x轴间隔值单位:秒
            ChartPlcLine.ChartAreas[0].AxisX.LabelStyle.Interval = 2;//设置X轴的值的间隔大小
            ChartPlcLine.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = false;//是否在轴末尾显示标记
            ChartPlcLine.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";//设置X轴的数据样式
            ChartPlcLine.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Minutes;
            ChartPlcLine.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Minutes; //度量单位
            ChartPlcLine.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1D;
            ChartPlcLine.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Minutes;
            ChartPlcLine.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;

            ChartPlcLine.ChartAreas[0].CursorX.Interval = 0D;
            ChartPlcLine.ChartAreas[0].CursorX.IntervalOffsetType = DateTimeIntervalType.Minutes;
            ChartPlcLine.ChartAreas[0].CursorX.IsUserEnabled = true;
            ChartPlcLine.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            ChartPlcLine.Cursor = Cursors.Cross;

            // ChartPlcLine.Customize += ChartPlcLine_Customize;
            ChartLine.Customize += ChartLine_Customize;
            // ChartLine.MouseWheel += ChartLine_MouseWheel;
            // ChartPlcLine.MouseWheel += ChartLine_MouseWheel;
            ChartLine.MouseClick += ChartLine_MouseClick;
            ChartPlcLine.MouseClick += ChartLine_MouseClick;

        }


        private void ChartLine_MouseClick(object sender, MouseEventArgs e)
        {
            Chart chart1 = sender as Chart;
            //右键恢复事件
            if (e.Button == MouseButtons.Right)
            { 
                chart1.ChartAreas[0].AxisX.ScaleView.ZoomReset(Math.Max(cutterDatas.Count, stateList.Count) / 2);
            }
            else if (e.Button == MouseButtons.Left)
            {
                SetScaleViewSize(chart1);
            }
        }

  鼠标框选放大后我用Timer定时器读取 ScaleView.Size,然后设置X时间轴 标签类型和间隔
   private void SetScaleViewSize(Chart chart) {
            /*适用于鼠标框选放大*/
            double size = chart.ChartAreas[0].AxisX.ScaleView.Size;
            LogTool.D("SetScaleView.Size: " + size);
            if (double.IsNaN(size))
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Hours;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
                return;
            }
            if (size < 0.00005)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
            }
            else if (size <= 0.0001)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 5;
            }
            else if (size <= 0.0075)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 10; 
            }
            else if (size <= 0.001)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 15; 
            }
            else if (size <= 0.075)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 30;
            }
            else if (size <= 0.05)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Minutes;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;
            }
            else if (size <= 0.025)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Minutes;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 5;
            }
            else if (size <= 0.1)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Minutes;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 10;
            }
            if (size <= 0.25)
            {
                chart.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Minutes;
                chart.ChartAreas[0].AxisX.LabelStyle.Interval = 15; 
            }
        }

运行结果及详细报错内容

首次加载获取到的 ScaleView.Size = NaN,标签能正常加载。

img


第一次框选放大也可以显示标签,第二次三次框选放大后就不显示时间轴标签了。

img

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%

用定时器实时读取 ,但也不能显示标签。
double size = chart.ChartAreas[0].AxisX.ScaleView.Size;
LogTool.D("SetScaleView.Size: " + size);

我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

每次框选局部放大都能按照 SetScaleViewSize函数设置 显示标签。

应该是标签太密了
你用的到底是个什么控件,dev吗