柱状图统计表
2016-05-20 11:56
435 查看
只有一个数据,用柱状图最好不过了,同样也是显示5条数据。
效果图:
![](http://img.blog.csdn.net/20160520115559985)
代码:
效果图:
代码:
package com.officialcar.offcialvehicles.ui.statistics; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.github.mikephil.charting.charts.BarChart; 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.interfaces.datasets.IBarDataSet; import com.google.gson.Gson; import com.officialcar.offcialvehicles.AppConfig; import com.officialcar.offcialvehicles.R; import com.officialcar.offcialvehicles.domian.User; import com.officialcar.offcialvehicles.domian.Youhao.YouhaoBean; import org.kymjs.kjframe.KJHttp; import org.kymjs.kjframe.http.HttpCallBack; import org.kymjs.kjframe.http.HttpConfig; import org.kymjs.kjframe.http.HttpParams; import java.util.ArrayList; import java.util.List; import java.util.Map; public class LichengActivity extends AppCompatActivity implements View.OnClickListener { HttpParams params; String qian_time; String date; String str; private User user; private BarChart mChart; //5.16 暂定为5条数据 private int page_data = 5; private int my_min = 0; private int km_num = 0; private String shang = "shang"; private String xia = "xia"; private List<Float> kmlist; private List<String> carNumList; private int kmSize; private int my_len; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_licheng_layout); mChart = (BarChart) findViewById(R.id.chart); TextView titlebar_text_title = (TextView) findViewById(R.id.titlebar_text_title); titlebar_text_title.setText("里程统计"); ImageView titlbar_img = (ImageView) findViewById(R.id.titlbar_img); titlbar_img.setOnClickListener(this); Button shangye_button = (Button) findViewById(R.id.shangye_button); shangye_button.setOnClickListener(this); Button xiaye_button = (Button) findViewById(R.id.xiaye_button); xiaye_button.setOnClickListener(this); user = User.getInstance1(); user.setListNull(); params = new HttpParams(); Intent intent = getIntent(); qian_time = intent.getStringExtra("QIAN_TIME"); date = intent.getStringExtra("DANG_TIME"); getData(); } /** * 获取到数据 */ public void getData() { HttpConfig httpConfig = new HttpConfig(); httpConfig.cacheTime = 0;//设置缓存时间 KJHttp http = new KJHttp(httpConfig); params.put("startoilWearCar_time", qian_time); params.put("endoilWearCar_time", date); params.put("carId", ""); params.put("car_num_arr", 0); params.put("className", "com.ycya.service.ReportService"); params.put("methodName", "queryOilWear1"); http.post(AppConfig.MY_URL, params, new HttpCallBack() { @Override public void onSuccess(Map<String, String> headers, byte[] t) { super.onSuccess(t); Log.e("tag", "onSuccess"); try { str = new String(t, "gbk"); Log.e("tag", "str:" + str); setUserList(str); } catch (Exception e) { Log.e("tag", "e:" + e); e.printStackTrace(); } } @Override public void onFailure(int errorNo, String strMsg) { super.onFailure(errorNo, strMsg); Log.e("tag", "errorNo:" + errorNo); Log.e("tag", "onFailure"); } }); } private void setUserList(String str){ Gson gson = new Gson(); YouhaoBean data = gson.fromJson(str, YouhaoBean.class); kmlist = data.getMapKm(); carNumList = data.getCarNum(); //往User里添加数据 int dataPage = 5; if (kmlist.size() < 5) { kmSize = kmlist.size() % 5; dataPage = kmSize; } Log.e("tag", "dataPage:" + dataPage); setBarChart(dataPage); } //取油耗最高值 给图表 public double getKmMax() { double kk = kmlist.get(0); for (int i = 1; i < kmlist.size(); i++) { if (kk < kmlist.get(i)) { kk = kmlist.get(i); } } return kk; } public void setBarChart(int datapage) { mChart.setDrawBarShadow(false); mChart.setDrawValueAboveBar(true); mChart.setDescription(""); mChart.setMaxVisibleValueCount((int) (getKmMax() + 100)); mChart.setPinchZoom(false); mChart.setDrawGridBackground(false); XAxis xAxis = mChart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); xAxis.setDrawGridLines(false); xAxis.setSpaceBetweenLabels(2); YAxis leftAxis = mChart.getAxisLeft(); leftAxis.setLabelCount(8, false); leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); leftAxis.setSpaceTop(15f); leftAxis.setAxisMinValue(0f); YAxis rightAxis = mChart.getAxisRight(); rightAxis.setDrawGridLines(false); rightAxis.setLabelCount(8, false); rightAxis.setSpaceTop(15f); rightAxis.setAxisMinValue(0f); Legend l = mChart.getLegend(); l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT); l.setForm(Legend.LegendForm.SQUARE); l.setFormSize(9f); l.setTextSize(11f); l.setXEntrySpace(4f); setData(datapage, xia); } private void setData(int count, String biaoshi) { mChart.animateXY(3000, 3000); ArrayList<String> xVals = new ArrayList<>(); for (int i = my_min; i < count; i++) { Log.e("tag", "my_min:" + my_min + ",count:" + count); xVals.add(carNumList.get(i)); } ArrayList<BarEntry> yVals1 = new ArrayList<>(); int len = page_data; if (biaoshi.equals(shang)) { if (my_len < 5) { km_num = km_num - 5 - my_len; } else { km_num = km_num - 10; } int currIndex = my_min + 5; if (currIndex >= kmlist.size()) { len = kmlist.size() % page_data; if (len == 0) { len = page_data; } } } if (count < 5) { len = count % 5; } Log.e("tag", "len:" + len); Log.e("tag", "km_num:" + km_num); for (int i = 0; i < len; i++) { float km = kmlist.get(km_num); km_num++; Log.e("tag", "km_num:" + km_num); if (km_num >= kmlist.size()) { len = kmlist.size() % page_data; Log.e("tag", "len:" + len); if (len == 0) { len = page_data; } } my_len = len; yVals1.add(new BarEntry(km, i)); } BarDataSet set1; set1 = new BarDataSet(yVals1, "里程"); set1.setBarSpacePercent(35f); set1.setColor(Color.parseColor("#709ed6")); ArrayList<IBarDataSet> dataSets = new ArrayList<>(); dataSets.add(set1); BarData data = new BarData(xVals, dataSets); data.setValueTextSize(10f); mChart.setData(data); mChart.invalidate(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.titlbar_img: finish(); break; case R.id.shangye_button: if (my_min == 0) { Toast.makeText(getApplication(), "已经是第一页", Toast.LENGTH_SHORT).show(); break; } int my_min3 = my_min; my_min = my_min - 5; mChart.animateXY(3000, 3000); setData(my_min3, shang); mChart.invalidate(); break; case R.id.xiaye_button: my_min = my_min + 5; if (my_min >= kmlist.size()) { Toast.makeText(getApplication(), "已经是最后一页", Toast.LENGTH_SHORT).show(); my_min = my_min - 5; break; } int my_min2 = my_min + 5; if (my_min2 >= kmlist.size()) { my_min2 = kmlist.size(); } mChart.animateXY(3000, 3000); setData(my_min2, xia); mChart.invalidate(); break; } } }
相关文章推荐
- 数据库链接出错,请检查数据库路径是否正确--这个问题搞了一下午
- ARC forbids explicit message send of release-关闭xCode项目的ARC设置
- 数据库触发联合索引的几个条件
- ACM博弈知识汇总(转)
- 手动漏洞挖掘-SQL注入小谈(二)
- eclipse下使用fiddler进行抓包
- 24点游戏:java的一种穷举实现
- n^n%10
- VS 2008忽略警告的方法
- 设置TextView里的部分文本的颜色
- 理解OpenCV中的宏定义 CVAPI(函数返回类型)
- stirng转json,String转成List<Map<String,object>>,然后转成json
- 微信红包最高能发520元啦!只限今天!
- 客户端无法退出AD域控,无法修改计算机名字(服务器上的安全数据没有此工作站信任关系的计算机账户!)
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
- [从头学数学] 第214节 带着计算机去高考(六)
- java heep space错误解决办法
- Android平板设备上的数字小键盘自定义控件的实现(非弹出软键盘)
- Confluence--BluePrint
- MpAndroidChart强大的图表功能