Select Visual Studio 图表中的时间跨度 (C#)
Select a timespan in a Visual Studio Chart (C#)
是否可以(可能使用第三方工具)在图表中添加两条可以移动的线,然后在这两条线的边缘获取两个日期(字符串)?
也许这张图更能说明问题。它看起来应该与此类似:
Mockup_picture
谢谢你的帮助!
您可以尝试 chart.SelectionRangeChanging 事件到 select 时间跨度并获取相应的日期时间。
这是一个代码示例,您可以参考。
private void Form1_Load(object sender, EventArgs e)
{
var s = new Series();
s.ChartType = SeriesChartType.Column;
var d = new DateTime(2020,10,1);
s.Points.AddXY(d, 30);
s.Points.AddXY(d.AddDays(1), 20);
s.Points.AddXY(d.AddDays(2), 70);
s.Points.AddXY(d.AddDays(3), 90);
s.Points.AddXY(d.AddDays(4), 80);
s.Points.AddXY(d.AddDays(5), 50);
s.Points.AddXY(d.AddDays(6), 60);
s.Points.AddXY(d.AddDays(7), 80);
s.Points.AddXY(d.AddDays(8), 90);
chart1.Series.Clear();
chart1.Series.Add(s);
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
chart1.Series[0].XValueType = ChartValueType.DateTime;
DateTime minDate = new DateTime(2020, 10, 01).AddSeconds(-1);
DateTime maxDate = new DateTime(2020, 10, 08); // or DateTime.Now;
chart1.ChartAreas[0].AxisX.Minimum = minDate.ToOADate();
chart1.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate();
chart1.ChartAreas[0].CursorX.IsUserEnabled = false; // red cursor at SelectionEnd
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false; // zoom into SelectedRange
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
}
private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
{
double x1 = e.NewSelectionStart;
double x2 = e.NewSelectionEnd;
DateTime date1 = DateTime.FromOADate(x1);
DateTime date2 = DateTime.FromOADate(x2);
txtStart.Text = date1.ToString("yyyy-MM-dd");
txtEnd.Text = date2.ToString("yyyy-MM-dd");
}
结果:
是否可以(可能使用第三方工具)在图表中添加两条可以移动的线,然后在这两条线的边缘获取两个日期(字符串)?
也许这张图更能说明问题。它看起来应该与此类似:
Mockup_picture
谢谢你的帮助!
您可以尝试 chart.SelectionRangeChanging 事件到 select 时间跨度并获取相应的日期时间。
这是一个代码示例,您可以参考。
private void Form1_Load(object sender, EventArgs e)
{
var s = new Series();
s.ChartType = SeriesChartType.Column;
var d = new DateTime(2020,10,1);
s.Points.AddXY(d, 30);
s.Points.AddXY(d.AddDays(1), 20);
s.Points.AddXY(d.AddDays(2), 70);
s.Points.AddXY(d.AddDays(3), 90);
s.Points.AddXY(d.AddDays(4), 80);
s.Points.AddXY(d.AddDays(5), 50);
s.Points.AddXY(d.AddDays(6), 60);
s.Points.AddXY(d.AddDays(7), 80);
s.Points.AddXY(d.AddDays(8), 90);
chart1.Series.Clear();
chart1.Series.Add(s);
chart1.Series[0].XValueType = ChartValueType.DateTime;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd";
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;
chart1.ChartAreas[0].AxisX.IntervalOffset = 1;
chart1.Series[0].XValueType = ChartValueType.DateTime;
DateTime minDate = new DateTime(2020, 10, 01).AddSeconds(-1);
DateTime maxDate = new DateTime(2020, 10, 08); // or DateTime.Now;
chart1.ChartAreas[0].AxisX.Minimum = minDate.ToOADate();
chart1.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate();
chart1.ChartAreas[0].CursorX.IsUserEnabled = false; // red cursor at SelectionEnd
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false; // zoom into SelectedRange
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
}
private void chart1_SelectionRangeChanging(object sender, CursorEventArgs e)
{
double x1 = e.NewSelectionStart;
double x2 = e.NewSelectionEnd;
DateTime date1 = DateTime.FromOADate(x1);
DateTime date2 = DateTime.FromOADate(x2);
txtStart.Text = date1.ToString("yyyy-MM-dd");
txtEnd.Text = date2.ToString("yyyy-MM-dd");
}
结果: