Java漏斗画画函数代码 java漏斗画画函数代码大全( 四 )


一旦调用了 plot() 函数,就必须依次采用以下步骤:
采用一个 SWT Composite,并将 FigureCanvas 放在它之上 。然后,将一个类似 Panel 的通用容器图放在画布上 。
计算将要绘制的级数的数量,然后填充创建 DirectedGraphs 所需数量的 NodeLists 和 EdgeLists 。
在 Panel 图上绘制 X 坐标轴和 Y 坐标轴 。(参见所附源代码中 \src\GraFix\Figure 目录下的 XRulerBar.java 和 YRulerBar.java 。)
创建和级数一样多的 DirectedGraphs,以便进行绘图 。
在 Panel 图上绘制点和连接线,同时采用步骤 d 中创建的 DirectedGraphs 中的图形数据 。
最后,通过提供 Panel 图来设置画布的内容,其中包括到目前为止您已经准备好的所有的点和连接线 。
在以下代码中:
第 6-11 行代码对应于上述的步骤 a 。
第 14 行 , 即函数 populateNodesAndEdges(),对应于上述的步骤 b 。
第 16 行 , 即函数 drawAxis(),对应于上述的步骤 c 。
第 17 行、第 18 行和第 19 行对应于上述的步骤 d 和步骤 e 。
第 20 行对应于上述的步骤 f 。
清单 4. plot() 函数
1. public void plot(){
2.//if no place to plot, or no data to plot, return.
3.if(null==_parent || null==_seriesData)
4.return;
5.
6.Composite composite = new Composite(_parent, SWT.BORDER);
7.composite.setLayout(new FillLayout());
8.FigureCanvas canvas = new FigureCanvas(composite);
9.
10.Panel contents = new Panel();//A Panel is a general purpose container figure
11.contents.setLayoutManager(new XYLayout());
12.initializeSpan(contents.getClientArea());
13.
14.populateNodesAndEdges();
15.
16.drawAxis(contents);
17.for(int i=0; i_numSeries; i++){
18.drawDotsAndConnections(contents,getDirectedGraph(i)); //
draw pointsconnecting wires
19.}
20.canvas.setContents(contents);
21. }
plot() 调用了两个重要内部函数来帮助绘制图形中的点:populateNodesAndEdges() 和 drawDotsAndConnections() 。在您发现这两个函数到底完成什么功能之前,让我们来看一下 DirectedGraph 。
DirectedGraph 是什么?为了使用 Draw2D 进行绘图 , 事实上您必须先创建一个图形,定义将要绘制的点和线 。一旦创建好这个图形,就可以使用它实际在画布上进行绘图 。您可以将 DirectedGraph 形象化为拥有有限数量的 Node 的一个 2-D 图形,在该图形中,每个 Node 都位于一些 Point 上,相邻的 Node 是通过 Edges 连接在一起的 。
您可以通过以下代码行来了解创建 DirectedGraph 的关键所在 。首先 , 创建一个 Node 列表和一个 Edges 列表 。然后,创建一个新的 DirectedGraph,并通过刚才创建的 NodeList 和 EdgeList 设置其成员(Nodes 和 Edges) 。现在,使用 GraphVisitor 来访问这个 DirectedGraph 。为了简便起见 , 包 org.eclipse.draw2d.internal.graph 中有许多 GraphVisitor 实现 , 这些 GraphVisitor 有一些用来访问图形的特定算法 。
因此,创建 DirectedGraph 的示例代码类似于下面这样:
清单 5. 示例 DirectedGraph
//This is a sample, you will need to add actual Node(s) to this NodeList.
NodeList nodes = new NodeList(); //create a list of nodes.
//This is a sample, you will need to add actual Edge(s) to this EdgeList.
EdgeList edges = new EdgeList(); //create a list of edges.
DirectedGraph graph = new DirectedGraph();
graph.nodes = nodes;
graph.edges = edges;
new BreakCycles().visit(graph);//ask BreakCycles to visit the graph.
//now our "graph" is ready to be used.
现在,已经知道 DirectedGraph 包含许多 Node , 其中 , 每个 Node 都可能包含一些数据,并且还存储了这些数据的 X 坐标和 Y 坐标,以及一个 Edges 的列表,每个 Edge 都知道在自己的两端分别有一个 Node,您可以通过以下技术,使用这些信息来绘图,其中涉及两个部分:部分 A —— 通过以下步骤填充 Node 和 Edge:

推荐阅读