package com.wissea.trs.activity;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.wissea.trs.Constant;
import com.wissea.trs.R;
import com.wissea.trs.ResponseStatus;
import com.wissea.trs.api.WeatherService;
import com.wissea.trs.utils.DateTools;
public class HistoryActivity extends BaseActivity implements OnCheckedChangeListener{
private WeatherService mWeatherService;
private String sensorCode;
private String endDate;
private String weekReportStartDate;
private String monthReportStartDate;
private String seasonReportStartDate;
private LineData mTemperatureDataWeek;
private LineData mHumidityDataWeek;
private LineData mAmmoniaDataWeek;
private List<String> mWeekXValues;
private List<Entry> mWeekYValuesTemperature;
private List<Entry> mWeekYValuesHumidity;
private List<Entry> mWeekYValuesAmmonia;
private LineData mTemperatureDataMonth;
private LineData mHumidityDataMonth;
private LineData mAmmoniaDataMonth;
private List<String> mMonthXValues;
private List<Entry> mMonthYValuesTemperature;
private List<Entry> mMonthYValuesHumidity;
private List<Entry> mMonthYValuesAmmonia;
private LineData mTemperatureDataSeason;
private LineData mHumidityDataSeason;
private LineData mAmmoniaDataSeason;
private List<String> mSeasonXValues;
private ArrayList<Entry> mSeasonYValuesTemperature;
private ArrayList<Entry> mSeasonYValuesHumidity;
private ArrayList<Entry> mSeasonYValuesAmmonia;
private LineChart lc_history_temperature;
private LineChart lc_history_humidity;
private LineChart lc_history_ammonia;
private RadioButton btn_week;
private RadioButton btn_month;
private RadioButton btn_season;
@Override
protected void onCreate(Bundle bundle) {
// TODO Auto-generated method stub
super.onCreate(bundle);
setContentView(R.layout.activity_history);
sensorCode = getIntent().getExtras().getString("sensorCode");
// 初始化ActionBar
setBackable(true);
setRightImageVisible(false);
setTitle("历史数据");
btn_week = (RadioButton) findViewById(R.id.btn_week);
btn_week.setOnCheckedChangeListener(this);
btn_month = (RadioButton) findViewById(R.id.btn_month);
btn_month.setOnCheckedChangeListener(this);
btn_season = (RadioButton) findViewById(R.id.btn_season);
btn_season.setOnCheckedChangeListener(this);
// 温度
lc_history_temperature = (LineChart) findViewById(R.id.lc_history_temperature);
// 湿度
lc_history_humidity = (LineChart) findViewById(R.id.lc_history_humidity);
// 氨气浓度
lc_history_ammonia = (LineChart) findViewById(R.id.lc_history_ammonia);
endDate = DateTools.getDateDaysBefore(0);
weekReportStartDate = DateTools.getDateDaysBefore(7);
monthReportStartDate = DateTools.getDateDaysBefore(30);
seasonReportStartDate = DateTools.getDateDaysBefore(365);
if (!TextUtils.isEmpty(sensorCode)) {
new GetHistoryTask(seasonReportStartDate, endDate).execute();
}
mWeekXValues = new ArrayList<String>();
mWeekYValuesTemperature = new ArrayList<Entry>();
mWeekYValuesHumidity = new ArrayList<Entry>();
mWeekYValuesAmmonia = new ArrayList<Entry>();
mMonthXValues = new ArrayList<String>();
mMonthYValuesTemperature = new ArrayList<Entry>();
mMonthYValuesHumidity = new ArrayList<Entry>();
mMonthYValuesAmmonia = new ArrayList<Entry>();
mSeasonXValues = new ArrayList<String>();
mSeasonYValuesTemperature = new ArrayList<Entry>();
mSeasonYValuesHumidity = new ArrayList<Entry>();
mSeasonYValuesAmmonia = new ArrayList<Entry>();
mWeatherService = new WeatherService();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
switch (buttonView.getId()) {
case R.id.btn_week:
// 周报
if (isChecked) {
showHistoryData(1);
}
break;
case R.id.btn_month:
// 月报
if (isChecked) {
showHistoryData(2);
}
break;
case R.id.btn_season:
// 季报
if (isChecked) {
showHistoryData(3);
}
break;
default:
break;
}
}
/**
* 生成一个数据
* @param count 表示图表中有多少个坐标点
* @param range 用来生成range以内的随机数
* @return
*/
private LineData getLineData(List<String> xValues, List<Entry> yValues) {
/*ArrayList<String> xValues = new ArrayList<String>();
for (int i = 0; i < count; i++) {
// x轴显示的数据,这里默认使用数字下标显示
xValues.add("" + i);
} */
ArrayList<LineDataSet> lineDataSets = new ArrayList<LineDataSet>();
// y轴的数据 1======================================start
/* ArrayList<Entry> yValues = new ArrayList<Entry>();
for (int i = 0; i < count; i++) {
float value = (float) (Math.random() * range) + 3;
yValues.add(new Entry(value, i));
}*/
// create a dataset and give it a type
// y轴的数据集合
LineDataSet lineDataSet = new LineDataSet(yValues, "" /*测试折线图*/);
// mLineDataSet.setFillAlpha(110);
// mLineDataSet.setFillColor(Color.RED);
//用y轴的集合来设置参数
// 线宽
lineDataSet.setLineWidth(1.0f);
// 显示的圆形大小
lineDataSet.setCircleSize(2.5f);
// 显示颜色
lineDataSet.setColor(Color.GREEN);
// 圆形的颜色
lineDataSet.setCircleColor(Color.GREEN);
// 高亮的线的颜色
lineDataSet.setHighLightColor(Color.GREEN);
// 设置圆点的颜色
lineDataSet.setFillColor(Color.GREEN);
lineDataSet.setDrawCircleHole(false);
//lineDataSet.setValueTextSize(9f);
lineDataSet.setFillAlpha(65);
lineDataSets.add(lineDataSet);
// y轴的数据 1======================================end
// create a data object with the datasets
LineData lineData = new LineData(xValues, lineDataSets);
return lineData;
}
/**
* 显示数据
* @param lineChart
* @param lineData
* @param color
*/
private void showChart(LineChart lineChart, LineData lineData, int color) {
//是否在折线图上添加边框
lineChart.setDrawBorders(false);
// 数据描述
lineChart.setDescription("");
// 如果没有数据的时候,会显示这个,类似listview的emtpyview
lineChart.setNoDataTextDescription("暂无数据");
// 是否显示表格颜色
lineChart.setDrawGridBackground(false);
// 表格的的颜色,在这里是是给颜色设置一个透明度
//lineChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF);
// 设置是否可以触摸
lineChart.setTouchEnabled(true);
// 是否可以拖拽
lineChart.setDragEnabled(true);
// 是否可以缩放
lineChart.setScaleEnabled(true);
// if disabled, scaling can be done on x- and y-axis separately
lineChart.setPinchZoom(false);
//lineChart.setBackgroundColor(Color.rgb(Integer.parseInt("7e", 16), Integer.parseInt("ce", 16), Integer.parseInt("f4", 16)));// 设置背景
// 设置数据
lineChart.setData(lineData);
// 设置比例图标示,就是那个一组y的value的
Legend mLegend = lineChart.getLegend();
//mLegend.setPosition(LegendPosition.BELOW_CHART_CENTER);
// 样式
mLegend.setForm(LegendForm.CIRCLE);
// 字体
mLegend.setFormSize(0f);
// 颜色
mLegend.setTextColor(Color.RED);
// 字体
//mLegend.setTypeface(mTf);
// 设置Y轴右边不显示数字
lineChart.getAxisRight().setEnabled(false);
XAxis xAxis = lineChart.getXAxis();
// 设置X轴的数据显示在报表的下方
xAxis.setPosition(XAxisPosition.BOTTOM);
//xAxis.setDrawAxisLine(false);
// 设置不从X轴发出纵向直线
xAxis.setDrawGridLines(false);
// 立即执行的动画,x轴
lineChart.animateX(2500);
}
/**
* 获取历史记录
* @author Jason
*
*/
private class GetHistoryTask extends AsyncTask<Void, Void, String> {
private String startDate;
private String endDate;
public GetHistoryTask(String startDate, String endDate) {
// TODO Auto-generated constructor stub
this.startDate = startDate;
this.endDate = endDate;
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
return mWeatherService.getHistory(sensorCode, startDate, endDate, Constant.UserID);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result != null) {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = null;
try {
jsonNode = mapper.readTree(result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (jsonNode != null && TextUtils.equals(jsonNode.get("Status").textValue(), ResponseStatus.SUCCESS)) {
// 成功获取到了历史记录
int count = jsonNode.get("Count").asInt();
if (count > 0) {
JsonNode contentNode = jsonNode.get("Content");
for (int i = 0; i < count; i++) {
JsonNode dayNode = contentNode.get(i);
mSeasonYValuesTemperature.add(new Entry(dayNode.get("Temperature").floatValue(), i));
mSeasonYValuesHumidity.add(new Entry(dayNode.get("Humidity").floatValue(), i));
mSeasonYValuesAmmonia.add(new Entry(dayNode.get("Extension1").floatValue(), i));
mSeasonXValues.add(dayNode.get("CollectingTime").textValue().substring(0, 10));
// 解决不显示数据的bug,注意后面的index
if (count - i < 30) {
mMonthYValuesTemperature.add(new Entry(dayNode.get("Temperature").floatValue(), 29-count+i));
mMonthYValuesHumidity.add(new Entry(dayNode.get("Humidity").floatValue(), 29-count+i));
mMonthYValuesAmmonia.add(new Entry(dayNode.get("Extension1").floatValue(), 29-count+i));
mMonthXValues.add(dayNode.get("CollectingTime").textValue().substring(0, 10));
}
if (count - i < 7) {
mWeekYValuesTemperature.add(new Entry(dayNode.get("Temperature").floatValue(), 6-count+i));
mWeekYValuesHumidity.add(new Entry(dayNode.get("Humidity").floatValue(), 6-count+i));
mWeekYValuesAmmonia.add(new Entry(dayNode.get("Extension1").floatValue(), 6-count+i));
mWeekXValues.add(dayNode.get("CollectingTime").textValue().substring(0, 10));
}
}
mTemperatureDataSeason = getLineData(mSeasonXValues, mSeasonYValuesTemperature);
mHumidityDataSeason = getLineData(mSeasonXValues, mSeasonYValuesHumidity);
mAmmoniaDataSeason = getLineData(mSeasonXValues, mSeasonYValuesAmmonia);
mTemperatureDataMonth = getLineData(mMonthXValues, mMonthYValuesTemperature);
mHumidityDataMonth = getLineData(mMonthXValues, mMonthYValuesHumidity);
mAmmoniaDataMonth = getLineData(mMonthXValues, mMonthYValuesAmmonia);
mTemperatureDataWeek = getLineData(mWeekXValues, mWeekYValuesTemperature);
mHumidityDataWeek = getLineData(mWeekXValues, mWeekYValuesHumidity);
mAmmoniaDataWeek = getLineData(mWeekXValues, mWeekYValuesAmmonia);
}
showHistoryData(1);
}
}
}
}
/**
* 切换及展示历史数据
*/
private void showHistoryData(int type) {
switch (type) {
case 1:
showChart(lc_history_temperature, mTemperatureDataWeek, R.color.red_failed);
showChart(lc_history_humidity, mHumidityDataWeek, R.color.red_failed);
showChart(lc_history_ammonia, mAmmoniaDataWeek, R.color.red_failed);
break;
case 2:
showChart(lc_history_temperature, mTemperatureDataMonth, R.color.red_failed);
showChart(lc_history_humidity, mHumidityDataMonth, R.color.red_failed);
showChart(lc_history_ammonia, mAmmoniaDataMonth, R.color.red_failed);
break;
case 3:
showChart(lc_history_temperature, mTemperatureDataSeason, R.color.red_failed);
showChart(lc_history_humidity, mHumidityDataSeason, R.color.red_failed);
showChart(lc_history_ammonia, mAmmoniaDataSeason, R.color.red_failed);
break;
default:
break;
}
}
}
解决使用了subList方法之后MPAndroidChart不能显示数据的Bug
最新推荐文章于 2024-04-19 22:57:45 发布