美文网首页
股票图或虚拟币图的绘制 【第三章:MarkerView】

股票图或虚拟币图的绘制 【第三章:MarkerView】

作者: Small_Cake | 来源:发表于2019-06-19 17:03 被阅读0次
QQ截图20180321093109.png

今天我们就要来绘制如上的时间标记和Y轴标记。

说明:K线图包含了蜡烛图和三条线,那么我们点击的位置要么是蜡烛图的中心位置,要么是三条线的位置,所以我们绘制的标记不应该在蜡烛渲染器(CandleStickChartRenderer)或者直线渲染器(LineChartRenderer)里面去修改方法。而应该直接在组合图(CombinedChart)中去寻找方法!

既然是两个标记,那我们看看官方文档17条是怎么介绍的:
https://github.com/PhilJay/MPAndroidChart/wiki/IMarker-Interface

我想文档已经说得很清楚了,就是我们需要实现IMarker这个接口就行了,但是更简便的是继承MarkerView这个类,它已经帮我们做了更多的事情,我们只需要关注这个MarkerView的样式和位置就好了。例如:

public class YourMarkerView extends MarkerView {

    private TextView tvContent;

    public MyMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);

        // find your layout components
        tvContent = (TextView) findViewById(R.id.tvContent);
    }

    // callbacks everytime the MarkerView is redrawn, can be used to update the
    // content (user-interface)
    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        tvContent.setText("" + e.getY());

        // this will perform necessary layouting
        super.refreshContent(e, highlight);
    }

    private MPPointF mOffset; 

    @Override
    public MPPointF getOffset() {

        if(mOffset == null) {
           // center the marker horizontally and vertically
           mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
        }

        return mOffset;
    }
}

样式:我们通过构造方法,来写入我们的布局xml就可以了,数据的刷新靠refreshContent()来完成,最后通过:

IMarker marker = new YourMarkerView();
chart.setMarker(marker);

来设置标记就可以啦~!

但是这样的标记默认是点击后的高亮中心点位置!这可不是我们想要的结果,

相关文章

网友评论

      本文标题:股票图或虚拟币图的绘制 【第三章:MarkerView】

      本文链接:https://www.haomeiwen.com/subject/wmdwqftx.html