如何在Visual Studio C#中为图表设置动画?

时间:2016-04-19 13:57:39

标签: c# visual-studio animation charts

有没有办法在Visual Studio 2013中的C#表单应用程序中为图表设置动画,以便在按下按钮后显示逐行绘制到屏幕上的行?在此先感谢:)

1 个答案:

答案 0 :(得分:1)

这是一个应该让你入门的例子。它首先创建测试数据,然后使用Timer显示它们:..

List<PointF> data = new List<PointF>();
Timer timer = new Timer();

private void button1_Click_1(object sender, EventArgs e)
{
    data.Clear();

    for (int i = 0; i < 400; i++)
    {
        float x = i / 50f * (float)( Math.Cos(i / 10f));
        float y = i / 50f * (float)(Math.Sin(i / 10f));
        data.Add(new PointF(x,y));
    }

    chart1.Series.Clear();
    Series S1 = chart1.Series.Add("S1");
    Series S2 = chart1.Series.Add("S2");

    S2.MarkerSize = 2;
    S2.MarkerStyle = MarkerStyle.Circle;
    S2.Color = Color.Green;
    S1.Color = Color.FromArgb(64, Color.Red);
    S1.BorderWidth = 9;

    S2.ChartType = SeriesChartType.Point;
    S1.ChartType = SeriesChartType.Line;
    chart1.ChartAreas[0].AxisX.Minimum = -10;
    chart1.ChartAreas[0].AxisX.Maximum = 10;
    chart1.ChartAreas[0].AxisY.Minimum = -10;
    chart1.ChartAreas[0].AxisY.Maximum = 10;
    chart1.ChartAreas[0].BackColor = Color.White;

    timer.Interval = 15;
    timer.Start();
}

请注意,我使用PointF结构来存储测试数据,因为它是一个方便的浮点结构。

这是Timer.Tick事件。别忘了hook it up!在这里我们绘制所有数据,直到我们通过;然后我们停止Timer

void timer_Tick(object sender, EventArgs e)
{
    Series S1 = chart1.Series[0];
    Series S2 = chart1.Series[1];
    int pointsSoFar = S1.Points.Count;
    if (pointsSoFar < data.Count)
    {
        S1.Points.AddXY(data[pointsSoFar].X, data[pointsSoFar].Y);
        S2.Points.AddXY(data[pointsSoFar].X, data[pointsSoFar].Y);
    }
    else
    {
        timer.Stop();
        chart1.ChartAreas[0].BackColor = Color.AntiqueWhite;
    }
}

请注意,我选择以半透明颜色绘制线条。观察线段强烈重叠的结果是有益的!

enter image description here

在这里..

..结果..

..动画......:

enter image description here