MarkerView未显示使用MPAndroidChart

时间:2016-04-18 07:25:57

标签: android mpandroidchart

我正在使用MPandroidchart在我的应用程序中显示折线图。我添加了以下代码来显示标记视图,但它没有显示在

private void initializeChart(LineChart chart, String chartName) {
    // Chart view
    chart.setDrawGridBackground(false);
    chart.setDescription("");
    chart.getLegend().setEnabled(true);
    //chart.setTouchEnabled(false);
    int color = getResources().getColor(R.color.white);
    chart.getAxisLeft().setTextColor(color); // left y-axis
    chart.getXAxis().setTextColor(color);

    chart.setTouchEnabled(true);
    CustomMarkerView mv = new CustomMarkerView(this.getActivity(), R.layout.marker_view_tv);
    chart.setMarkerView(mv);

    //X axis
    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawLabels(true);

    //Y axis
    YAxis leftAxis = chart.getAxisLeft();
    YAxis rightAxis = chart.getAxisRight();
    rightAxis.setDrawLabels(false);
    rightAxis.setDrawGridLines(false);
    leftAxis.setDrawLabels(true);
    leftAxis.setDrawGridLines(false);
    leftAxis.setStartAtZero(false);

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());


    leftAxis.setLabelCount(Constants.KEY_LINE_YAXIS_SCALECOUNT, true);
    ChartItem item = CannonJsonParser.parseCanonJson(act, act.res);
    int maxYVal = pref.getInt(Constants.KEY_YAXIS_VALUE, 0);
    leftAxis.setAxisMaxValue(maxYVal);
    leftAxis.setAxisMinValue(0);
    setLineData(item, chartName);
    // set data
    chart.setData(lineData);

    chart.getLegend().setEnabled(false);
    //animate
    //chart.animateX(2000, Easing.EasingOption.EaseInExpo);
    chart.setDragEnabled(true);
    chart.setScaleXEnabled(true);
    chart.setScaleYEnabled(false);
    chart.setHighlightPerDragEnabled(false);
    chart.setHighlightPerTapEnabled(false);


}

我的CustomMarkerView类

public class CustomMarkerView extends MarkerView {
private TextView tvContent;

public CustomMarkerView(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.getVal(), 0, true));
    }
}

@Override
public int getXOffset(float xpos) {
    // this will center the marker-view horizontally
    return -(getWidth() / 2);
}

@Override
public int getYOffset(float ypos) {
    // this will cause the marker-view to be above the selected value
    return -getHeight();
}

}

注意:我使用片段来显示图表。

4 个答案:

答案 0 :(得分:4)

您的MarkerView未显示,因为您的图表中未突出显示任何条目MarkerView仅针对突出显示的条目显示。

由于您禁用了每次点击突出显示条目的功能(通过调用chart.setHighlightPerTapEnabled(false)),因此您只能以编程方式突出显示值,如下所示:

chart.highlightValue(...)

documentation中的更多内容。

答案 1 :(得分:3)

遇到类似问题。您应该调用super刷新markerview。 关于Kotlin的示例:

 class CustomMarkerView(context: Context, layoutResource: Int) : MarkerView(context, layoutResource) {

    override fun refreshContent(e: Entry?, highlight: Highlight?) {
        markerDate.text = e?.data.toString()
        markerValue.text = e?.y.toString()
        super.refreshContent(e, highlight)
    }
}

答案 2 :(得分:1)

试试这个,上课

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.utils.Utils;

/**
 * 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, int dataSetIndex) {

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

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

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

    @Override
    public int getXOffset() {
        // this will center the marker-view horizontally
        return -(getWidth() / 2);
    }

    @Override
    public int getYOffset() {
        // this will cause the marker-view to be above the selected value
        return -getHeight();
    }
}

在Fragment Class中,添加

        MyMarkerView mv = new MyMarkerView(this.getActivity(),    R.layout.custom_marker_view);

        // set the marker to the chart
        mChart.setMarkerView(mv);

这对你有帮助..

答案 3 :(得分:0)

以前的答案都没有对我有用,我发现当您将xAxis中的条目从高到低填充时,标记不会出现,甚至可能还会出现其他错误,类似这样的错误会引起错误

int countX = speedTest.size(); 
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < speedTests.size(); i++) {
    SpeedTest st = speedTests.get(i);
    yVals1.add(new BarEntry(countX,st.getResultFloat()));
    countX--;
}

对我来说,解决此问题的唯一方法是将xAxis中的条目从最低到最高填充,因此在经过数小时的调试后,下一个代码对我有用

int countX = 0;
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < speedTests.size(); i++) {
    SpeedTest st = speedTests.get(i);
    yVals1.add(new BarEntry(countX,st.getResultFloat()));
    countX++;
}