我正在通过单击按钮创建C#图表。
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]);
}
}
我大约有30000点。因此,当我单击按钮时,将立即显示消息框,但是图形将在5-10秒后绘制。用户之前会收到消息框,但是大约10秒钟左右的图表区域中没有图表。
我该如何解决这个问题?
答案 0 :(得分:1)
尝试使用Chart事件。
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在这里为我第一次尝试。它不是那么优雅,但是仍然可以工作。