我需要哪种概念和技术来实现这种图形

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我需要哪种概念和技术来实现这种图形相关的知识,希望对你有一定的参考价值。

我想动态观察某些功能块如何相互影响。我很难定义所需的图形,以及最适合开始使用的d3布局功能(如果有)。我什至不确定我的示例是否在图形的定义之内。

Rough concept of what I try to achieve

总体思路是将一组功能及其输入和输出可视化。它以一组输入开始,以一组输出结束。在这两者之间,有几个函数,每个函数接受输入并生成1个或多个输出。每个输出可以用作一个或多个功能的输入。因此,每条边/线代表一个输出,该输出正被传递到函数以用作输入(并且是单向的)]

我不是在代码中寻找答案,而是在深入了解我需要从哪些概念入手。该图像可能存在问题,无法从字面上实现,但我无法确切指出。

答案

对不起,我英语不好,我说西班牙语。我也正在寻找类似的东西,研究我已经做到了这一点,但我很想念:

  • 将节点的圆转换为矩形
  • 向每个节点和每个链接添加文本标签
  • 一旦开始一切都组织好,它们将保留在固定位置
  • 捕获每个节点和/或链接上的点击以触发另一个应用程序

我在下面留下代码,尤其要注意nodes_data和links_data的定义,以及定义大小和颜色的函数:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: black ;
  stroke-width: 0px;
}

</style>
<svg width="1000" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

//create somewhere to put the force directed graph
var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

//var radius = 15; 

var nodes_data =  [
    {"name": "PO1", "entity": "PO"},
    {"name": "PO2", "entity": "PO"},
    {"name": "PO3", "entity": "PO"},
    {"name": "PO4", "entity": "PO"},
    {"name": "PO5", "entity": "PO"},
    {"name": "PO6", "entity": "PO"},
    {"name": "PO7", "entity": "PO"},
    {"name": "PY1", "entity": "PY"},
    {"name": "PY2", "entity": "PY"},
    {"name": "L1", "entity": "X"},
    {"name": "L2", "entity": "X"},
    {"name": "L3", "entity": "X"},
    {"name": "L4", "entity": "X"},
    {"name": "TK1", "entity": "TK"},
    {"name": "TK2", "entity": "TK"},
    {"name": "PIL1", "entity": "TK"},
    {"name": "BBA1", "entity": "BA"},
    {"name": "BBA2", "entity": "BA"},
    {"name": "ULAC1", "entity": "UL"},
    {"name": "VtaYPF", "entity": "VTA"}
    ]

//Sample links data 
//type: A for Ally, E for Enemy
var links_data = [
    {"source": "PO1", "target": "L1", "type":"A" },
    {"source": "PO2", "target": "L1", "type":"A" },
    {"source": "PO3", "target": "L1", "type":"A"},
    {"source": "PO4", "target": "L2", "type":"A"},
    {"source": "PO5", "target": "L2", "type":"A"},
    {"source": "PO6", "target": "L3", "type":"A"},
    {"source": "PO7", "target": "L3", "type":"A"},
    {"source": "L1", "target": "L3", "type":"A"},
    {"source": "L2", "target": "L3", "type":"A"},
    {"source": "L3", "target": "TK1", "type":"A"},
    {"source": "L3", "target": "TK2", "type":"A"},
    {"source": "TK1", "target": "L4", "type":"A"},
    {"source": "TK2", "target": "L4", "type":"A"},
    {"source": "L4", "target": "PIL1", "type":"A"},
    {"source": "PIL1", "target": "ULAC1", "type":"A"},
    {"source": "PIL1", "target": "BBA1", "type":"A"},
    {"source": "PIL1", "target": "BBA2", "type":"A"},
    {"source": "ULAC1", "target": "VtaYPF", "type":"A"},
    {"source": "BBA1", "target": "PY1", "type":"A"},
    {"source": "BBA2", "target": "PY2", "type":"A"}

]


//set up the simulation and add forces  
var simulation = d3.forceSimulation()
                    .nodes(nodes_data);

var link_force =  d3.forceLink(links_data)
                        .id(function(d) { return d.name; });            

var charge_force = d3.forceManyBody()
    .strength(-100); 

var center_force = d3.forceCenter(width / 2, height / 2);  

simulation
    .force("charge_force", charge_force)
    .force("center_force", center_force)
    .force("links",link_force)
 ;


//add tick instructions: 
simulation.on("tick", tickActions );

//add encompassing group for the zoom 
var g = svg.append("g")
    .attr("class", "everything");

//draw lines for the links 
var link = g.append("g")
      .attr("class", "links")
    .selectAll("line")
    .data(links_data)
    .enter().append("line")
      .attr("stroke-width", 2)
      .style("stroke", linkColour);        

//draw circles for the nodes 
var node = g.append("g")
        .attr("class", "nodes") 
        .selectAll("circle")
        .data(nodes_data)
        .enter()
        .append("circle")
        .attr("r", radius)
        .attr("fill", circleColour);


//add drag capabilities  
var drag_handler = d3.drag()
    .on("start", drag_start)
    .on("drag", drag_drag)
    .on("end", drag_end);   

drag_handler(node);


//add zoom capabilities 
var zoom_handler = d3.zoom()
    .on("zoom", zoom_actions);

zoom_handler(svg);     

/** Functions **/

//Function to choose what color circle we have
//Let's return blue for males and red for females
function circleColour(d){
   var my_color
   switch (d.entity) {
    case "PO": my_color = "black";  break;
    case "PY": my_color = "cyan";   break;
    case "TK": my_color = "blue";   break;
    case "UL": my_color = "green";  break;
    case "VTA": my_color = "green"; break;
    case "BA": my_color = "cyan";   break;
    case "X": my_color = "grey";    break;
    }
   return my_color
}

//Function to choose the line colour and thickness 
//If the link type is "A" return green 
//If the link type is "E" return red 
function linkColour(d){
    if(d.type == "A"){
        return "green";
    } else {
        return "red";
    }
}

//Drag functions 
//d is the node 
function drag_start(d) {
 if (!d3.event.active) simulation.alphaTarget(0.3).restart();
    d.fx = d.x;
    d.fy = d.y;
}

//make sure you can't drag the circle outside the box
function drag_drag(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function drag_end(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}

//Zoom functions 
function zoom_actions(){
    g.attr("transform", d3.event.transform)
}

function tickActions() {
    //update circle positions each tick of the simulation 
       node
        .attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });

    //update link positions 
    link
        .attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });
} 

function radius(d){
  var my_rad
   switch (d.entity) {
    case "PO": my_rad = 10; break;
    case "PY": my_rad = 10; break;
    case "TK": my_rad = 20; break;
    case "UL": my_rad = 20; break;
    case "VTA": my_rad = 15;    break;
    case "BA": my_rad = 10; break;
    case "X": my_rad = 3;   break;
    }
   return my_rad
}
</script>

The above code generate this live image

以上是关于我需要哪种概念和技术来实现这种图形的主要内容,如果未能解决你的问题,请参考以下文章

用zrender实现工作流图形化设计(附范例代码)

代码片段如何使用CSS来快速定义多彩光标

大数据可视化实现途径

java解析xml的几种方式哪种最好?

iOS图形学(二):bitmap位图详解

云原生周刊:一文读懂 Pod 网络 | 2023.4.10