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?
- You must login to post comments
Any help? It is an urgent task for me.
I’ve attached a test project to demonstrate it
- Alexander Erkabaev answered 5 years ago
- You must login to post comments
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.
- Oleksandr Shvets answered 5 years ago
-
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 login to post comments
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;
}
- Patrik Adolfsson answered 3 years ago
- You must login to post comments
Please login first to submit.