mirror of
https://github.com/dataease/dataease.git
synced 2025-02-25 20:42:55 +08:00
feat(视图): 快速计算支持占比
This commit is contained in:
parent
d4c8492313
commit
a08ccdeed2
@ -10,4 +10,5 @@ public class ChartConstants {
|
|||||||
public static final String YEAR_YOY = "year_yoy";
|
public static final String YEAR_YOY = "year_yoy";
|
||||||
public static final String DAY_MOM = "day_mom";
|
public static final String DAY_MOM = "day_mom";
|
||||||
public static final String MONTH_YOY = "month_yoy";
|
public static final String MONTH_YOY = "month_yoy";
|
||||||
|
public static final String[] M_Y = {YEAR_MOM, MONTH_MOM, YEAR_YOY, DAY_MOM, MONTH_YOY};
|
||||||
}
|
}
|
||||||
|
@ -670,7 +670,8 @@ public class ChartViewService {
|
|||||||
}
|
}
|
||||||
boolean hasParameters = false;
|
boolean hasParameters = false;
|
||||||
if (StringUtils.isNotEmpty(table.getSqlVariableDetails())) {
|
if (StringUtils.isNotEmpty(table.getSqlVariableDetails())) {
|
||||||
List<SqlVariableDetails> sqlVariables = new Gson().fromJson(table.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {}.getType());
|
List<SqlVariableDetails> sqlVariables = new Gson().fromJson(table.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {
|
||||||
|
}.getType());
|
||||||
for (String parameter : Optional.ofNullable(request.getParameters()).orElse(new ArrayList<>())) {
|
for (String parameter : Optional.ofNullable(request.getParameters()).orElse(new ArrayList<>())) {
|
||||||
if (sqlVariables.stream().map(SqlVariableDetails::getVariableName).collect(Collectors.toList()).contains(parameter)) {
|
if (sqlVariables.stream().map(SqlVariableDetails::getVariableName).collect(Collectors.toList()).contains(parameter)) {
|
||||||
hasParameters = true;
|
hasParameters = true;
|
||||||
@ -994,20 +995,6 @@ public class ChartViewService {
|
|||||||
if (StringUtils.isNotEmpty(compareCalc.getType())
|
if (StringUtils.isNotEmpty(compareCalc.getType())
|
||||||
&& !StringUtils.equalsIgnoreCase(compareCalc.getType(), "none")) {
|
&& !StringUtils.equalsIgnoreCase(compareCalc.getType(), "none")) {
|
||||||
String compareFieldId = compareCalc.getField();// 选中字段
|
String compareFieldId = compareCalc.getField();// 选中字段
|
||||||
String resultData = compareCalc.getResultData();// 数据设置
|
|
||||||
// 获取选中字段以及下标
|
|
||||||
List<ChartViewFieldDTO> checkedField = new ArrayList<>(xAxis);
|
|
||||||
if (StringUtils.containsIgnoreCase(view.getType(), "stack")) {
|
|
||||||
checkedField.addAll(extStack);
|
|
||||||
}
|
|
||||||
int timeIndex = 0;// 时间字段下标
|
|
||||||
ChartViewFieldDTO timeField = null;
|
|
||||||
for (int j = 0; j < checkedField.size(); j++) {
|
|
||||||
if (StringUtils.equalsIgnoreCase(checkedField.get(j).getId(), compareFieldId)) {
|
|
||||||
timeIndex = j;
|
|
||||||
timeField = checkedField.get(j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 计算指标对应的下标
|
// 计算指标对应的下标
|
||||||
int dataIndex = 0;// 数据字段下标
|
int dataIndex = 0;// 数据字段下标
|
||||||
if (StringUtils.containsIgnoreCase(view.getType(), "stack")) {
|
if (StringUtils.containsIgnoreCase(view.getType(), "stack")) {
|
||||||
@ -1015,50 +1002,88 @@ public class ChartViewService {
|
|||||||
} else {
|
} else {
|
||||||
dataIndex = xAxis.size() + i;
|
dataIndex = xAxis.size() + i;
|
||||||
}
|
}
|
||||||
// 无选中字段,或者选中字段已经不在维度list中,或者选中字段日期格式不符合对比类型的,直接将对应数据置为null
|
if (Arrays.asList(ChartConstants.M_Y).contains(compareCalc.getType())) {
|
||||||
if (ObjectUtils.isEmpty(timeField) || !checkCalcType(timeField.getDateStyle(), compareCalc.getType())) {
|
String resultData = compareCalc.getResultData();// 数据设置
|
||||||
// set null
|
// 获取选中字段以及下标
|
||||||
for (String[] item : data) {
|
List<ChartViewFieldDTO> checkedField = new ArrayList<>(xAxis);
|
||||||
item[dataIndex] = null;
|
if (StringUtils.containsIgnoreCase(view.getType(), "stack")) {
|
||||||
|
checkedField.addAll(extStack);
|
||||||
}
|
}
|
||||||
} else {
|
int timeIndex = 0;// 时间字段下标
|
||||||
// 计算 同比/环比
|
ChartViewFieldDTO timeField = null;
|
||||||
// 1,处理当期数据;2,根据type计算上一期数据;3,根据resultData计算结果
|
for (int j = 0; j < checkedField.size(); j++) {
|
||||||
Map<String, String> currentMap = new LinkedHashMap<>();
|
if (StringUtils.equalsIgnoreCase(checkedField.get(j).getId(), compareFieldId)) {
|
||||||
for (String[] item : data) {
|
timeIndex = j;
|
||||||
String[] dimension = Arrays.copyOfRange(item, 0, checkedField.size());
|
timeField = checkedField.get(j);
|
||||||
currentMap.put(StringUtils.join(dimension, "-"), item[dataIndex]);
|
}
|
||||||
}
|
}
|
||||||
|
// 无选中字段,或者选中字段已经不在维度list中,或者选中字段日期格式不符合对比类型的,直接将对应数据置为null
|
||||||
for (int index = 0; index < data.size(); index++) {
|
if (ObjectUtils.isEmpty(timeField) || !checkCalcType(timeField.getDateStyle(), compareCalc.getType())) {
|
||||||
String[] item = data.get(index);
|
// set null
|
||||||
String cTime = item[timeIndex];
|
for (String[] item : data) {
|
||||||
String cValue = item[dataIndex];
|
|
||||||
|
|
||||||
// 获取计算后的时间,并且与所有维度拼接
|
|
||||||
String lastTime = calcLastTime(cTime, compareCalc.getType(), timeField.getDateStyle(), timeField.getDatePattern());
|
|
||||||
String[] dimension = Arrays.copyOfRange(item, 0, checkedField.size());
|
|
||||||
dimension[timeIndex] = lastTime;
|
|
||||||
|
|
||||||
String lastValue = currentMap.get(StringUtils.join(dimension, "-"));
|
|
||||||
if (StringUtils.isEmpty(cValue) || StringUtils.isEmpty(lastValue)) {
|
|
||||||
item[dataIndex] = null;
|
item[dataIndex] = null;
|
||||||
} else {
|
}
|
||||||
if (StringUtils.equalsIgnoreCase(resultData, "sub")) {
|
} else {
|
||||||
item[dataIndex] = new BigDecimal(cValue).subtract(new BigDecimal(lastValue)).toString();
|
// 计算 同比/环比
|
||||||
} else if (StringUtils.equalsIgnoreCase(resultData, "percent")) {
|
// 1,处理当期数据;2,根据type计算上一期数据;3,根据resultData计算结果
|
||||||
if (new BigDecimal(lastValue).compareTo(BigDecimal.ZERO) == 0) {
|
Map<String, String> currentMap = new LinkedHashMap<>();
|
||||||
item[dataIndex] = null;
|
for (String[] item : data) {
|
||||||
} else {
|
String[] dimension = Arrays.copyOfRange(item, 0, checkedField.size());
|
||||||
item[dataIndex] = new BigDecimal(cValue)
|
currentMap.put(StringUtils.join(dimension, "-"), item[dataIndex]);
|
||||||
.divide(new BigDecimal(lastValue), 8, RoundingMode.HALF_UP)
|
}
|
||||||
.subtract(new BigDecimal(1))
|
|
||||||
.setScale(8, RoundingMode.HALF_UP)
|
for (int index = 0; index < data.size(); index++) {
|
||||||
.toString();
|
String[] item = data.get(index);
|
||||||
|
String cTime = item[timeIndex];
|
||||||
|
String cValue = item[dataIndex];
|
||||||
|
|
||||||
|
// 获取计算后的时间,并且与所有维度拼接
|
||||||
|
String lastTime = calcLastTime(cTime, compareCalc.getType(), timeField.getDateStyle(), timeField.getDatePattern());
|
||||||
|
String[] dimension = Arrays.copyOfRange(item, 0, checkedField.size());
|
||||||
|
dimension[timeIndex] = lastTime;
|
||||||
|
|
||||||
|
String lastValue = currentMap.get(StringUtils.join(dimension, "-"));
|
||||||
|
if (StringUtils.isEmpty(cValue) || StringUtils.isEmpty(lastValue)) {
|
||||||
|
item[dataIndex] = null;
|
||||||
|
} else {
|
||||||
|
if (StringUtils.equalsIgnoreCase(resultData, "sub")) {
|
||||||
|
item[dataIndex] = new BigDecimal(cValue).subtract(new BigDecimal(lastValue)).toString();
|
||||||
|
} else if (StringUtils.equalsIgnoreCase(resultData, "percent")) {
|
||||||
|
if (new BigDecimal(lastValue).compareTo(BigDecimal.ZERO) == 0) {
|
||||||
|
item[dataIndex] = null;
|
||||||
|
} else {
|
||||||
|
item[dataIndex] = new BigDecimal(cValue)
|
||||||
|
.divide(new BigDecimal(lastValue), 8, RoundingMode.HALF_UP)
|
||||||
|
.subtract(new BigDecimal(1))
|
||||||
|
.setScale(8, RoundingMode.HALF_UP)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (StringUtils.equalsIgnoreCase(compareCalc.getType(), "percent")) {
|
||||||
|
// 求和
|
||||||
|
BigDecimal sum = new BigDecimal(0);
|
||||||
|
for (int index = 0; index < data.size(); index++) {
|
||||||
|
String[] item = data.get(index);
|
||||||
|
String cValue = item[dataIndex];
|
||||||
|
if (StringUtils.isEmpty(cValue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sum = sum.add(new BigDecimal(cValue));
|
||||||
|
}
|
||||||
|
// 计算占比
|
||||||
|
for (int index = 0; index < data.size(); index++) {
|
||||||
|
String[] item = data.get(index);
|
||||||
|
String cValue = item[dataIndex];
|
||||||
|
if (StringUtils.isEmpty(cValue)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
item[dataIndex] = new BigDecimal(cValue)
|
||||||
|
.divide(sum, 8, RoundingMode.HALF_UP)
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1599,7 +1624,8 @@ public class ChartViewService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String handleVariable(String sql, ChartExtRequest requestList, QueryProvider qp, DataSetTableDTO table, Datasource ds) throws Exception {
|
private String handleVariable(String sql, ChartExtRequest requestList, QueryProvider qp, DataSetTableDTO table, Datasource ds) throws Exception {
|
||||||
List<SqlVariableDetails> sqlVariables = new Gson().fromJson(table.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {}.getType());
|
List<SqlVariableDetails> sqlVariables = new Gson().fromJson(table.getSqlVariableDetails(), new TypeToken<List<SqlVariableDetails>>() {
|
||||||
|
}.getType());
|
||||||
if (requestList != null && CollectionUtils.isNotEmpty(requestList.getFilter())) {
|
if (requestList != null && CollectionUtils.isNotEmpty(requestList.getFilter())) {
|
||||||
for (ChartExtFilterRequest chartExtFilterRequest : requestList.getFilter()) {
|
for (ChartExtFilterRequest chartExtFilterRequest : requestList.getFilter()) {
|
||||||
if (CollectionUtils.isEmpty(chartExtFilterRequest.getValue())) {
|
if (CollectionUtils.isEmpty(chartExtFilterRequest.getValue())) {
|
||||||
@ -1610,8 +1636,8 @@ public class ChartViewService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (String parameter : chartExtFilterRequest.getParameters()) {
|
for (String parameter : chartExtFilterRequest.getParameters()) {
|
||||||
if(parameter.contains("|DE|")){
|
if (parameter.contains("|DE|")) {
|
||||||
if(!parameter.split("\\|DE\\|")[0].equals(table.getId())){
|
if (!parameter.split("\\|DE\\|")[0].equals(table.getId())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
List<SqlVariableDetails> parameters = sqlVariables.stream().filter(item -> item.getVariableName().equalsIgnoreCase(parameter.split("\\|DE\\|")[1])).collect(Collectors.toList());
|
List<SqlVariableDetails> parameters = sqlVariables.stream().filter(item -> item.getVariableName().equalsIgnoreCase(parameter.split("\\|DE\\|")[1])).collect(Collectors.toList());
|
||||||
@ -1619,7 +1645,7 @@ public class ChartViewService {
|
|||||||
String filter = qp.transFilter(chartExtFilterRequest, parameters.get(0));
|
String filter = qp.transFilter(chartExtFilterRequest, parameters.get(0));
|
||||||
sql = sql.replace("${" + parameter.split("\\|DE\\|")[1] + "}", filter);
|
sql = sql.replace("${" + parameter.split("\\|DE\\|")[1] + "}", filter);
|
||||||
}
|
}
|
||||||
}else {
|
} else {
|
||||||
List<SqlVariableDetails> parameters = sqlVariables.stream().filter(item -> item.getVariableName().equalsIgnoreCase(parameter)).collect(Collectors.toList());
|
List<SqlVariableDetails> parameters = sqlVariables.stream().filter(item -> item.getVariableName().equalsIgnoreCase(parameter)).collect(Collectors.toList());
|
||||||
if (CollectionUtils.isNotEmpty(parameters)) {
|
if (CollectionUtils.isNotEmpty(parameters)) {
|
||||||
String filter = qp.transFilter(chartExtFilterRequest, parameters.get(0));
|
String filter = qp.transFilter(chartExtFilterRequest, parameters.get(0));
|
||||||
|
@ -1429,7 +1429,8 @@ export default {
|
|||||||
reserve_one: '1',
|
reserve_one: '1',
|
||||||
reserve_two: '2',
|
reserve_two: '2',
|
||||||
proportion: 'Proportion',
|
proportion: 'Proportion',
|
||||||
label_content: 'Label Content'
|
label_content: 'Label Content',
|
||||||
|
percent: 'Percent'
|
||||||
},
|
},
|
||||||
dataset: {
|
dataset: {
|
||||||
parse_filed: 'Parse Field',
|
parse_filed: 'Parse Field',
|
||||||
|
@ -1429,7 +1429,8 @@ export default {
|
|||||||
reserve_one: '一位',
|
reserve_one: '一位',
|
||||||
reserve_two: '两位',
|
reserve_two: '两位',
|
||||||
proportion: '佔比',
|
proportion: '佔比',
|
||||||
label_content: '標籤展示'
|
label_content: '標籤展示',
|
||||||
|
percent: '占比'
|
||||||
},
|
},
|
||||||
dataset: {
|
dataset: {
|
||||||
parse_filed: '解析字段',
|
parse_filed: '解析字段',
|
||||||
|
@ -1428,7 +1428,8 @@ export default {
|
|||||||
reserve_one: '一位',
|
reserve_one: '一位',
|
||||||
reserve_two: '两位',
|
reserve_two: '两位',
|
||||||
proportion: '占比',
|
proportion: '占比',
|
||||||
label_content: '标签展示'
|
label_content: '标签展示',
|
||||||
|
percent: '占比'
|
||||||
},
|
},
|
||||||
dataset: {
|
dataset: {
|
||||||
parse_filed: '解析字段',
|
parse_filed: '解析字段',
|
||||||
|
@ -153,7 +153,7 @@
|
|||||||
</el-dropdown>
|
</el-dropdown>
|
||||||
</el-dropdown-item>
|
</el-dropdown-item>
|
||||||
|
|
||||||
<!--同比/环比-->
|
<!--同比/环比等快速计算-->
|
||||||
<el-dropdown-item v-show="!item.chartId && chart.type !== 'table-info'">
|
<el-dropdown-item v-show="!item.chartId && chart.type !== 'table-info'">
|
||||||
<el-dropdown
|
<el-dropdown
|
||||||
placement="right-start"
|
placement="right-start"
|
||||||
@ -175,6 +175,7 @@
|
|||||||
:disabled="disableEditCompare"
|
:disabled="disableEditCompare"
|
||||||
:command="beforeQuickCalc('setting')"
|
:command="beforeQuickCalc('setting')"
|
||||||
>{{ $t('chart.yoy_label') }}...</el-dropdown-item>
|
>{{ $t('chart.yoy_label') }}...</el-dropdown-item>
|
||||||
|
<el-dropdown-item :command="beforeQuickCalc('percent')">{{ $t('chart.percent') }}</el-dropdown-item>
|
||||||
</el-dropdown-menu>
|
</el-dropdown-menu>
|
||||||
</el-dropdown>
|
</el-dropdown>
|
||||||
</el-dropdown-item>
|
</el-dropdown-item>
|
||||||
@ -383,6 +384,10 @@ export default {
|
|||||||
case 'setting':
|
case 'setting':
|
||||||
this.editCompare()
|
this.editCompare()
|
||||||
break
|
break
|
||||||
|
case 'percent':
|
||||||
|
this.item.compareCalc.type = 'percent'
|
||||||
|
this.$emit('onQuotaItemChange', this.item)
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@
|
|||||||
</el-dropdown>
|
</el-dropdown>
|
||||||
</el-dropdown-item>
|
</el-dropdown-item>
|
||||||
|
|
||||||
<!--同比/环比-->
|
<!--同比/环比等快速计算-->
|
||||||
<el-dropdown-item v-show="!item.chartId && chart.type !== 'table-info'">
|
<el-dropdown-item v-show="!item.chartId && chart.type !== 'table-info'">
|
||||||
<el-dropdown
|
<el-dropdown
|
||||||
placement="right-start"
|
placement="right-start"
|
||||||
@ -175,6 +175,7 @@
|
|||||||
:disabled="disableEditCompare"
|
:disabled="disableEditCompare"
|
||||||
:command="beforeQuickCalc('setting')"
|
:command="beforeQuickCalc('setting')"
|
||||||
>{{ $t('chart.yoy_label') }}...</el-dropdown-item>
|
>{{ $t('chart.yoy_label') }}...</el-dropdown-item>
|
||||||
|
<el-dropdown-item :command="beforeQuickCalc('percent')">{{ $t('chart.percent') }}</el-dropdown-item>
|
||||||
</el-dropdown-menu>
|
</el-dropdown-menu>
|
||||||
</el-dropdown>
|
</el-dropdown>
|
||||||
</el-dropdown-item>
|
</el-dropdown-item>
|
||||||
@ -380,6 +381,10 @@ export default {
|
|||||||
case 'setting':
|
case 'setting':
|
||||||
this.editCompare()
|
this.editCompare()
|
||||||
break
|
break
|
||||||
|
case 'percent':
|
||||||
|
this.item.compareCalc.type = 'percent'
|
||||||
|
this.$emit('onQuotaItemChange', this.item)
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user