Search code examples
javaandroidteechart

How to add custom label on axis in TeeChart Android?


I'm trying to add a custom label on a vertical left axis on my chart:

TChart chart = ...
[...]
Candle series = new Candle(chart.getChart());
series.fillSampleValues(50);
chart.addSeries(series);
[...]
Axis leftAxis = chart.getAxes().getLeft();

TextShape label = new TextShape(chart.getChart());
leftAxis.drawAxisLabel(leftAxis.getPosition(), leftAxis.calcPosValue(500), 0, "CLOSE", label);
[...]

I'm getting an exception:

Caused by: java.lang.NullPointerException
    at com.steema.teechart.android.Graphics3DAndroid.rectangle(Graphics3DAndroid.java:168)
    at com.steema.teechart.TextShape.internalDrawShape(TextShape.java:345)
    at com.steema.teechart.TextShape.drawRectRotated(TextShape.java:387)
    at com.steema.teechart.axis.Axis.drawAxisLabel(Axis.java:2915)
    at com.steema.teechart.axis.Axis.drawAxisLabel(Axis.java:2649)
    at com.forexite.chart.teechart.TeeChartFragment.addSeries(TeeChartFragment.java:102)
    at com.forexite.chart.teechart.TeeChartFragment.onCreateView(TeeChartFragment.java:43)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
    at android.app.Activity.performStart(Activity.java:5241)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
    ... 11 more

Here's an exampled that I found for the similar question: http://www.teechart.net/support/viewtopic.php?f=4&t=8092&p=32273&hilit=drawAxisLabel#p32273

Could anyone suggest how to walkaround my issue?


Solution

  • As you see in the example you referenced, direct drawing functions must be called after drawing the component, preferably in AfterDraw/chartPainted event.
    The following code works fine for me here:

    tChart1.addChartPaintListener(new ChartPaintAdapter() {
    
        @Override
        public void chartPainted(ChartDrawEvent e) {
            Axis leftAxis = tChart1.getAxes().getLeft();
            TextShape label = new TextShape(tChart1.getChart());
            leftAxis.drawAxisLabel(leftAxis.getPosition(), leftAxis.calcPosValue(500), 0, "CLOSE", label);
        }
    });