LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

bar graph first data point problem

I've got a problem with a graph control and plotting with vertical bar style for displaying data histograms.

 

My big goal here is to plot the data points successively as they arrive via the serial port.  These points take a while, a little over a second each.

 

I didn't find a better way of doing this with the limitations of a graph control, so here's how I did it:

1) have a timer collect the data (up to you to use asynch, multi-thread, etc)

2) when we get a new data point, pack into a local array (x and y values)

3) delete previous plots and display all data points up to this index as a new plot

 

So this works well enough, except on the very first data point.  My x values happen to be integers (but they may not be), and my first point should be 1, but the graph shows 20 something. 

 

bad first pointbad first pointall points after first are goodall points after first are good

I've attached the project if you want to observe...

 

0 Kudos
Message 1 of 3
(3,224 Views)

I can confirm your observation: it appears that the graph control has problems drawing one single non-zero bar (I have tried always including an initial (0, 0) point in the array with no effect)

One possible workaround can be to plot an additional zero bar after the first point (or always adding a zero bar after the last point plotted: it has no drawback other than correctly dimensioning the memory 1 sample more than needed)

 

This sample code works as expected:

GetCtrlVal (handle, PANEL_TRACECOLOR, &color);

y[plotCount] = Random (2.0, 100.0);
x[plotCount] = (double)plotCount + 1;
if (!plotCount) {
	y[plotCount + 1] = 0.0;
	x[plotCount + 1] = (double)plotCount + 2;
}
	
DeleteGraphPlot (handle, PANEL_GRAPH, -1, VAL_IMMEDIATE_DRAW);
if (!plotCount)
	PlotXY (handle, PANEL_GRAPH, x, y, plotCount + 2, VAL_DOUBLE, VAL_DOUBLE, VAL_VERTICAL_BAR, VAL_NO_POINT, VAL_SOLID, 1, color);
else
	PlotXY (handle, PANEL_GRAPH, x, y, plotCount + 1, VAL_DOUBLE, VAL_DOUBLE, VAL_VERTICAL_BAR, VAL_NO_POINT, VAL_SOLID, 1, color);
ProcessDrawEvents();

As a side, curious effect, if you try adding two zero point bars before the first real point you end up having thinner bars on the whole graph!



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(3,183 Views)

I looked into this issue, and while the behavior seems to be intentional, the result can certainly can be misleading. I've created CAR #691557 to hopefully improve it.

 

Under normal circumstances, the width of the individual bars in a bar plot is calculated based on the minimum gap between any two adjacent points in the graph (it's roughly 80% of that gap). But when there is only point in the plot, and so there are no adjacent points at all to obtain a gap from, the graph uses the entire width of the plot area as the gap, and therefore the bar width will take up a fairly hefty 80% of the plot area. This isn't necessarily a terrible thing, as a fall back. However, if the graph is not autoscaled, a good chunk of that bar could end up clipped out, which is what happened to you, and so it looks as if the bar is centered around a data point other than the one you plotted. In other words, the plot looks incorrect.

 

You can see better what the graph is doing if you change the minimum value of the x-axis (or if you enable auto-scaling). For example, this is what plotting a single [1,40] data point would look like if the x-axis range were [-50,50]:

 

barplot.png

Message 3 of 3
(3,133 Views)