直接上代碼,xml就基本的LineChart與BarChart? 效果圖在最后
public class LineChartTestextends AppCompatActivity {
LineChartlc;
BarChartbc,bc1;
TextViewtextView;
@Override
? ? protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_line_chart_test);
lc = findViewById(R.id.chart1);
bc = findViewById(R.id.barchart1);
bc1 = findViewById(R.id.barchart2);
textView = findViewById(R.id.tev_test);
setXAxis();
setYAxis();
loadData();
setviewListener();
}
private void setviewListener() {
textView.setOnClickListener(new View.OnClickListener() {
@Override
? ? ? ? ? ? public void onClick(View v) {
float datas[] = {1,0,3,2,6,7,3,4,9};
List yData =new ArrayList();
for (int i =0; i < datas.length; i++) {
yData.add(new Entry(i, datas[datas.length-i-1]));
}
LineDataSet lineDataSet =new LineDataSet(yData,"");
lineDataSet.setLineWidth(1);//設(shè)置線條寬度
? ? ? ? ? ? ? ? lineDataSet.setColor(Color.BLUE);//設(shè)置線條顏色
? ? ? ? ? ? ? ? lineDataSet.setCircleColor(Color.BLUE);//設(shè)置圓點(diǎn)顏色
? ? ? ? ? ? ? ? lineDataSet.setCircleRadius(2);//設(shè)置圓的大小
? ? ? ? ? ? ? ? lineDataSet.setDrawValues(false);//不繪制文本
//? ? ? ? lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);//設(shè)置為貝塞爾曲線
? ? ? ? ? ? ? ? lineDataSet.setCubicIntensity(0.1f);//曲線強(qiáng)度0.1
? ? ? ? ? ? ? ? lineDataSet.setDrawCircleHole(false);//設(shè)置為實(shí)心圓
? ? ? ? ? ? ? ? LineData lineData =new LineData(lineDataSet);
lc.setData(lineData);
lc.invalidate();
}
});
}
/**
? ? * 設(shè)置Y軸
? ? */
? ? private void setYAxis() {
//得到Y(jié)Axis對象
? ? ? ? YAxis yAxis =lc.getAxisLeft();
lc.getAxisRight().setEnabled(false);//不顯示右側(cè)的y軸
? ? ? ? //設(shè)置字體顏色白色
? ? ? ? yAxis.setTextColor(Color.BLUE);
//設(shè)置最大值和最小值
? ? ? ? yAxis.setAxisMaximum(10);
yAxis.setAxisMinimum(0);
//設(shè)置字體大小
? ? ? ? yAxis.setTextSize(11f);
//字體加粗
? ? ? ? yAxis.setTypeface(Typeface.DEFAULT_BOLD);
yAxis.setDrawAxisLine(false);//不繪制第一條橫線
? ? ? ? yAxis.setGridColor(Color.RED);//網(wǎng)格顏色
? ? ? ? //強(qiáng)制只繪制5個(gè)Label
//? ? ? ? yAxis.setLabelCount(10, true);
? ? }
public void setXAxis() {
//得到XAxis對象
? ? ? ? XAxis xAxis =lc.getXAxis();
xAxis.setTextColor(Color.RED);//字體顏色
? ? ? ? //設(shè)置最大最小值
//? ? ? ? xAxis.setAxisMaximum(15);
//? ? ? ? xAxis.setAxisMinimum(0);
? ? ? ? xAxis.setGranularity(1f);//設(shè)置間隔
//? ? ? ? xAxis.setLabelCount(11, true);
? ? ? ? //將x顯示在下方
? ? ? ? xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(14f);//字體大小
? ? ? ? //自定義格式
? ? ? ? xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, AxisBase axis) {
System.out.println(value);
if (value <100) {
String tep = value +"";
return tep.substring(0, tep.indexOf("."));
}
if (value ==63) {
return "(S)";
}
String tep1 = value +"";
return tep1.substring(0, tep1.indexOf("."));
}
});
xAxis.setLabelCount(20);
xAxis.setDrawGridLines(false);
}
private void loadData() {
//設(shè)置邊距
? ? ? ? lc.setExtraTopOffset(10);
lc.setExtraBottomOffset(10);
lc.setExtraLeftOffset(10);
lc.setExtraRightOffset(0);
//不繪制圖例和標(biāo)題
? ? ? ? lc.getLegend().setEnabled(false);
lc.getDescription().setEnabled(false);
lc.zoom(1.2f,1,0,0);
//設(shè)置背景顏色
//? ? ? ? lc.setBackgroundColor(Color.parseColor("#fff123"));
? ? ? ? float datas[] = {1,5,3,2,6,7,3,4,9};
List yData =new ArrayList();
for (int i =0; i < datas.length; i++) {
yData.add(new Entry(i, datas[i]));
}
LineDataSet lineDataSet =new LineDataSet(yData,"");
lineDataSet.setLineWidth(1);//設(shè)置線條寬度
? ? ? ? lineDataSet.setColor(Color.BLUE);//設(shè)置線條顏色
? ? ? ? lineDataSet.setCircleColor(Color.BLUE);//設(shè)置圓點(diǎn)顏色
? ? ? ? lineDataSet.setCircleRadius(2);//設(shè)置圓的大小
? ? ? ? lineDataSet.setDrawValues(false);//不繪制文本
//? ? ? ? lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);//設(shè)置為貝塞爾曲線
? ? ? ? lineDataSet.setCubicIntensity(0.1f);//曲線強(qiáng)度0.1
? ? ? ? lineDataSet.setDrawCircleHole(false);//設(shè)置為實(shí)心圓
? ? ? ? LineData lineData =new LineData(lineDataSet);
lc.setData(lineData);
setBarChart();
}
public void setBarChart() {
setDesc();
setLegend();
setXAxis1();
setYAxis1();
loadData1();
}
//設(shè)置Title
? ? private void setDesc() {
Description description =bc.getDescription();
// 獲取屏幕中間x 軸的像素坐標(biāo)
? ? ? ? WindowManager wm = (WindowManager) LineChartTest.this.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm =new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
float x = dm.widthPixels /2;
description.setPosition(x,50);
description.setText("年齡群體車輛違章的占比統(tǒng)計(jì)");
description.setTextSize(10f);
description.setTextColor(Color.BLACK);
bc.setDescription(description);
}
//設(shè)置圖例
? ? private void setLegend() {
Legend legend =bc.getLegend();
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setDrawInside(true);
legend.setTextSize(14f);
legend.setTypeface(Typeface.DEFAULT_BOLD);
//? ? ? ? legend.setXOffset(30);
? ? ? ? legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setTypeface(Typeface.DEFAULT_BOLD);
Legend legend1 =bc1.getLegend();
legend1.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend1.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend1.setDrawInside(true);
legend1.setTextSize(14f);
legend1.setTypeface(Typeface.DEFAULT_BOLD);
//? ? ? ? legend.setXOffset(30);
? ? ? ? legend1.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend1.setTypeface(Typeface.DEFAULT_BOLD);
}
//設(shè)置Y軸
? ? private void setYAxis1() {
YAxis yAxis =bc.getAxisLeft();
yAxis.setTextSize(14f);
yAxis.setAxisLineWidth(0f);
yAxis.setTextColor(Color.BLACK);
//? ? ? ? yAxis.setXOffset(15);
//? ? ? ? yAxis.setAxisMaximum(800);
? ? ? ? yAxis.setDrawGridLines(true);//顯示橫線
? ? ? ? yAxis.setAxisMinimum(0);
yAxis.setGranularity(0f);
//? ? ? ? yAxis.setLabelCount(7);
? ? }
//設(shè)置X軸
? ? private void setXAxis1() {
XAxis xAxis =bc.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(9f);
xAxis.setTypeface(Typeface.DEFAULT_BOLD);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(10);
//? ? ? ? xAxis.setAxisMaximum(11f);
//? ? ? ? xAxis.setAxisMinimum(0f);
? ? ? ? xAxis.setGranularity(1f);
//自定義格式
? ? ? ? xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, AxisBase axis) {
//? ? ? ? ? ? ? ? int tep = (int) (9 - value);
//? ? ? ? ? ? ? ? return tep + "0后";
? ? ? ? ? ? ? ? return (int)value +"";
}
});
}
//設(shè)置數(shù)據(jù)
? ? private void loadData1() {
bc.setExtraOffsets(5,30,0,30);//邊距
? ? ? ? bc.animateXY(1000,1000);
bc.setFitBars(true);//否將柱塊上的文字繪制在柱塊的上面
? ? ? ? bc.getAxisLeft().setDrawAxisLine(false);//去掉左右豎直邊線
? ? ? ? bc.getAxisRight().setDrawAxisLine(false);//去掉左右豎直邊線
? ? ? ? bc.getAxisRight().setEnabled(false);//不顯示右側(cè)的y軸
? ? ? ? bc.zoom(3f,1,0,0);
MyMarkerView mv =new MyMarkerView(this, R.layout.custom_marker_view);
mv.setChartView(bc);
bc.setMarker(mv);
final float d_1[] = {230f,100f,480f,480f,230f,480f,480f,480f,230f,480f};
final float d_2[] = {120f,600f,400f,200f,80f,120f,600f,400f,200f,80f};
List entries =new ArrayList();
List entries1 =new ArrayList();
List entries2 =new ArrayList();
List entries3 =new ArrayList();
for (int i =0; i < d_1.length; i++) {
//使用BarEntry的構(gòu)造方法的第三個(gè)參數(shù)來保存需要在柱塊上顯示的數(shù)據(jù)
? ? ? ? ? ? entries.add(new BarEntry(i, d_1[i], (d_1[i] / (d_1[i] + d_2[i])) *100));
entries1.add(new BarEntry(i, d_2[i], (d_2[i] / (d_1[i] + d_2[i])) *100));
entries2.add(new BarEntry(i, d_2[i], (d_2[i] / (d_1[i] + d_2[i])) *100));
entries3.add(new BarEntry(i, d_2[i], (d_2[i] / (d_1[i] + d_2[i])) *100));
}
BarDataSet barDataSet =new BarDataSet(entries,"無違章");
BarDataSet barDataSet1 =new BarDataSet(entries1,"有違章");
BarDataSet barDataSet2 =new BarDataSet(entries2,"有違章1");
BarDataSet barDataSet3 =new BarDataSet(entries3,"無違章1");
barDataSet.setColor(Color.parseColor("#6D9C00"));
barDataSet.setValueFormatter(new IValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {
//獲取entry中的數(shù)據(jù)
? ? ? ? ? ? ? ? float res = (float) entry.getData();
return String.format("%.1f", res) +"%";
}
});
barDataSet3.setColor(Color.parseColor("#6D9C00"));
barDataSet3.setValueFormatter(new IValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {
//獲取entry中的數(shù)據(jù)
? ? ? ? ? ? ? ? float res = (float) entry.getData();
return String.format("%.1f", res) +"%";
}
});
barDataSet1.setColor(Color.parseColor("#F07408"));
barDataSet1.setValueTextColor(Color.RED);
barDataSet1.setValueFormatter(new IValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {
//獲取entry中的數(shù)據(jù)
? ? ? ? ? ? ? ? float res = (float) entry.getData();
return String.format("%.1f", res) +"%";
}
});
barDataSet2.setColor(Color.parseColor("#fff123"));
barDataSet2.setValueTextColor(Color.BLUE);
barDataSet2.setValueFormatter(new IValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {
//獲取entry中的數(shù)據(jù)
? ? ? ? ? ? ? ? float res = (float) entry.getData();
return String.format("%.1f", res) +"%";
}
});
List barDataSets =new ArrayList();
barDataSets.add(barDataSet);
barDataSets.add(barDataSet1);
barDataSets.add(barDataSet2);
//? ? ? ? barDataSets.add(barDataSet3);
? ? ? ? BarData barData =new BarData(barDataSets);
barData.setBarWidth(0.21f);//設(shè)置柱塊的寬度
? ? ? ? //參數(shù)1:距左邊的距離(開始會偏移一個(gè)組的距離)
? ? ? ? //參數(shù)二:組與組之間的間距
? ? ? ? //參數(shù)三:一組柱塊之中每個(gè)之間的距離? 通過 setBarWidth與groupSpace調(diào)整柱狀與x軸對應(yīng)? 4個(gè)情況? 4*(0.19+0.01)+0.2=1? 3個(gè)的情況3*(0.21+0.01)+0.34=1
? ? ? ? barData.groupBars(-0.50f,0.34f,0.01f);
bc.setTouchEnabled(true);
bc.setScaleEnabled(false);
bc.setData(barData);
addbc1();
}
private String[]types = {"銀行","非銀金融","建筑材料","化工","醫(yī)藥生物","電子","銀行","非銀金融","建筑材料","化工","醫(yī)藥生物","電子"};
private float[]changes = {27.91f,5.9f,0.4f,17.79f,21.85f,39.58f,27.91f,5.9f,0.4f,17.79f,21.85f,39.58f};
private void addbc1(){
initBarChart(bc1);
setBarChartData(types.length,bc1);
}
private void initBarChart(BarChart bc) {
bc.setExtraOffsets(5,30,0,30);//邊距
? ? ? ? bc.animateXY(1000,1000);
bc.setFitBars(true);//否將柱塊上的文字繪制在柱塊的上面
? ? ? ? bc.getAxisLeft().setDrawAxisLine(false);//去掉左右豎直邊線
? ? ? ? bc.getAxisRight().setDrawAxisLine(false);//去掉左右豎直邊線
? ? ? ? bc.getAxisRight().setEnabled(false);//不顯示右側(cè)的y軸
? ? ? ? bc.zoom(1f,1,0,0);
MyMarkerView mv =new MyMarkerView(this, R.layout.custom_marker_view);
mv.setChartView(bc);
bc.setMarker(mv);
Description description = bc.getDescription();
// 獲取屏幕中間x 軸的像素坐標(biāo)
? ? ? ? WindowManager wm = (WindowManager) LineChartTest.this.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm =new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
float x = dm.widthPixels /2;
description.setPosition(x,50);
description.setText("年齡群體車輛違章的占比統(tǒng)計(jì)");
description.setTextSize(10f);
description.setTextColor(Color.BLACK);
bc.setDescription(description);
Legend legend = bc.getLegend();
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setDrawInside(true);
legend.setTextSize(14f);
legend.setTypeface(Typeface.DEFAULT_BOLD);
//? ? ? ? legend.setXOffset(30);
? ? ? ? legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
legend.setTypeface(Typeface.DEFAULT_BOLD);
YAxis yAxis = bc.getAxisLeft();
yAxis.setTextSize(14f);
yAxis.setAxisLineWidth(0f);
yAxis.setTextColor(Color.BLACK);
//? ? ? ? yAxis.setXOffset(15);
//? ? ? ? yAxis.setAxisMaximum(800);
? ? ? ? yAxis.setDrawGridLines(true);//顯示橫線
? ? ? ? yAxis.setAxisMinimum(0);
yAxis.setGranularity(0f);
XAxis xAxis = bc.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(9f);
xAxis.setTypeface(Typeface.DEFAULT_BOLD);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(10);
//? ? ? ? xAxis.setAxisMaximum(11f);
//? ? ? ? xAxis.setAxisMinimum(0f);
? ? ? ? xAxis.setGranularity(1f);
//自定義格式
? ? ? ? xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, AxisBase axis) {
//? ? ? ? ? ? ? ? int tep = (int) (9 - value);
//? ? ? ? ? ? ? ? return tep + "0后";
? ? ? ? ? ? ? ? return (int)value +"";
}
});
final float d_1[] = {230f,100f,480f,480f,230f,480f,480f,480f,230f,480f};
List entries =new ArrayList();
for (int i =0; i < d_1.length; i++) {
//使用BarEntry的構(gòu)造方法的第三個(gè)參數(shù)來保存需要在柱塊上顯示的數(shù)據(jù)
? ? ? ? ? ? entries.add(new BarEntry(i, d_1[i], d_1[i] +""));
}
BarDataSet barDataSet =new BarDataSet(entries,"無違章");
barDataSet.setColor(Color.parseColor("#6D9C00"));
barDataSet.setValueFormatter(new IValueFormatter() {
@Override
? ? ? ? ? ? public String getFormattedValue(float value, Entry entry,int dataSetIndex, ViewPortHandler viewPortHandler) {
//獲取entry中的數(shù)據(jù)
//? ? ? ? ? ? ? ? float res = (float) entry.getData();
//? ? ? ? ? ? ? ? return String.format("%.1f", res) + "%";
? ? ? ? ? ? ? ? return value+"";
}
});
List barDataSets =new ArrayList();
barDataSets.add(barDataSet);
BarData barData =new BarData(barDataSets);
bc.setTouchEnabled(true);
bc.setScaleEnabled(false);
bc.setData(barData);
}
private void setBarChartData(int count, BarChart mChart) {
}
}
效果圖
