MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

onValueSelected Highlight.getXPx and Highlight.getYPx always 0 when set highlight value programmatically

Open jndefosse opened this issue 7 years ago • 5 comments

Hi,

I want to get the pos X and Y of the Highlight value when I set the highlight value programmatically

When i use the drag to show highlight value, the onValueSelected methods is called and getXPx and getYPx of the highlight variable are correct.

capture d ecran 2017-04-06 a 10 29 50

But when i set the highlight value programmatically, the onValueSelected methods is called and getXPx and getYPx of the highlight variable always are 0. The variable X and Y are correct but not the position on the view.

capture d ecran 2017-04-06 a 10 29 30

Here is my code to set the value. (The chart is drawed before, Vertical and Horizontal highlight line are draw after onValueSelected)

highlightedEntry = lineChartDataSet.getValues().get(lineChartDataSet.getValues().size()-1);
final Highlight highlight = new Highlight(highlightedEntry.getX(), highlightedEntry.getY(), 0);
mLineChartView.highlightValue(highlight, true);
@Override
public void onValueSelected(Entry e, Highlight h) {
        float markerPositionX = h.getXPx();   //  0
        float markerPositionY = h.getYPx();  // 0
}

jndefosse avatar Apr 06 '17 08:04 jndefosse

Hi,

According to your code above. You use this constructor :

new Highlight(highlightedEntry.getX(), highlightedEntry.getY(), 0)

public Highlight(float x, float y, int dataSetIndex) {

As you can see you're currently setting the dataSetIndex to zero. So you're index is always 0. The getXPx and getYPx are based on the index. If you want to programmatically set the Highlighted Entry, you need to calculate the index yourself

Florian.

AlonsoFloo avatar Apr 06 '17 13:04 AlonsoFloo

The dataSetIndex is the index of the set of data containing in LineData, no?

When Chart highlight a value, the DataSetIndex is 0 too because LineData contains an array of LineDataSet and the first LineDataSet contains my values

jndefosse avatar Apr 06 '17 13:04 jndefosse

I'm seeing this too. xPx and yPx should be calculated by the highlighter and have nothing to do with the dataSetIndex being zero.

davidgoli avatar Sep 19 '17 16:09 davidgoli

The paradigm for highlights is that they are initiated by touching the screen. So xPx and yPx are assumed to be known a priori and getHighlight(xPx, yPx) calculates the corresponding data values (mX and mY) by calling ChartHighlighter.getValsForTouch(xPx, yPx) .

You are trying to work backward, and will have to compute the pixel values yourselves: mChart.getTransformer(axisDependency).getPixelForValues(mX, mY). Then you can use the complete constructor: Highlight(float x, float y, float xPx, float yPx, int dataSetIndex, YAxis.AxisDependency axis).

This cannot be done by Highlight(float x, float y, int dataSetIndex) because it has no idea of which transformer to use.

Perhaps the Highlight(float x, float y, int dataSetIndex) constructor should be removed.

regas99 avatar Mar 28 '19 01:03 regas99

public void onValueSelected(Entry e, Highlight h) { int x= (int) h.getX(); int y= (int) h.getY();}

prathamesh29 avatar Mar 16 '23 11:03 prathamesh29