|
|
@@ -0,0 +1,181 @@
|
|
|
+package com.xzjmyk.pm.activity.ui.erp.activity.oa;
|
|
|
+
|
|
|
+import android.graphics.Color;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.util.DisplayMetrics;
|
|
|
+
|
|
|
+import com.github.mikephil.charting.charts.BarChart;
|
|
|
+import com.github.mikephil.charting.charts.PieChart;
|
|
|
+import com.github.mikephil.charting.components.Legend;
|
|
|
+import com.github.mikephil.charting.components.XAxis;
|
|
|
+import com.github.mikephil.charting.components.YAxis;
|
|
|
+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.data.Entry;
|
|
|
+import com.github.mikephil.charting.data.PieData;
|
|
|
+import com.github.mikephil.charting.data.PieDataSet;
|
|
|
+import com.github.mikephil.charting.utils.LargeValueFormatter;
|
|
|
+import com.lidroid.xutils.ViewUtils;
|
|
|
+import com.lidroid.xutils.view.annotation.ViewInject;
|
|
|
+import com.xzjmyk.pm.activity.R;
|
|
|
+import com.xzjmyk.pm.activity.ui.erp.view.MyMarkerView;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+public class TestActivity extends AppCompatActivity {
|
|
|
+ @ViewInject(R.id.bar_chart)
|
|
|
+ private BarChart barChart;
|
|
|
+ @ViewInject(R.id.pie_chart)
|
|
|
+ private PieChart pieChart;
|
|
|
+ private MyMarkerView mv;
|
|
|
+ private ArrayList<ArrayList<String>> gridlists = new ArrayList<ArrayList<String>>();
|
|
|
+ private ArrayList<Integer> colors;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_test);
|
|
|
+ ViewUtils.inject(this);
|
|
|
+ init();
|
|
|
+ initView();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void init() {
|
|
|
+ //初始化饼图颜色
|
|
|
+ colors = new ArrayList();
|
|
|
+ colors.add(Color.rgb(0, 191, 255));
|
|
|
+ colors.add(Color.rgb(255, 254, 179));
|
|
|
+ colors.add(Color.rgb(205, 133, 63));
|
|
|
+ colors.add(Color.rgb(173, 255, 47));
|
|
|
+ colors.add(Color.rgb(0, 128, 0));
|
|
|
+ colors.add(Color.rgb(139, 0, 0));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initView() {
|
|
|
+ initBar();
|
|
|
+ initPieChart();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void initPieChart() {
|
|
|
+ pieChart.setHoleColorTransparent(true);
|
|
|
+ pieChart.setHoleRadius(30f); //半径
|
|
|
+ pieChart.setTransparentCircleRadius(64f); // 半透明圈
|
|
|
+ pieChart.setDescription("");
|
|
|
+ pieChart.setDrawCenterText(true); //饼状图中间可以添加文字
|
|
|
+ pieChart.setDrawHoleEnabled(true);
|
|
|
+ pieChart.setTransparentCircleRadius(40f); // 设置中间透明圈的大小
|
|
|
+ pieChart.setRotationAngle(90); // 初始旋转角度
|
|
|
+ pieChart.setRotationEnabled(true); // 可以手动旋转
|
|
|
+ pieChart.setUsePercentValues(true); //显示成百分比
|
|
|
+ pieChart.setCenterText("8000"); //饼状图中间的文字
|
|
|
+ pieChart.setData(getPieData(6)); //设置数据
|
|
|
+ Legend mLegend = pieChart.getLegend(); //设置说明
|
|
|
+ mLegend.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); //设置比例显示位置
|
|
|
+ mLegend.setXEntrySpace(10f);
|
|
|
+ mLegend.setYEntrySpace(10f);
|
|
|
+ pieChart.animateXY(1000, 1000); //设置动画
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param count 分成几部分
|
|
|
+ * @param
|
|
|
+ */
|
|
|
+ private PieData getPieData(int count) {
|
|
|
+ ArrayList<String> xValues = new ArrayList<String>(); //xVals用来表示每个饼块上的内容
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ xValues.add("YT000" + (i + 1)); //饼块上显示成Quarterly1, Quarterly2, Quarterly3, Quarterly4
|
|
|
+ }
|
|
|
+ ArrayList<Entry> yValues = new ArrayList<Entry>(); //yVals用来表示封装每个饼块的实际数据
|
|
|
+ // 饼图数据
|
|
|
+ float quarterly1 = 14;
|
|
|
+ float quarterly2 = 5;
|
|
|
+ float quarterly3 = 33;
|
|
|
+ float quarterly4 = 21;
|
|
|
+ float quarterly5 = 11;
|
|
|
+ float quarterly6 = 16;
|
|
|
+ yValues.add(new Entry(quarterly1, 0));
|
|
|
+ yValues.add(new Entry(quarterly2, 1));
|
|
|
+ yValues.add(new Entry(quarterly3, 2));
|
|
|
+ yValues.add(new Entry(quarterly4, 3));
|
|
|
+ yValues.add(new Entry(quarterly5, 4));
|
|
|
+ yValues.add(new Entry(quarterly6, 5));
|
|
|
+ //y轴的集合
|
|
|
+ PieDataSet pieDataSet = new PieDataSet(yValues, ""/*显示在比例图上*/);
|
|
|
+ pieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离
|
|
|
+ pieDataSet.setColors(colors);
|
|
|
+ DisplayMetrics metrics = getResources().getDisplayMetrics();
|
|
|
+ float px = 5 * (metrics.densityDpi / 160f);
|
|
|
+ pieDataSet.setSelectionShift(px); // 选中态多出的长度
|
|
|
+ PieData pieData = new PieData(xValues, pieDataSet);
|
|
|
+ return pieData;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initBar() {
|
|
|
+ barChart.setDrawBorders(false); ////是否在折线图上添加边框
|
|
|
+ // 如果没有数据的时候,会显示这个,类似ListView的EmptyView
|
|
|
+ barChart.setNoDataTextDescription("没有数据");
|
|
|
+ barChart.setDrawGridBackground(false); // 是否显示表格颜色
|
|
|
+ barChart.setTouchEnabled(true); // 设置是否可以触摸
|
|
|
+ barChart.setDragEnabled(true);// 是否可以拖拽
|
|
|
+ barChart.setScaleEnabled(true);// 是否可以缩放
|
|
|
+ barChart.setPinchZoom(false);//
|
|
|
+ mv = new MyMarkerView(this, R.layout.custom_marker_view);
|
|
|
+ barChart.setMarkerView(mv);
|
|
|
+ barChart.setDescription("");
|
|
|
+ barChart.setMaxVisibleValueCount(60);
|
|
|
+ barChart.setDrawBarShadow(false);
|
|
|
+ XAxis xAxis = barChart.getXAxis();
|
|
|
+ xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
|
|
|
+ // xAxis.setSpaceBetweenLabels(30);
|
|
|
+ xAxis.setLabelsToSkip(0);
|
|
|
+ xAxis.setAxisLineColor(getResources().getColor(R.color.red));
|
|
|
+ xAxis.setSpaceBetweenLabels(8);
|
|
|
+ xAxis.setAxisLineWidth(0f);
|
|
|
+ xAxis.setDrawGridLines(false);
|
|
|
+ barChart.getAxisLeft().setDrawGridLines(false);
|
|
|
+// barChart.setBackgroundColor();// 设置背景
|
|
|
+ barChart.setDrawBarShadow(true);
|
|
|
+ barChart.setData(getBarData(7, 500)); // 设置数据
|
|
|
+ Legend mLegend = barChart.getLegend(); // 设置比例图标示
|
|
|
+ mLegend.setForm(Legend.LegendForm.CIRCLE);// 样式
|
|
|
+ mLegend.setFormSize(6f);// 字体
|
|
|
+ mLegend.setTextColor(Color.BLACK);// 颜色
|
|
|
+ YAxis leftAxis = barChart.getAxisLeft();
|
|
|
+ leftAxis.setValueFormatter(new LargeValueFormatter());
|
|
|
+ leftAxis.setDrawGridLines(false);
|
|
|
+ leftAxis.setSpaceTop(25f);
|
|
|
+ leftAxis.setAxisLineColor(getResources().getColor(R.color.red));
|
|
|
+ barChart.getAxisRight().setEnabled(false);
|
|
|
+ barChart.setMaxVisibleValueCount(10);
|
|
|
+
|
|
|
+ barChart.animateY(2500);
|
|
|
+ barChart.animateX(2500); // 立即执行的动画,x轴
|
|
|
+ }
|
|
|
+
|
|
|
+ private BarData getBarData(int count, float range) {
|
|
|
+ ArrayList<String> xValues = new ArrayList<String>();
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ xValues.add("YT00\n00" + count);
|
|
|
+ }
|
|
|
+ BarEntry entry = null;
|
|
|
+
|
|
|
+ ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
|
|
|
+
|
|
|
+ yValues.add(new BarEntry(value, i));
|
|
|
+ }
|
|
|
+ // y轴的数据集合
|
|
|
+ BarDataSet barDataSet = new BarDataSet(yValues, "测试饼状图");
|
|
|
+ barDataSet.setColors(colors);
|
|
|
+ ArrayList<BarDataSet> barDataSets = new ArrayList<BarDataSet>();
|
|
|
+ barDataSets.add(barDataSet); // add the datasets
|
|
|
+ BarData barData = new BarData(xValues, barDataSets);
|
|
|
+ return barData;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|