【问题标题】:Convert chart coords to pixels将图表坐标转换为像素
【发布时间】:2011-08-12 13:11:04
【问题描述】:

我应该在极坐标图中画一个圆圈,上面有一些文字。

我开始玩 PostPaint,得到了图表图形,所以我可以在上面画和写自定义的东西。

我的主要问题是位置。

例如我想在 x 和 y 轴相交的地方绘制 sg,但我没有找到任何有效的方法将图形坐标(在我的示例中为 0,0)转换为像素坐标。

我该怎么做?如何将图表坐标转换为像素?

.net4/winforms/vs2010/c#

【问题讨论】:

    标签: c# .net visual-studio-2010 mschart


    【解决方案1】:

    所以,我解决了。

    我处理 mschart 的 postpaint 事件:

    private void chartMain_PostPaint(object sender, ChartPaintEventArgs e)
        {
            try
            {
                if (chartMain.ChartAreas.FirstOrDefault(a=>a.Name == "Default") == null)
                    return;
    
                Graphics graph = e.ChartGraphics.Graphics;
    
                var day = Date.GetBoundaries(daySelectorMain.DateTimePickerDay.Value);
                var toDate = day.Item2; //it is maxdate value (max value of x axis)
    
                var centerY = (float)chartMain.ChartAreas["Default"].AxisY.ValueToPixelPosition(-80); //-80 is the min value of y (y axis)
                var centerX = (float)chartMain.ChartAreas["Default"].AxisX.ValueToPixelPosition(toDate.ToOADate());
    
                var origoY = (float)chartMain.ChartAreas["Default"].AxisY.ValueToPixelPosition(0);
                var origoX = (float)chartMain.ChartAreas["Default"].AxisX.ValueToPixelPosition(toDate.ToOADate());
    
                var radius = (float)(centerY - origoY) - 1;
    
                graph.DrawLine(System.Drawing.Pens.Blue,
                               new PointF(origoX, origoY),
                               new PointF(centerX, centerY));
    
                graph.FillEllipse(new SolidBrush(Color.White), centerX - radius, centerY - radius, radius * 2, radius * 2);
            }
            catch (System.Exception exc)
            {
                Debug.Assert(false, exc.Message);
            }
        }
    

    【讨论】:

      【解决方案2】:

      自我回答的解决方案既不适用于更一般的情况,也不能实际将DataPoint 转换为像素坐标

      以下是绘制始终有效的圆圈的解决方案:

      private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
      {
          RectangleF ipp = InnerPlotPositionClientRectangle(chart1, chart1.ChartAreas[0]);
          using (Graphics g = chart1.CreateGraphics())
              g.DrawEllipse(Pens.Red, new RectangleF(ipp.Location, ipp.Size));
      }
      

      它使用了两个辅助函数:

      RectangleF ChartAreaClientRectangle(Chart chart, ChartArea CA)
      {
          RectangleF CAR = CA.Position.ToRectangleF();
          float pw = chart.ClientSize.Width / 100f;
          float ph = chart.ClientSize.Height / 100f;
          return new RectangleF(pw * CAR.X, ph * CAR.Y, pw * CAR.Width, ph * CAR.Height);
      }
      
      RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea CA)
      {
          RectangleF IPP = CA.InnerPlotPosition.ToRectangleF();
          RectangleF CArp = ChartAreaClientRectangle(chart, CA);
      
          float pw = CArp.Width / 100f;
          float ph = CArp.Height / 100f;
      
          return new RectangleF(CArp.X + pw * IPP.X, CArp.Y + ph * IPP.Y,
                                  pw * IPP.Width, ph * IPP.Height);
      }
      

      And here is a link to a general coordinate transformation function for polar charts.

      【讨论】:

        【解决方案3】:

        求比例? 如果线是 100x100 单位是伪的,那么当转换为 200x200 像素时,您只需要使用比率 2.0 如果坐标为 200x100 并且物理上为 100x100 X 的比率为 2.0 而 Y 的比率为 1.0 当我做 SVG 时,我必须将 0,0(左上角)偏移到笛卡尔坐标(中心的 0,0),方法是全部偏移 +1000 http://en.wikipedia.org/wiki/Cartesian_coordinate_system

        【讨论】:

        • 谢谢。 MSChart 中真的没有一些 ChartCoordsToPixels 函数吗?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 2015-01-17
        • 2021-08-22
        • 1970-01-01
        • 2013-12-16
        • 1970-01-01
        相关资源
        最近更新 更多