ShinobiChart Xamarin.android data not refreshed


#1

I started to play with the trial version using the Xamarin sample ShinobiQuickStart project.

I build it and run that works fine.

I just change it little to refresh the data adapter on timer basis let’s say:

namespace ShinobiQuickStart
{
    [Activity(Label = "ShinobiQuickStart", MainLauncher = true)]
    public class MainActivity : Activity
    {
        private Timer timer;
        IShinobiChart shinobiChart;
        
        int count;
        
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
 
            // Only setup the chart the first time the activity is created
            if (bundle == null)
            {
                ChartFragment chartFragment =
                    (ChartFragment)FragmentManager.FindFragmentById(Resource.Id.chart);
 
                shinobiChart = chartFragment.ShinobiChart;
                shinobiChart.Title = "Trigonometric Functions";
 
                // TODO: replace <license_key_here> with you trial license key
                shinobiChart.SetLicenseKey("myKey");
 
                NumberAxis xAxis = new NumberAxis(new NumberRange(0, 100));
                shinobiChart.XAxis = xAxis;
 
                NumberAxis yAxis = new NumberAxis();
                shinobiChart.YAxis = yAxis;
                yAxis.DefaultRange = new NumberRange(-1.05, 1.05);
 
                LineSeries series1 = new LineSeries();
                series1.DataAdapter = new SimpleDataAdapter();
                shinobiChart.AddSeries(series1);
 
                LineSeries series2 = new LineSeries();
                series2.DataAdapter = new SimpleDataAdapter();
                shinobiChart.AddSeries(series2);
 
                // Here, we're setting colors using the Color class, supplying it
                // with values for alpha, red, green and blue. Alternatively, you
                // could take a more Android-like approach and define these in a
                // resources file. See
                // http://developer.android.com/guide/topics/resources/index.html
                // for more information.
 
                LineSeriesStyle style1 = series1.Style;
                style1.FillStyle = SeriesStyle.FillStyle.Gradient;
                style1.AreaColor = Color.Argb(179, 94, 51, 95);
                style1.AreaColorGradient = Color.Argb(255, 94, 51, 95);
                style1.AreaColorBelowBaseline = Color.Argb(179, 94, 51, 95);
                style1.AreaColorGradientBelowBaseline = Color.Argb(255, 94, 51, 95);
 
                LineSeriesStyle style2 = series2.Style;
                style2.FillStyle = SeriesStyle.FillStyle.Gradient;
                style2.AreaColor = Color.Argb(179, 26, 96, 164);
                style2.AreaColorGradient = Color.Argb(255, 26, 96, 164);
                style2.AreaColorBelowBaseline = Color.Argb(179, 26, 96, 164);
                style2.AreaColorGradientBelowBaseline = Color.Argb(255, 26, 96, 164);
 
                shinobiChart.RedrawChart();
 
                timer = new Timer(UpdateGraph, null, 5000, 3000);
            }
        }
 
        private void UpdateGraph(object state)
        {
            //RunOnUiThread(() =>
            //    {
            if (count > 100)
                return;
 
            for (int i = 0; i < 10; i++)
            {
                double radians = (count + i) * Math.PI / 25.0;
                shinobiChart.Series[0].DataAdapter.Add(new DataPoint(radians, Math.Sin(radians)));
                shinobiChart.Series[1].DataAdapter.Add(new DataPoint(radians, Math.Cos(radians)));
            }
            shinobiChart.RedrawChart();
            count += 10;
 
            //});
        }
    }
}

The data is not getting refreshed with those points

Am I wrong? The dataadapter should notify the added point to the chartfragment automaticaly?

I thought I should run that on main thread since the data is from another thread, but still nothing.

Shoudl I care about thefragment transaction also?

Any idea Please

Thanks


#2

Hi liass,

It looks as if you have defined class fields and local variables with the same names, dataAdapter1 and dataAdapter2. So you’re setting the local variable objects onto the chart, but adding data to the field instances, which are different. There a couple of ways to fix this: either remove the declarations in the onCreate method, or remove the class-level ones and get at the data adapters via the chart and the series in your UpdateGraph method.

It’s also worth a look at the custom data adapter how-to guide, as that isa very similar scenario to what you are doing here.

Best regards,

Robin


#3

Thanks for your answer.

I made a typo when copy/paste the code below (sorry about that). it’s not because of the scope! here is a corrected code pasted.

The problem is still there.


#4

I strongly suggest that you follow the pattern in the CustomDataAdapter sample and/or do some reading/googling around why using timers to update UI is not a good idea in Android, and why patterns involving Runnable and Handler are recommended. This is an Android programming issue, not something specific to ShinobiCharts. In our experience, Android rewards doing things ‘The Android Way’ and you do need to know the platform, an understanding which is somewhat obscured when you’re using Xamarin, as one tends to think it’s the same as coding in C# for Windows.  

There are 2 other issues that the CustomDataAdapter sample will address for you: In your example, the SimpleDataAdapter will reload every time you add a data point, and the call to redrawChart is unnecessary.


#5

You are right. I manage dit to work on the quickstart example by using a postdelayed insteadof playing around with timer and nethier threadpool mechanism.

Now it looks working better

Thaks


#6

No problem. I’m glad it’s working for you now.


#7

I am also having a similar issue, I want to update a chart so it will show say 10 datapoints instead of 5 on a button click, rather than it run continuously, any help would be appreciated. 


#8

Hi Theresa,

I’m not sure I understand your question. 10 data points instead of 5? Can you not simply add the 10 data points to your data adapter in the button’s onClickListener handler? 

Best regards,

Robin Sillem


#9

got it to works thanks


#10

Glad to hear it Theresa!

Patrick