Merge pull request #11647 from dataease/pr@dev-v2@chart-indicator-yoy-fix

fix(图表): 修改指标卡图表的同环比计算逻辑,以及修复查看数据的数据和同环比数据不一样的问题
This commit is contained in:
jianneng-fit2cloud 2024-08-19 21:30:41 +08:00 committed by GitHub
commit 7aa60bddd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 43 deletions

View File

@ -115,7 +115,7 @@ public class YoyChartHandler extends DefaultChartHandler {
yoyData.addAll(resultData);
var result = this.buildNormalResult(view, formatResult, filterResult, yoyData);
expandedResult.setData(result);
expandedResult.setOriginData(originData);
expandedResult.setOriginData(resultData);
expandedResult.setQuerySql(originSql);
}
return expandedResult;

View File

@ -1,42 +1,38 @@
package io.dataease.chart.charts.impl.numeric;
import io.dataease.chart.charts.impl.YoyChartHandler;
import io.dataease.chart.utils.ChartDataBuild;
import io.dataease.engine.sql.SQLProvider;
import io.dataease.engine.trans.Dimension2SQLObj;
import io.dataease.engine.trans.Quota2SQLObj;
import io.dataease.engine.utils.Utils;
import io.dataease.extensions.datasource.dto.DatasourceRequest;
import io.dataease.extensions.datasource.dto.DatasourceSchemaDTO;
import io.dataease.extensions.datasource.model.SQLMeta;
import io.dataease.extensions.datasource.provider.Provider;
import io.dataease.extensions.view.dto.*;
import io.dataease.extensions.view.util.FieldUtil;
import lombok.Getter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class IndicatorHandler extends NumericalChartHandler {
public class IndicatorHandler extends YoyChartHandler {
@Getter
private String render = "custom";
@Getter
private String type = "indicator";
@Override
public Map<String, Object> buildNormalResult(ChartViewDTO view, AxisFormatResult formatResult, CustomFilterResult filterResult, List<String[]> data) {
boolean isDrill = filterResult.getFilterList().stream().anyMatch(ele -> ele.getFilterType() == 1);
var xAxis = formatResult.getAxisMap().get(ChartAxis.xAxis);
var yAxis = formatResult.getAxisMap().get(ChartAxis.yAxis);
return ChartDataBuild.transNormalChartData(xAxis, yAxis, view, data, isDrill);
}
@Override
public <T extends ChartCalcDataResult> T calcChartResult(ChartViewDTO view, AxisFormatResult formatResult, CustomFilterResult filterResult, Map<String, Object> sqlMap, SQLMeta sqlMeta, Provider provider) {
var dsMap = (Map<Long, DatasourceSchemaDTO>) sqlMap.get("dsMap");
List<String> dsList = new ArrayList<>();
for (Map.Entry<Long, DatasourceSchemaDTO> next : dsMap.entrySet()) {
dsList.add(next.getValue().getType());
}
boolean needOrder = Utils.isNeedOrder(dsList);
boolean crossDs = Utils.isCrossDs(dsMap);
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setDsList(dsMap);
this.setIndicatorHandlerXAxis(formatResult, filterResult);
return (T) super.calcChartResult(view, formatResult, filterResult, sqlMap, sqlMeta, provider);
}
private void setIndicatorHandlerXAxis(AxisFormatResult formatResult, CustomFilterResult filterResult) {
var xAxis = formatResult.getAxisMap().get(ChartAxis.xAxis);
var yAxis = formatResult.getAxisMap().get(ChartAxis.yAxis);
var allFields = (List<ChartViewFieldDTO>) filterResult.getContext().get("allFields");
@ -47,36 +43,18 @@ public class IndicatorHandler extends NumericalChartHandler {
if (isYoy) {
xAxis.clear();
// 设置维度字段从同环比中获取用户选择的字段
xAxis.addAll(allFields.stream().filter(i-> StringUtils.endsWithIgnoreCase(i.getId().toString(),yAxis.get(0).getCompareCalc().getField().toString())).toList());
xAxis.addAll(allFields.stream().filter(i -> org.springframework.util.StringUtils.endsWithIgnoreCase(i.getId().toString(), yAxis.get(0).getCompareCalc().getField().toString())).toList());
xAxis.get(0).setSort("desc");
if(StringUtils.endsWithIgnoreCase("month_mom",compareCalc.getType())){
if (org.springframework.util.StringUtils.endsWithIgnoreCase("month_mom", compareCalc.getType())) {
xAxis.get(0).setDateStyle("y_M");
}
if(StringUtils.endsWithIgnoreCase("day_mom",compareCalc.getType())){
if (org.springframework.util.StringUtils.endsWithIgnoreCase("day_mom", compareCalc.getType())) {
xAxis.get(0).setDateStyle("y_M_d");
}
if(StringUtils.endsWithIgnoreCase("year_mom",compareCalc.getType())){
if (org.springframework.util.StringUtils.endsWithIgnoreCase("year_mom", compareCalc.getType())) {
xAxis.get(0).setDateStyle("y");
}
}
Dimension2SQLObj.dimension2sqlObj(sqlMeta, xAxis, FieldUtil.transFields(allFields), crossDs, dsMap, Utils.getParams(FieldUtil.transFields(allFields)), view.getCalParams());
Quota2SQLObj.quota2sqlObj(sqlMeta, yAxis, FieldUtil.transFields(allFields), crossDs, dsMap, Utils.getParams(FieldUtil.transFields(allFields)), view.getCalParams());
String querySql = SQLProvider.createQuerySQL(sqlMeta, true, needOrder, view);
querySql = provider.rebuildSQL(querySql, sqlMeta, crossDs, dsMap);
datasourceRequest.setQuery(querySql);
logger.debug("indicator chart sql: " + querySql);
List<String[]> data = (List<String[]>) provider.fetchResultField(datasourceRequest).get("data");
boolean isdrill = filterResult
.getFilterList()
.stream()
.anyMatch(ele -> ele.getFilterType() == 1);
quickCalc(xAxis, yAxis, data);
Map<String, Object> result = ChartDataBuild.transNormalChartData(xAxis, yAxis, view, data, isdrill);
T calcResult = (T) new ChartCalcDataResult();
calcResult.setData(result);
calcResult.setContext(filterResult.getContext());
calcResult.setQuerySql(querySql);
calcResult.setOriginData(data);
return calcResult;
formatResult.getAxisMap().put(ChartAxis.xAxis, xAxis);
}
}