简体   繁体   中英

C# desktop chart draw complete takes long time

I am creating C# chart using button click.

    void myButton_Click(object sender, RoutedEventArgs e)
    {
       DrawChart();
       MessageBox.Show("Draw complated");
    }

    private void DrawChart()
    {           
        for (int i = 0; i < 30000; i++)
        {               
            Series series = this.chart1.Series.Add(seriesArray[i]);
            series.Points.Add(pointsArray[i]);
        }
    }

I have about 30000 points. So when I click the button, the message box shows immediately, but the graphic is drawn after 5-10 seconds. The users gets the message box before but there is no chart on chart area about 10 seconds.

How can I solve this problem?

Try using Chart event.

public partial class Form1 : Form
    {
        private int _pointsCount;

        public Form1()
        {
            InitializeComponent();
        }

        private void Draw()
        {
            _pointsCount = 300000;
            var range = Enumerable.Range(0, _pointsCount);
            Series series = new Series();

            foreach (var i in range)
            {
                series.Points.Add(new DataPoint(0, i));
            }

            chart1.PostPaint += OnDrawingFinished;
            chart1.Series.Add(series);
        }

        private void OnDrawingFinished(object sender, ChartPaintEventArgs e)
        {
            var chart = (Chart)sender;
            var points = chart.Series.SelectMany(x => x.Points).Count();

            if (points < _pointsCount) return;

            MessageBox.Show("Done!");
            chart1.PostPaint -= OnDrawingFinished;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Draw();
        }
    }

Wokrs here for me for first try. Its not so elegant way but still works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM