C# 图表滚动条不随鼠标移动

C# Chart ScrollBar does not move with Mouse

为什么我放大数据后鼠标拖动图表上的ScrollBar不动了?

我在 hh.mm.ss:fff 上使用 DateTime 作为 AxisX 格式,需要图形的比例数据。

示例如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Windows.Forms.DataVisualization.Charting;

namespace ChartEx
{
    public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
        Init();
    }

    void Init()
    {

        chart1.Series.Clear();
        chart1.ChartAreas.Clear();
        chart1.Cursor = null;
        chart1.ChartAreas.Add("NewChart");

        ChartArea chrArr = chart1.ChartAreas[0];
        chrArr.AxisX.TitleFont = new Font("Arial", 14.25F, 
                    System.Drawing.FontStyle.Regular, 
                    System.Drawing.GraphicsUnit.Point, ((byte)(204)));
        chrArr.AxisX.LabelStyle.Enabled = true;
        chrArr.AxisX.LabelStyle.Format = "hh.mm.ss";//"hh.mm.ss:fff";
        chrArr.AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
        chrArr.AxisX.LabelStyle.Interval = 1;
        chrArr.AxisX.LabelAutoFitStyle = LabelAutoFitStyles.StaggeredLabels;
        chrArr.AxisY.LabelStyle.Enabled = true;
        chrArr.AxisX.IsLabelAutoFit = false;
        chrArr.AxisX.ScrollBar.Size = 10;

        chrArr.CursorX.Interval = 30D;
        chrArr.CursorX.IntervalOffset = 30D;
        chrArr.CursorX.IntervalOffsetType = DateTimeIntervalType.Milliseconds;
        chrArr.CursorX.IntervalType = DateTimeIntervalType.Milliseconds;
        chrArr.CursorX.IsUserEnabled = true;
        chrArr.CursorX.IsUserSelectionEnabled = true;
        chrArr.CursorX.LineDashStyle = ChartDashStyle.Solid;
        chrArr.CursorX.SelectionColor = System.Drawing.Color.OrangeRed;
        chrArr.CursorX.LineColor = System.Drawing.Color.Red;
        chrArr.CursorX.LineWidth = 1;
        chrArr.CursorY.LineColor = Color.Transparent;
        chart1.Cursor = System.Windows.Forms.Cursors.Cross;

        chrArr.AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
        chrArr.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
        chrArr.AxisX.MajorGrid.IntervalType = DateTimeIntervalType.Seconds;

        chrArr.AxisX.LabelAutoFitMinFontSize = 8;
        chrArr.AxisX.IntervalType = DateTimeIntervalType.Seconds;


        chrArr.AxisX.ScaleView.Zoomable = true;
        chrArr.AxisX.ScrollBar.IsPositionedInside = true;
        chrArr.AxisX.ScrollBar.Enabled = true;
        chrArr.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;

        chrArr.AxisY.Maximum = 12;

        chrArr.AxisX.MajorGrid.LineWidth = 1;
        chrArr.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot;

    }


    DateTime[] dt= new DateTime[]{ 
     new DateTime(2012,1,1,12,20,1,100),
     new DateTime(2012,1,1,12,20,2,200),
     new DateTime(2012,1,1,12,20,3,300),
     new DateTime(2012,1,1,12,20,4,400),
     new DateTime(2012,1,1,12,20,5,500),
     new DateTime(2012,1,1,12,20,6,600),
     new DateTime(2012,1,1,12,20,7,700),
     new DateTime(2012,1,1,12,20,8,800),
     new DateTime(2012,1,1,12,20,9,900),
     new DateTime(2012,1,1,12,20,10,0),
     new DateTime(2012,1,1,12,20,11,100),
     new DateTime(2012,1,1,12,20,12,200),
    };


    int[] Value = new int[]{
        1,2,3,4,5,6,7,8,9,
        0,1,2
    };

    private void button1_Click(object sender, EventArgs e)
    {
        const String NLine = "line1";
        chart1.Series.Clear();
        chart1.Series.Add(NLine);
        chart1.Series[NLine].ChartArea = chart1.ChartAreas[0].Name;
        chart1.Series[NLine].ChartType = SeriesChartType.FastLine;
        chart1.Series[NLine].XValueType = ChartValueType.DateTime;

        for (int i = 0; i < 12; i++)
        {
            chart1.Series[NLine].Points.AddXY(dt[i], Value[i]);
        }


    }

}

}

您需要添加这一行:

  chrArr.AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Milliseconds;

让电梯知道正确的比例..

AxisScaleView.SmallScrollMinSizeType 的默认设置是自动,这不适用于 DateTime DataPoints 范围从 12 分钟到 Milliseconds

你可以调整电梯在几秒钟内到达,但毫秒感觉好多了..