我嘗試從像這樣的 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 的結果。這是我的總和代碼
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){
}
}
我的圖表

有沒有辦法解決這個問題?
uj5u.com熱心網友回復:
我不確定您為什么要按月中的某天繪制此圖(如果資料集跨越多個月,這將中斷),但下面的代碼適用于您提出的案例。
它首先按日期收集您想要繪制到 HashMap 中的資料,如果在給定日期有多個條目,則將值相加,然后將該映射轉換為圖表的 xy 條目。
// 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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/453402.html
上一篇:當我在CodingBatPython(https://codingbat.com/prob/p118406)上提交代碼時出現超時錯誤
