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.
Related
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.
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.
I am using MPAndroidChart and I want to show values of xlabes and ylabes with markerview, I have shown the value of y-axis. And I don't konw how to show the value of x-axis(xVals), Can u show me how to do this or give me some tutorial links? Thanks a lot
I suggest you just hand your array of x-values to the MarkerView.
Then in the refreshContent(...) method:
#Override
public void refreshContent(Entry e, Highlight highlight) {
String xValue = xValuesArray.get((int) e.getX());
}
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
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)