Pre loader

How to render axes to bitmap in code

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
0

I’m trying to render the SciChartSurface control to a BitmapSource based on the tutorial  http://http://www.scichart.com/screenshots-xps-printing-x-axis-text-labels/ .

The example uses a View to start the rendering from but this is something I wish to prevent, doing everything in C# without any use of XAML.

I got to the point that the chart is rendered properly with the exception of the X and Y-Axis. I also get the error message ‘SciChartSurface didn’t render, Because the SciChartSurface Viewport Size is not valid (e.g. 0 sized). ‘ twice, most likely both originating from the rendering of each axis.

I got the following code so far:

var chart = new SciChartSurface
{
    Background = new SolidColorBrush(Colors.White),
    Foreground = new SolidColorBrush(Colors.Black),

    XAxis = new NumericAxis()
    {
        AutoRange = AutoRange.Never,
        AxisTitle = "axis X",
        Background = new SolidColorBrush(Colors.White),
        TickTextBrush = new SolidColorBrush(Colors.Black),
        GrowBy = new DoubleRange(0.025, 0.025),
        VisibleRange = new DoubleRange(-1, 6),
    },

    YAxis = new NumericAxis()
    {
        AutoRange = AutoRange.Never,
        AxisTitle = "axis Y",
        Background = new SolidColorBrush(Colors.White),
        TickTextBrush = new SolidColorBrush(Colors.Black),
        GrowBy = new DoubleRange(0.025, 0.025),
        VisibleRange = new DoubleRange(0, 15),
    },

    Width = requestedSize.Width,
    Height = requestedSize.Height,

    // RenderPriority.Immediate is required to print or render to bitmap any SciChartSurface that has not been 
    // added to the Visual Tree. This ensures that re-draw events are processed synchronously.
    RenderPriority = RenderPriority.Immediate,
};

// ApplyTemplate is required on the newly created chart to ensure the theme is applied
chart.ApplyTemplate();

// Force the Loaded event for the SciChartSurface
chart.OnLoad();

chart.SeriesSource = new ObservableCollection();
var data = new XyDataSeries<int, int>();
data.Append(0, 10);
data.Append(1, 5);
data.Append(2, 9);
data.Append(3, 12);
data.Append(4, 2);
data.Append(5, 6);
chart.SeriesSource.Add(new ChartSeriesViewModel(data, new FastColumnRenderableSeries
{
    FillBrush = new SolidColorBrush(Colors.Gray),
}));

chart.Measure(new Size(chart.Width, chart.Height));
chart.Arrange(new Rect(new Point(0, 0), chart.DesiredSize));
chart.UpdateLayout();

// Trigger a redraw
chart.InvalidateElement();

// Force the labels of the axes to be correctly placed
new UserControl { Content = chart }.UpdateLayout();

This renders everything from the gridlines, columns, axis titles and minor/major-ticks as expected. However the labels on the axis are all printed in the left-bottom (x-axis) and top-right (y-axis) corners instead of uniformly divided over the axis.

My conclusion therefore is that the Viewport is crucial to the axis label placement but I’m not aware on how to set this correctly in code. Any suggestions on how to do so would be appreciated.

  • You must to post comments
0
0

Hi there,

We had similar issue reported earlier, it occurred when tried to print chart on paper. Please, take a look at this post, where workaround was suggested which resolved the issue.

Please, let us know if this helps,

Best regards,
Yuriy

  • dwight.berendse
    Hi Yuriy, Thank you for the quick reply. Your example gave me the idea that the chart on its own has trouble determining the its size for the axes. I failed to see how the solution example in the referenced post could help in this situation. However, I could solve the issue by encapsulating the chart control in a UserControl and calling the UpdateLayout() method on it. I've updated the code in the code example so others may enjoy the solution as well.
  • Olya
    Hi, Great idea! Glad you sorted it out! And many thanks for sharing it with others! If in doubt, please feel free to ask – we’re always here to help or at least direct a bit :wink: Best regards, - Olya
  • You must to post comments
0
0

Update: June 2014

We have gone through all the tutorials on Screenshots, Printing to XPS, Export to Bitmap and updated them. Please see the updated links below below.

The Screenshots, XPS Printing, X-Axis Text Labels article now has an updated sample which compiles against SciChart v3.0 so you can download it and get started.

Hope this helps!
Andrew

  • You must to post comments
Showing 2 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