自定义标记 - MPAndroidChart

Pra*_*abs 7 android mpandroidchart

我使用了创建了一个简单的条形图MPAndroidChart.出于Marker的目的,我正在扩展MarkerView.我需要一个标记,如git hub所示,如下所示:

在此输入图像描述

但是我没有得到那支箭.所以它看起来不像评论,它是一个矩形框:/

在此输入图像描述

为了使它看起来像一个注释框而不是我应该使用的类的矩形框.我已经检查过了IMarker,MarkerImage但不知道应该继续哪些.

甚至MPPointF不工作.无法导入包

import com.github.mikephil.charting.utils.MPPointF;

码:

@Override
public void refreshContent(Entry e, Highlight highlight) { 
    tv_turnOver.setText("Turn over: " + (int) e.getVal());

    /*
    if (e instanceof CandleEntry) {

        CandleEntry ce = (CandleEntry) e;

        tv_label.setText("" + Utils.formatNumber(ce.getVal(), 0, true));
    } else {

        tv_label.setText("" + Utils.formatNumber(e.getXIndex(), 0, true));
    }*/

    //  super.refreshContent(e, highlight);
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*abs 7

https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/res/drawable-nodpi/marker2.png

使用图像作为背景

而已...

而已??!!!

而不是背景颜色'红色',我已经使用此图像来获得该箭头.


Dav*_*son 5

确保您拥有最新版本的 MPAndroidChart (3.0.1)。在您的设备上克隆、构建和运行示例项目。您可以看到名为“折线图 - 折线图的简单演示”的菜单上的第一个示例具有您想要的突出显示视图。它看起来像这样:

MPAndroidChart 中 LineChart 的图片,其中显示了符合 OP 要求的标记视图

代码位于LineChartActivity1. xml如下:

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text=""
        android:textSize="12dp"
        android:textColor="@android:color/white"
        android:ellipsize="end"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

MarkerView 如下:

package com.xxmassdeveloper.mpchartexample.custom;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;
import com.xxmassdeveloper.mpchartexample.R;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
public class MyMarkerView extends MarkerView {

    private TextView tvContent;

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

        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) {

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

            tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 0, true));
        } else {

            tvContent.setText("" + Utils.formatNumber(e.getY(), 0, true));
        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
Run Code Online (Sandbox Code Playgroud)

它是这样消耗的:

MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
mv.setChartView(mChart); // For bounds control
Run Code Online (Sandbox Code Playgroud)