PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.68k stars 9.02k forks source link

Get Highlight X position #3547

Open lucasharada opened 7 years ago

lucasharada commented 7 years ago

I'm currently highlighting an Entry based on promixity with my SeekBar and it's working like a charm. Now I need to show a label near to the Entry which is Highlighted.

I found a related problem on #2993 , but it didn't fix my problem. Instead, changing from

mChart.highlightValue(mData.get(getNearestXValue(progress)).getX(), 0);

to

Entry entry = mData.get(getNearestXValue(progress));
mChart.highlightValue(entry.getX(), mData.indexOf(entry));

made the call mChart.getHighlight() return null instead of one Highlight. Also, the Highlight only appeared when targetting the first Entry point.

The rest of my code follow below:

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ABOVE, seekBar.getId());
Highlight[] hs = mChart.getHighlighted(); // returns null
p.setMargins(Math.round(hs[0].getXPx()), Math.round(hs[0].getYPx()), 0, 0);
lucasharada commented 7 years ago

Tried to create a Highlight object and use it to get the XPx and YPx, but I keep getting those values as 0. This is what I tried:

final Entry entry = mData.get(getNearestXValue(progress));
final Highlight highlight = new Highlight(entry.getX(), entry.getY(), getNearestXValue(progress));
mChart.highlightValue(highlight);

RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ABOVE, seekBar.getId());

p.setMargins(
        Math.round(highlight.getXPx()), Math.round(highlight.getYPx()), 0, 0
); // XPx: 0; YPx: 0;