how can i improve these code's display efficient?
-
hi,all: This question has been bothering me for a long time. I have some series in chart1, for exampe,i have 100 series, each series have 1 point(or more). And i need paint a rectangle when mouse move, so i have code as follows, but when i run it, the rectangle can not move it smoothly, it is so slow . And i found if series number is less than 10, it maybe move fast. how can i solve it,i have no idea. public Form1() { InitializeComponent(); Series series1; for (int i = 0; i < 100; i++) { series1 = new Series(); series1.Points.Add(new DataPoint(i, i)); //Just for example series1.IsVisibleInLegend = false; chart1.Series.Add(series1); } } bool _isdown = false; Rectangle r=new Rectangle(); private void chart1_MouseDown(object sender, MouseEventArgs e) { _isdown = true; } private void chart1_MouseMove(object sender, MouseEventArgs e) { if (_isdown) { r.Width = 100; r.X = e.X; r.Y = e.Y; r.Height = 100; panel1.Invalidate(); } } private void chart1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.LightBlue); p.Width = 2; p.Color = Color.Red; g.DrawRectangle(p, r); }
-
hi,all: This question has been bothering me for a long time. I have some series in chart1, for exampe,i have 100 series, each series have 1 point(or more). And i need paint a rectangle when mouse move, so i have code as follows, but when i run it, the rectangle can not move it smoothly, it is so slow . And i found if series number is less than 10, it maybe move fast. how can i solve it,i have no idea. public Form1() { InitializeComponent(); Series series1; for (int i = 0; i < 100; i++) { series1 = new Series(); series1.Points.Add(new DataPoint(i, i)); //Just for example series1.IsVisibleInLegend = false; chart1.Series.Add(series1); } } bool _isdown = false; Rectangle r=new Rectangle(); private void chart1_MouseDown(object sender, MouseEventArgs e) { _isdown = true; } private void chart1_MouseMove(object sender, MouseEventArgs e) { if (_isdown) { r.Width = 100; r.X = e.X; r.Y = e.Y; r.Height = 100; panel1.Invalidate(); } } private void chart1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen p = new Pen(Color.LightBlue); p.Width = 2; p.Color = Color.Red; g.DrawRectangle(p, r); }
First, your code leaks resources at an insane rate. Every time the Paint event is called you're creating a new Pen and not Disposing it. This will eventually cause Windows to crash. Create the Pen once, outside the event handler and reuse it. Dispose it when your application closes. Next, this runs slow because you're drawing 100 series on a chart! It wasn't really designed to do that and isn't optimized to handle that kind of a load. Every time you move the mouse with the button down, the chart has to be redrawn to "unpaint" the rectangle that your painting and redraw the rectangle in its new position and dimensions. To make it worse, you only have one data point per series! The more points you add to search series the exponentially slower the painting will become. The only solution you have is to not draw so many chart series.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
First, your code leaks resources at an insane rate. Every time the Paint event is called you're creating a new Pen and not Disposing it. This will eventually cause Windows to crash. Create the Pen once, outside the event handler and reuse it. Dispose it when your application closes. Next, this runs slow because you're drawing 100 series on a chart! It wasn't really designed to do that and isn't optimized to handle that kind of a load. Every time you move the mouse with the button down, the chart has to be redrawn to "unpaint" the rectangle that your painting and redraw the rectangle in its new position and dimensions. To make it worse, you only have one data point per series! The more points you add to search series the exponentially slower the painting will become. The only solution you have is to not draw so many chart series.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiakactually ,i need plot 20 series with 400 points, but when i plot them in the chartarea, it's also slow when mouse move. But if i put 20*400 points in one series, the mouse move is fast. But my requirement is 20 line-series, so i don't want one series's end point line to the other's begin point.
-
actually ,i need plot 20 series with 400 points, but when i plot them in the chartarea, it's also slow when mouse move. But if i put 20*400 points in one series, the mouse move is fast. But my requirement is 20 line-series, so i don't want one series's end point line to the other's begin point.
The only way to speed things up is to reduce the number of points you have to draw in the graph. 20 * 400 = 8000 points. Now, the question is why are you drawing boxes on the chart?
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
The only way to speed things up is to reduce the number of points you have to draw in the graph. 20 * 400 = 8000 points. Now, the question is why are you drawing boxes on the chart?
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
The only way to speed things up is to reduce the number of points you have to draw in the graph. 20 * 400 = 8000 points. Now, the question is why are you drawing boxes on the chart?
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave KreskowiakWhen i plot in the chart, i found chart speed is slower than other soft. Now i think chartarea refresh is cost most of time, but some times i don't need to refresh background series, just need to use invalidate to call paint function. So is there any method to stop backgound series refresh ,not refresh paint function content?
-
When i plot in the chart, i found chart speed is slower than other soft. Now i think chartarea refresh is cost most of time, but some times i don't need to refresh background series, just need to use invalidate to call paint function. So is there any method to stop backgound series refresh ,not refresh paint function content?
No, there isn't, and YES, you HAVE TO PAINT THE BACKGROUND, which means painting the entire chart area. It's the only way to "undo" the previous painting of your rectangle so it can be repainted with it's new coordinates and size.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
No, there isn't, and YES, you HAVE TO PAINT THE BACKGROUND, which means painting the entire chart area. It's the only way to "undo" the previous painting of your rectangle so it can be repainted with it's new coordinates and size.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
oh, but why some other soft can work so fast? Maybe other language can good work for this?
The language used doesn't matter at all. What does matter is the design of your graphing component, which you didn't write. You're trying to adapt a graphing component that isn't designed to do the job you want to do. If you make your own graphing component that also supports drawing boxes on it and is OPTIMIZED to support what you want to do, then you can get the speed out of it.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
The language used doesn't matter at all. What does matter is the design of your graphing component, which you didn't write. You're trying to adapt a graphing component that isn't designed to do the job you want to do. If you make your own graphing component that also supports drawing boxes on it and is OPTIMIZED to support what you want to do, then you can get the speed out of it.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak