I happened to have the opportunity to develop a simple app using Android Studio. When I wanted to draw a graph in it, I found that I should use a library called "MPAndroidChart", so I made a bar graph while referring to some sites. However, I had a hard time setting the x-axis label for about an hour, so I will describe the process and the solution. (If you have the spare capacity, I would like to paste the image that was actually moved later)
The part that is not related to this problem is largely broken, but the outline is as follows. I refer to this article.
"Creating a simple bar graph with MPAndroidChart" https://qiita.com/iKimishima/items/7fd192a074739cf5290b
MainActivity.java
public class MainActivity extends AppCompatActivity {
protected BarChart chart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chart = (BarChart) findViewById(R.id.chart1);
//Display data acquisition
BarData data = new BarData(getBarData());
chart.setData(data);
//X axis
XAxis xAxis = chart.getXAxis();
//List of Labels to display on the X-axis(the first""Is the position of the origin)
final String[] labels = {"","Apple", "Mandarin orange", "Peaches"};
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
}
}
Especially this last part.
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
Even on other sites, etc., what is defined in advance as a String array is given as an argument to new IndexAxisValueFormatter, but for some reason the axis may not be displayed properly depending on the data to be assigned. (Originally, apples, oranges, and thighs should be labeled on the three elements, but for some reason, the three elements are labeled as oranges, thighs, and thighs (without labels). Just the contents of labels. It worked in some cases)
Regardless of the reason, I had the feeling that "just move fast", so when I looked it up, I arrived at the following site.
"MPAndroidChart-Adding Labels to Bar Charts" https://www.366service.com/jp/qa/94329e5e85f6eddc6886945506ec0759
Here, there is an explanation that the label is set by creating a formatter without using IndexAxisValueFormatter. The graph was fixed by inserting LabelFormatter into the class you want to use and adjusting the argument of setValueFormatter accordingly.
hoge.java
//Until a while ago
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
//After update
xAxis.setValueFormatter(new LabelFormatter(labels));
fuga.java
public class LabelFormatter implements IAxisValueFormatter {
private final String[] mLabels;
public LabelFormatter(String[] labels) {
mLabels = labels;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mLabels[(int) value];
}
}
I wasn't sure why the IndexAxisValueFormatter didn't work as intended. I think it has something to do with the version of the library ... Surprisingly, there were few articles around MPAndroidChart, and I could not find a Japanese site that hits a similar wall, so I made it an article although it is a collection of knowledge. For the actual code etc., I think it is better to refer to the following article.
There are many items to set for graphs, and I couldn't find one that was organized in an easy-to-understand manner, so I wanted to organize it myself if I had the opportunity.
"Creating a simple bar graph with MPAndroidChart" https://qiita.com/iKimishima/items/7fd192a074739cf5290b "MPAndroidChart-Adding Labels to Bar Charts" https://www.366service.com/jp/qa/94329e5e85f6eddc6886945506ec0759
Recommended Posts