【问题标题】:Line Chart Rectangle Annotation resize acording to the point counts折线图矩形注释根据点数调整大小
【发布时间】:2014-09-15 10:31:35
【问题描述】:

我有一个折线图。我从数据库中读取一个值及其记录时间,并每秒将它们添加到折线图中。所以我的分数每秒都在增加。

问题是这样的。我在折线图中添加了折线和矩形注解。 Line annonation 保持与开始时相同的宽度,但矩形 annonation 的宽度会根据我添加的点数而变化。

像这样;

我只希望那个矩形标注有一个固定的宽度我该怎么做?

代码;

    VerticalLineAnnotation VA;
    RectangleAnnotation RA;
    double xFactor = 0.03;
    double yFactor = 0.02;


    VA = new VerticalLineAnnotation();
    VA.AxisX = chartMonitor.ChartAreas[0].AxisX;
    VA.AllowMoving = true;
    VA.IsInfinitive = true;
    VA.ClipToChartArea = chartMonitor.ChartAreas[0].Name;
    VA.Name = "myLine";
    VA.LineColor = Color.Red;
    VA.LineWidth = 2;
    VA.X = 1;


    RA = new RectangleAnnotation();
    RA.AxisX = chartMonitor.ChartAreas[0].AxisX;
    RA.IsSizeAlwaysRelative = false;
    RA.Width = 10 * xFactor;
    RA.Height = 30 * yFactor;
    VA.Name = "myRect";
    RA.LineColor = Color.Red;
    RA.BackColor = Color.Red;
    RA.AxisY = chartMonitor.ChartAreas[0].AxisY;
    RA.Y = -RA.Height;
    RA.X = VA.X - RA.Width / 2;

    RA.Text = "M1";
    RA.ForeColor = Color.White;
    RA.Font = new System.Drawing.Font("Arial", 8f);

    chartMonitor.Annotations.Add(VA);
    chartMonitor.Annotations.Add(RA);
    chartMonitor.AnnotationPositionChanged += new EventHandler(chartMonitor_AnnotationPositionChanged);
    chartMonitor.AnnotationPositionChanging += new EventHandler<AnnotationPositionChangingEventArgs>(chartMonitor_AnnotationPositionChanging);

void chartMonitor_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e)
        {
            if (sender == VA) RA.X = VA.X - RA.Width / 2;
        }

        void chartMonitor_AnnotationPositionChanged(object sender, EventArgs e)
        {
            VA.X = (int)(VA.X + 0.5);
            RA.X = VA.X - RA.Width / 2;
        }

【问题讨论】:

    标签: c# winforms charts annotations linechart


    【解决方案1】:

    我想我记得那段代码..

    注释绑定到 X 轴,这意味着它的大部分属性都以 X 轴值测量。 (但不是 LineWidth,它以像素为单位,也不是 FontSize,它像往常一样工作......)

    我已经加入了一个因素,让您可以根据需要进行扩展。

    假设 f1 = 0.03 值适用于 N1 数据点,您需要重新计算 N2 数据点,如下所示:

     xFactor = f1 * N1 / N2;
    

    所以你应该引入一个 const f1 并在添加或删除绘图点时重新计算因子..

    这是一个可以做到这一点的函数:

     void scaleAnnotation(Annotation A)
     {
         ChartArea CA = chart1.ChartAreas[0];// pick your chartarea..
         A.AxisX = CA.AxisX;                 // .. and axis!
         int N = S1.Points.Count;            // S1 being your Series !
         // to keep the label width constant the chart's width must be considered
         // 60 is my 'magic' number; you must adapt for your chart's x-axis scale!
         double xFactor = 60 * N / chart1.Width;   
         A.Width = 1 * xFactor ;
         A.X = VA.X - A.Width / 2;    
     }
    

    我发现即使在调整图表大小、添加 10.000 个点并逐个删除它们时,标签的宽度也几乎是恒定的。

    注意:您必须使 xFactor 适应图表的 x 值比例;做几个测试..! 我发现每次添加点时调用它都会使标签保持相当恒定的大小,即使图表本身有数百个点也是如此。

    【讨论】:

    • 但是如果添加的点太多,一些点会从折线图中消失。如何获得出现点数?
    • 但是如果添加的点太多,有些点会从折线图上消失。 你是什么意思?你实现了滚动吗? Series S 中的数据点总数为 n = S.Points.Count。您需要计算总点数,而不仅仅是添加的数字..
    • 例如,如果有 5 个点,当再添加 10 个点时,它会自动显示点 1,5,9,13
    • 听起来好像您已将间隔设置为 4。数据点可能显示在行中,只是缺少底部的刻度和标签,对吗?您可以像这样控制图表区域 CA 的轴的间隔 CA.AxisX.Interval = 1;
    • 我没有设置任何间隔,它是自动的。
    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2020-10-07
    • 2020-02-23
    • 1970-01-01
    • 2018-01-07
    • 2015-09-17
    相关资源
    最近更新 更多