Problem with AxisY.AddRange when using with BackgroundWorker

4 posts, 0 answers
  1. Alan
    Alan avatar
    3 posts
    Member since:
    May 2010

    Posted 05 Jan 2011 Link to this post

    I try to manually set the axis range in RunWorkerCompleted event. The chart did render the new data, but not the axis value. Here is an example code:
    System.ComponentModel.BackgroundWorker worker = new System.ComponentModel.BackgroundWorker();
    worker.DoWork += (o, e) => { System.Threading.Thread.Sleep(500); };
    worker.RunWorkerCompleted += (o, e) =>
    {
        CreateDummyView();
     
        AxisY myRangeAxis = MyChart.DefaultView.ChartArea.AxisY;
        myRangeAxis.AddRange(-2, 2, 0.4);
    };
    worker.RunWorkerAsync();

    I use BackgroundWorker to load a large data then plot it when it finished reading the data. I'm wondering if there is a way to update the axis in this worker completed event.

    - Alan
  2. Nikolay
    Admin
    Nikolay avatar
    397 posts

    Posted 07 Jan 2011 Link to this post

    Hi Alan,

    You should be able to update the axis range within a worker completed event. From your code snippet it appears that nowhere do you set the axis AutoRange to false, which is why the custom range is not applied. What you need to do is add a line which sets the AutoRange property to false :
    AxisY myRangeAxis = MyChart.DefaultView.ChartArea.AxisY;
                    myRangeAxis.AutoRange = false;
                    myRangeAxis.AddRange(-2, 2, 0.4);

    Hope this helps.

    Regards,
    Nikolay
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. Alan
    Alan avatar
    3 posts
    Member since:
    May 2010

    Posted 07 Jan 2011 Link to this post

    I set AutoRange to false but I still have the same issue. The library version I'm using is 2010.2.714.1040 and here are my codes:
    C#
    using System;
    using System.Windows.Controls;
    using Telerik.Windows.Controls.Charting;
     
    namespace RadChartTest
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
     
                SetupChartControl();
                CreateDummyView();
     
                System.ComponentModel.BackgroundWorker worker = new System.ComponentModel.BackgroundWorker();
                worker.DoWork += (o, e) => { System.Threading.Thread.Sleep(4000); };
                worker.RunWorkerCompleted += (o, e) =>
                {
                    CreateDummyView();
     
                    AxisY myRangeAxis = MyChart.DefaultView.ChartArea.AxisY;
                    myRangeAxis.AddRange(-3, 3, 0.6);
                    System.Diagnostics.Debug.WriteLine("Worker is Over.");
                };
                worker.RunWorkerAsync();
            }
     
            private void SetupChartControl()
            {
                ChartLegend myLegend = MyChart.DefaultView.ChartLegend;
                myLegend.Visibility = System.Windows.Visibility.Collapsed;
     
                ChartArea myArea = MyChart.DefaultView.ChartArea;
     
                AxisY myRangeAxis = myArea.AxisY;
                myRangeAxis.AutoRange = false;
                myRangeAxis.AddRange(0, 1, 0.1);
            }
     
            private void CreateDummyView()
            {
                ChartArea myArea = MyChart.DefaultView.ChartArea;
                myArea.DataSeries.Clear();
     
                myArea.DataSeries.SuspendNotifications();
                myArea.DataSeries.Add(SineData(2));
                myArea.DataSeries.ResumeNotifications();
            }
     
            public static DataSeries SineData(double amp = 1, double left = 0, double right = 1, double step = 1e-3)
            {
                double offset = new Random().NextDouble() * Math.PI * 2;
     
                LineSeriesDefinition lineDef = new LineSeriesDefinition();
                lineDef.ShowItemLabels = false;
                lineDef.ShowItemToolTips = false;
                lineDef.ShowPointMarks = false;
     
                DataSeries ds = new DataSeries();
                ds.Definition = lineDef;
                for (double t = left; t < right; t += step)
                {
                    ds.Add(new DataPoint(t, amp * Math.Sin(2 * Math.PI * 10 * t + offset)));
                }
     
                return ds;
            }
        }
    }

    XAML
    <UserControl
        x:Class="RadChartTest.MainPage"
        mc:Ignorable="d"
        d:DesignHeight="480"
        d:DesignWidth="600">
        <telerik:RadChart
            x:Name="MyChart" />
    </UserControl>

    - Alan
  4. Yavor
    Admin
    Yavor avatar
    401 posts

    Posted 12 Jan 2011 Link to this post

    Hello Alan,

    Thank you for getting back to us!
    This issue is already resolved and you can get the fix with the latest binaries. I have attached a runnable solution that uses your code and the latest binaries. Please give it a try in your environment and let us know if you still experience this problem.

    Hope this helps!

    Kind regards,
    Yavor Ivanov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Back to Top