Android Chart框架 MPAndroidChart學習筆記15_標簽設置

Android Chart框架 MPAndroidChart學習筆記15_標簽設置


點擊這里查看項目源碼

1.自定義標記

在MPAndroid中提供了兩種方法:

  • 實現IMaker接口類
  • 繼承MarkerView實現MakerView類

這里我們選擇第二種方式,代碼如下:

1.1 繼承MarkerView實現MakerView類

public class MyMarkView extends MarkerView{
    private TextView tvContent;
    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public MyMarkView(Context context, int layoutResource) {
        super(context, layoutResource);
        tvContent= (TextView) findViewById(R.id.tv_content);

    }

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        tvContent.setTextColor(getResources().getColor(R.color.colorAccent));
        tvContent.setText("您選擇的是"+e.getY());

        super.refreshContent(e, highlight);
    }

    private MPPointF mOffset;

    @Override
    public MPPointF getOffset() {
            if(mOffset==null){
                mOffset=new MPPointF(-(getWidth()/2),-getHeight());
        }
        return mOffset;
    }
}

1.2 實例化并引用

IMarker marker= new MyMarkView(getContext(),R.layout.makerview);
mChart.setMarker(marker);

當然我們需要一個layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@id/tv_content"/>
</LinearLayout>

1.3分析方法

在1.1的繼承類方法:

構造函數:在這里實例化對象

public MyMarkView(Context context, int layoutResource) {
    super(context, layoutResource);
    tvContent= (TextView) findViewById(R.id.tv_content);
}

刷新內容:這里設置標記顯示的內容

    @Override
    public void refreshContent(Entry e, Highlight highlight) {
        tvContent.setTextColor(getResources().getColor(R.color.colorAccent));
        tvContent.setText("您選擇的是"+e.getY());

        super.refreshContent(e, highlight);
    }

設置標簽出現位置

getWidth()、getHeight()表示標簽的寬度、高度

    private MPPointF mOffset;

    @Override
    public MPPointF getOffset() {
        if(mOffset==null){
            mOffset=new MPPointF(-(getWidth()/2),-getHeight());
        }
        return mOffset;
    }

所以這里的顯示效果如下:

new MPPointF(-(getWidth()/2),-getHeight())

如果我們嘗試設置為

mOffset=new MPPointF(0,0);

那么便會出現下面的樣式

new MPPointF(0,0)

怎么樣,察覺到不一樣了嗎?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,535評論 0 17
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,967評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,420評論 25 708
  • 感恩董董的關心,再三把重慶的事情交待清楚,支持我的事業,感恩朋友的相聚多年來感情深厚,感恩妻子一直支持我的事業與成...
    日精進_a07d閱讀 96評論 0 1
  • (一) 凌晨六點,如果說,六點還算是凌晨的話,我一個人走在從操場回教室的路上,其實周圍有很多人的,只是沒有小五牽著...
    雨竹悠悠閱讀 307評論 3 4