无法在折线图中显示总和数据的正确结果
Cannot display right result of sum data in line graph
我尝试从 JSON 中获取数据,JSON 结构如下
{"count" : 5,"result" : [
{"id": 1,
"date_invoice": "2022-03-25",
"state": "paid",
"amount_untaxed": 25000
},
{"id": 2,
"date_invoice": "2022-03-26",
"state": "paid",
"amount_untaxed": 161500
},
{"id": 3,
"date_invoice": "2022-03-27",
"state": "paid",
"amount_untaxed": 25000
},
{"id": 4,
"date_invoice": "2022-03-30",
"state": "paid",
"amount_untaxed": 165000
},
{"id": 5,
"date_invoice": "2022-03-30",
"state": "paid",
"amount_untaxed": 70000
}
]}
我已经可以获取 json 数据并将其显示为折线图,我使用 mpchartandroid 库来显示我的图表。我的问题是,当某些数据具有相同日期时,我无法显示总和数据 amount_untaxed 的结果。这是我的 sum
代码
detail = new HashMap<>();
detail = new Gson().fromJson(_response, new TypeToken<HashMap<String, Object>>(){}.getType());
str= (new Gson()).toJson(detail.get("result"), new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
listmap_detail = new Gson().fromJson(str, new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
listmap_Item = listmap_detail;
price_subtotal = Float.valueOf(0);
for (int i=0;i<listmap_detail.toArray().length;i++){
dateString = listmap_detail.get(i).get("date_invoice").toString().replace("-","");
format1 = new SimpleDateFormat("yyyyMMdd");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
format2 = new SimpleDateFormat("dd");
SimpleDateFormat format3 = new SimpleDateFormat("MMMM yyyy");
SimpleDateFormat format4 = new SimpleDateFormat("dd-MM");
try {
today = new Date();
current_date = format3.format(today);
Date date = format1.parse(dateString);
dateFinal = format2.format(date);
String dateSementara = format2.format(today);
if (dateSementara.equals(dateFinal)){
price_subtotal += Float.valueOf(listmap_detail.get(i).get("amount_untaxed").toString());
}
line_entries.add(new Entry(Float.parseFloat(dateFinal),price_subtotal));
}
catch (Exception e){
}
}
我的图表
有什么办法可以解决吗?
我不确定您为什么按月中的某天绘制此图(如果数据集跨越多个月,这将中断)但下面的代码适用于您提供的案例。
它首先按日期收集要绘制到 HashMap 中的数据,如果在给定日期有多个条目,则对值求和,然后将该映射转换为图表的 x-y 个条目。
// no need to call Gson multiple times - once you have
// parsed it you can just get the entries from the map
HashMap<String,Object> detail = new Gson().fromJson(jsonStr, new TypeToken<HashMap<String, Object>>(){}.getType());
List<Map<String,Object>> listmap_detail = (List<Map<String, Object>>) detail.get("result");
// Collect values per day into a map first to handle
// summing duplicate values
Map<String,Float> daily_sum = new HashMap<>();
for(Map<String,Object> entry : listmap_detail) {
String date = (String) entry.get("date_invoice");
Float amt = ((Double) entry.get("amount_untaxed")).floatValue();
if( !daily_sum.containsKey(date) ) {
daily_sum.put(date, amt);
}
else {
daily_sum.put(date, amt + daily_sum.get(date));
}
}
// Print statements are your friend when it comes to
// debugging code like this
System.out.println("DEBUG: daily sum = " + daily_sum);
// Sort the dates so the data is in chronological order
List<String> keys = new ArrayList<>(daily_sum.keySet());
Collections.sort(keys);
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dayOfMonthFormat = new SimpleDateFormat("dd");
// WARNING: MPAndroidChart requires entries to be ordered by x value - if your
// data set spans multiple months this will cause a problem
// below since the data will not be in order by day of month.
// Adjust for your use case as-needed.
for(String isoDate : keys) {
try {
Date date = isoFormat.parse(isoDate);
float dayOfMonth = Float.parseFloat(dayOfMonthFormat.format(date));
Float dailyAmt = daily_sum.get(isoDate);
System.out.println("DEBUG: plotting " + dayOfMonth + "," + dailyAmt);
line_entries.add(new Entry(dayOfMonth,dailyAmt));
} catch (ParseException e) {
e.printStackTrace();
}
}
我尝试从 JSON 中获取数据,JSON 结构如下
{"count" : 5,"result" : [
{"id": 1,
"date_invoice": "2022-03-25",
"state": "paid",
"amount_untaxed": 25000
},
{"id": 2,
"date_invoice": "2022-03-26",
"state": "paid",
"amount_untaxed": 161500
},
{"id": 3,
"date_invoice": "2022-03-27",
"state": "paid",
"amount_untaxed": 25000
},
{"id": 4,
"date_invoice": "2022-03-30",
"state": "paid",
"amount_untaxed": 165000
},
{"id": 5,
"date_invoice": "2022-03-30",
"state": "paid",
"amount_untaxed": 70000
}
]}
我已经可以获取 json 数据并将其显示为折线图,我使用 mpchartandroid 库来显示我的图表。我的问题是,当某些数据具有相同日期时,我无法显示总和数据 amount_untaxed 的结果。这是我的 sum
代码 detail = new HashMap<>();
detail = new Gson().fromJson(_response, new TypeToken<HashMap<String, Object>>(){}.getType());
str= (new Gson()).toJson(detail.get("result"), new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
listmap_detail = new Gson().fromJson(str, new TypeToken<ArrayList<HashMap<String, Object>>>(){}.getType());
listmap_Item = listmap_detail;
price_subtotal = Float.valueOf(0);
for (int i=0;i<listmap_detail.toArray().length;i++){
dateString = listmap_detail.get(i).get("date_invoice").toString().replace("-","");
format1 = new SimpleDateFormat("yyyyMMdd");
format1.setTimeZone(TimeZone.getTimeZone("GMT+7"));
format2 = new SimpleDateFormat("dd");
SimpleDateFormat format3 = new SimpleDateFormat("MMMM yyyy");
SimpleDateFormat format4 = new SimpleDateFormat("dd-MM");
try {
today = new Date();
current_date = format3.format(today);
Date date = format1.parse(dateString);
dateFinal = format2.format(date);
String dateSementara = format2.format(today);
if (dateSementara.equals(dateFinal)){
price_subtotal += Float.valueOf(listmap_detail.get(i).get("amount_untaxed").toString());
}
line_entries.add(new Entry(Float.parseFloat(dateFinal),price_subtotal));
}
catch (Exception e){
}
}
我的图表
有什么办法可以解决吗?
我不确定您为什么按月中的某天绘制此图(如果数据集跨越多个月,这将中断)但下面的代码适用于您提供的案例。
它首先按日期收集要绘制到 HashMap 中的数据,如果在给定日期有多个条目,则对值求和,然后将该映射转换为图表的 x-y 个条目。
// no need to call Gson multiple times - once you have
// parsed it you can just get the entries from the map
HashMap<String,Object> detail = new Gson().fromJson(jsonStr, new TypeToken<HashMap<String, Object>>(){}.getType());
List<Map<String,Object>> listmap_detail = (List<Map<String, Object>>) detail.get("result");
// Collect values per day into a map first to handle
// summing duplicate values
Map<String,Float> daily_sum = new HashMap<>();
for(Map<String,Object> entry : listmap_detail) {
String date = (String) entry.get("date_invoice");
Float amt = ((Double) entry.get("amount_untaxed")).floatValue();
if( !daily_sum.containsKey(date) ) {
daily_sum.put(date, amt);
}
else {
daily_sum.put(date, amt + daily_sum.get(date));
}
}
// Print statements are your friend when it comes to
// debugging code like this
System.out.println("DEBUG: daily sum = " + daily_sum);
// Sort the dates so the data is in chronological order
List<String> keys = new ArrayList<>(daily_sum.keySet());
Collections.sort(keys);
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dayOfMonthFormat = new SimpleDateFormat("dd");
// WARNING: MPAndroidChart requires entries to be ordered by x value - if your
// data set spans multiple months this will cause a problem
// below since the data will not be in order by day of month.
// Adjust for your use case as-needed.
for(String isoDate : keys) {
try {
Date date = isoFormat.parse(isoDate);
float dayOfMonth = Float.parseFloat(dayOfMonthFormat.format(date));
Float dailyAmt = daily_sum.get(isoDate);
System.out.println("DEBUG: plotting " + dayOfMonth + "," + dailyAmt);
line_entries.add(new Entry(dayOfMonth,dailyAmt));
} catch (ParseException e) {
e.printStackTrace();
}
}