MPAndroidchart how to display label inside MarkerView? - android

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());
}

Related

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)

Achartengine: setClickListener() on x labels

I use Achartengine 1.1.0. Is posible to setClickListener() on x label's view? For exemple when i click "Dec" i want to show a toast: "December =14240"
There is not any direct method to get the touch event on labels.
But by getting the touched position and implementing this, you can achieve what u want.
You will need to check that position touched is of ur label.
Then do what u want.
To get the touched position see this
aChartEngine: getting coordinates of any point on the graph area
Also u can use
mChartView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
System.out.println("X :" + mChartView.toRealPoint(0)[0]);
System.out.println("Y :" + mChartView.toRealPoint(0)[1]);
}
});
and check x.y values for labels positions.

Categories

Resources