无法在我的c#图表上创建行

时间:2014-12-24 14:17:16

标签: c# winforms

我没有错误,但它没有创建点上线。我在图表上有4个点,但没有线。

    private void button4_Click(object sender, EventArgs e)
    {
        int x1 = Convert.ToInt16(textBox1.Text);
        int x2 = Convert.ToInt16(textBox2.Text);
        int x3 = Convert.ToInt16(textBox3.Text);
        int x4 = Convert.ToInt16(textBox4.Text);

        int y1 = Convert.ToInt16(textBox1.Text);
        int y2 = Convert.ToInt16(textBox2.Text);
        int y3 = Convert.ToInt16(textBox3.Text);
        int y4 = Convert.ToInt16(textBox4.Text);
        x1 = 15; x2 = 19; x3 = 24; x4 = 29;
        this.chart1.Series["HR"].Points.AddXY(x1, y1);
        this.chart1.Series["HR"].Points.AddXY(x2, y2);
        this.chart1.Series["HR"].Points.AddXY(x3, y3);
        this.chart1.Series["HR"].Points.AddXY(x4, y4);
    }

    public void DrawlinePoint(PaintEventArgs e)
    { 
        //Create pen.
        Pen blackPen = new Pen(Color.Black,3);
        // Create coordinates of points that define line.
        int x1 = 11;
        int y1 = 0;
        int x4 = 37;
        int y4 = 220;
        //DrawLine to screen.
        e.Graphics.DrawLine(blackPen, x1,y1,x4, y4);
    }

1 个答案:

答案 0 :(得分:0)

从哪里调用DrawlinePoint方法?从PointEventArgs参数判断,您必须在图表的Paint事件中执行此操作。您是否在图表Paint事件中添加了事件处理程序?如果不是,那么您可以在表单的构造函数中添加它:

public Form1()
{
    InitializeComponent();
    this.chart1.Paint += (sender, args) => this.DrawlinePoint(args);
}

(作为旁注,如果您不使用.Net 3.5或更高版本,则必须将lambda更改为标准事件处理程序。)