Beispiel #1
0
        /// <summary>
        /// Returning a brush based on a value
        /// </summary>
        /// <param name="z">Actual value</param>
        /// <param name="zmin">Minimum</param>
        /// <param name="zmax">Maximum</param>
        /// <param name="cm">A colormap object</param>
        /// <returns></returns>
        public static SolidColorBrush GetBrush(double z, double zmin, double zmax, ColormapBrush cm, double opacity = 1)
        {
            SolidColorBrush brush = new SolidColorBrush();

            int colorIndex = (int)(((cm.Ydivisions - 1) * (z - zmin) + zmax - z) / (zmax - zmin));

            if (colorIndex < 0)
            {
                colorIndex = 0;
            }

            if (colorIndex >= cm.Ydivisions)
            {
                colorIndex = cm.Ydivisions - 1;
            }

            if (cm.ColormapBrushes == null || cm.ColormapBrushes.Count == 0)
            {
                cm.CalculateColormapBrushes(opacity);
            }

            brush = (SolidColorBrush)cm.ColormapBrushes[colorIndex].Brush;

            brush.Freeze();

            return(brush);
        }
        public static Bitmap ToBitmap(this double[,] array, ColormapBrush ColorMap)
        {
            int width  = array.GetLength(1);
            int height = array.GetLength(0);

            System.Drawing.Bitmap             Image      = new System.Drawing.Bitmap(width, height);
            System.Drawing.Imaging.BitmapData bitmapData = Image.LockBits(
                new System.Drawing.Rectangle(0, 0, width, height),
                System.Drawing.Imaging.ImageLockMode.ReadWrite,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb
                );

            //Iterating over height
            for (int j = 0; j < height; j++)
            {
                //Iterating over width
                for (int k = 0; k < width; k++)
                {
                    try
                    {
                        System.Windows.Media.Brush br = ColorMapHelper.GetBrush((double)array[j, k], ColorMap.Ymin, ColorMap.Ymax, ColorMap);
                        byte a = ((Color)br.GetValue(System.Windows.Media.SolidColorBrush.ColorProperty)).A;
                        byte g = ((Color)br.GetValue(System.Windows.Media.SolidColorBrush.ColorProperty)).G;
                        byte r = ((Color)br.GetValue(System.Windows.Media.SolidColorBrush.ColorProperty)).R;
                        byte b = ((Color)br.GetValue(System.Windows.Media.SolidColorBrush.ColorProperty)).B;

                        Image.SetPixel(k, j, System.Drawing.Color.FromArgb(a, r, g, b));
                    }
                    catch
                    {
                        continue;
                    }
                }
            }

            return(Image);
        }
        /// <summary>
        /// Add a 3D line to a chart canvas
        /// </summary>
        /// <param name="cs"></param>
        public async Task <List <UIElement> > AddLine3D(LineSeries3D ls, ColormapBrush cm, CancellationToken token)
        {
            List <UIElement> symbols = new List <UIElement>();

            await Application.Current.Dispatcher.InvokeAsync(async() =>
            {
                var pLine = new Polyline();

                Matrix3D m = Chart3DHelper.AzimuthElevation(Elevation, Azimuth);

                Point3D[] pts = new Point3D[ls.LinePoints.Count];

                int stepWidth = 1;

                if (ls.LinePoints.Count > 20000)
                {
                    stepWidth = (ls.LinePoints.Count / 20000);
                }

                for (int i = 0; i < ls.LinePoints.Count; i += stepWidth)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    if (i == 0)
                    {
                        Console.WriteLine("1:" + DateTime.Now);
                    }

                    await Task.Delay(1);

                    if (ls.LinePoints[i].Item1.X <Xmin || ls.LinePoints[i].Item1.X> Xmax ||
                        ls.LinePoints[i].Item1.Y <Ymin || ls.LinePoints[i].Item1.Y> Ymax ||
                        ls.LinePoints[i].Item1.Z <Zmin || ls.LinePoints[i].Item1.Z> Zmax)
                    {
                        continue;
                    }


                    try
                    {
                        if (i == 0)
                        {
                            Console.WriteLine("2:" + DateTime.Now);
                        }

                        pts[i] = Normalize3D(m, ls.LinePoints[i].Item1);

                        pLine.Points.Add(new Point(pts[i].X, pts[i].Y));

                        if (i == 0)
                        {
                            Console.WriteLine("3:" + DateTime.Now);
                        }

                        if (!IsBubbleChart)
                        {
                            if (ls.Symbols.SymbolType != SymbolTypeEnum.None)
                            {
                                ls.Symbols.AddSymbol(ChartCanvas, new Point(pts[i].X, pts[i].Y));
                            }
                        }
                        else if (ls.Symbols.SymbolType != SymbolTypeEnum.None)
                        {
                            ls.Symbols.FillColor = ColorMapHelper.GetBrush(ls.LinePoints[i].Item2, cm.Ymin, cm.Ymax, cm);
                            symbols.AddRange(ls.Symbols.GetSymbol(new Point(pts[i].X, pts[i].Y)));
                        }
                    }
                    catch
                    {
                        continue;
                    }

                    if (i == 0)
                    {
                        Console.WriteLine("4:" + DateTime.Now);
                    }
                }

                if (!IsBubbleChart)
                {
                    SetLinePattern(ls, pLine);
                    symbols.Add(pLine);
                }
            }).Result;

            return(symbols);
        }
        private async Task ResizeChart()
        {
            cts = new CancellationTokenSource();
            var token = cts.Token;

            CommandHelper ch = new CommandHelper();

            await ch.RunBackgroundWorkerWithFlagHelperAsync(() => IsDrawing, async() =>
            {
                //Filtering data based on the selection
                try
                {
                    await this.Dispatcher.InvokeAsync(async() =>
                    {
                        int count = 0;

                        count = chartCanvas.Children.Count;

                        chartCanvas.Children.RemoveRange(0, chartCanvas.Children.Count);

                        cs.SetChartStyle();

                        var dc = new BindableCollection <LineSeries3D>(DataCollection);

                        if (dc != null)
                        {
                            if (dc.Count > 0)
                            {
                                symbols.Clear();
                                ColormapBrush cm = new ColormapBrush(ColorMap);

                                for (int i = 0; i < dc.Count; i++)
                                {
                                    try
                                    {
                                        var symbolCollection = await cs.AddLine3D(dc[i], cm, token).ConfigureAwait(false);
                                        symbols.AddRange(symbolCollection);
                                    }
                                    catch
                                    {
                                    }
                                }

                                foreach (UIElement x in symbols)
                                {
                                    if (!cts.IsCancellationRequested)
                                    {
                                        try
                                        {
                                            await Task.Delay(1);
                                            this.Dispatcher.Invoke(() => chartCanvas.Children.Add(x));
                                        }
                                        catch
                                        {
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        symbols.Clear();
                                        break;
                                    }
                                }

                                this.Dispatcher.Invoke(() =>
                                {
                                    if (IsBubble)
                                    {
                                        lg.AddColorBar(chartCanvas, cm);
                                    }
                                });
                            }
                        }
                    });
                }
                catch
                {
                }
                finally
                {
                }
            });
        }