首页 文章

自定义标记 - MPAndroidChart

提问于
浏览
4

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

enter image description here

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

enter image description here

为了使它看起来像一个注释框而不是我应该使用的类的矩形框 . 我已经检查了 IMarkerMarkerImage 但不确定我应该继续这样做 .

甚至 MPPointF 也没有't working. Can'吨导入包

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

2 回答

  • 2

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

    Using an image as a background

    而已...

    而已??!!!

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

  • 5

    确保您拥有最新版本的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>
    

    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
    

相关问题