public class InputData {
private String location;
private float amount;
private int dayOfWeek;
public InputData(String location, float amount, int dayOfWeek) {
this.location = location;
this.amount = amount;
this.dayOfWeek = dayOfWeek;
}
public String getLocation() {
return location;
}
public float getAmount() {
return amount;
}
public int getDayOfWeek() {
return dayOfWeek;
}
}
@SuppressLint("MissingInflatedId")
public class DataStatus extends AppCompatActivity implements View.OnClickListener{
private BarChart barChart;
private YAxis leftAxis; //左侧Y轴
private YAxis rightAxis; //右侧Y轴
private XAxis xAxis; //X轴
private Legend legend; //图例
Button btn_line ,btn_pie,btn_bar;
ViewFlipper viewFlipper;
private static PieChart pieChart;
private static LineChart chart;
private static String PIECHART = "PieChart";
private static String BARCHART = "BarChart";
private static String LINECHART = "LineChart";
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_bill_chart);
initView();
}
public void initView(){
chart = findViewById(R.id.linechart);
barChart = findViewById(R.id.barchart);
pieChart = findViewById(R.id.piechart);
btn_line = findViewById(R.id.btn_line);
btn_pie = findViewById(R.id.btn_pie);
btn_bar = findViewById(R.id.btn_bar);
viewFlipper = findViewById(R.id.viewfilpper);
btn_line.setOnClickListener(this);
btn_bar.setOnClickListener(this);
btn_pie.setOnClickListener(this);
}
@Override
public void onClick(View v) {
List<InputData> inputDataList = new ArrayList<>();
inputDataList.add(new InputData("转账",10f,0));
inputDataList.add(new InputData("红包",50f,1));
inputDataList.add(new InputData("买花",1f,2));
inputDataList.add(new InputData("心思",3f,3));
inputDataList.add(new InputData("购物",50f,4));
inputDataList.add(new InputData("餐饮",1f,5));
inputDataList.add(new InputData("交通",3f,6));
if (v.getId()==R.id.btn_line){
viewFlipper.setDisplayedChild(0);
showChart(inputDataList,LINECHART);
} else if (v.getId() == R.id.btn_pie) {
viewFlipper.setDisplayedChild(1);
showChart(inputDataList,PIECHART);
}else if (v.getId() == R.id.btn_bar){
viewFlipper.setDisplayedChild(2);
showChart(inputDataList,BARCHART);
}
}
/**
* 初始化数据 InputData分别是三个成员变量 作用 日期 数量
* 通过type去返回需要的数据
* @param inputData
* @param type
* @return
*/
public static ArrayList<?> initLineeData(List<InputData> inputData,String type){
if (type.equals(PIECHART)){
//
ArrayList<PieEntry> pieEntries = new ArrayList<>();
for (InputData data : inputData) {
PieEntry pieEntry = new PieEntry(data.getAmount(), data.getLocation()); // 数量和作用
pieEntries.add(pieEntry);
}
return pieEntries;
}else if (type.equals(LINECHART)){
ArrayList<Entry> lineEntries = new ArrayList<>();
for (InputData data:inputData){
Entry entry = new Entry(data.getDayOfWeek(),data.getAmount());//数量和时间
lineEntries.add(entry);
}
return lineEntries;
}else if (type.equals(BARCHART)){
ArrayList<BarEntry> barEntries = new ArrayList<>();
for (InputData data: inputData){
BarEntry barEntry = new BarEntry(data.getDayOfWeek(),data.getAmount());//数量和时间
barEntries.add(barEntry);
}
return barEntries;
}
return null;
}
/**
* 设置图表的样式
* @param object
* @param type
*/
public void setConfig(Object object ,String type){
if (type.equals(BARCHART)){
BarChart barChart = (BarChart) object;
barChart.setBackgroundColor(Color.WHITE);
//不显示图表网格
barChart.setDrawGridBackground(false);
//背景阴影
barChart.setDrawBarShadow(false);
barChart.setHighlightFullBarEnabled(false);
//显示边框
barChart.setDrawBorders(true);
//设置动画效果
barChart.animateY(1000, Easing.Linear);
barChart.animateX(1000, Easing.Linear);
/***XY轴的设置***/
//X轴设置显示位置在底部
xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisMinimum(0f);
xAxis.setGranularity(1f);
leftAxis = barChart.getAxisLeft();
rightAxis = barChart.getAxisRight();
//保证Y轴从0开始,不然会上移一点
leftAxis.setAxisMinimum(0f);
rightAxis.setAxisMinimum(0f);
/***折线图例 标签 设置***/
legend = barChart.getLegend();
legend.setForm(Legend.LegendForm.LINE);
legend.setTextSize(11f);
//显示位置
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
}else if (type.equals(PIECHART)){
PieChart pieChart = (PieChart) object;
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleRadius(40f);//设置中间洞的大小
// 半透明圈
pieChart.setTransparentCircleRadius(30f);
pieChart.setTransparentCircleColor(Color.WHITE); //设置半透明圆圈的颜色
pieChart.setTransparentCircleAlpha(125); //设置半透明圆圈的透明度
pieChart.setRotationAngle(0);// 初始旋转角度
pieChart.setRotationEnabled(true);// 可以手动旋转
pieChart.setUsePercentValues(true);//显示成百分比
pieChart.getDescription().setEnabled(false); //取消右下角描述
Legend legend = pieChart.getLegend();
legend.setOrientation(Legend.LegendOrientation.VERTICAL); //设置图例水平显示
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); //顶部
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); //右对其
legend.setXEntrySpace(7f);//x轴的间距
legend.setYEntrySpace(10f); //y轴的间距
legend.setYOffset(10f); //图例的y偏移量
legend.setXOffset(10f); //图例x的偏移量
legend.setTextColor(Color.parseColor("#000000")); //图例文字的颜色
legend.setTextSize(10); //图例文字的大小
}else if (type.equals(LINECHART)){
LineChart lineChart = (LineChart)object;
lineChart.setDrawBorders(true);
//设置动画效果
lineChart.animateY(1000, Easing.Linear);
lineChart.animateX(1000, Easing.Linear);
XAxis xAxis = lineChart.getXAxis();
xAxis.setGranularity(1f);
}
}
/**
*展示图表
* @param type
*/
public void showChart(List<InputData> inputDataList,String type){
if (type.equals(PIECHART)){
setConfig(pieChart,PIECHART);
ArrayList<PieEntry> pieEntryArrayList = (ArrayList<PieEntry>) initLineeData(inputDataList,PIECHART);
PieDataSet pieDataSet = new PieDataSet(pieEntryArrayList, "");
PieData pieData = new PieData(pieDataSet);
pieData.setValueFormatter(new PercentFormatter());
pieChart.setData(pieData);
}else if (type.equals(LINECHART)){
setConfig(chart,LINECHART);
ArrayList<Entry> lineEntrylist = (ArrayList<Entry>) initLineeData(inputDataList, LINECHART);
LineDataSet lineDataSet = new LineDataSet(lineEntrylist,"");
LineData lineData = new LineData(lineDataSet);
chart.setData(lineData);
}else if (type.equals(BARCHART)){
setConfig(barChart,BARCHART);
ArrayList<BarEntry> barEntriesList = (ArrayList<BarEntry>) initLineeData(inputDataList,BARCHART);
BarDataSet barDataSet = new BarDataSet(barEntriesList,"");
BarData barData = new BarData(barDataSet);
barChart.setData(barData);
}
}
}
个人博客:https://yunhao.fun