与柱形图无太多差别,主要差别在添加数据
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private BarChart barChart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barChart=findViewById(R.id.barChart2);
List<String> namelist = new ArrayList<>();
List<BarEntry> entries = new ArrayList<>();
namelist.add("111");
namelist.add("222");
namelist.add("333");
entries.add(new BarEntry(new float[]{123.0f,456.0f},0));
entries.add(new BarEntry(new float[]{123.0f,456.0f},1));
entries.add(new BarEntry(new float[]{123.0f,456.0f},2));
barChart.setDrawValueAboveBar(false);
barChart.setEnabled(false);
BarDataSet barData1 = new BarDataSet(entries,"京东");
barData1.setColors(new int[]{Color.RED,Color.GREEN,Color.rgb(111,32,16)});
BarData barData = new BarData(namelist,barData1);
barChart.setData(barData);
}
}
网友评论