【发布时间】:2020-04-06 06:00:13
【问题描述】:
很抱歉,这个问题的标题不是 100% 正确的。因此,我将在这里解释我的情况。 我创建了一个函数来将 4 个数据集合并为一种返回格式。因为这是前端需要的格式。所以现在可以正常工作了。
public ReturnFormat makeThribleLineChart(List<NameCountModel> totalCount, List<NameCountModel>,p1Count, List<NameCountModel> p2Count, List<NameCountModel> average) {
ReturnFormat returnFormat = new ReturnFormat(null,null);
try {
String[] totalData = new String[totalCount.size()];
String[] p1Data = new String[p1Count.size()];
String[] p2Data = new String[p2Count.size()];
String[] averageData = new String[p2Count.size()];
String[] lableList = new String[totalCount.size()];
for (int x = 0; x < totalCount.size(); x++) {
totalData[x] = totalCount.get(x).getCount();
p1Data[x] = p1Count.get(x).getCount();
p2Data[x] = p2Count.get(x).getCount();
averageData[x] = average.get(x).getCount();
lableList[x] = totalCount.get(x).getName();
}
FormatHelper<String[]> totalFormatHelper= new FormatHelper<String[]>();
totalFormatHelper.setData(totalData);
totalFormatHelper.setType("line");
totalFormatHelper.setLabel("Uudet");
totalFormatHelper.setyAxisID("y-axis-1");
FormatHelper<String[]> p1FormatHelper= new FormatHelper<String[]>();
p1FormatHelper.setData(p1Data);
p1FormatHelper.setType("line");
p1FormatHelper.setLabel("P1 päivystykseen heti");
FormatHelper<String[]> p2FormatHelper= new FormatHelper<String[]>();
p2FormatHelper.setData(p2Data);
p2FormatHelper.setType("line");
p2FormatHelper.setLabel("P2 päivystykseen muttei yöllä");
FormatHelper<String[]> averageFormatHelper= new FormatHelper<String[]>();
averageFormatHelper.setData(averageData);
averageFormatHelper.setType("line");
averageFormatHelper.setLabel("Jonotusaika keskiarvo");
averageFormatHelper.setyAxisID("y-axis-2");
List<FormatHelper<String[]>> formatHelpObj = new ArrayList<FormatHelper<String[]>>();
formatHelpObj.add(totalFormatHelper);
formatHelpObj.add(p1FormatHelper);
formatHelpObj.add(p2FormatHelper);
formatHelpObj.add(averageFormatHelper);
returnFormat.setData(formatHelpObj);
returnFormat.setLabels(lableList);
returnFormat.setMessage(Messages.Success);
returnFormat.setStatus(ReturnFormat.Status.SUCCESS);
} catch (Exception e) {
returnFormat.setData(null);
returnFormat.setMessage(Messages.InternalServerError);
returnFormat.setStatus(ReturnFormat.Status.ERROR);
}
return returnFormat;
}
所以,正如您在此处看到的,所有格式都是硬编码的。所以我的问题是如何自动化此代码以进行列表计数。假设下次我必须为五个数据集创建图表格式。所以我必须为它创建另一个函数。这就是我想要减少的东西。所以我希望你能理解我的问题。
谢谢。
【问题讨论】:
-
看起来你的每个数据集都有几个属性——数据本身、标签和类型——对吗?如果您需要概括这一点,我建议考虑使用builder pattern。
-
不,所有数据集都具有相同的属性。数据集仅包含两个属性。 “名称”和“计数”。这意味着名称是标签,计数是数据。在上面的代码中,我将 4 个数据集传递给了参数列表。我打算将这些数据集作为单个列表传递。然后我希望循环该列表并为每个数据集创建一个单独的字符串数组。但我的问题是,我无法访问这些变量,因为它们在循环范围内。希望你能理解。
标签: java arraylist collections formatting