Pre loader

How programatically add legend to chart

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

Answered
1
0

Here is XAML code which works:

                                <!--  Defines the renderable series. Each series may be styled  -->
                                <visuals:SciChartSurface.RenderableSeries>
                                    <visuals:FastLineRenderableSeries SeriesColor="Red" DataSeries="{Binding Measured1StPointsSeries}" IsVisible="{Binding Is1StMeasured}">
                                    </visuals:FastLineRenderableSeries>

                                    <visuals:FastLineRenderableSeries SeriesColor="Green" DataSeries="{Binding Measured2NdPointsSeries}"  IsVisible="{Binding Is2NdMeasured}">
                                    </visuals:FastLineRenderableSeries>

                                    <visuals:FastLineRenderableSeries SeriesColor="Blue" DataSeries="{Binding Measured3RdPointsSeries}"  IsVisible="{Binding Is3RdMeasured}">
                                    </visuals:FastLineRenderableSeries>
                                </visuals:SciChartSurface.RenderableSeries>

                                <!--  Defines the XAxis, using an explicit VisibleRange  -->
                                <visuals:SciChartSurface.XAxis >
                                    <visuals:NumericAxis AxisTitle="RA Pump Current, A" DrawMajorTicks="True" FontSize="30" TitleFontSize="35" Orientation="Horizontal"  MajorDelta="5" MinorDelta="1" AutoTicks="True" AutoRange="Always" VisibleRange="{Binding PowerXRange}">
                                    </visuals:NumericAxis>
                                </visuals:SciChartSurface.XAxis>

                                <!--  Defines the YAxis  -->
                                <visuals:SciChartSurface.YAxis>
                                    <visuals:NumericAxis AxisAlignment="Left" FontSize="30" TitleFontSize="35" AxisTitle="Power, W" MajorDelta="0.2" MinorDelta="0.1" AutoTicks="False" AutoRange="Always" VisibleRange="{Binding PowerYRange}"/>
                                </visuals:SciChartSurface.YAxis>

                                <!--  Declare ChartModifiers  -->
                                <visuals:SciChartSurface.ChartModifier>

                                    <visuals:LegendModifier x:Name="PowerLegendModifier" Background="White" GetLegendDataFor="AllVisibleSeries"/>

                                </visuals:SciChartSurface.ChartModifier>

                            </visuals:SciChartSurface>
                            <visuals:SciChartLegend  x:Name="PowerChartLegend" Foreground="Black" FontSize="28" Background="White"  LegendData="{Binding LegendData, ElementName=PowerLegendModifier, Mode=OneWay}" Margin="125,23,0,0"  />

But I tried to create chart programaticall to render it in memory and export to bitmap, but I can not add legend:

        //Scichart thing

        var series1St = new FastLineRenderableSeries() {
            SeriesColor = Colors.Red,
            DataSeries = Measured1StPointsSeries,
            IsVisible = Is1StMeasured

        };
        var series2Nd = new FastLineRenderableSeries() {
            SeriesColor = Colors.Green,
            DataSeries = Measured2NdPointsSeries
        };
        var series3Rd = new FastLineRenderableSeries() {
            SeriesColor = Colors.Blue,
            DataSeries = Measured3RdPointsSeries
        };


        var xAxes = new AxisCollection() { new NumericAxis() };

        var yAxes = new AxisCollection() { new NumericAxis() };

        //var powerLegendModifier = new LegendModifier() {
        //    Background = Brushes.White,
        //    GetLegendDataFor = SourceMode.AllVisibleSeries

        //};

        var surface = new SciChartSurface() {
            //ChartTitle = "Rendered In Memory",
            XAxes = xAxes,
            YAxes = yAxes,
            RenderableSeries = new ObservableCollection<IRenderableSeries>() { series1St, series2Nd, series3Rd },
            ChartModifier = new LegendModifier() {
                Background = Brushes.White,
                GetLegendDataFor = SourceMode.AllVisibleSeries
            }
        };

Seems like SourceMode.AllVisibleSeries to be the culprit – tries to update non existing data. How could I have chart modifier added with c# direct code?

Images
  • You must to post comments
Good Answer
1
0

Hi Justas,

We’ve investigated it and found out that it happens inside the LegendModifier in an attempt to get data before the modifier gets attached to a surface. So setting “IsEnabled” to “False” and then changing it to “True” just before rendering fixes the issue on your side.

Also please note that this forum is like StackOverflow, so it is highly recommended following the 1-question – many answers model. Here you can find guidelines: http://stackoverflow.com/help/how-to-ask

P.S.: In your sample occurs one more issue after fixing that, during surface unloading. Not sure what causes it, please try and let us know if you can reproduce it either. If so, I think it is worth creating a new thread.

Hope this helps!

  • justas.bukys
    I managed to generate legend. Thank You.
  • Andrew Burnett-Thompson
    I believe Yuriy also pushed a fix to the nightly build, revision 5199. Sorry for the long wait Justas and thanks for being persistent :)
  • You must to post comments
0
0

Hope this code will help you. It should generate chart image and copy it to clipboard.

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using Abt.Controls.SciChart;
using Abt.Controls.SciChart.ChartModifiers;
using Abt.Controls.SciChart.Model.DataSeries;
using Abt.Controls.SciChart.Visuals;
using Abt.Controls.SciChart.Visuals.Axes;
using Abt.Controls.SciChart.Visuals.RenderableSeries;

namespace TestAppForSciChart {
///

/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow {
public MainWindow() {
InitializeComponent();
}

    private void Button_Click(object sender, RoutedEventArgs e) {
        GenerateReport();
    }

    private void GenerateReport() {

        Random rnd = new Random();

        const int measureCount = 25;

        IXyDataSeries<double, double> measured1StPointsSeries = new XyDataSeries<double, double>();
        IXyDataSeries<double, double> measured2NdPointsSeries = new XyDataSeries<double, double>();
        IXyDataSeries<double, double> measured3RdPointsSeries = new XyDataSeries<double, double>();

        measured1StPointsSeries.Clear();
        measured2NdPointsSeries.Clear();
        measured3RdPointsSeries.Clear();
        measured1StPointsSeries.SeriesName = "1St Random set";
        measured2NdPointsSeries.SeriesName = "2Nd Random set";
        measured3RdPointsSeries.SeriesName = "3Rd Random set";

        for (int i = 0; i < measureCount; i++) {
            measured1StPointsSeries.Append(i, i);
            measured2NdPointsSeries.Append(i, i * 2);
            measured3RdPointsSeries.Append(i, i * (-1));
        }


        //Scichart thing

        var series1St = new FastLineRenderableSeries {
            SeriesColor = Colors.Red,
            DataSeries = measured1StPointsSeries,

        };
        var series2Nd = new FastLineRenderableSeries {
            SeriesColor = Colors.Green,
            DataSeries = measured2NdPointsSeries
        };
        var series3Rd = new FastLineRenderableSeries {
            SeriesColor = Colors.Blue,
            DataSeries = measured3RdPointsSeries
        };


        var xAxes = new AxisCollection { new NumericAxis() };

        var yAxes = new AxisCollection { new NumericAxis() };


        var surface = new SciChartSurface {
            //ChartTitle = "Rendered In Memory",
            XAxes = xAxes,
            YAxes = yAxes,
            RenderableSeries = new ObservableCollection<IRenderableSeries> { series1St, series2Nd, series3Rd },
            ChartModifier = new LegendModifier() {
                Background = Brushes.White,
                GetLegendDataFor = SourceMode.AllVisibleSeries
            }
        };




        surface.Background = Brushes.White;
        ThemeManager.SetTheme(surface, "BrightSpark");
        surface.Width = 500;
        surface.Height = 500;
        surface.XAxes[0].AxisTitle = "RA Pump Current, A";
        surface.XAxes[0].DrawMajorGridLines = true;
        const int majorDeltaX = 1;
        surface.XAxes[0].MajorDelta = majorDeltaX;
        surface.XAxes[0].MinorDelta = majorDeltaX;
        surface.XAxes[0].AutoTicks = false;
        var minusModulusAdd = 0;
        var plusModulusAdd = 0;
        surface.XAxes[0].AutoRange = AutoRange.Never;

        if ((Math.Round((double)series1St.DataSeries.XMin / majorDeltaX)) * majorDeltaX > (double)series1St.DataSeries.XMin) {
            minusModulusAdd = -1;
        }

        if ((Math.Round((double)series1St.DataSeries.XMax / majorDeltaX)) * majorDeltaX < (double)series1St.DataSeries.XMax) {
            plusModulusAdd = 1;
        }


        surface.XAxes[0].VisibleRange = new DoubleRange(
            (Math.Round((double)series1St.DataSeries.XMin / majorDeltaX) + minusModulusAdd) * majorDeltaX,
            (Math.Round((double)series1St.DataSeries.XMax / majorDeltaX) + plusModulusAdd) * majorDeltaX
            );

        surface.YAxes[0].AxisTitle = "Power, W";
        const int majorDeltaY = 2;
        surface.YAxes[0].DrawMajorGridLines = true;
        surface.YAxes[0].MajorDelta = majorDeltaY;
        surface.YAxes[0].MinorDelta = majorDeltaY;
        surface.YAxes[0].AxisAlignment = AxisAlignment.Left;
        surface.YAxes[0].AutoTicks = false;
        surface.YAxes[0].AutoRange = AutoRange.Never;

        minusModulusAdd = 0;
        plusModulusAdd = 0;


        var minY = (double)series1St.DataSeries.YMin;
        var maxY = (double)series1St.DataSeries.YMax;

        if ((double)series2Nd.DataSeries.YMin < minY) {
            minY = (double)series2Nd.DataSeries.YMin;
        }
        if ((double)series3Rd.DataSeries.YMin < minY) {
            minY = (double)series3Rd.DataSeries.YMin;
        }

        if ((double)series2Nd.DataSeries.YMax > maxY) {
            maxY = (double)series2Nd.DataSeries.YMax;
        }
        if ((double)series3Rd.DataSeries.YMax > maxY) {
            maxY = (double)series3Rd.DataSeries.YMax;
        }

        if ((Math.Round(minY / majorDeltaY)) * majorDeltaY > minY) {
            minusModulusAdd = -1;
        }

        if ((Math.Round(maxY / majorDeltaY)) * majorDeltaY < maxY) {
            plusModulusAdd = 1;
        }




        surface.YAxes[0].VisibleRange = new DoubleRange(
            (Math.Round(minY / majorDeltaY) + minusModulusAdd) * majorDeltaY,
            (Math.Round(maxY / majorDeltaY) + plusModulusAdd) * majorDeltaY
            );



        surface.ChartModifier = new LegendModifier {
            DataContext = surface.RenderableSeries,
            Background = Brushes.White,
            Name = "legendModifier",
            //ShowLegend = true
            //GetLegendDataFor = SourceMode.AllVisibleSeries
        };



        var powerChartLegend = new SciChartLegend {
            Name = "legendModifier",
            Foreground = Brushes.Black,
            Background = Brushes.White,
            FontSize = 28,
            Margin = new Thickness(125, 23, 0, 0),
            //LegendData = powerChartModifier.LegendData

        };
        var legendDataBinding = new Binding("LegendData") { Source = surface.ChartModifier };
        powerChartLegend.SetBinding(SciChartLegend.LegendDataProperty, legendDataBinding);
        // Export to bitmap
        var bitmapSource = surface.ExportToBitmapSource();
        bitmapSource = ((SciChartSurface)(surface.ChartModifier.ParentSurface)).ExportToBitmapSource();
        Clipboard.SetImage(bitmapSource);



    }

}

}

P.S.: fix your file uploading – I can’t upload whole project in zip.

  • Andrew Burnett-Thompson
    Hi Justas, thanks for this! are you all set now? BTW the file upload limits files to 1MB (its written by the upload button). are you sure the zip file was smaller than a MB? To make it small, you can delete bin, obj directories and DLLs.
  • Andrew Burnett-Thompson
    Something else you might want to try is LegendModifier.ShowLegend, and LegendModifier.LegendPlacement. In v3.1 we added the ability to include the legend inside the chart visual tree making it much easier to export to bitmap. See http://support.scichart.com/index.php?/Knowledgebase/Article/View/17218/39/scichart-legend-api-overview for details
  • justas.bukys
    Thanks for reply Andrew, but only if I comment out GetLegendDataFor = SourceMode.AllVisibleSeries, line then it passes without error, but no legend is rendered.
  • You must to post comments
0
0

Whole project

  • You must to post comments
0
0

So have You guys abandoned this yet?

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