My code:
public BarData getBarData(String fieldName) {
ArrayList entries = new ArrayList<>();
entries.add(new BarEntry
It's working version of Barchart with new BarData(datasets); here is code with xml. you needto edit tools:context= in xml.
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
public class SubActivity_05 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_05);
BarChart bchart = (BarChart) findViewById(R.id.chart1);
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = (int) 0; i < 10 + 1; i++) {
float val = (float) (Math.random());
yVals1.add(new BarEntry(i, val));
}
BarDataSet set1;
set1 = new BarDataSet(yVals1, "The year 2017");
set1.setColors(ColorTemplate.MATERIAL_COLORS);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(0.9f);
bchart.setTouchEnabled(false);
bchart.setData(data);
}
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.jjikmeok.SubActivity_05">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
It's good to check out tutorial page. https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started
For version 3 you must use like this
// the labels that should be drawn on the XAxis
final String[] quarters = new String[] { "Q1", "Q2", "Q3", "Q4" };
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return quarters[(int) value];
}
// we don't draw numbers, so no decimal digits needed
@Override
public int getDecimalDigits() { return 0; }
};
XAxis xAxis = mLineChart.getXAxis();
xAxis.setGranularity(1f); // minimum axis-step (interval) is 1
xAxis.setValueFormatter(formatter);
Using the newest version (3.0.0-beta1) of MPAndroidChart?
The constructor of this class has changed:
public BarData(List<IBarDataSet> dataSets) {
super(dataSets);
}
This commit has removed X-axis labels.
Look into this example as it provides a new way to use a library.
Because you are using the latest version which includes major changes. Replace dependency version to this : implementation 'com.github.PhilJay:MPAndroidChart:v2.2.4'