Mpandroidchart: How to show highlight at once after drawing chart

Created on 22 Feb 2017  路  6Comments  路  Source: PhilJay/MPAndroidChart

Requirement

I'd like to show highlight by default after drawing chat without any touch event.
For example, I'd like to show 36,260 at once after drawing chart.

eeee

Problem

I've tried to use Chart.hightlight(), but it does not work. Here is my code:

private void drawChart(final List<Entry> entries) {
    LineDataSet dataSet = new LineDataSet(entries, "A");
    dataSet.setColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
    dataSet.setHighlightEnabled(true);
    dataSet.setDrawHighlightIndicators(false);
    dataSet.setDrawCircles(false);

    LineData lineData = new LineData(dataSet);
    lineData.setDrawValues(false);
    mChart.setData(lineData);
    mChart.invalidate();

    mChart.getDescription().setEnabled(false);
    mChart.getLegend().setEnabled(false);

    mChart.getAxisRight().setEnabled(false);
    YAxis yAxis = mChart.getAxisLeft();
    yAxis.setDrawGridLines(false);
    yAxis.setTextColor(Color.GRAY);
    yAxis.setLabelCount(3, true);
    yAxis.setAxisMinimum(0.0f);
    yAxis.setDrawZeroLine(true);
    yAxis.setValueFormatter(new LargeValueFormatter());

    XAxis xAxis = mChart.getXAxis();
    xAxis.setTextColor(Color.GRAY);
    xAxis.setValueFormatter(new DayValueFormatter());
    xAxis.setLabelCount(7, true);

    CustomMarkerView markerView = new CustomMarkerView(getActivity(), R.layout.view_chart_marker);
    mChart.setMarkerView(markerView);

    mChart.animateX(1500, Easing.EasingOption.EaseInBack);
    mChart.fitScreen();
    mChart.setPinchZoom(false);
    mChart.setDoubleTapToZoomEnabled(false);

    mTextEmptyChart.setVisibility(View.GONE);
    mChart.highlightValue(entries.get(entries.size() - 1).getY(), entries.size() - 1);
}

Most helpful comment

@603530013 Yes, my solution is mChart.highlightValue(mChart.getHighlighter().getHighlight(mChart.getWidth(), 0));

I call highlightValue(Highlight highlight) instead of highlightValue(float x, int dataSetIndex) where Highlight is gotten from Chart.getHighLighter().

highlightValue(float x, int dataSetIndex) does not work at all, I think that it is a bug.

All 6 comments

Hi, trying to do the same thing. Do you find any solution?

@603530013 Yes, my solution is mChart.highlightValue(mChart.getHighlighter().getHighlight(mChart.getWidth(), 0));

I call highlightValue(Highlight highlight) instead of highlightValue(float x, int dataSetIndex) where Highlight is gotten from Chart.getHighLighter().

highlightValue(float x, int dataSetIndex) does not work at all, I think that it is a bug.

@enginebai
Thanks for your solution, but I still have some problem:
getHighlight(x, y) is the solution when x is known
Is there any method to highlight both min and max y values, but x is unknown at once?

Can you get the x from min/max y values? If yes, I think all you have to do is call highlight(float x, int dataSetIndex). If no, just define a data structure for reverse query from y to x.

@enginebai
Thanks for your help. You can see how I solved my question at #2925 .

Even if this is 2y later, the answer for people searching the same issue after me:

The problem is with Combined charts as far as I've seen.

You can do:

Entry lastEntry = entries.get(entries.size()-1);
Highlight highlight = new Highlight(lastEntry.getX(), lastEntry.getY(), 0);
highlight.setDataIndex(0); // or whatever your linedata/bardata index it has
chart.highlightValue(highlight);

The problem is that mDataIndex is default initialised to -1 and never gets set when you construct a highlight.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

galex picture galex  路  3Comments

Giammaofwar picture Giammaofwar  路  3Comments

JungYongWook picture JungYongWook  路  3Comments

DarkHelmet67 picture DarkHelmet67  路  3Comments

SutharRohit picture SutharRohit  路  3Comments