在 MS 图表中添加注释(例如 (10, 20) (20, 39) 等)和水平滚动条

use*_*345 3 c# charts winforms

我想在 MS 图表(winforms)中添加文本(例如注释),例如 (10, 20) , (30, 40) ,其中我已经有一个滚动条。

我可以在图表中绘制字符串(graphics.drawstring),但是在滚动水平滚动条时,我绘制的文本保持静态且不可移动。

在滚动滚动条时,我绘制的文本也应该随着我的水平滚动而移动。

我的代码如下:

 chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
 chart2.BorderlineColor      = System.Drawing.Color.FromArgb(26, 59, 105);
 chart2.BorderlineWidth = 3;
 chart2.BackColor       = Color.White;

 chart2.ChartAreas.Add("chtArea");
 chart2.ChartAreas[0].AxisX.Title = "Category Name";
 chart2.ChartAreas[0].AxisX.TitleFont = 
        new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
 chart2.ChartAreas[0].AxisY.Title = "UnitPrice";
 chart2.ChartAreas[0].AxisY.TitleFont = 
        new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
 chart2.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;
 chart2.ChartAreas[0].BorderWidth = 2;

 chart2.ChartAreas["chtArea"].AxisX.ScrollBar.Enabled = true;
 chart2.ChartAreas["chtArea"].CursorX.IsUserEnabled = true;
 chart2.ChartAreas["chtArea"].CursorX.IsUserSelectionEnabled = true;
 chart2.ChartAreas["chtArea"].AxisX.ScaleView.Zoomable = false;
 chart2.ChartAreas["chtArea"].AxisX.ScrollBar.IsPositionedInside = true;
 chart2.ChartAreas["chtArea"].AxisX.ScaleView.Size = 20;
 chart2.ChartAreas[0].AxisX.ScaleView.SmallScrollSizeType = DateTimeIntervalType.Seconds;
 chart2.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = 1;

 chart2.Legends.Add("UnitPrice");
 chart2.Series.Add("UnitPrice");
 chart2.Series[0].ChartType = SeriesChartType.Line;

 Random rand = new Random();
 var valuesArray = Enumerable.Range(0, 500).Select(x => rand.Next(0, 100)).ToArray();

 for (int i = 0; i < 500; i++)
 {                         
      chart2.Series["UnitPrice"].Points.AddXY(i+10, valuesArray[i]);               
 }
Run Code Online (Sandbox Code Playgroud)

我尝试了 TextAnnotations、Line annotations 等,但没有任何帮助。

然后我也尝试在 MS 图表中绘制动态标签。滚动水平滚动条时标签保持不动。

此代码在您的机器上也能完美运行。

TaW*_*TaW 6

听起来好像你想添加TextAnnotations.

如果您希望它们坚持您的数据点,您应该将它们锚定到它们应保留的点上。

这里有一些例子:

在此处输入图片说明

    // directly anchored to a point
    TextAnnotation TA1 = new TextAnnotation();
    TA1.Text = "DataPoint 222";
    TA1.SetAnchor(chart2.Series["UnitPrice"].Points[222]);
    chart2.Annotations.Add(TA1);

    // anchored to a point but shifted down
    TextAnnotation TA2 = new TextAnnotation();
    TA2.Text = "DataPoint 111";
    TA2.AnchorDataPoint = chart2.Series["UnitPrice"].Points[111];
    TA2.AnchorY = 0;   

    chart2.Annotations.Add(TA2);

    // this one is not anchored on a point:
    TextAnnotation TA3 = new TextAnnotation();
    TA3.Text = "At 50% width BC";
    TA3.AnchorX = 50;  // 50% of chart width
    TA3.AnchorY = 20;  // 20% of chart height, from top!
    TA3.Alignment = ContentAlignment.BottomCenter;  // try a few!

    chart2.Annotations.Add(TA3);
Run Code Online (Sandbox Code Playgroud)

默认情况下,他们要么锚DataPoints或定位在%该图表的尺寸。

可以根据像素坐标设置位置,但为此您需要在图表每次更改视图时计算位置!

有关如何将图表数据位置转换为图表控件坐标,反之亦然的示例,请参见此处..(虽然不推荐)