积木报表 散点图 自定义颜色

发布时间:2023年12月19日

前言 : 积木报表 散点图支持自定义颜色,但是如果 type 变化的话,自定义的颜色就会置为默认颜色,所以这里添加自定义颜色方法

[
{"name":28604,"value":77,"type":"1990"},
{"name":44056,"value":81.8,"type":"2015"},
]

代码编写

  1. 请求报表时,先修改报表的json字符串

颜色枚举类

@Getter
@AllArgsConstructor
public enum ReportColor {

    // 这里定义自己需要的颜色
    BLUE("#2D8CF0", 1),
    YELLOW("#DEF02D", 2),
    ORIGIN("#F0BD2D", 3),
    RED("#F0322D", 4)
    ;


    private final String code;


    private final Integer sort;


    public static ReportColor ofSort(Integer sort) {
        return Stream.of(values()).filter(color -> color.getSort().equals(sort)).findFirst().orElse(null);
    }
}

实体类

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class JimuColor {

    private String type;

    private String r;

    private List<JimuColorStops> colorStops;

}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class JimuColorStops {

    private String offset;


    private String color;

}

@Data
public class JimuReport {

    private String id;

    private String code;

    private String name;

    private String note;

    private String status;

    private String type;

    private String jsonStr;

    private String apiUrl;
}


修改方法

private void updateColor(Report report) {
        JimuReport jimuReport = jimuReportMapper.selectById(report.getId());
        String jsonStr = jimuReport.getJsonStr();
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        if (null == jsonObject) {
            return;
        }
        String chartListStr = jsonObject.getString("chartList");
        JSONArray chartList = JSONArray.parseArray(chartListStr);
        if (null == chartList) {
            return;
        }
        JSONArray chartListNew = new JSONArray();
        chartList.forEach(data -> {
            JSONObject configObj = JSONObject.parseObject(JSONObject.toJSONString(data));
            if (null == configObj) {
                return;
            }
            String configStr = configObj.getString("config");
            JSONObject config = JSONObject.parseObject(configStr);
            if (null == config) {
                return;
            }
            String seriesStr = config.getString("series");
            JSONArray seriesList = JSONArray.parseArray(seriesStr);
            if (null == seriesList) {
                return;
            }
            JSONArray seriesListNew = new JSONArray();
            AtomicInteger sort = new AtomicInteger();
            seriesList.forEach(dataOne -> {
                JSONObject seriesOneStr = JSONObject.parseObject(JSONObject.toJSONString(dataOne));
                if (null == seriesOneStr) {
                    return;
                }
                String itemStyleStr = seriesOneStr.getString("itemStyle");
                JSONObject itemStyle = JSONObject.parseObject(itemStyleStr);
                sort.getAndAdd(1);
                ReportColor reportColor = ReportColor.ofSort(sort.get());
                if (null != reportColor) {
                    List<JimuColorStops> list = Lists.newArrayList();
                    list.add(JimuColorStops.builder().offset("0").color(reportColor.getCode()).build());
                    list.add(JimuColorStops.builder().offset("1").color(reportColor.getCode()).build());
                    JimuColor jimuColor = JimuColor.builder().type("radial").r("0.5").colorStops(list).build();
                    itemStyle.put("color", JSONObject.parseObject(JSONObject.toJSONString(jimuColor)));
                    seriesOneStr.put("itemStyle", itemStyle);
                    seriesListNew.add(seriesOneStr);
                }
            });
            config.put("series", seriesListNew);
            configObj.put("config", config.toJSONString());
            chartListNew.add(configObj);
        });
        jsonObject.put("chartList", chartListNew);
        jimuReport.setJsonStr(jsonObject.toJSONString());
        jimuReportMapper.updateById(jimuReport);
    }

mapper

@Component
public interface BizReportMapper {

    /**
     * @param id
     * @return {@link JimuReport}
     */
    JimuReport selectById(String id);

    /**
     * @param jimuReport
     */
    void updateById(JimuReport jimuReport);
}

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.business.mapper.BizReportMapper">

    <resultMap type="com.ruoyi.business.domain.jimu.JimuReport" id="JimuReportResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="note"    column="note"    />
        <result property="status"    column="status"    />
        <result property="type"    column="type"    />
        <result property="jsonStr"    column="json_str"    />
        <result property="apiUrl"    column="api_url"    />

    </resultMap>

    <sql id="selectReport">
        select
            `id`, `code`, `name`, `note`, `status`, `type`, `json_str`, `api_url`
        from jimu_report
    </sql>

    <select id="selectById" resultMap="JimuReportResult">
        <include refid="selectReport"/>
        WHERE id = #{id}
    </select>

    <update id="updateById">
        UPDATE jimu_report SET json_str = #{jsonStr} where id = #{id}
    </update>

</mapper>

文章来源:https://blog.csdn.net/wonder_dog/article/details/135086074
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。