简体   繁体   English

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

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

I want to add text(say, annotations) in MS chart(winforms) like (10, 20) , (30, 40) where I already have a scroll bar.我想在 MS 图表(winforms)中添加文本(例如注释),例如 (10, 20) , (30, 40) ,其中我已经有一个滚动条。

I can able to draw strings(graphics.drawstring) in Chart, but on scrolling the horizontal scroll bar, the text which I have drawn remains static and immovable.我可以在图表中绘制字符串(graphics.drawstring),但是在滚动水平滚动条时,我绘制的文本保持静态且不可移动。

On scrolling the scrollbar, the text which I have drawn also should move along with my horizontal scrolling.在滚动滚动条时,我绘制的文本也应该随着我的水平滚动而移动。

My code follows:我的代码如下:

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

I tried TextAnnotaions, Line annotations, etc Nothing helped me.我尝试了 TextAnnotations、Line annotations 等,但没有任何帮助。

Then I tried drawing dynamic labels inside MS chart also.然后我也尝试在 MS 图表中绘制动态标签。 Labels remain immovable while scrolling horizontal scroll bar.滚动水平滚动条时标签保持不动。

This code works perfectly in your machine also.此代码在您的机器上也能完美运行。

Sounds a lot as if you want to add TextAnnotations .听起来好像你想添加TextAnnotations

If you want them to stick with your data points you should anchor them to the points they shall stay with.如果您希望它们坚持您的数据点,您应该将它们锚定到它们应保留的点上。

Here are a few examples:这里有一些例子:

在此处输入图片说明

    // 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);

By default they either anchor to DataPoints or are positioned in % of the chart size.默认情况下,他们要么锚DataPoints或定位在%图表大小的。

It is also possible to set the positions according to pixel coordinates, but for this you need to calculate the positions each time the chart changes its view!可以根据像素坐标设置位置,但为此您需要在图表每次更改视图时计算位置!

See here for an example how to transform chart data positions to chart control coordinates and vice versa.. (Not really recommended, though) 有关如何将图表数据位置转换为图表控件坐标,反之亦然的示例, 请参见此处..(虽然不推荐)

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

相关问题 如何增加两组时间? 例如00:10:10 + 00:00:20 = 00:10:30 - How to add two sets of time? e.g 00:10:10 + 00:00:20 = 00:10:30 C#如何做例如5 * 10 ^ 4或数字到十的幂等 - C# how to do e.g. 5*10^4 or numbers to the power of ten etc 我可以并发10ms以下的微小任务吗? (例如矩阵求逆) - Can I paralellize tiny sub-10ms tasks? (e.g. matrix inversion) wpf图表显示水平滚动条/查看器 - wpf chart show horizontal scroll bar/viewer 如何将标签保持为滚动图的5的倍数 - How to keep labels at multiples of e.g. 5 for a scrolling chart 如何按10、0-10、11-20等长度对数组进行分组 - How to Group an array by lengths of 10, 0-10, 11-20, etc 如何为.dll添加自己的资源本地化(例如,对于AvalonDock) - How to add own resource localization for a .dll (e.g. for AvalonDock) 封装“类成员”的术语(例如,字段,属性,方法等) - Encapsulating term for 'kind of class member' (e.g. field, property, method, etc.) 如何在Silverlight中获取默认滚动条的子元素(例如,repeatbutton,thumb等)? - How to get child element of default scrollbar(e.g. repeatbutton, thumb, etc.) in silverlight? 如果列表中的索引超过 20,如何激活水平滚动视图并拉伸图表 - 使用 xamarin c# - How to activate horizontal ScrollView if indexes from List is more than 20 and stretch the chart - using xamarin c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM