繁体   English   中英

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

[英]Add annotations in MS chart (e.g. (10, 20) (20, 39) etc) and horizontal Scroll Bar

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

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

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

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

听起来好像你想添加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);

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM