Graphs

From LabVIEW Wiki
Jump to: navigation, search

Differences Between Charts and graphs

LabVIEW provides the ability to plot charts in 2 dimensions and graphs in both 2 and 3 dimensions. The latter can be either in the form of a surface, toroid or wire graph.

Charts are the easiest of the LabVIEW plots to use. Charts provide the fastest plotting of data as they use an internal FIFO buffer to retain a specified amount of existing data points, with new points simply being appended to the plot. Charts plot all data as a function where all data is plotted against the 'X' axis in an incremental manner. This is the fundamental difference between charts and graphs allows charts to simply append each new point to the plot.

Inputs to charts can be numeric values in which case each new point is plotted point by point, or an input can consist of an array allowing the plot to be constructed array by array. Charts can draw in three different modes: strip, scope or sweep. The strip chart is where each point is plotted on the right hand side of the chart boundary. With the scope chart, once the existing chart display is full, the chart is cleared and the next point is started from the left hand side of the screen. The sweep chart is similar to the scope chart, but does not clear the existing chart and each new point redraws over an existing point if present.

Graphs on the other hand replot each and every point every single time the graph is updated. Although this increases the drawing overhead, graphs provide flexibility in terms of what they will plot. The two types of graph are waveform and XY.

The waveform graph plots data against the X axis using the default values for X0 and delta x. The input in this case is simply a 1D array of data. Alternatively you can plot data against the X axis by defining the X0 point and the delta x. In this case the inputs are numeric values defining X0 and delta x, the final input is still a 1D array. These three values are bundled together to make a cluster input to the graph.

The XY graph is the most flexible of all LabVIEW's plotting functions. This graph may be any shape as required. The input to this graph can be either two individual arrays bundled together representing the x and y values or an array of clusters where each cluster contains an x and y value.

Graphs do have the overhead of requiring that the complete graph be replotted for every new point that is received, and hence may plot slower than charts. A graph plot will eventually become slower and slower (this will only become evident with a very large number of data points being plotted) with subsequent addition of data if new data is simply left to append onto the existing array. To restrict data points simply set up a FIFO buffer and truncate from the front of the plotting buffer.

Plotting multiple Y axes on a single plot

Prior to LabVIEW 6.0i according to the National Instruments' KnowledgeBase Document ID: 1B077H0W, "Currently you cannot have multiple Y-axes on your XY Graph in LabVIEW." For versions earlier than 6.0i they explain how you can create the appearance of two Y-axes by superimposing a slider on a graph. Their example VI looks good but the plotted data doesn't match the scales. (Turn off autoscaling for the Y-axis to see this more easily.) Check it out anyway at www.ni.com to see how the various parts are combined.

An alternative for those with earlier versions earlier of LabVIEW is to use the Componentworks control (also from NI), but with the of release of 6.0i multiple X and Y axes are supported and the LabVIEW upgrade is cheaper than purchasing Componentworks. We'll now discuss alternatives for LabVIEW versions prior to 6.0i.

If you want real performance with multiple axes, you will have to do your own scaling. Make the graph have a scale of 0 to 1 and then normalize your incoming data to that scale. Hide the graph scale and add two sliders (instead of just one), one to each side of the graph. Edit the sliders to only show their scales and set the min and max values of the scale to the desired values.

To size the sliders to the exact size of the graph, temporarily create attribute nodes for the sliders and set them to "housing size >> height". Wire a control to each of these attribute nodes and size the height that way. Another approach is to select the slider and choose Edit >> Edit Control... to open the control editor. In the control editor, choose Windows >> Show Parts Window to display a window in which dimensions of all the parts of the slider can be adjusted digitally.

You can make the color of the slider scale the same as the plot color. Change the frame color for better contrast of the scales.

Autoscaling can be easily accomplished by creating an attribute node for the "Y Range" of the graph, reading that value, multiplying by the scaling factor of each waveform, and feeding it to the "Range" attribute of each Slider.

Another approach is to overlay two separate graphs, with the uppermost having a transparent background *and* plot area. That allows each plot to have a separate y axis scale. As long as the plots are the same size, this gives the appearance of 2 plots on 1 graph with 2 scales. With this approach, you cannot use the zoom function of the palette.

Programmatic Clear

To clear Waveform Charts: 1. create an attribute node for the chart 2. select "History data" as the attribute 3. wire up an empty array to the attribute node.

As a tip, don't do this when the VI starts but instead do it when the VI exits. That way, when the VI loads it will be ready to go and the user won't see any data from the last run at all.

Change plot colours based on a threshold

Copy your data to two arrays, in one array make all values at or above the threshold "NaN" and in the other array make all values at or below the threshold "NaN". Plot both arrays on the same graph indicator and you can set the properties of each data set independently, even though they appear to be the same line.

Time Display Wrong

With internet technologies available for data acquisition it is important to have the capturing machine time stamp its data correctly regardless of it's location in the world. So, for example, if we place a Waveform Chart on our front panel and change the X axis Formatting, Format and Precision to Date & Time, we see that the X0 is set to 1/1/1904 with an offset equivalent in hours equal to your time zone offset from GMT. Thus to get the offset correct you need to correctly set your X0 value.

For example, using a Waveform Graph, you could correct your graphs using the Get Date/Time In Seconds VI for X0. The Get Date/Time In Seconds VI description says that this VI returns the seconds that have elapsed since 12:00am Friday 1st January 1904 Universal Time. Therefore literally 12:00am at 0 degrees longitude. Wiring the output from the Get Date/Time In Seconds VI to X0 will set your graph to show the current time on its X axis.

Does this GMT offset have other implications? If for example you thought of using the Get Date/Time In Seconds VI to test for time since midnight, you could think that dividing the output by 86400 (seconds in a day) and checking the remainder would give the seconds since midnight at your local time. It doesn't. What it effectively returns is the seconds since midnight minus your time zone offset. In other words LabVIEW makes the correct assumption about midnight at your locality as it occurred GMT offsets hours relative to midnight UST.


Example: NI - Displaying Current Time on a LabVIEW Chart

Autoscaling for the maximum data point

Autoscaling affects both ends of the scale as well as the intermediate markers. It isn't possible to "auto" just one marker. You can write to an attribute node to set any of these individually; so you can turn off autoscaling, preview the data being wired to the chart and update the attributes when needed. If you have LV5.1, the picture control examples contain VIs that do autoscaling for graphs.

If your data is always above zero, then another trick would be to make a second plot that is always zero. You can even make the plot be the background color or transparent; so it won't even show up. This doesn't work if the idea is to ignore negative values when scaling, but can always be used to expand the scales.