处于缩放状态时,将MS图表区域滚动/移动到突出显示的数据点

时间:2019-06-13 12:56:37

标签: c# scroll mschart

在Windows窗体中,我正在从Datagridview的MS Chart中显示数据。

在datagridview中选择一行时,我用不同的颜色突出显示了图表中的相应数据点。

当图表处于缩放状态时,如果一个数据点被新高亮显示,而不处于可见状态,则必须将图表滚动/移动到突出显示的数据点。

        chart.ChartAreas.Add("LineGraphHistory");
        chart.ChartAreas["LineGraphHistory"].AxisX.Title = "X Axis";
        chart.ChartAreas["LineGraphHistory"].AxisX.MajorGrid.LineColor = System.Drawing.Color.Black;
        chart.ChartAreas["LineGraphHistory"].AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
        chart.ChartAreas["LineGraphHistory"].AxisY.Title = "Y Axis";
        chart.ChartAreas["LineGraphHistory"].AxisY.MajorGrid.LineColor = Color.Black;
        chart.ChartAreas["LineGraphHistory"].AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
        chart.ChartAreas["LineGraphHistory"].BackColor = Color.White;

        chart.ChartAreas["LineGraphHistory"].CursorX.IsUserEnabled = true;

      chart.ChartAreas["LineGraphHistory"].CursorX.IsUserSelectionEnabled = true;
        chart.ChartAreas["LineGraphHistory"].CursorX.Interval = 0;
        chart.ChartAreas["LineGraphHistory"].AxisX.ScaleView.Zoomable = true;
        chart.ChartAreas["LineGraphHistory"].AxisX.ScrollBar.Enabled = true;

        chart.Legends.Add("Legend");
        chart.Legends["Legend"].BorderColor = Color.Tomato;
        chart.DataSource = CSVDataTable;
        chart.ChartAreas["LineGraphHistory"].AxisX.IntervalType = DateTimeIntervalType.Seconds;
        chart.ChartAreas["LineGraphHistory"].AxisX.LabelStyle.Format ="dd-MM-yyyy\n hh:mm:ss"; ;
        chart.Series[s].XValueType =ChartValueType.DateTime ;
        chart.DataBind();
        chart.Update();



       private void cDataGrid_SelectionChanged(object sender, EventArgs e)
       {
            int nCount = csvDataGrid.SelectedRows.Count;
            if (nCount > 0)
            {
                for (int i = 0; i < nCount; i++)
                {
                    int index = csvDataGrid.SelectedRows[i].Index;
                    if (index >= csvDataGrid.Rows.Count-1)
                        return;
                     for (int k = 0; k < chart.Series.Count; k++)
                     {
                         DataPointCollection pr = chart.Series[k].Points;
                         pr[index].MarkerColor = Color.DarkGoldenrod;
                         pr[index].MarkerStyle = MarkerStyle.Star10;
                         pr[index].MarkerSize = 20;
                        // chart.
                       }
                       chart.Update();
                   }
               }
           }      

如何实现?

按照Taw的建议,我试图设置比例视图位置。 我有10个数据点。数据点的x值范围是20到200。每个x值具有20的相等差。视图大小是100。在缩放模式下,当我滚动到最大时,视图中的x范围是101到200,最后一个点在视图中显示为第5点。而如果我使用您的代码来设置scaleview位置以突出显示最后一个数据点,则x范围将变为180到240,突出显示的最后一个数据点将显示为第一个范围。

为什么paintviewmin和paintviewmax值在变化?

图像是 while scrolling

while setting scaleview

1 个答案:

答案 0 :(得分:0)

您需要计算与DataPoint dp.XValue的偏移量,也许像这样:

Axis ax = chart.ChartAreas[0].AxisX;
var size = ax.ScaleView.ViewMaximum - ax.ScaleView.ViewMinimum;
ax.ScaleView.Position = dp.XValue - size / 2.0;

示例:

enter image description here

更新:显示较小的数据集时,自动添加的边距会干扰上面的简单计算。为避免这种情况,您可以添加:

chart.ChartAreas[0].AxisX.IsMarginVisible = false;