Pre loader

Tag: Optimization

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

0 votes
0 answers
10k views

Have a good day!

We are working with scichart render context to create 2d sprite. We have already optimized our code, but there is still problem.
When we put annotation, where we bind our image, outside the function, the image stops rendering. We’ve made that you had told us to do in previous post
( https://www.scichart.com/questions/question/memory-usage-part-2 ), but it doesn’t make a sense. We’ve found out that when we create
this annotation inside the function and when we pan the image it takes too many memory resources. It is caused by creating many annotations of this type
and therefore there is queue to the garbage collector. That’s why our goal is to put this annotation outside the function and just change image that is binded
to the annotation. Is there any way to solve it?

Code snippets:

Annotation outside the function (image shows, but doesn't render, it takes less memory)

        BackgroundImageAnnotation backgroundImage = null;

        private void OnRedrawWellSection(object sender, IRenderContext2D e)
        {
            e.Clear();
            e.SetPrimitvesCachingEnabled(false);
            double density;                        
            double density1;                        
            var minThltest = (double)SciChartSurface.XAxis.VisibleRange.Min;
            var maxThlTest = (double)SciChartSurface.XAxis.VisibleRange.Max;
            var minTvdTest = -2000;
            var maxTvdTest = -700;
            var xCalc = SciChartSurface.XAxis.GetCurrentCoordinateCalculator();
            var yCalc = SciChartSurface.YAxis.GetCurrentCoordinateCalculator();

            density = SciChartSurface.GridLinesPanel.Height/(double) SciChartSurface.YAxis.VisibleRange.Diff;

            density1 = SciChartSurface.GridLinesPanel.Width/(double)SciChartSurface.XAxis.VisibleRange.Diff;

            using (SciChartSurface.SuspendUpdates())
            {
                if (backgroundImage == null)
                {
                    backgroundImage = new BackgroundImageAnnotation
                    {
                        CoordinateMode = AnnotationCoordinateMode.Relative,
                        X1 = 0,
                        Y1 = 0,
                        AnnotationCanvas = AnnotationCanvas.BelowChart,
                    };
                }

                var mainWindow = Application.Current.MainWindow;
                var source = PresentationSource.FromVisual(mainWindow);
                if (source == null || source.CompositionTarget == null) return;

                var width = (int)SciChartSurface.ActualWidth;
                var height = (int)SciChartSurface.ActualHeight;
                if ((int)SciChartSurface.ActualWidth == 0)
                    width = 1;
                if ((int)SciChartSurface.ActualHeight == 0)
                    height = 1;
                WriteableBitmap writeableBmp = BitmapFactory.New(width, height);
                var color = Colors.Blue;
                for (double i = minTvdTest; i < maxTvdTest; i=i+density)
                {
                    var t1 = (int) xCalc.GetCoordinate(minThltest);
                    var t2 = (int) yCalc.GetCoordinate(i);
                    var t3 = (int) xCalc.GetCoordinate(maxThlTest);
                    var t4 = (int) yCalc.GetCoordinate(i);
                    if (i<-1500)
                    {
                        color = Colors.Green;
                    }
                    else
                    {
                        if (i<-1000)
                        {
                            color = Colors.Red;
                        }
                        else
                        {
                            color = Colors.Orange;
                        }
                    }
                    writeableBmp.DrawLineBresenham(t1, t2, t3, t4, color);
                }
                backgroundImage.ImageSource = writeableBmp;
                var sprite = e.CreateSprite(backgroundImage);
                e.DrawSprite(sprite, new Rect(0, 0, width, height), new Point(0, 0));
                e.Layers.Flush();
                writeableBmp = null;
                backgroundImage.ImageSource = null;
                e.DisposeResourceAfterDraw(sprite);
            }
        }

Annotation inside the function (image shows, renders, but when we pan, it takes many resources and it doesn’t invoke destructor at once)

// We put the declaration of annotation in the function scope
// BackgroundImageAnnotation backgroundImage = null;

private void OnRedrawWellSection(object sender, IRenderContext2D e)
{
    e.Clear();
    e.SetPrimitvesCachingEnabled(false);
    double density;                        
    double density1;                        
    var minThltest = (double)SciChartSurface.XAxis.VisibleRange.Min;
    var maxThlTest = (double)SciChartSurface.XAxis.VisibleRange.Max;

    ...

    using (SciChartSurface.SuspendUpdates())
    {

        BackgroundImageAnnotation backgroundImage = null;
        if (backgroundImage == null)
        {
            backgroundImage = new BackgroundImageAnnotation
            {
                CoordinateMode = AnnotationCoordinateMode.Relative,
                X1 = 0,
                Y1 = 0,
                AnnotationCanvas = AnnotationCanvas.BelowChart,
            };
        }

        var mainWindow = Application.Current.MainWindow;
        ....
  • Egor asked 8 years ago
Showing 1 result

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies