c# - 我想在 mschart 中查看剩余行注释

标签 c# annotations mschart

在mschart中,我将线注释与SetAnchor(点8,点12)一起使用。 如果我滚动图表并隐藏点 8,我将看不到剩余的线注释(点 9 到 12)。 我想查看剩余的行注释。帮助我!

我的引用; Samples Environments for Microsoft Chart Controls --> 图表功能 --> 注释 --> 注释锚定

private void AddLineAnnotation()
{
    // create a line annotation
    LineAnnotation annotation = new LineAnnotation();

    // setup visual attributes
    annotation.StartCap = LineAnchorCapStyle.Arrow;
    annotation.EndCap = LineAnchorCapStyle.Arrow;
    annotation.LineWidth = 3;
    annotation.LineColor = Color.OrangeRed;
    annotation.ShadowOffset = 2;
    annotation.ClipToChartArea = "Default";

    // prevent moving or selecting
    annotation.AllowMoving = false;
    annotation.AllowAnchorMoving = false;
    annotation.AllowSelecting = false;

    if(Chart1.Series[0].Points.Count > 13)
    {
        // Use the Anchor Method to anchor to points 8 and 12...
        annotation.SetAnchor(Chart1.Series[0].Points[8], Chart1.Series[0].Points[12]);
    }

    // add the annotation to the collection
    Chart1.Annotations.Add(annotation);
}

enter image description here

最佳答案

这是一个棘手的问题。

坏消息是:我认为这是不可能的。我认为 MSChart 将忽略所有在可见区域之外开始的注释。也许原因是为了避免困惑,但谁能知道......?

解决方法必须考虑两个端点都在外部并且我们仍然希望看到注释的情况。

好消息是,通过所有者绘制,人们可以编写一种解决方法,确实可以为这两种情况划定界限。

以下示例显示了绘图代码。确保将拖动缩放模式与拖动绘制新注释模式分开。我使用一个复选框及其 CheckChanged 事件。

让我们首先看看它的实际效果:

enter image description here

当注释的开头滚动时,线条画就会开始。很难注意到。

以下是 xxxPaint 事件的代码:

private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
    // loop only over line annotations:
    List<LineAnnotation> annos =
        chart1.Annotations.Where(x => x is LineAnnotation)
                            .Cast<LineAnnotation>().ToList();
    if (!annos.Any()) return;

    // a few short references
    Graphics g = e.ChartGraphics.Graphics;
    ChartArea ca = chart1.ChartAreas[0];
    Axis ax = ca.AxisX;
    Axis ay = ca.AxisY;

    // we want to clip the line to the innerplotposition excluding the scrollbar:
    Rectangle r = Rectangle.Round(InnerPlotPositionClientRectangle(chart1, ca));
    g.SetClip(new Rectangle(r.X, r.Y, r.Width, r.Height - (int)ax.ScrollBar.Size));
    g.InterpolationMode = InterpolationMode.NearestNeighbor;  // pick your mode!
    foreach (LineAnnotation la in annos)
    {
        if (Double.IsNaN(la.Width)) continue;  // *
        // calculate the coordinates
        int x1 = (int)ax.ValueToPixelPosition(la.AnchorX);
        int y1 = (int)ay.ValueToPixelPosition(la.AnchorY);
        int x2 = (int)ax.ValueToPixelPosition(la.AnchorX + la.Width);
        int y2 = (int)ay.ValueToPixelPosition(la.AnchorY + la.Height);

        // now we draw the line if necessary:
        if (x1 < r.X || x1 > r.Right)
            using (Pen pen = new Pen(la.LineColor, 0.5f)) g.DrawLine(pen, x1, y1, x2, y2);
    }
    // reset the clip to allow the system drawing a scrollbar
    g.ResetClip();
}

一些注意事项:

  • 代码假设 (*)注释 均以 AnchorX/Y 锚定,并设置了宽度/高度 。如果您使用了不同的锚定方式,则需要调整代码。

  • 对于裁剪部分,我们需要知道InnerPlotPosition的像素大小/位置。为此,您可以使用例如中的代码在 this link 的底部.

  • 除了一条直线之外,我没有编写任何代码。如果您已经修饰了注释,则可能需要扩展代码;

关于c# - 我想在 mschart 中查看剩余行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54510660/

相关文章:

c# - MS Chart ChartImageHandler 存储选项

C# 停止 BackgroundWorker

delphi - Delphi的属性语言特性可以注释哪些语言元素?

java - Spring:@Component 与 @Bean

c# - 滚动时图表控件 Y 轴自动缩放

c# - 如何在鼠标悬停在 C# Web 应用程序上的 Ms 图表中显示数据

c# - Unity 访问非静态成员需要对象引用 C#

c# - 在 mvc Controller 中键入后,新表达式需要 ()、[] 或 {}

c# - 在 Visual Studio 2013 中创建 CSS 文件时,上下文菜单中不存在构建样式项?

android - Skobbler Android - 如何在 map 上的注释中设置自定义图像?