`
kanpiaoxue
  • 浏览: 1781295 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

jgraph快速生成drawio可绘制图(2):CSV格式

 
阅读更多

 

官方文档:https://drawio-app.com/import-from-csv-to-drawio/

 

public class BaseLineObj {
    /**
     * Builder to build {@link BaseLineObj}.
     */
    public static final class Builder {
        private Integer flowId;
        private Integer queueLevel;

        private Builder() {
        }

        /**
         * Builder method of the builder.
         * 
         * @return built class
         */
        public BaseLineObj build() {
            return new BaseLineObj(this);
        }

        /**
         * Builder method for flowId parameter.
         * 
         * @param flowId
         *            field to set
         * @return builder
         */
        public Builder withFlowId(Integer flowId) {
            this.flowId = flowId;
            return this;
        }

        /**
         * Builder method for queueLevel parameter.
         * 
         * @param queueLevel
         *            field to set
         * @return builder
         */
        public Builder withQueueLevel(Integer queueLevel) {
            this.queueLevel = queueLevel;
            return this;
        }
    }

    private Integer flowId;

    private Integer queueLevel;

    /**
     *
     */
    public BaseLineObj() {
        super();
    }

    private BaseLineObj(Builder builder) {
        this.flowId = builder.flowId;
        this.queueLevel = builder.queueLevel;
    }

    /**
     * Creates builder to build {@link BaseLineObj}.
     * 
     * @return created builder
     */
    public static Builder builder() {
        return new Builder();
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        BaseLineObj other = (BaseLineObj) obj;
        if (flowId == null) {
            if (other.flowId != null) {
                return false;
            }
        } else if (!flowId.equals(other.flowId)) {
            return false;
        }
        return true;
    }

    /**
     * @return the flowId
     */
    public Integer getFlowId() {
        return flowId;
    }

    /**
     * @return the queueLevel
     */
    public Integer getQueueLevel() {
        return queueLevel;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((flowId == null) ? 0 : flowId.hashCode());
        return result;
    }

    /**
     * @param flowId
     *            the flowId to set
     */
    public void setFlowId(Integer flowId) {
        this.flowId = flowId;
    }

    /**
     * @param queueLevel
     *            the queueLevel to set
     */
    public void setQueueLevel(Integer queueLevel) {
        this.queueLevel = queueLevel;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return GsonUtils.toJSON(this);
    }

}

 

public class CSVFormatRecord {

    /**
     * Builder to build {@link CSVFormatRecord}.
     */
    public static final class Builder {
        private String id;
        private String step;
        private String fill;
        private String stroke;
        private String shape;
        private String refs;

        private Builder() {
        }

        /**
         * Builder method of the builder.
         *
         * @return built class
         */
        public CSVFormatRecord build() {
            return new CSVFormatRecord(this);
        }

        /**
         * Builder method for fill parameter.
         *
         * @param fill
         *            field to set
         * @return builder
         */
        public Builder withFill(String fill) {
            this.fill = fill;
            return this;
        }

        /**
         * Builder method for id parameter.
         *
         * @param id
         *            field to set
         * @return builder
         */
        public Builder withId(String id) {
            this.id = id;
            return this;
        }

        /**
         * Builder method for refs parameter.
         *
         * @param refs
         *            field to set
         * @return builder
         */
        public Builder withRefs(String refs) {
            this.refs = refs;
            return this;
        }

        /**
         * Builder method for shape parameter.
         *
         * @param shape
         *            field to set
         * @return builder
         */
        public Builder withShape(String shape) {
            this.shape = shape;
            return this;
        }

        /**
         * Builder method for step parameter.
         *
         * @param step
         *            field to set
         * @return builder
         */
        public Builder withStep(String step) {
            this.step = step;
            return this;
        }

        /**
         * Builder method for stroke parameter.
         *
         * @param stroke
         *            field to set
         * @return builder
         */
        public Builder withStroke(String stroke) {
            this.stroke = stroke;
            return this;
        }
    }

    private String id;

    private String step;
    private String fill;
    private String stroke;
    private String shape;
    private String refs;

    /**
     *
     */
    public CSVFormatRecord() {
        super();
    }

    private CSVFormatRecord(Builder builder) {
        this.id = builder.id;
        this.step = builder.step;
        this.fill = builder.fill;
        this.stroke = builder.stroke;
        this.shape = builder.shape;
        this.refs = builder.refs;
    }

    /**
     * Creates builder to build {@link CSVFormatRecord}.
     *
     * @return created builder
     */
    public static Builder builder() {
        return new Builder();
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        CSVFormatRecord other = (CSVFormatRecord) obj;
        if (fill == null) {
            if (other.fill != null) {
                return false;
            }
        } else if (!fill.equals(other.fill)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (refs == null) {
            if (other.refs != null) {
                return false;
            }
        } else if (!refs.equals(other.refs)) {
            return false;
        }
        if (shape == null) {
            if (other.shape != null) {
                return false;
            }
        } else if (!shape.equals(other.shape)) {
            return false;
        }
        if (step == null) {
            if (other.step != null) {
                return false;
            }
        } else if (!step.equals(other.step)) {
            return false;
        }
        if (stroke == null) {
            if (other.stroke != null) {
                return false;
            }
        } else if (!stroke.equals(other.stroke)) {
            return false;
        }
        return true;
    }

    /**
     * @return the fill
     */
    public String getFill() {
        return fill;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @return the refs
     */
    public String getRefs() {
        return refs;
    }

    /**
     * @return the shape
     */
    public String getShape() {
        return shape;
    }

    /**
     * @return the step
     */
    public String getStep() {
        return step;
    }

    /**
     * @return the stroke
     */
    public String getStroke() {
        return stroke;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((fill == null) ? 0 : fill.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((refs == null) ? 0 : refs.hashCode());
        result = prime * result + ((shape == null) ? 0 : shape.hashCode());
        result = prime * result + ((step == null) ? 0 : step.hashCode());
        result = prime * result + ((stroke == null) ? 0 : stroke.hashCode());
        return result;
    }

    /**
     * @param fill
     *            the fill to set
     */
    public void setFill(String fill) {
        this.fill = fill;
    }

    /**
     * @param id
     *            the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @param refs
     *            the refs to set
     */
    public void setRefs(String refs) {
        this.refs = refs;
    }

    /**
     * @param shape
     *            the shape to set
     */
    public void setShape(String shape) {
        this.shape = shape;
    }

    /**
     * @param step
     *            the step to set
     */
    public void setStep(String step) {
        this.step = step;
    }

    /**
     * @param stroke
     *            the stroke to set
     */
    public void setStroke(String stroke) {
        this.stroke = stroke;
    }

    public String toGraphCSV() {
        Preconditions.checkNotNull(id, "id is null");
        Preconditions.checkNotNull(step, "step is null");
        Preconditions.checkNotNull(fill, "fill is null");
        Preconditions.checkNotNull(stroke, "stroke is null");
        Preconditions.checkNotNull(shape, "shape is null");
        Preconditions.checkNotNull(refs, "refs is null");
        return Joiner.on(",").join(id, step, fill, stroke, shape, refs);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return GsonUtils.toJSON(this);
    }

}

 

package test;
import org.apache.commons.collections.CollectionUtils;
import org.jgrapht.Graphs;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DirectedAcyclicGraph;
import org.kanpiaoxue.code.KeyValue;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;


import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName: Test1
 * @author kanpiaoxue
 * @version 1.0
 * @CreateTime: 2019/11/11 20:17:31
 * @Description:
 */
public class Test1 {

    private static final KeyValue<String, String> CURRENT_NODE_COLOR =
            new KeyValue<String, String>("#fff2cc", "#d6b656");

    private static final KeyValue<String, String> DEFAULT_NODE_COLOR =
            new KeyValue<String, String>("#dae8fc", "#6c8ebf");

    private static final Map<Integer, KeyValue<String, String>> COLOR_MAP = Maps.newHashMap();

    static {
        COLOR_MAP.put(1, DEFAULT_NODE_COLOR);
        COLOR_MAP.put(2, new KeyValue<String, String>("#d5e8d4", "#82b366"));
        COLOR_MAP.put(3, new KeyValue<String, String>("#f8cecc", "#b85450"));
    }

    private static List<String> CSV_HEADER = Lists.newArrayList();

    static {
        CSV_HEADER.add("## Hello World");
        CSV_HEADER.add("# label: %step%");
        CSV_HEADER.add("# style: shape=%shape%;fillColor=%fill%;strokeColor=%stroke%;");
        CSV_HEADER.add("# namespace: csvimport-");
        CSV_HEADER.add(
                "# connect: {\"from\":\"refs\", \"to\":\"id\", \"invert\":true, \"style\":\"curved=0;endArrow=blockThin;endFill=1;\"}");
        CSV_HEADER.add("# width: auto");
        CSV_HEADER.add("# height: auto");
        CSV_HEADER.add("# padding: 15");
        CSV_HEADER.add("# ignore: id,shape,fill,stroke,refs");
        CSV_HEADER.add("# nodespacing: 40");
        CSV_HEADER.add("# levelspacing: 100");
        CSV_HEADER.add("# edgespacing: 40");
        CSV_HEADER.add("# layout: auto");
        CSV_HEADER.add("## CSV starts under this line");
    }

    private static final List<String> HEADER_LIST = Lists.newArrayList("id,step,fill,stroke,shape,refs");

    /**
     *
     * @param args
     * @author kanpiaoxue * @CreateTime: 2019/05/20 10:43:16
     */
    public static void main(String[] args) throws Exception {
        test07();
    }

    public static void test07() throws Exception {
        BaseLineObj currentNode = BaseLineObj.builder().withFlowId(4).withQueueLevel(3).build();
        List<String> datas = buildDatas(currentNode);

        List<String> list = Lists.newArrayList();
        list.addAll(CSV_HEADER);
        list.addAll(HEADER_LIST);
        list.addAll(datas);

        System.out.println(Joiner.on("\n").skipNulls().join(list));

    }

    private static DirectedAcyclicGraph<BaseLineObj, DefaultEdge> buildDAG() {
        DirectedAcyclicGraph<BaseLineObj, DefaultEdge> g =
                new DirectedAcyclicGraph<BaseLineObj, DefaultEdge>(DefaultEdge.class);
        BaseLineObj o1 = BaseLineObj.builder().withFlowId(1).withQueueLevel(3).build();
        BaseLineObj o2 = BaseLineObj.builder().withFlowId(2).withQueueLevel(3).build();
        BaseLineObj o3 = BaseLineObj.builder().withFlowId(3).withQueueLevel(3).build();
        BaseLineObj o4 = BaseLineObj.builder().withFlowId(4).withQueueLevel(2).build();
        BaseLineObj o5 = BaseLineObj.builder().withFlowId(5).withQueueLevel(2).build();
        BaseLineObj o6 = BaseLineObj.builder().withFlowId(6).withQueueLevel(1).build();
        BaseLineObj o7 = BaseLineObj.builder().withFlowId(7).withQueueLevel(1).build();
        g.addVertex(o1);
        g.addVertex(o2);
        g.addVertex(o3);
        g.addVertex(o4);
        g.addVertex(o5);
        g.addVertex(o6);
        g.addVertex(o7);

        g.addEdge(o1, o2);
        g.addEdge(o1, o3);
        g.addEdge(o2, o4);
        g.addEdge(o5, o4);
        g.addEdge(o4, o6);
        g.addEdge(o6, o7);
        g.addEdge(o1, o7);
        return g;
    }

    private static List<String> buildDatas(BaseLineObj current) {
        DirectedAcyclicGraph<BaseLineObj, DefaultEdge> g = buildDAG();

        List<CSVFormatRecord> datas = g.vertexSet().stream().map(o -> {
            List<BaseLineObj> successorListOf = Graphs.successorListOf(g, o);
            String refs = processRefs(successorListOf, BaseLineObj::getFlowId);

            KeyValue<String, String> kv =
                    current.equals(o) ? CURRENT_NODE_COLOR : processBaseLineColor(o.getQueueLevel());
            String fill = kv.getKey();
            String stroke = kv.getValue();

            return CSVFormatRecord.builder().withId(o.getFlowId().toString())
                    .withStep(String.format("\"flowId:%s,level:%s\"", o.getFlowId(), o.getQueueLevel()))
                    .withFill(fill).withStroke(stroke).withShape("rectangle").withRefs(refs).build();
        }).collect(Collectors.toList());

        List<String> rs = datas.stream().map(CSVFormatRecord::toGraphCSV).collect(Collectors.toList());
        return rs;

    }

    private static KeyValue<String, String> processBaseLineColor(Integer queueLevel) {
        KeyValue<String, String> obj = COLOR_MAP.get(queueLevel);
        if (Objects.isNull(obj)) {
            return DEFAULT_NODE_COLOR;
        }
        return obj;
    }

    private static <E> String processRefs(List<E> list, Function<? super E, ? extends Integer> mapper) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        String tmp = Joiner.on(",").skipNulls().join(list.stream().map(mapper).collect(Collectors.toList()));
        if (1 == list.size()) {
            return tmp;
        }
        return String.format("\"%s\"", tmp);
    }
}

 

输出结果 写道
## Hello World
# label: %step%
# style: shape=%shape%;fillColor=%fill%;strokeColor=%stroke%;
# namespace: csvimport-
# connect: {"from":"refs", "to":"id", "invert":true, "style":"curved=0;endArrow=blockThin;endFill=1;"}
# width: auto
# height: auto
# padding: 15
# ignore: id,shape,fill,stroke,refs
# nodespacing: 40
# levelspacing: 100
# edgespacing: 40
# layout: auto
## CSV starts under this line
id,step,fill,stroke,shape,refs
1,"flowId:1,level:3",#f8cecc,#b85450,rectangle,
2,"flowId:2,level:3",#f8cecc,#b85450,rectangle,1
3,"flowId:3,level:3",#f8cecc,#b85450,rectangle,1
4,"flowId:4,level:2",#fff2cc,#d6b656,rectangle,"2,5"
5,"flowId:5,level:2",#d5e8d4,#82b366,rectangle,
6,"flowId:6,level:1",#dae8fc,#6c8ebf,rectangle,4
7,"flowId:7,level:1",#dae8fc,#6c8ebf,rectangle,"6,1"

 

按照官网 https://drawio-app.com/import-from-csv-to-drawio/ 的描述,将上面输出的内容到 https://www.draw.io/ 里面,

Click on 菜单:Arrange > Insert > Advanced > CSV.

进行操作即可。

如果觉得图形布局不够好看,可以操作菜单:Arrange > Layout

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics