Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
25
Axis Labeling
posted

Hi all!

I would like to use NucliOS 2013.1 in a Xamarin.iOS project but I'm stuck with a problem. I can't find out how set custom labels on the X-axis for a given data points in a series. I tried to implement a custom data source class and implemented the ResolvePointAtIndex method. However, the IGDataPoint defined in IGChart.dll does not contain a Value property, it only has a Label property. So using this approach I can set the label, but not the value - which results in a runtime exception. Am I missing something? Are there any alternative approaches?

Thanks

Markus

Parents
  • 4940
    Offline posted

    To customize axis labels, you override the ResolveLabelForAxis method in a class that derives from IGChartViewDelegate. The code below populates an array of custom data objects and customizes both the x and y axis labels of a column series chart.

    namespace ChartCustomDataAxisLabels
    {
        public class GraphObject : NSObject
        {
            [Export("Territory")]
            public String Territory {get; set;}
            [Export("Cost")]
            public Double Cost {get; set;}

            public GraphObject() {}
        }

        public class ChartViewDelegate : IGChartViewDelegate
        {
            public override string ResolveLabelForAxis (IGChartView chartView, IGAxis axis, NSObject item)
            {
                if (axis.Key.Equals("xAxis"))
                {
                    return ((IGCategoryPoint)item).Label;
                }
                else if (axis.Key.Equals("yAxis"))
                {
                    NSNumberFormatter formatter = new NSNumberFormatter();
                    formatter.NumberStyle = NSNumberFormatterStyle.Currency;
                    return formatter.StringFromNumber((NSNumber)item);
                }

                return String.Empty;
            }
        }

        public partial class ChartCustomDataAxisLabelsViewController : UIViewController
        {
            IGChartView _chart = new IGChartView();
            List _data = new List();
            IGCategorySeriesDataSourceHelper _source = new IGCategorySeriesDataSourceHelper();

            public ChartCustomDataAxisLabelsViewController () {}
            
            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();

                this.generateData();
                _source.Data = _data.ToArray();
                _source.ValuePath = @"Cost";
                _source.LabelPath = @"Territory";

                RectangleF chartRect = this.View.Frame;
                chartRect.Inflate(-30.0f, -30.0f);
                _chart.Frame = chartRect;
                _chart.AutoresizingMask = UIViewAutoresizing.FlexibleHeight|UIViewAutoresizing.FlexibleWidth;
                _chart.Theme = IGChartGradientThemes.IGThemeDark();
                _chart.Delegate = new ChartViewDelegate();
                this.View.Add(_chart);

                _chart.AddSeries(new MonoTouch.ObjCRuntime.Class("IGColumnSeries"), "series", _source, "xAxis", "yAxis");
            }

            private void generateData()
            {
                String[] territories = new string[] {@"Canada", @"Finland", @"Holland", @"Japan", @"USA"};
                
                for (int i = 0; i < 5; i++)
                {
                    GraphObject graphObject = new GraphObject();
                    graphObject.Territory = territories[i];
                    graphObject.Cost = new Random(i).Next(100000);
                    _data.Add(graphObject);
                }
            }
        }
    }

Reply Children
No Data