MPAndroidChart: как добавить подсветку к значениям LimiteLine по оси Y - PullRequest
0 голосов
/ 30 апреля 2020

Я пытаюсь выделить LimiteLine (верхний и нижний пределы), как показано в Запрос извлечения GitHub: выделите ось # 4477 .

ниже это изображение, используемое в качестве примера:

enter image description here

И не только это, я хотел бы настроить прямоугольный черный ящик, который содержит выделенный значения.

Я также хотел бы, чтобы они были исправлены, я имею в виду: я не хочу, чтобы они исчезали при прикосновении к диаграмме, показывая при этом MarkerView для других точек на графике.

Но мне не ясно, как этого добиться.

Я использую MP Android версии 3.1.0 и LineChart

Я создаю свой LineChart из примера, показанного в https://blog.csdn.net/a8688555/article/details/80344159:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_settings, container, false);

    mLineChart = view.findViewById(R.id.settings_graph);
    List<Entry> entries = new ArrayList<>();
    for (int i = 0; i < 12; i++)
        entries.add(new Entry(i, new Random().nextInt(300)));

    LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset
    dataSet.setDrawCircles(false);
    dataSet.setColor(Color.parseColor("#7d7d7d"));
    dataSet.setCircleColor(Color.parseColor("#7d7d7d"));
    dataSet.setLineWidth(3f);
    mLineChart.setScaleEnabled(false);
    mLineChart.setExtraRightOffset(15f);
    mLineChart.setExtraLeftOffset(30f);

    LimitLine upper_limit = new LimitLine(dataSet.getYMax(), "Preco maximo");
    upper_limit.setLineWidth(2f);
    upper_limit.enableDashedLine(10f, 10f, 0f);
    upper_limit.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
    upper_limit.setTextSize(15f);

    LimitLine lower_limit = new LimitLine(dataSet.getYMin(), "Preco minimo");
    lower_limit.setLineWidth(2f);
    lower_limit.enableDashedLine(10f, 10f, 0f);
    lower_limit.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
    lower_limit.setTextSize(15f);

    // Setup Y-Axis
    YAxis rightAxis = mLineChart.getAxisRight();
    // Disable Y-axis on the right
    rightAxis.setEnabled(true);
    // Disable Y grid lines
    rightAxis.setDrawGridLines(false);

    rightAxis.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            return super.getFormattedValue(value);
        }
    });

    YAxis leftAxis = mLineChart.getAxisLeft();
    //  Disable Y-axis on the left
    leftAxis.setEnabled(false);
    leftAxis.setDrawGridLines(false);
    rightAxis.setAxisMaximum(dataSet.getYMax() + (float) (dataSet.getYMax() * 0.1));
    leftAxis.setAxisMaximum(dataSet.getYMax() + (float) (dataSet.getYMax() * 0.1));
    // Set max and min values
    rightAxis.removeAllLimitLines();
    rightAxis.addLimitLine(upper_limit);
    rightAxis.addLimitLine(lower_limit);
    rightAxis.setDrawLimitLinesBehindData(true);

    // Setup X-Axis
    XAxis xAxis = mLineChart.getXAxis();
    xAxis.setTextColor(Color.parseColor("#333333"));
    xAxis.setTextSize(11f);
    xAxis.setAxisMinimum(0f);
    xAxis.setDrawAxisLine(true);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawLabels(true);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setGranularity(1f);

    String[] mValues = {"Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"};
    xAxis.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            return mValues[(int) value];
        }
    });


    // Transparent legend
    Legend legend = mLineChart.getLegend();
    legend.setForm(Legend.LegendForm.NONE);
    legend.setTextColor(Color.WHITE);


    mLineChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
        @Override
        public void onValueSelected(Entry e, Highlight h) {
            Log.i(TAG, "onValueSelected: highlight is: "+h);

            if (mLineChart.isMarkerAllNull()) {
                Log.i(TAG, "onValueSelected: isMarkerAllNull()");
                // adding the markers
                createMakerView();
                // highlight the custom marker
                mLineChart.highlightValue(h);
            }
        }

        @Override
        public void onNothingSelected() {

        }
    });

    // text on X-axis
    Description description = new Description();
    description.setEnabled(false);
    mLineChart.setDescription(description);

    // create objects on the chart
    createMakerView();

    //3. setup data
    LineData lineData = new LineData(dataSet);
    // Draw text over line
    lineData.setDrawValues(false);
    mLineChart.setData(lineData);
    mLineChart.animateXY(1000, 500);
    mLineChart.invalidate(); // refresh


    // Inflate the layout for this fragment
    return view;
}

public void createMakerView() {
    DetailsMarkerView detailsMarkerView = new DetailsMarkerView(getContext());
    detailsMarkerView.setChartView(mLineChart);
    mLineChart.setDetailsMarkerView(detailsMarkerView);
    mLineChart.setPositionMarker(new PositionMarker(getContext()));
    mLineChart.setRoundMarker(new RoundMarker(getContext()));
}

На изображении ниже показан результат, которого я пытаюсь достичь:

enter image description here

Я ценю любую помощь!

...