Correct usage of sChart:alterTickMark:beforeAddingToAxis: in Monotouch


#1

I’m trying to set custom view tick mark in my X axis. According to the documentation, I implemented the following method to get rid of.

- (void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis

While it’s possible to find some examples in Objective-C (people say that they work), in MT I cannot do the same.

This is the code I’m using.

protected override void OnAddingTickMark (ShinobiChart chart, SChartTickMark tickMark, SChartAxis axis)
{
    if (axis.IsXAxis())
    {
        UILabel tickMarkLabel = new UILabel (new RectangleF (tickMark.TickMarkX - 25, tickMark.TickMarkY, 50, 50));
        UIImageView logoImageView = new UIImageView (UIImage.FromBundle("Images/CustomImage.png"));

        logoImageView.Frame = tickMarkLabel.Bounds;

        tickMarkLabel.AddSubview (logoImageView);

        tickMark.TickLabel = tickMarkLabel;

    }
}

In this I’m able to insert an image in TickLabelproperty. In this case my doubt is the following. Why do I need to create a label first and then assign it to that property?

The same hack achieved like the following does not work.

UIImageView logoImageView = new UIImageView(UIImage.FromBundle("Images/CustomImage.png"));
logoImageView.Frame = new RectangleF (tickMark.TickMarkX - 25, tickMark.TickMarkY, 50, 50));
tickMark.TickMarkView = logoImageView;

The mark view is not displayed. Any suggestion? Is it possible to follow a more clean approach?

Thank you in advance. 


#2

Hi lorenz,

The problem you’re having seems to be that while you’re assigning a UILabel to the TickLabel property, that label has never been added as a subview to the chart, so it doesn’t appear.

You can achieve what you describe with something like the following:

protected override void OnAddingTickMark (ShinobiChart chart, SChartTickMark tickMark, SChartAxis axis)
{
    if (axis.IsXAxis () && tickMark.TickLabel != null) {
        tickMark.TickLabel.AddSubview (new UIView() {
            Frame = tickMark.TickLabel.Bounds,
            BackgroundColor = UIColor.Green
        });
    }
}

The TickLabels may or may not have been added as subviews to the chart when this delegate method is called, so I recommend adding your custom view to the existing label like above, rather than creating a new label.

The tickMark.TickMarkView property is the view for the actual tick mark itself (the little grey line), so I wouldn’t recommend adding your custom view to this view.

William


#3

William,

Thank you very much for your reply.