【问题标题】:Customize the marker - MPAndroidChart自定义标记 - MPAndroidChart
【发布时间】:2017-01-04 07:15:46
【问题描述】:

我使用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);
}

【问题讨论】:

  • @PhilippJahoda 你能帮忙吗
  • 听起来您使用了错误版本的库,因为我的 MarkerViews 显示其中带有箭头。这将解释您在导入 MPPointF 时遇到的问题。确保您使用的是 3.0.1 版
  • 我使用的是 v2.1.6 :( 检查与 v3.0.1 相同
  • 将所有内容更改为最新版本后,它仍然是一样的。我做了这个例子:github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/…

标签: android mpandroidchart


【解决方案1】:

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

使用图片作为背景

就是这样……

就这样??!!!

我使用这张图片而不是背景颜色“红色”来获得那个箭头。

【讨论】:

  • 如果内容更多,背景栏的提示无法正确显示
【解决方案2】:

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

代码位于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>

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

它是这样消耗的:

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

【讨论】:

  • 我已经完成了所有这些@David。我成功地得到了标记。该箭头背后的真正技巧(唯一方法)是stackoverflow.com/a/41479610/2820534
  • 感谢您让我意识到我没有使用当前版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多