image-20240715095923286

图表类

/**
 * 图表类
 * @Author: lizheng
 * @Date: 2023/10/21 17:28
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "图表")
public class Chart<T> {
    @Schema(description = "x轴数据集合")
    private List<String> xData;
    @Schema(description = "y轴数据集合")
    private List<YData<T>> yData;
}

y 轴数据类

@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "y轴数据")
public class YData<T> {
    @Schema(description = "显示名称")
    private String name;
    @Schema(description = "字段名称")
    private String key;
    @Schema(description = "图表类型(柱状、折线等)")
    private String type;
    @Schema(description = "数据")
    private List<T> data;
}

x 轴数据,我们需要进行遍历年,业务代码如下:

@Override
    public Chart<Double> naturalGasSupplyAndDemandBalance() {
        MPJLambdaWrapper<CompGasBalance> wrapper = new MPJLambdaWrapper<>();
        List<CompGasBalance> compGasBalances = compGasBalanceMapper.selectList(wrapper);
        if (compGasBalances.isEmpty()) {
            return null;
        }
        //y 轴数据
        List<YData<Double>> yData = new ArrayList<>();
        //x 轴数据
        List<String> xData = new ArrayList<>();
        //revenue
        List<Double> supplyNum = new ArrayList<>();
        //increase
        List<Double> gasNeedTotal = new ArrayList<>();
        for (CompGasBalance compGasBalance : compGasBalances) {
            xData.add(compGasBalance.getYear());
            supplyNum.add(compGasBalance.getSupplyNum());
            gasNeedTotal.add(compGasBalance.getGasNeedTotal());
        }
        yData.add(new YData<>("收入","revenue", ChartTypeConstants.line,supplyNum));
        yData.add(new YData<>("增长","increase", ChartTypeConstants.line,gasNeedTotal));
        return new Chart<>(xData,yData);
    }