If you want to make a simple graph with MPAndroidChart, On the contrary, it was difficult to make a simple graph because of the rich functions. It became ver3.0.2 and it was a little different from the previous version (x-axis label display etc ...)
I wanted to make a simple bar graph like this.
・ Display label on x-axis ・ Set any color for each graph
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);
//Y axis(left)
YAxis left = chart.getAxisLeft();
left.setAxisMinimum(0);
left.setAxisMaximum(100);
left.setLabelCount(5);
left.setDrawTopYLabelEntry(true);
//In integer display
left.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return "" + (int)value;
}
});
//Y axis(right)
YAxis right = chart.getAxisRight();
right.setDrawLabels(false);
right.setDrawGridLines(false);
right.setDrawZeroLine(true);
right.setDrawTopYLabelEntry(true);
//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 = {"","National language", "Math", "English"};
xAxis.setValueFormatter(new IndexAxisValueFormatter(labels));
XAxis bottomAxis = chart.getXAxis();
bottomAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
bottomAxis.setDrawLabels(true);
bottomAxis.setDrawGridLines(false);
bottomAxis.setDrawAxisLine(true);
//Display on the graph
chart.setDrawValueAboveBar(true);
chart.getDescription().setEnabled(false);
chart.setClickable(false);
//Usage Guide
chart.getLegend().setEnabled(false);
chart.setScaleEnabled(false);
//animation
chart.animateY(1200, Easing.EasingOption.Linear);
}
//Get bar chart data
private List<IBarDataSet> getBarData(){
//Data to be displayed
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(1, 60));
entries.add(new BarEntry(2, 80));
entries.add(new BarEntry(3, 70));
List<IBarDataSet> bars = new ArrayList<>();
BarDataSet dataSet = new BarDataSet(entries, "bar");
//Display as an integer
dataSet.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return "" + (int) value;
}
});
//Don't highlight
dataSet.setHighlightEnabled(false);
//Set the color of the bar
dataSet.setColors(new int[]{R.color.material_blue, R.color.material_green, R.color.material_yellow}, this);
bars.add(dataSet);
return bars;
}
}
To display a label on the x-axis It feels like converting the numerical value displayed on the x-axis of the data into a label. Arbitrary color setting for each graph can be executed by setting the color list in the data list. In addition, by default, you can zoom in and out, tap to highlight, etc. I had to do quite a lot to adjust it because there were a lot of scales.
reference Labels on the x-axis (https://github.com/PhilJay/MPAndroidChart/issues/2044) Color for each graph (https://stackoverflow.com/questions/38872181/mpandroidchart-bar-chart-how-to-change-color-of-each-label)
Recommended Posts