【问题标题】:Chart: Vertical line and string on X label [closed]图表:X 标签上的垂直线和字符串 [关闭]
【发布时间】:2015-01-05 20:29:21
【问题描述】:

我正在使用 WinForms 创建折线图。我有两个相关的问题:

  1. 如何在图表上画一条垂直线?

    我尝试在 (0,0) 和 (0,5) 的两点之间画一条垂直线:

    chart1.Series["Pakistan"].Points.AddXY(0, 0);
    chart1.Series["Pakistan"].Points.AddXY(0, 5);
    

    但是,我得到一条从 (0,0) 到 (1,5) 的歪斜线。

  2. 如何在 X 轴上用字符串标记这条线?

【问题讨论】:

  • .NET 没有内置图表控件。因此,您要么自己编写,要么使用第三方控件。无论哪种情况,如果您想得到答案,您都需要在问题中提供 很多 更多详细信息。见stackoverflow.com/help/mcvestackoverflow.com/help/how-to-ask
  • 抱歉我的问题,但我认为这很清楚......我想使用 WinForms 绘制一个从例如点 (0,0) 到点 (0,5) 的垂直线的折线图.但是当我想画一条线时,图表上有一条从 (1,0) 到 (2, 5) 的线,而不是从 (0,0) 到 (0,5) 的线。所以我不能使用 WinForms Toolbox 中的图表来做到这一点?
  • @PeterDuniho,有一个.Net Chart Control by Microsoft。可能是 OP 的机器已经安装了它,所以他们不知道它是一个单独的包。
  • @PeterDuniho 图表控件包含在 .Net 4.0 及更高版本中。
  • 嗯,如果您选择 x=0 以外的任何其他坐标,您可以轻松做到这一点。一个错误imo;见here虽然那里有一个新帖子似乎有所不同,但我看不懂..

标签: c# winforms forms charts linechart


【解决方案1】:

您可以使用StripLine 在 x 轴上显示一条垂直线:

StripLine stripLine = new StripLine();
stripLine.Interval = 0; // only show 1 line
stripLine.Offset = 0; // start it at x=0
stripLine.StripWidth = 1; // the width is 1
// set colors, etc
chart1.ChartAreas["Default"].AxisX.StripLines.Add(stripLine);

您还可以创建第二个系列RangeColumn,它在每个 x 值处接收 2 个 y 值,以创建垂直线:

Series lineSeries = chart1.Series.Add("line");
lineSeries.ChartType = RangeColumn;
lineSeries.Points.AddXY(0, new []{0, 1});
// Set line widths, colors, etc

最后,您可以处理ChartPostPaint 事件以使用`Graphics 对象绘制一条线:

private void chart1_PostPaint(object sender, ChartPaintEventArgs e)
{
  if(sender is ChartArea)
  {

    ChartArea area = (ChartArea)sender;
    if(area.Name == "Default")
    {
        // Get Graphics object from chart
        Graphics graph = e.ChartGraphics.Graphics;

        // Convert X and Y values to screen position
        float pixelYMax = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.Y,1);
        float pixelXMax = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.X,0);
        float pixelYMin = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.Y,0);
        float pixelXMin = (float)e.ChartGraphics.GetPositionFromAxis("Default",AxisName.X,0);

        PointF point1 = PointF.Empty;
        PointF point2 = PointF.Empty;

        // Set Maximum and minimum points
        point1.X = pixelXMax;
        point1.Y = pixelYMax;
        point2.X = pixelXMin;
        point2.Y = pixelYMin;

        // Convert relative coordinates to absolute coordinates.
        point1 = e.ChartGraphics.GetAbsolutePoint(point1);
        point2 = e.ChartGraphics.GetAbsolutePoint(point2);

        // Draw connection line
        graph.DrawLine(new Pen(Color.Yellow,3), point1,point2);
    }
  }
}

处理PostPaint 可能是最好的选择,并且可以让您更好地控制您的线路及其外观。

【讨论】:

  • 或者 LineAnnotation..?
【解决方案2】:

第一季度。 :在我看来,您在Chart 控件中遇到了一个错误。这是一个解决方法

选择 ChartType 'Line' 和合适的线宽:

 chart1.Series["Pakistan"].ChartType = SeriesChartType.Line;
 chart1.Series["Pakistan"].BorderWidth = 2;

在真实数据之前插入这个虚拟点:

chart1.Series["Pakistan"].Points.AddXY(-1, 0);

现在隐藏第一个实点之前的线段:

chart1.Series["Pakistan"].Points[1].Color = Color.Transparent;

您可能希望将显示窗口设置为排除虚拟对象..

Axis XA = chart1.ChartAreas[0].AxisX;
XA.Minimum = -1;
XA.Maximum = yourMaximum;

(直到有人解释它,我认为这种行为是一个错误,只发生在位置 X=0..)

第二季度。 : 要将字符串作为标签添加到数据点,请使用如下代码。

chart1.Series["Pakistan"].Points[1].AxisLabel = "Label 1";

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 2011-04-02
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2017-08-24
    相关资源
    最近更新 更多