Pre loader

LabelProvider is not taken into account in SciChartSurface.ExportToFile?

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

1
0

I need to export a load of images from one graph. Export from the View is very long. So, I’ve implemented the export in memory like in this example:
https://support.scichart.com/index.php?/Knowledgebase/Article/View/17213/34/rendering-chart-to-bitmap-in-memory

Export is fast and flexible. One of my axes has a custom label provider, but I see a default axis labels after export. Export from View gives correct label values. Here is the code of the label provider:

public class MyCustomLabelProvider: NumericLabelProvider
{

    public override string FormatLabel(IComparable dataValue)
    {
        return string.Format("{0:" + this.ParentAxis.TextFormatting + "}", 1 / (double)dataValue);
    }

    public override string FormatCursorLabel(IComparable dataValue)
    {
        return string.Format("{0:" + this.ParentAxis.TextFormatting + "}", 1 / (double)dataValue);
    }
}

And a code of export:

    var sciChartSurface = new SciChartSurface();
    ThemeManager.SetTheme(sciChartSurface, "BrightSpark");
    sciChartSurface.YAxes = new AxisCollection()
    {
        new NumericAxis()
        {
            AxisAlignment = AxisAlignment.Left,
            VisibleRangeLimitMode = RangeClipMode.MinMax,
            LabelProvider = new MyCustomLabelProvider(),
            DrawMajorGridLines = false,
            DrawMinorGridLines = false,
            DrawMajorBands = false,
        }
    };
    sciChartSurface.XAxes = new AxisCollection()
    {
        new NumericAxis()
        {
            AxisAlignment = AxisAlignment.Bottom,
            LabelProvider = new CurvatureLabelProvider(),
            VisibleRangeLimitMode = RangeClipMode.MinMax,
            DrawMajorGridLines = false,
            DrawMinorGridLines = false,
            DrawMajorBands = false
        }
    };

    sciChartSurface.RenderableSeries = new ObservableCollection<IRenderableSeries>()
    {
        new FastLineRenderableSeries() { DataSeries = lineSeries, Stroke = Colors.Green }
    };

    ...

    sciChartSurface.XAxes.FirstOrDefault().VisibleRange = new DoubleRange(0, 100);
    sciChartSurface.ExportToFile(Path.Combine(folderPath, $"Object.png"), ExportType.Png, false, new Size(1500, 750));

Any suggestions?

Version
5.3
  • You must to post comments
0
0

Any help? It is an urgent task for me.

I’ve attached a test project to demonstrate it

Attachments
  • You must to post comments
0
0

Hi Alexander,

Thanks for your question.
The Export feature relies on serialization to create a copy of a SciChartSurface. Some properties cannot be serialized because of the way serialization works. For example, properties of collection and interface types cannot be processed.

So such properties need to be set manually. For this purpose, SciChartSurface exposes the CreateCloneOfSurfaceInMemory(Size) method. You have to override it and reapply LabelProvider to a copy of the surface that is to be exported. It can be obtained from the base implementation:

public class SciChartSurfaceEx : SciChartSurface
{
    protected override SciChartSurfaceBase CreateCloneOfSurfaceInMemory(Size newSize)
    {
        var clonedSurface = (SciChartSurface)base.CreateCloneOfSurfaceInMemory(newSize);

        // Apply custom LabelProvider here

        return clonedSurface;
    }
}

Hope this helps.

  • Alexander Erkabaev
    Hi, thanks for your reply, but it throws an exception “An error occurred when using serialization to clone a chart for export to file. Please check the inner exception for details.” on CreateCloneOfSurfaceInMemory
  • You must to post comments
0
0

I had this issue, but Alexander’s suggestion works fine in SciChart 6.

I created a new UserControl and changed the parent class to SciChartSurface. Then it’s easy to re-apply the label providers automatically:

    protected override SciChartSurfaceBase CreateCloneOfSurfaceInMemory(System.Windows.Size newSize)
    {
        var clonedSurface = (SciChartSurface)base.CreateCloneOfSurfaceInMemory(newSize);

        if (XAxes != null)
        {
            int numAxis = clonedSurface.XAxes.Count;
            if (numAxis == XAxes.Count)
            {
                for (int i = 0; i < numAxis; i++)
                {
                    clonedSurface.XAxes[i].LabelProvider = XAxes[i].LabelProvider;
                }
            }
        }

        if (YAxes != null)
        {
            int numAxis = clonedSurface.YAxes.Count;
            if (numAxis == YAxes.Count)
            {
                for (int i = 0; i < numAxis; i++)
                {
                    clonedSurface.YAxes[i].LabelProvider = YAxes[i].LabelProvider;
                }
            }
        }

        return clonedSurface;
    }
  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.

Try SciChart Today

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

Start TrialCase Studies