如何在TeeChart Android中在轴上添加自定义标签?

时间:2014-08-12 18:05:40

标签: java android teechart

我试图在我的图表的垂直左轴上添加自定义标签:

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);
[...]

我得到了例外:

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

以下是我为类似问题找到的示例: http://www.teechart.net/support/viewtopic.php?f=4&t=8092&p=32273&hilit=drawAxisLabel#p32273

有人可以建议如何解决我的问题吗?

1 个答案:

答案 0 :(得分:0)

如您所引用的示例所示,绘制组件后必须调用直接绘图函数,最好是在AfterDraw / chartPainted事件中。 以下代码在这里适合我:

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);
    }
});