Steema Issues Database

Note: This database is for bugs and wishes only. For technical support help, if you are a customer please visit our online forums;
otherwise you can use StackOverflow.
Before using this bug-tracker we recommend a look at this document, Steema Bug Fixing Policy.



Bug 1215 - Improve ColorGrid performance
Summary: Improve ColorGrid performance
Status: CONFIRMED
Alias: None
Product: .NET TeeChart
Classification: Unclassified
Component: Series (show other bugs)
Version: unspecified
Hardware: PC Windows
: --- enhancement
Target Milestone: ---
Assignee: Steema Issue Manager
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-20 05:38 EDT by christopher ireland
Modified: 2015-05-20 05:38 EDT (History)
0 users

See Also:
Chart Series: ---
Delphi / C++ Builder RAD IDE Version:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description christopher ireland 2015-05-20 05:38:36 EDT
The following code takes over six seconds to draw; improvements to this performance are required:

		private Stopwatch watch = new Stopwatch();
		private ColorGrid _colorGrid;
		private LegendPalette _legendPalette;
		private Color[] _rainbowLowBlackPalette;

		private double _colorBarMax;
		private double _colorBarMin;
		private double _colorBarStep;

		private double[] _xData;
		private double[] _yData;
		private double[] _zData;


		private void InitializeChart()
		{
			_colorBarMax = 0.0;
			_colorBarMin = -50.0;
			_colorBarStep = 5.0;

			watch.Reset();
			watch.Start();

			tChart1.AfterDraw += TChart1_AfterDraw;

			SetupColorGrid();
			GenerateData();

			_colorGrid.Add(_xData, _zData, _yData);

      _colorGrid.IrregularGrid = true;
			_legendPalette.Series = _colorGrid;
			_legendPalette.Gradient.Visible = false;
		}

		private void TChart1_AfterDraw(object sender, Graphics3D g)
		{
			watch.Stop();
			label1.Text = watch.ElapsedMilliseconds.ToString();
		}

		public void SetupColorGrid()
		{
			this.SuspendLayout();
			this.tChart1.Aspect.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
			this.tChart1.Aspect.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
			this.tChart1.Aspect.View3D = false;


			tChart1.Header.Visible = false;
			tChart1.Legend.Visible = false;
			tChart1.Panel.MarginUnits = Steema.TeeChart.PanelMarginUnits.Pixels;
			tChart1.Panel.MarginRight = 80;

			tChart1.Axes.Bottom.Automatic = false;
			tChart1.Axes.Bottom.AutomaticMaximum = false;
			tChart1.Axes.Bottom.AutomaticMinimum = false;
			tChart1.Axes.Bottom.Increment = 0.2;
			tChart1.Axes.Bottom.Maximum = 1.0;
			tChart1.Axes.Bottom.Minimum = -1.0;

			tChart1.Axes.Left.Automatic = false;
			tChart1.Axes.Left.AutomaticMaximum = false;
			tChart1.Axes.Left.AutomaticMinimum = false;
			tChart1.Axes.Left.Increment = 0.2;
			tChart1.Axes.Left.Maximum = 1.0;
			tChart1.Axes.Left.Minimum = -1.0;

			this._colorGrid = new ColorGrid();
			this._colorGrid.Pen.Visible = false;
			this._colorGrid.Title = "colorGrid1";
			this._colorGrid.UseColorRange = false;
			this._colorGrid.UsePalette = true;
			this._colorGrid.XStep = 1;

			this.tChart1.Series.Add(this._colorGrid);

			LoadColorPalette();
			_legendPalette = new LegendPalette(tChart1.Chart);

			_legendPalette.Axis = LegendPaletteAxis.laOther;
			_legendPalette.Width = 60;
			_legendPalette.Pen.Visible = false;
			_legendPalette.Series = _colorGrid;

			this.ResumeLayout(false);

			tChart1.SizeChanged += new System.EventHandler(this.Chart_SizeChanged);


		}

		private void Chart_SizeChanged(object sender, EventArgs e)
		{
			watch.Reset();
			watch.Start();
			tChart1.AutoRepaint = false;
			_legendPalette.Left = tChart1.Width - _legendPalette.Width - 10;
			_legendPalette.Top = (tChart1.Height - _legendPalette.Height) / 2;
			tChart1.AutoRepaint = true;
			tChart1.Refresh();
		}

		private void LoadColorPalette()
		{
			double minValue = _colorBarMin;
			double maxValue = _colorBarMax;
			double delta = _colorBarMax - _colorBarMin;

			_colorGrid.ClearPalette();
			_rainbowLowBlackPalette = new Color[256];
			int redIndex;
			int greenIndex;
			int blueIndex;
			for (int i = 0; i < 256; i++)
			{
				if (i == 0)
				{
					redIndex = 0;
					greenIndex = 0;
					blueIndex = 0;
				}
				else if (i > 0 && i <= 32)
				{
					redIndex = -255 / 32 * i + 255;
					greenIndex = 0;
					blueIndex = 255;
				}
				else if (i > 32 && i <= 85)
				{
					redIndex = 0;
					greenIndex = (255 / (85 - 32)) * (i - 32);
					blueIndex = 255;

				}
				else if (i > 85 && i <= 143)
				{
					redIndex = 0;
					greenIndex = 255;
					blueIndex = (-255 / (143 - 86)) * (i - 143);
				}
				else if (i > 143 && i <= 199)
				{
					redIndex = (255 / (199 - 143)) * (i - 143);
					greenIndex = 255;
					blueIndex = 0;
				}
				else
				{
					redIndex = 255;
					greenIndex = (-255 / (255 - 199)) * (i - 255);
					blueIndex = 0;
				}
				_rainbowLowBlackPalette[i] = Color.FromArgb(redIndex, greenIndex, blueIndex);

				_colorGrid.AddPalette(((delta / 255.0) * (double)i + minValue), _rainbowLowBlackPalette[i]);
			}
		}

		private void GenerateData()
		{
			var Nx = 50;
			var Ny = 25;
			var Nu = 1001;
			var Nv = 1001;

			_xData = new double[Nu * Nv];
			_yData = new double[Nu * Nv];
			_zData = new double[Nu * Nv];

			var du = 2.0 / (Nu - 1);
			var dv = 2.0 / (Nv - 1);

			int k = 0;
			for (int i = 0; i < Nu; i++)
			{
				var v = -1.0 + dv * i;
				for (int j = 0; j < Nv; j++)
				{
					var u = -1.0 + du * j;
					var argx = 0.5 * Nx * u;
					var argy = 0.5 * Ny * v;

					_xData[k] = u;
					_yData[k] = v;
					_zData[k] = 20.0 * Math.Log10(Math.Abs(sinc(argx) * sinc(argy)));
					k++;
				}
			}
		}

		private double sinc(double x)
		{
			if (Math.Abs(x) < 1.0e-7)
			{
				return 1.0;
			}
			else
			{
				return (Math.Sin(Math.PI * x) / (Math.PI * x));
			}
		}