SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and now iOS Charting & Android Chart Components

0
0

Hi guys,

I have a big problem that It may be small for you đŸ™‚

I have a chart with multiple series. the number of series is not fix. sometimes the chart can have 3 series but sometimes it can have 20 series. Anyway…. so can you give me an idea or perhaps a test application where I can set the properties for each serie, especially the color of each serie which will be different(random).

I did the following for another chart where the series number was defined, but remember that now I have to create this another chart where the series number is not defined.

In the .XAML:

<sciChart:SciChartSurface.RenderableSeries>
<sciChart:FastLineRenderableSeries SeriesColor="{Binding SeriesColor1}" StrokeThickness="1" YAxisId="Right1"/>
<sciChart:FastLineRenderableSeries SeriesColor="{Binding SeriesColor2}" StrokeThickness="1" YAxisId="Left1"/>
<sciChart:FastLineRenderableSeries SeriesColor="{Binding SeriesColor3}" StrokeThickness="1" YAxisId="Left1"/>
<sciChart:FastLineRenderableSeries SeriesColor="{Binding SeriesColor4}" StrokeThickness="1" YAxisId="Left1"/>
<sciChart:FastLineRenderableSeries SeriesColor="{Binding SeriesColor5}" StrokeThickness="1" YAxisId="Left1"/>
<sciChart:FastLineRenderableSeries SeriesColor="{Binding SeriesColor6}" StrokeThickness="1" YAxisId="Left1"/>
</sciChart:SciChartSurface.RenderableSeries>

And in the ViewModel:

private static readonly PropertyChangedEventArgs SeriesColor1ChangeArgs = ObservableHelper.CreateArgs<AnalysisVelocityChartViewModel>(x => x.SeriesColor1);
private string _seriesColor1;
public string SeriesColor1
{
get { return _seriesColor1; }
set
{
_seriesColor1 = value;
NotifyPropertyChanged(SeriesColor1ChangeArgs);
}
}

    private static readonly PropertyChangedEventArgs SeriesColor2ChangeArgs = ObservableHelper.CreateArgs&lt;AnalysisVelocityChartViewModel&gt;(x =&gt; x.SeriesColor2);
    private string _seriesColor2;
    public string SeriesColor2
    {
        get { return _seriesColor2; }
        set
        {
            _seriesColor2 = value;
            NotifyPropertyChanged(SeriesColor2ChangeArgs);
        }
    }


    private static readonly PropertyChangedEventArgs SeriesColor3ChangeArgs = ObservableHelper.CreateArgs&lt;AnalysisVelocityChartViewModel&gt;(x =&gt; x.SeriesColor3);
    private string _seriesColor3;
    public string SeriesColor3
    {
        get { return _seriesColor3; }
        set
        {
            _seriesColor3 = value;
            NotifyPropertyChanged(SeriesColor3ChangeArgs);
        }
    }

    private static readonly PropertyChangedEventArgs SeriesColor4ChangeArgs = ObservableHelper.CreateArgs&lt;AnalysisVelocityChartViewModel&gt;(x =&gt; x.SeriesColor4);
    private string _seriesColor4;
    public string SeriesColor4
    {
        get { return _seriesColor4; }
        set
        {
            _seriesColor4 = value;
            NotifyPropertyChanged(SeriesColor4ChangeArgs);
        }
    }

    private static readonly PropertyChangedEventArgs SeriesColor5ChangeArgs = ObservableHelper.CreateArgs&lt;AnalysisVelocityChartViewModel&gt;(x =&gt; x.SeriesColor5);
    private string _seriesColor5;
    public string SeriesColor5
    {
        get { return _seriesColor5; }
        set
        {
            _seriesColor5 = value;
            NotifyPropertyChanged(SeriesColor5ChangeArgs);
        }
    }

    private static readonly PropertyChangedEventArgs SeriesColor6ChangeArgs = ObservableHelper.CreateArgs&lt;AnalysisVelocityChartViewModel&gt;(x =&gt; x.SeriesColor6);
    private string _seriesColor6;
    public string SeriesColor6
    {
        get { return _seriesColor6; }
        set
        {
            _seriesColor6 = value;
            NotifyPropertyChanged(SeriesColor6ChangeArgs);
        }
    }


        SeriesColor1 = &quot;Brown&quot;;
        SeriesColor2 = &quot;Blue&quot;;
        SeriesColor3 = &quot;Red&quot;;
        SeriesColor4 = &quot;Yellow&quot;;
        SeriesColor5 = &quot;Green&quot;;
        SeriesColor6 = &quot;Black&quot;;

Thanks so much Guys,

đŸ™‚

  • You must to post comments
0
0

Hi there,

You could consider using of SciChartSurface.SeriesSource instead. It allows you to bind to an ObservableCollection which marries one DataSeries with one RenderableSeries, and in this case the DataSet creation is implicit (done internally by SciChart). Please, take a look at Databinding annotations with MVVM tutorial for an example of series creation via SeriesSource.

Also there is the tutorial How to add/remove series dynamically, which shows how to achieve this using the DataSet/RenderableSeries, and you can find corresponding example in our example suite Add/remove data series.

Hope this helps, please, feel free to ask if you have more questions!

Best regards,
Yuriy

  • vaquita50
    Hi Yuriy.I tried to implement it using the examples on the links that you provided. But I am getting the following error ("The calling thread must be STA, because many UI components require this." and I cannot understand because I am doing exactly the same thing as in the examples..XAML
    <sciChart:SciChartSurface x:Name="SciChartSurface" Padding="0 0 0 30" Margin="0"
                                          VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                                          MouseLeftButtonDown="OnSciChartMouseLeftButtonDown"
                                          SeriesSource="{Binding SeriesSource}"
                                          sciChart:ThemeManager.Theme="Chrome">
                    <!--  Declare Axes  -->
                    <sciChart:SciChartSurface.XAxis>
                        <sciChart:DateTimeAxis x:Name="XAxisData" AxisTitle="{x:Static prop:Resources.time}" VerticalAlignment="Top"
                                               Margin="0" Padding="0" FontSize="10" TitleFontSize="10"
                                               VisibleRange="{Binding SharedXVisibleRange, Mode=TwoWay}"  
                                               DrawMajorGridLines="True"
                                               DrawMinorGridLines="True"  
                                               TextFormatting="HH:mm MMM dd" 
                                               SubDayTextFormatting="HH:mm:ss MMM dd">
                            <sciChart:DateTimeAxis.GrowBy>
                                <sciChart:DoubleRange Min="0.1" Max="0.1"/>
                            </sciChart:DateTimeAxis.GrowBy>
                        </sciChart:DateTimeAxis>
                    </sciChart:SciChartSurface.XAxis>
    
                    <sciChart:SciChartSurface.YAxes>
                        <sciChart:NumericAxis Name="YAxisData" Id="Left1" Margin="0" Padding="0" AxisAlignment="Left"  FontSize="10" TitleFontSize="10"
                                              Width="50" AxisTitle="{Binding ChartModel.YaxisName}" MinorDelta="0" AutoRange="True">
                            <sciChart:NumericAxis.GrowBy>
                                <sciChart:DoubleRange Min="0.1" Max="0.1"/>
                            </sciChart:NumericAxis.GrowBy>
                        </sciChart:NumericAxis>
                        <sciChart:NumericAxis Id="Right1" Margin="0" Padding="0" AxisAlignment="Right"  FontSize="10" TitleFontSize="10"
                                              AxisTitle="{Binding RightYaxisName}" MinorDelta="0" AutoRange="True">
                            <sciChart:NumericAxis.GrowBy>
                                <sciChart:DoubleRange Min="0.1" Max="0.1"/>
                            </sciChart:NumericAxis.GrowBy>
                        </sciChart:NumericAxis>
                    </sciChart:SciChartSurface.YAxes>
    
                    <!--  Declare ChartModifiers  -->
                    <sciChart:SciChartSurface.ChartModifier>
                        <sciChart:ModifierGroup Margin="0" Padding="0" FontSize="8" sciChart:MouseManager.MouseEventGroup="myCustomGroup">
                            <sciChart:LegendModifier x:Name="LegendModifier" GetLegendDataFor="AllSeries"/>
                            <sciChart:RubberBandXyZoomModifier IsEnabled="True" IsXAxisOnly="True"  ReceiveHandledEvents="True" />
                            <sciChart:ZoomPanModifier IsEnabled="False"/>
                            <sciChart:MouseWheelZoomModifier IsEnabled="False"/>
                            <sciChart:RolloverModifier IsEnabled="True" ReceiveHandledEvents="True"/>
                            <sciChart:CursorModifier ReceiveHandledEvents="True" ShowTooltip="False" ShowAxisLabels="True"/>
                            <sciChart:YAxisDragModifier YAxisId="Right1"/>
                            <sciChart:YAxisDragModifier YAxisId="Left1"/>
                            <sciChart:XAxisDragModifier/>
                            <sciChart:ZoomExtentsModifier ExecuteOn="MouseRightButton"/>
                        </sciChart:ModifierGroup>
                    </sciChart:SciChartSurface.ChartModifier>
                    <sciChart:SciChartSurface.Annotations>
                        <sciChart:VerticalLineAnnotation YAxisId="Left1" X1="{Binding ScanRange.StartScan.Date, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Stretch" Stroke="DimGray" StrokeThickness="2" />
                        <sciChart:VerticalLineAnnotation YAxisId="Left1" X1="{Binding ScanRange.EndScan.Date, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Stretch" Stroke="Blue" StrokeThickness="2" />
                    </sciChart:SciChartSurface.Annotations>
                </sciChart:SciChartSurface>
    ViewModel.CS
    private static readonly PropertyChangedEventArgs SeriesSourceChangeArgs = ObservableHelper.CreateArgs<AnalysisVelocityChartViewModel>(x => x.SeriesSource);
            private ObservableCollection<IChartSeriesViewModel> _seriesSource;
            public ObservableCollection<IChartSeriesViewModel> SeriesSource
            {
                get { return _seriesSource; }
                set
                {
                    _seriesSource = value;
                    NotifyPropertyChanged(SeriesSourceChangeArgs);
                }
            }
    private void LoadVelocityChartViewModel()
            {
                // Create a dataset on the ChartViewModel
                ChartData = new DataSeriesSet<DateTime, double>();
    
               var renderableSeries = new FastLineRenderableSeries();
                foreach (var chartDataModel in _chartModel.ChartDataModels)
                {
                    var dataSeries = ChartData.AddSeries();
                    dataSeries.SeriesName = chartDataModel.SeriesName;
                    dataSeries.Append(chartDataModel.ChartData.Keys, chartDataModel.ChartData.Values);
    
                    renderableSeries.SeriesColor = GetRandomColor();
                    var vm = new ChartSeriesViewModel(dataSeries, renderableSeries);
    
                    SeriesSource.Add(vm);
                }
    
                RightYaxisName = ChartSeriesName.VelocityDeformation;
    
               NotifyPropertyChanged(ChartModelChangeArgs);
            }
    
    Thanks so much, I will really appreciate your help.Sandra
  • Yuriy
    Hi Sandra,Sorry, cannot tell you how to fix it without looking in the code. Generally, you need to check if all your methods can be called in the UI thread. Consider using BackgroundWorker and Dispatcher.Invoke() for those calls which can't be called.Best regards, Yuriy
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.