Tutorials > Chart Tutorials > Vector Fields and Directed Graphs

Vector Fields and Directed Graphs

This style of plot can be used in two very different ways, to show either directed graphs, or to plot vector fields as a scatter diagram. The examples illustrate both of these.

Plotting a Vector Field

This style of chart is common in oceanography or weather-forecasting. The first (xy) pair is used to plot the location of the reading, and the offset to the second pair represents the direction and value of some measured quantity like windspeed or current flow.


sp.Heading = "Map of Current Flow";
sp.VectorStyle = VectorStyles.ArrowLines;
sp.DrawVectors(VectorField);

Typically the lines have arrowheads to indicate the flow direction. This chart can also be used for any kind of vector field, for example to show the slope at any point on a landscape.

Labelling the Vectors

This example adds value-tags to each vector (giving the length of the line) and shows the ‘root’ of each line as well as the length and direction.


sp.Heading = "Wind Direction and strength";
sp.VectorStyle = VectorStyles.Rooted|VectorStyles.ValueTags|VectorStyles.ArrowLines;
sp.ValueTagFormat = "##0";
sp.SetMarkers(Marker.Bullet);

sp.DrawVectors(VectorField);

In most cases, you would set your own value data here as the scaling of the lines is generally quite arbitrary.

Simple Connected Graph

This chart can easily be used to draw simple diagrams, typically representing connected tasks, such as an assembly process.


y1 = new int[] {1,2,2,1,2,2,3,2,3};
x2 = new int[] {2,2,2,3,3,3,3,4,4};
y2 = new int[] {1,2,3,2,2,3,3,2,2};

sp.Reset(width,height);

sp.SetPenWidths(1.4);
sp.SetMarkerScales(2);
sp.SetMarkers(Marker.Bullet);
sp.VectorStyle = VectorStyles.Terminated|VectorStyles.Rooted|VectorStyles.NoAxes;

sp.DrawVectors(new int[][]{x1,y1,x2,y2});

Alternatively, a ScatterPlot could be used to draw the nodes, and the Vector chart simply to add the map of connections.

Drawing a PERT Chart

The final example assumes that the arrows represent activities of some kind, and the diagram as a whole illustrates a project plan where activities must be carried out in the correct order.


y = new int[] {1,1,2,2,2,3,3,4};

// Links as 4 arrays
x1 = new int[] {1,1,1,2,2,2,2,3,3,2};
y1 = new int[] {1,2,2,1,2,2,3,2,3,1};
x2 = new int[] {2,2,2,3,3,3,3,4,4,1};
y2 = new int[] {1,2,3,2,2,3,3,2,2,1};

sp.Reset(width,height);

sp.SetMargins(60,12,12,12);
sp.Heading = "PERT Chart with Labelled Activities";

sp.SetColors(new Color[]{Color.Navy,Color.Maroon});

// Draw the markers
sp.SetMarkers(Marker.Node);
sp.SetMarkerScales(2);
sp.SetPenWidths(2);

sp.ScatterPlotStyle = ScatterPlotStyles.NoAxes;
sp.DrawScatterPlot(x,y);

// Fill in the labelled lines
sp.VectorStyle = VectorStyles.ArrowLines|VectorStyles.Dissected|VectorStyles.ValueTags;
sp.SetValueTags(new string[]{"One","Two","Three",
         "Four has\n<b>two</b> lines","Five","Six","Seven\nis here",
         "Eight","Nine","Home"});
sp.SetArrowStyle(8,24);
sp.SetValueFont("Arial",9,Color.DarkGreen);

sp.DrawVectors(new int[][]{x1,y1,x2,y2});

Here, the activities have been labelled, and the nodes drawn with a separate scatterplot. Notice the use of the ‘Dissected’ style to prevent the arrows from colliding where several lines converge on the same node.

Summary

This chart has several good uses on its own, but is very likely to be combined with standard LineGraphs and ScatterPlots, typically to label bunches of points using indicator lines and notes, or to connect arbitrary points on a timeseries (for example Buy and Sell points in a historical price plot).


Send comments on this topic
© Dyalog Ltd 2013