MPAndroidChart grouped barchart onclick - android

I've successfully used grouped barchart. I've got two bars per result (one red and one blue bar) but I haven't been able to retrieve X value when I click the blue one, only get the X value when I click the red bar.
LINK TO IMAGE
When I click either bar, the setOnChartValueSelectedListener method is called, but only when I click the red bar I get the XAxis value with this code:
barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, Highlight h) {
if (null != barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis()) && !barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis()).isEmpty()) {
ContentManager.getInstance().setRegion(barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis()));
}
}
#Override
public void onNothingSelected() {
}
});
how can I get the value when I click the blue bar?
barChart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), barChart.getXAxis())
that line returns the value clicking the red bar

FIXED.
Just use Math.floor to use a rounded value on a bar click.

Related

Android bar chart: undo selection

There's some way to undo a selection on Mikephil charting? I have an application which opens an activity when I selects one value in a bar chart. That works fine, however, when I returns to activity that contains the chart, the selection remains. So, when I selects again, the selection is cleared and the activity does not open. What I want is ever I select a value on bar chart the function "onValueSelected" be executed. How can I do that?
That is the code fragment which calls assyncronously an activity when a value is selected.
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
if(e.getVal() == 0);
else {
GetClientesCadastradosDiaAsync task = new GetClientesCadastradosDiaAsync();
task.execute();
}
}
#Override
public void onNothingSelected() {
// do nothing
}
});
At the end of your onValueSelected() method, call:
chart.highlightValues(null);
Now, this will only remove the highlighting. If you select that same bar again, onNothingSelected() will be called.
Therefore, in onNothingSelected(), again call onValueSelected(). You will have to pass the parameters here, but looks like you will only need the Entry parameter, and for the other 2 you can pass null.

How to properly use MPAndroidChart LineChart and selection listener?

I use MPAndroidChart for my project, it's a line chart. I want to achieve when I select the value, the circle stands for the value changes it's color to be red, when I onNothingSelected, it's color is blue. The following is my code:
mLineChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
lineDataSet.setCircleColor(Color.RED);
}
#Override
public void onNothingSelected() {
lineDataSet.setCircleColor(Color.rgb(26, 115, 197));
}
});
If I do like that, when I select the value, all of circle is red. How can I do for what I want to achieve?
The problem is that there is no option to style a single entry in a dataset object.
A solution can be to create an empty data set that has the styling of your choice, and upon clicking an entry, you add that entry to the data set.

How to get selected bar x-axis value using MPAndroidChart?

I am using the MPAndroidChart library in my Android graphs app and I need to display the dialog with a title containing the selected bar's x-axis values.
I referred to this wiki entry for click events to the bars in the bar graph. But now I need to get the selected bar x-axis value as a title. Can any one tell me how to achieve it?
Use the OnChartValueSelectedListener:
#Override
public void onValueSelected(Entry e, Highlight h) {
final String x = chart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), chart.getXAxis());
}
The Highlight object contains additional information regarding the selected position, such as the dataSetIndex, x- and y-position of the selected value in pixels, the selected stack value (in stacked bar chart), ...
Also hava a look at the documentation of highlighting values.
Update: for MPAndroidChart 3.x.x the following works as per this answer:
chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, Highlight h) {
chart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), chart.getXAxis());
}
#Override
public void onNothingSelected() {
}
});
Use the onValueSelected:
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
int position = e.getXIndex();
Log.d("positin", position );
//XValue
final String selectedValue=barchart.getXAxis().getValues().get(position);
Log.d("selctdX", selectedValue);
//YValue
final String selectedYValue = String.valueOf(e.getVal());
Log.d("selctdY", selectedValue);
}
You can get position, xaxis value and yaxis value for selected bar using this code.

MPAndroidChart Changing slice color after clicking

I successfully followed the YouTube tutorial to draw a PieChart in my app using MPAndroidChart, giving each slice of the pie its own color.
I created an OnChartValueSelectedListener so I can know which slice of the pie has been clicked on by the user, like in the following :
public class MyActivity implements OnChartValueSelectedListener {
#Override
public void onNothingSelected() {
// do stuff
}
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h){
Log.i("I clicked on", String.valueOf(e.getXIndex()));
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
...
(PieChart)chart = (PieChart) findViewById(R.id.chart);
...
chart.setOnChartValueSelectedListener(this);
}
}
But even by knowing which slice has been clicked on, I don't seem to find a way to change its color.
The official doc (https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Colors) gives us a way to define and change colors, but only for a dataset, and it seems that a PieChart only have one dataset, so if I change the color of the dataset, every other sliced will see their color changing.
So, I want to know if there is a way, in the following listener
public void onValueSelected(Entry e, int dataSetIndex, Highlight h)
to change the color of the slice which has been clicked on ?
Is it a problem you have already faced ?
Thats quite simple.
Just replace the color value you have set for the DataSet object with a new one.
// get the color(s) you provided for the chart
List<Integer> colors = chart.getData().getDataSetByIndex(dataSetIndex).getColors();
int newcolor = Color.RED;
colors.set(e.getXIndex(), newcolor); // replace the color at the specified index
chart.invalidate(); // refresh

Changing circle color in line chart using mpandroid

I am trying to listen for the click on a particular circle on the line chart so that its (the clicked circle's) color gets changed on click.
Here is what I tried:
LineDataSet set1 = new LineDataSet(yVals, "DataSet 1");
#Override
public void onValueSelected(Entry entry, int i) {
set1.setCircleColorHole(Color.GREEN);
}
What is currently happening is that, on click, all the circles' color is changing. What should happen is that only the circle that has been clicked - its color should change.
You are currently setting the colour for set1 which is the entire dataset. This is why you report that all the circles' color is changing. You need to do this but for a single entity.
According to the documentation for OnChartValueSelectedListener, the method onValueSelected can be overridden to contain an extra parameter, Highlight. Disclaimer I have never used this graphing library before so it may take some experimentation.
Taken directly from the source code for chart found here (line 562 at the time of writing):
https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
/**
* Highlights the values represented by the provided Highlight object
* This DOES NOT generate a callback to the OnChartValueSelectedListener.
*
* #param highlight contains information about which entry should be highlighted
*/
public void highlightValue(Highlight highlight) {
highlightValue(highlight, false);
}
Theoretically this will highlight the value selected. The other methods which do similar things have the following signatures:
public void highlightValue(int xIndex, int dataSetIndex)
public void highlightValues(Highlight[] highs)
public void highlightValue(Highlight high, boolean callListener)

Categories

Resources