3

我有以下标记视图:

public class DiagramLineMarker extends MarkerView {
    private TextView textViewValue = null;

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

        this.textViewValue = (TextView) this.findViewById(R.id.line_fragment_marker_textview_value);
    }

    @Override
    public void refreshContent(Entry entry, int dataSetIndex) {
        this.textViewValue.setText("" + entry.getVal());
    }

    @Override
    public float getXOffset() {
        return - (getWidth() / 2);
    }

    @Override
    public float getYOffset() {
        return - getHeight();
    }
}

我将其设置为LineChartActivity1MPAndroidChart 示例中所示:

this.lineChart.setMarkerView(new DiagramLineMarker(this.getActivity(), R.layout.activity_diagrams_line_fragment_marker));

不幸的是,当我选择一个值时没有任何反应。

完整实现:

public class DiagramLineFragment extends DiagramBaseFragment {
    private LineChart lineChart = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setRetainInstance(true);
        this.setHasOptionsMenu(true);

        this.sessionLoginPreferences = new SessionLoginPreferences(this.getActivity());
        this.sessionLoginSingleton = SessionLoginSingleton.getInstance(this.getActivity());
        this.sessionProfilePreferences = new SessionProfilePreferences(this.getActivity());
        this.sessionMeasureDataPreferences = new SessionMeasureDataPreferences(this.getActivity());
        this.sessionMeasureDataList = SessionMeasureDataList.getInstance();
        this.sessionMeasureDataListFavourite = SessionMeasureDataListFavourite.getInstance();

        if(this.sessionMeasureDataList.getMeasureDataList() == null) {
            this.sessionMeasureDataList.setMeasureDataList(new MeasureDataList());
        }

        if(this.sessionMeasureDataListFavourite.getMeasureDataList() == null) {
            this.sessionMeasureDataListFavourite.setMeasureDataList(new MeasureDataList());
        }

        Bundle extras = this.getActivity().getIntent().getExtras();

        if(extras != null) {
            if(extras.getString(Globals.KEY_EMAIL_FAVOURITE) != null) {
                this.favourite = true;
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_diagrams_line_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.lineChart = (LineChart) view.findViewById(R.id.line_fragment_line_chart);
        this.lineChart.setDrawUnitsInChart(false);
        this.lineChart.setStartAtZero(false);
        this.lineChart.setDrawYValues(false);
        this.lineChart.setDrawXLabels(false);
        this.lineChart.setDrawBorder(false);
        this.lineChart.setDrawLegend(true);
        this.lineChart.setDescription("");
        this.lineChart.setNoDataTextDescription("");
        this.lineChart.setHighlightEnabled(true);
        this.lineChart.setTouchEnabled(true);
        this.lineChart.setDragEnabled(true);
        this.lineChart.setScaleEnabled(true);
        this.lineChart.setPinchZoom(true);
        this.lineChart.setMarkerView(new DiagramLineMarker(this.getActivity(), R.layout.activity_diagrams_line_fragment_marker));
        this.lineChart.setHighlightIndicatorEnabled(true);
        this.lineChart.setDrawGridBackground(false);
    }

    @Override
    public void onResume() {
        super.onResume();

        this.fragmentBecameVisible();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.diagram_activity_actions, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if((this.sessionMeasureDataListFavourite != null || this.sessionMeasureDataList != null) && this.lineChart != null) {
            this.fragmentBecameVisible();
        }
    }

    public void fragmentBecameVisible() {
        this.initData();
    }

    private void initData() {
        List<MeasureDataListEntry> measureDataListEntry = null;

        if(this.favourite) {
            measureDataListEntry = this.sessionMeasureDataListFavourite.getMeasureDataList().getMeasureDataListEntries();
        } else {
            measureDataListEntry = this.sessionMeasureDataList.getMeasureDataList().getMeasureDataListEntries();
        }

        ArrayList<String> xValues = new ArrayList<String>();
        ArrayList<Entry> yValuesMonday = new ArrayList<Entry>();
        ArrayList<Entry> yValuesTuesday = new ArrayList<Entry>();
        ArrayList<Entry> yValuesWednesday = new ArrayList<Entry>();
        ArrayList<Entry> yValuesThursday = new ArrayList<Entry>();
        ArrayList<Entry> yValuesFriday = new ArrayList<Entry>();
        ArrayList<Entry> yValuesSaturday = new ArrayList<Entry>();
        ArrayList<Entry> yValuesSunday = new ArrayList<Entry>();

        for(int i = 0; i < measureDataListEntry.size(); i++) {
            String StringValue = measureDataListEntry.get(i).getTime();
            Long value = Long.parseLong(StringValue) * 1000;
            DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
            String date = dateFormat.format(new Date(value));

            xValues.add(date);
        }

        for(int i = 0; i < measureDataListEntry.size(); i++) {
            String stringValue = measureDataListEntry.get(i).getValue();
            int dayOfWeek = Helper.getDayOfWeek(measureDataListEntry.get(i).getTime());
            float value = Float.parseFloat(stringValue);

            switch(dayOfWeek) {
            case Calendar.MONDAY:
                yValuesMonday.add(new Entry(value, i));
                break;
            case Calendar.TUESDAY:
                yValuesTuesday.add(new Entry(value, i));
                break;
            case Calendar.WEDNESDAY:
                yValuesWednesday.add(new Entry(value, i));
                break;
            case Calendar.THURSDAY:
                yValuesThursday.add(new Entry(value, i));
                break;
            case Calendar.FRIDAY:
                yValuesFriday.add(new Entry(value, i));
                break;
            case Calendar.SATURDAY:
                yValuesSaturday.add(new Entry(value, i));
                break;
            case Calendar.SUNDAY:
                yValuesSunday.add(new Entry(value, i));
                break;
            }
        }

        LineDataSet lineDataSetMonday = new LineDataSet(yValuesMonday, this.getResources().getString(R.string.text_monday));

        lineDataSetMonday.enableDashedLine(10f, 5f, 0f);
        lineDataSetMonday.setColor(this.getResources().getColor(R.color.diagramGreenColor));
        lineDataSetMonday.setCircleColor(this.getResources().getColor(R.color.diagramGreenColor));
        lineDataSetMonday.setLineWidth(1f);
        lineDataSetMonday.setCircleSize(4f);
        lineDataSetMonday.setFillAlpha(65);
        lineDataSetMonday.setFillColor(this.getResources().getColor(R.color.diagramGreenColor));

        LineDataSet lineDataSetTuesday = new LineDataSet(yValuesTuesday, this.getResources().getString(R.string.text_tuesday));

        lineDataSetTuesday.enableDashedLine(10f, 5f, 0f);
        lineDataSetTuesday.setColor(this.getResources().getColor(R.color.diagramOrangeColor));
        lineDataSetTuesday.setCircleColor(this.getResources().getColor(R.color.diagramOrangeColor));
        lineDataSetTuesday.setLineWidth(1f);
        lineDataSetTuesday.setCircleSize(4f);
        lineDataSetTuesday.setFillAlpha(65);
        lineDataSetTuesday.setFillColor(this.getResources().getColor(R.color.diagramOrangeColor));

        LineDataSet lineDataSetWednesday = new LineDataSet(yValuesWednesday, this.getResources().getString(R.string.text_wednesday));

        lineDataSetWednesday.enableDashedLine(10f, 5f, 0f);
        lineDataSetWednesday.setColor(this.getResources().getColor(R.color.diagramPinkColor));
        lineDataSetWednesday.setCircleColor(this.getResources().getColor(R.color.diagramPinkColor));
        lineDataSetWednesday.setLineWidth(1f);
        lineDataSetWednesday.setCircleSize(4f);
        lineDataSetWednesday.setFillAlpha(65);
        lineDataSetWednesday.setFillColor(this.getResources().getColor(R.color.diagramPinkColor));

        LineDataSet lineDataSetThursday = new LineDataSet(yValuesThursday, this.getResources().getString(R.string.text_thursday));

        lineDataSetThursday.enableDashedLine(10f, 5f, 0f);
        lineDataSetThursday.setColor(this.getResources().getColor(R.color.diagramBlackColor));
        lineDataSetThursday.setCircleColor(this.getResources().getColor(R.color.diagramBlackColor));
        lineDataSetThursday.setLineWidth(1f);
        lineDataSetThursday.setCircleSize(4f);
        lineDataSetThursday.setFillAlpha(65);
        lineDataSetThursday.setFillColor(this.getResources().getColor(R.color.diagramBlackColor));

        LineDataSet lineDataSetFriday = new LineDataSet(yValuesFriday, this.getResources().getString(R.string.text_friday));

        lineDataSetFriday.enableDashedLine(10f, 5f, 0f);
        lineDataSetFriday.setColor(this.getResources().getColor(R.color.diagramBlueColor));
        lineDataSetFriday.setCircleColor(this.getResources().getColor(R.color.diagramBlueColor));
        lineDataSetFriday.setLineWidth(1f);
        lineDataSetFriday.setCircleSize(4f);
        lineDataSetFriday.setFillAlpha(65);
        lineDataSetFriday.setFillColor(this.getResources().getColor(R.color.diagramBlueColor));

        LineDataSet lineDataSetSaturday = new LineDataSet(yValuesSaturday, this.getResources().getString(R.string.text_saturday));

        lineDataSetSaturday.enableDashedLine(10f, 5f, 0f);
        lineDataSetSaturday.setColor(this.getResources().getColor(R.color.diagramRedColor));
        lineDataSetSaturday.setCircleColor(this.getResources().getColor(R.color.diagramRedColor));
        lineDataSetSaturday.setLineWidth(1f);
        lineDataSetSaturday.setCircleSize(4f);
        lineDataSetSaturday.setFillAlpha(65);
        lineDataSetSaturday.setFillColor(this.getResources().getColor(R.color.diagramRedColor));

        LineDataSet lineDataSetSunday = new LineDataSet(yValuesSunday, this.getResources().getString(R.string.text_sunday));

        lineDataSetSunday.enableDashedLine(10f, 5f, 0f);
        lineDataSetSunday.setColor(this.getResources().getColor(R.color.diagramYellowColor));
        lineDataSetSunday.setCircleColor(this.getResources().getColor(R.color.diagramYellowColor));
        lineDataSetSunday.setLineWidth(1f);
        lineDataSetSunday.setCircleSize(4f);
        lineDataSetSunday.setFillAlpha(65);
        lineDataSetSunday.setFillColor(this.getResources().getColor(R.color.diagramYellowColor));

        ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();

        dataSets.add(lineDataSetMonday);
        dataSets.add(lineDataSetTuesday);
        dataSets.add(lineDataSetWednesday);
        dataSets.add(lineDataSetThursday);
        dataSets.add(lineDataSetFriday);
        dataSets.add(lineDataSetSaturday);
        dataSets.add(lineDataSetSunday);

        LineData data = new LineData(xValues, dataSets);

        if(!this.favourite) {
            float hyper = (float)this.sessionProfilePreferences.getProfile(this.getEmail()).getHyperGlycemia();
            float hypo = (float)this.sessionProfilePreferences.getProfile(this.getEmail()).getHypoGlycemia();

            if(hyper > 0.0) {
                LimitLine limitLineHyper = new LimitLine(hyper);
                limitLineHyper.setLineWidth(4f);
                limitLineHyper.enableDashedLine(10f, 10f, 0f);
                limitLineHyper.setDrawValue(true);
                limitLineHyper.setLabelPosition(LimitLabelPosition.RIGHT);

                data.addLimitLine(limitLineHyper);
            }

            if(hypo > 0.0) {
                LimitLine limitLineHypo = new LimitLine(hypo);
                limitLineHypo.setLineWidth(4f);
                limitLineHypo.enableDashedLine(10f, 10f, 0f);
                limitLineHypo.setDrawValue(true);
                limitLineHypo.setLabelPosition(LimitLabelPosition.RIGHT);

                data.addLimitLine(limitLineHypo);
            }
        }

        this.lineChart.setData(data);
        this.lineChart.animateXY(1000, 1000);
    }
}

标记的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/line_fragment_marker_textview_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="7dp"
        android:gravity="center"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/textcolorprimary"
        android:textSize="12sp" />

</LinearLayout>
4

3 回答 3

3

我发现了问题。

您必须在 refreshContent 函数上调用 super 来更新 MarkerView。但是使用新的lib版本。请参见下面的代码:

@Override
public void refreshContent(Entry e, Highlight highlight) {

    tvContent.setText("" + e.getY());

    // this will perform necessary layouting
    super.refreshContent(e, highlight);
}
于 2018-03-23T13:07:33.550 回答
1

在标记的布局中尝试设置layout_width="50dp"。给它一个特定的宽度同样适用于 textview layout_width。我在显示时也遇到了问题,然后通过设置 layout_width 属性,它得到了解决。

于 2020-06-03T04:28:17.857 回答
0

这真的很奇怪。实际上你的代码应该可以工作。您是否确保您所做的一切都与示例中的完全一样LineChartActivity1

也许你可以尝试提供一种不同的Context? (而不是this.getActivity()

您也可以尝试将用于设置的代码移动LineChartonCreateView(...)方法中。

于 2015-02-20T14:56:49.330 回答