C#Chart ScrollBar不随鼠标移动

时间:2015-04-10 08:03:29

标签: c# charts scrollbar mouse

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

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

以下示例:

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]);
        }


    }

}

}

1 个答案:

答案 0 :(得分:2)

您需要添加以下行:

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

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

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

您可以在几秒钟内缩放升力,但毫秒感觉更好......