【发布时间】:2016-09-22 15:26:07
【问题描述】:
我在图表中绘制了这个简单的图表。 X 轴上的值为DateTime 值。
public partial class Form1 : Form
{
List<double> valuelist = new List<double>();
List<DateTime> timelist = new List<DateTime>();
public Form1()
{
InitializeComponent();
// fill the lists with values
for (int i = 0; i < 2000; i++)
{
double value = Math.Sin(i/20.0);
valuelist.Add(value);
timelist.Add(DateTime.Now.AddMinutes(i + 2));
}
// add the Values to the chart
for (int i = 0; i < valuelist.Count; i++)
{
this.chart1.Series[0].Points.AddXY(timelist[i], valuelist[i]);
}
this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "dd.MM-hh:mm";
}
private void Form1_Load(object sender, EventArgs e)
{
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.ChartAreas[0].AxisX.Maximum = timelist.Max().ToOADate();
chart1.ChartAreas[0].AxisX.Minimum = timelist.Min().ToOADate();
chart1.ChartAreas[0].CursorX.AutoScroll = true;
chart1.ChartAreas[0].CursorY.AutoScroll = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
DateTime intervall = timelist.Min().AddHours(3);
chart1.ChartAreas[0].AxisX.ScaleView.Zoom(chart1.ChartAreas[0].AxisX.Minimum, intervall.ToOADate());
// disable zoom-reset button
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
// set scrollbar small change to blockSize
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = intervall.ToOADate();
}
}
我的问题是我无法让滚动条平滑移动。当我仅绘制 Y 值并仅对 AxisX.Maximum、AxisX.Minimum、AxisX.ScaleView.Zoom 和 AxisX.ScaleView.SmallScrollSize 使用 double 值时,它就像一个魅力。但是,一旦我将 DateTime 用于 X 值,我就只能逐步滚动。有人知道如何超越吗?我感觉这段代码是障碍:
// set scrollbar small change to blockSize (e.g. 100)
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = intervall.ToOADate();
编辑:
X 轴的间隔是自动的,范围由 chart1.ChartAreas[0].AxisX.ScaleView.Zoom 的 ZoomLevel 设置。这是一张图片:
编辑 2:
X 轴的值是模拟每分钟采样 1 个值的日期时间值:
timelist.Add(DateTime.Now.AddMinutes(i + 2));
因为值很多,我没有设置间隔。 代码以这种方式发布,因此可以按原样复制并立即运行以进行尝试。
【问题讨论】:
标签: c# datetime scroll charts axis