`
kanpiaoxue
  • 浏览: 1781194 次
  • 性别: 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

分享到:
评论

相关推荐

    jgraph-draw.io

    **jgraph-draw.io** 是一个在线的图形编辑工具,专用于创建和编辑流程图。它基于 **jGraph** 技术,提供了一个用户友好的Web界面,使得非技术人员也能轻松绘制各种流程、图表和组织结构。这个工具的灵活性和易用性使...

    drawio桌面版流程图绘制工具64位.rar

    4. 导入导出兼容:Drawio可导入SVG、XML、PDF等多种格式的文件,导出时也能选择PNG、PDF、SVG等格式,方便与其他应用进行数据交换。 5. 版本控制:通过集成Git,Drawio提供了版本控制功能,用户可以追踪和恢复历史...

    draw.io可配置的图表绘制工具

    draw.io是一个可配置的图表/白板可视化应用程序。draw.io 由英国软件公司JGraph Ltd拥有和开发。这是一个开源项目(但对贡献是封闭的),github链接:https://github.com/jgraph,可以绘制流程图、UML、类图、组织...

    drawIo 2030 绘图神器,直接安装 ,中文

    是一款图表编辑工具, 可以用来编辑工作流, 商务图,BPM, org charts, UML, ER图, 网络拓朴图,思维导图等并且可以快速制作,且支持中文版本方便快捷. drawio是一款强大、免费的绘图工具(基本可以替代收费的visio等)...

    jgraph

    通过使用SVG(可缩放矢量图形)或VML(矢量标记语言)作为后端渲染技术,jgraph能够兼容各种现代和旧版浏览器。在iteye博客中,作者liujianeye分享了关于如何利用jgraph进行图形绘制和交互实现的一些实践经验和技巧...

    在线画图wwwdrawio网站的源码基于mxGraph开发

    2. **文件结构**:压缩包中的`jgraph-drawio-e6d99f2`目录包含了源码的各个模块,例如`src/js`存放JavaScript代码,`src/css`存储样式文件,`src/images`包含各种图形资源,`src/json`保存预设的图形模板。...

    draw.io-13.6.2-windows-no-installer

    draw.io-13.6.2-windows-no-installer

    drawio-webapp

    用于drawio嵌入模式微调了一些样式,扩展了几个消息私有化drawio的部署与微调我们可以直接git clone https://github.com/jgraph/drawio并部署src/main/webapp目录即可如果部分内容需要微调,可加入脚本,或者修改...

    jgraph源码+例子

    JGraph是一款强大的图形绘制库,主要用于创建复杂的二维图表和图形用户界面。它是用Java语言编写的,因此可以跨平台运行。这个压缩包“jgraph源码+例子”包含的是JGraph库的源代码以及相关的示例,对于学习和理解...

    JGraph

    通过JGraph,用户可以轻松地创建各种类型的图表,包括但不限于流程图、网络图、组织结构图等。该功能主要依赖于其强大的绘图引擎,能够支持复杂的图表结构和样式定制。 ##### 2.2 图表交互(Graph Interaction) ...

    draw.io-universal-20.8.16.dmg

    draw.io一款画图软件,draw.io Mac版本,官网https://github.com/jgraph/drawio-desktop

    Java图形组件 JGraph

    它提供了丰富的功能,让开发者能够轻松地构建可交互的、动态的图形模型,适用于数据可视化、流程图、组织结构图等多种场景。JGraph不仅支持基本的图形绘制,还支持拖放操作、事件处理、布局算法等高级特性,使得图形...

    JGraph 手册+The JGraph Tutorial

    它提供了丰富的功能,使开发者能够轻松地在应用程序中集成可交互的图形元素,如节点、边和复杂的图布局。JGraph的核心是其直观的API,允许程序员通过简单的代码实现复杂的图形操作。 **JGraph手册内容** JGraph的...

    JGraph document

    7. **保存和加载图形**:JGraph支持XML格式的数据交换,可以将整个图形结构序列化为XML,便于保存和恢复。 **四、JGraph教程** 1. **基础教程**:从创建基本的图形节点和边开始,了解如何在JGraph中布局和绘制图形...

    jgraph官方手册

    - **导出功能**:用户可以将绘制好的图形导出为多种格式,如PDF、SVG等。 #### 四、总结 jgraph作为一款功能强大的Java图形组件库,不仅提供了丰富的图形绘制和编辑功能,还支持高级的图形分析与布局算法。通过本...

    jgraph的jar包 zip 源代码

    **JGraph:图形绘制库** JGraph是一款强大的Java图形绘制库,它允许开发者在Java应用程序中创建和编辑交互式图表。这个"jgraph的jar包 zip 源代码"包含的是JGraph库的编译后的jar包以及源码文件,使得用户可以深入...

    java源码:Java图形组件 JGraph.rar

    2. **图形组件**: JGraph中的"组件"指的是可以绘制在屏幕上的图形对象,如节点、边、图形等。这些组件可以被定制以适应各种需求,比如改变形状、颜色、大小或添加交互行为。 3. **模型-视图-控制器(MVC)模式**: ...

    JGraph小列子加jar包

    首先,JGraph的核心组件是mxGraph,这是一个灵活且高度可定制的图形编辑框架。它支持各种图形类型,如节点、边和连接,以及复杂的布局算法。通过mxGraph,你可以构建出用于流程图、网络拓扑图、组织结构图等用途的...

    jgraph官方文档

    它提供了多种预定义的形状和样式,以便快速绘制常见的图表元素。 - **3.2 图形交互**:除了基本的可视化功能外,jgraph还允许用户与图表进行互动,例如拖动节点、添加或删除边等操作。这些交互能力极大地增强了图表...

    JGRAPH开发jar

    **JGRAPH开发jar详解** JGRAPH是一款强大的图形库,主要用于Java平台上的...无论是数据分析的可视化展示,还是流程图的绘制,JGRAPH都能够满足需求,并且提供了足够的定制空间,让开发者能够打造出个性化的图形界面。

Global site tag (gtag.js) - Google Analytics