This is a migrated thread and some comments may be shown as answers.

Problem with AxisY.AddRange when using with BackgroundWorker

3 Answers 64 Views
Chart
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 05 Jan 2011, 07:25 PM
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

3 Answers, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 07 Jan 2011, 08:28 AM
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
0
Alan
Top achievements
Rank 1
answered on 07 Jan 2011, 04:00 PM
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
0
Yavor
Telerik team
answered on 12 Jan 2011, 03:19 PM
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>>
Tags
Chart
Asked by
Alan
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Alan
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or