自定义标记 - MPAndroidChart

10

我使用MPAndroidChart创建了一个简单的柱状图。为了Marker刻度线,我正在扩展MarkerView。我需要一个像这样的标记器,就像在git hub上看到的那样:

enter image description here

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

enter image description here

为了使其看起来像一个评论框,而不是一个矩形框,我应该使用哪个类?我已经检查过IMarkerMarkerImage,但不确定该使用哪个。

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

@PhilippJahoda,你能帮忙吗? - Prabs
1
听起来你正在使用错误版本的库,因为我的MarkerViews中带有箭头。这可能会解释你导入“MPPointF”时遇到的问题。请确保你正在使用3.0.1版本。 - David Rawson
我之前使用的是v2.1.6 :( 现在正在检查是否与v3.0.1相同。 - Prabs
将所有内容更改为最新版本后,仍然是相同的。我已经完成了这个示例:https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java - Prabs
2个回答

10

点击此链接

使用图片作为背景

就是这样...

就是这样吗?!

我用这张图片代替了红色的背景,从而得到了那个箭头。


如果内容在提示栏的顶部显示不正确,则可能是背景问题。 - Suresh Maidaragi

9

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

a picture of a LineChart in MPAndroidChart which shows a marker view that meets the OP's requirement

代码位于示例项目中的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>

标记视图如下:
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());
    }
}

它的使用方法如下:

MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
mv.setChartView(mChart); // For bounds control

我已经完成了所有这些事情,@David。我成功地获得了标记。那个箭头背后的真正诀窍(唯一的方法)是https://dev59.com/MFgR5IYBdhLWcg3wV8Ay#41479610。 - Prabs
谢谢你让我意识到我没有使用当前版本。 - Prabs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接