当您想绘制一架 2-d 飞机上的点时,必须找出每个点的 x 坐标和 y 坐标。绘图的奇妙之处在于能够将某一个给定数据值从一个范围按比例缩放到另一个范围中,也就是说,如果给定一组值,如 {10,20,30},那么您应该能够确定 2-d 飞机上具体哪些点(x 坐标和 y 坐标)表示的是 10、20 和 30 这些数据值。
绘制总是在按照某一个限定缩放比例进行的。换句话说,在同一限定区域内,可以绘制任意数量的点。因为该区域是固定的,所以您总是可以找到 x 坐标轴的跨度(长度)和 y 坐标轴的跨度(高度)。x 坐标轴和 y 坐标轴的跨度只是等式的一部分。另一部分是找出数据值的范围,并根据每个数据值在新范围内的等效值来计算这些值的坐标。
计算 x 坐标和 y 坐标
x 坐标:x 坐标是某一个点距离原点的水平距离。计算元素的数量,然后将 x 坐标轴的跨度分成 n 个区段,其中,n 是给定集合中的元素的数量,通过这种方式,可以计算某一集合中的所有点的横向坐标。用这种分割方法可以获得每个区段的长度。集合中的第一个点位于等于区段长度的第一段距离内。后续的每个点则位于区段长度加上原点到前一个点的距离的那一段距离内。
例如,给出一个集合 {10,20,30,40},您立刻就可以知道要绘制 4 个点,因为集合中包含 4 个元素。所以,应该将 x 坐标轴的跨度分成 4 个相等的区段,每个区段的长度 = 跨度/4。因此,如果 x 坐标轴的跨度是 800,那么区段的长度将是 800/4,即 200。第一个元素(10)的 x 坐标将是 200,第二个元素(20)的 x 坐标将是 400,依此类推。 清单 2. 计算 x 坐标
private int[] getxcoordinates(arraylist seriesdata){ int xspan = (int)grafixconstants.xspan; int longestseries = utilities.getlongestseries(seriesdata); int numsegments = ((double[])seriesdata.get(longestseries)).length; int sectionwidth = (int)xspan / numsegments; //want to divide span of xaxis int xpositions[] = new int[numsegments]; // will contain x-coordinate of all dots. for(int i=0; i<numsegments; i++){ xpositions[i]= (i+1)*sectionwidth;//dots spaced at distance of sectionwidth } return xpositions; }
y 坐标:y 坐标是某一个点距离原点的纵向距离。计算 y 坐标要将某一个值按比例从一个范围缩放到另一个范围。例如,给出相同的集合 {10,20,30,40},您可以看出,数据的范围是 0 到 40,新的范围就是 y 坐标轴的跨度(高度)。假设 y 坐标轴的高度为 400,那么第一个元素(10)的高度将是100,第二个元素的高度将是 200,依此类推。
public class maingrafixview extends viewpart{ public void createpartcontrol(composite parent) { //create or get data in an arraylist arraylist seriesdata = datagenerator(); //instantiate a plotter, and provide data to it. directedgraphxyplotter dgxygraph = new directedgraphxyplotter(parent); dgxygraph.setdata(seriesdata); dgxygraph.plot(); //ask it to plot } public void setfocus() { } }
//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,您可以通过以下技术,使用这些信息来绘图,其中涉及两个部分:
private void populatenodesandedges(){ _seriesscaledvalues = new arraylist(getscaledvalues(_seriesdata)); _nodelists = new arraylist(); _edgelists = new arraylist(); for(int i=0; i<_numseries; i++){ _nodelists.add(new nodelist());// one nodelist per series. _edgelists.add(new edgelist());// one edgelist per series. } //populate all nodelists with the nodes. for(int i=0; i<_numseries; i++){//for each series double data[] = (double[])_seriesdata.get(i);//get the series int xcoords[] = getxcoordinates(_seriesdata); int ycoords[] = getycoordinates(i, data); //each nodelist has as many nodes as points in a series for(int j=0; j<data.length; j++){ double doublevalue = new double(data[j]); node node = new node(doublevalue); node.x = xcoords[j]; node.y = ycoords[j]; ((nodelist)_nodelists.get(i)).add(node); } } //populate all edgelists with the edges. for(int i=0; i<_numseries; i++){ nodelist nodes = (nodelist)_nodelists.get(i); for(int j=0; j<nodes.size()-1; j++){ node leftnode = nodes.getnode(j); node rightnode = nodes.getnode(j+1); edge edge = new edge(leftnode,rightnode); edge.start = new point(leftnode.x, leftnode.y); edge.end = new point(rightnode.x, rightnode.y); ((edgelist)_edgelists.get(i)).add(edge); } } int breakpoint = 0; }
private void drawdotsandconnections(ifigure contents, directedgraph graph){ for (int i = 0; i < graph.nodes.size(); i++) { node node = graph.nodes.getnode(i); drawnode(contents, node); } for (int i = 0; i < graph.edges.size(); i++) { edge edge = graph.edges.getedge(i); drawedge(contents, edge); } } private void drawnode(ifigure contents, node node){ dot dotfigure = new dot(); node.data = dotfigure; int xpos = node.x; int ypos = node.y; contents.add(dotfigure); contents.setconstraint(dotfigure, new rectangle(xpos,ypos,-1,-1)); } private void drawedge(ifigure contents, edge edge){ polylineconnection wirefigure = new polylineconnection(); //edge.source is the node to the left of this edge ellipseanchor sourceanchor = new ellipseanchor((dot)edge.source.data); //edge.target is the node to the right of this edge ellipseanchor targetanchor = new ellipseanchor((dot)edge.target.data); wirefigure.setsourceanchor(sourceanchor); wirefigure.settargetanchor(targetanchor); contents.add(wirefigure); }