Custom Image for Tick Mark Labels on Android Chart


#1

Is there a way to customize the tick mark labels using a drawable resource (ImageView) instead of the text label in the Android version of ShinobiCharts?

I know that the iOS version had a handy delegate method called sChart(_ chart: ShinobiChart, alter tickMark: SChartTickMark, beforeAddingTo axis: SChartAxis) where I was able to replace the label with an image view and update the constraints to fit the new image for each tick mark.

I saw that there’s ShinobiChart.OnTickMarkDrawListener but when I try to override the onDrawTickMark method, it removes all the tick marks completely.

I currently have:

public class MainActivity extends AppCompatActivity implements ShinobiChart.OnTickMarkDrawListener {

    private ShinobiChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            setupChart();
        }
    }

    private void setupChart() {
        ChartFragment chartFragment =
                (ChartFragment) getFragmentManager().findFragmentById(R.id.chart);

        chart = chartFragment.getShinobiChart();
        chart.setOnTickMarkDrawListener(this);
        // chart setup goes here for axis, series, dataAdapter, and styling
    }


    @Override
    public void onDrawTickMark(Canvas canvas, TickMark tickMark, Rect rect, Rect rect1, Axis<?, ?> axis) {  
    }
}

Any help is greatly appreciated. Thanks!

Jess


#2

Hello Jess,
If you add an instance of an OnTickMarkDrawListener to your ShinobiChart you will be responsible for drawing the tick marks and labels for all axes. Adding this listener effectively disables the default drawing. If you are customising only 1 of your axes for example, you must replicate the default behaviour to any that remain. I recommend you take a look at the ChartUtils class which may help.
As for using a drawable resource as your tick mark you should be able to achieve this using the onDrawTickMark method on your own OnTickMarkDrawListener. This method provides you with a number of useful parameters, including the Canvas and the Rects of both the tick mark and label. You should be able to draw to the canvas using a standard Android approach, using the appropriate Rect to position your image.
Thanks,
Kai.