Calling Excel Add-In functions from a Macro

From support mail:

Q: I have done quite a bit with your Macro language. However, I have a question about your Excel Add-In. Do you know how I could call it from an Excel Macro? I tried recording a Macro and calling your Add-In, but it didn't show in the code when I was done.

A: Unfortunately Excel doesn't record calls to Add-In functions when recording a macro using keystrokes/button presses. But the fix is simple enough: Go through your normal steps in your macro recording just before you call XYXY or XYYY or whatever. Stop recording. For Excel 2007 or 2010 select the Developer tab, click Macros, select your macro, then click Edit. On Excel 2003 select Tools>Addins>Macros>select macro>Edit. At the end of the macro add a new line with

Call XYYY ' or whatever

And one final step: in the VBA window select Tools>References and check dplotlib.

If you are prompted for a password for the dplotlib entry on this screen, it is "dplot", without the quotation marks.

Edit: Starting with the Add-In distributed with v2.2.8.1 on March 9, 2010 you have access to several Add-In functions that were previously marked "private" and so were inaccessible to your macros. Namely DPlotGetActiveDocument, DPlot_Command, and DPlot_Request. How is this useful? If the default values for scaling, fonts, tick mark intervals, plot size, or whatever set by Add-In functions are not to your liking, you can control just about any aspect of the plot with a call to DPlot_Command, as in the following:

To call DPlot_Command or DPlot_Request you need the index of the currently active document (XYYY and other Add-In commands activate the plots that they create). That's where DPlotGetActiveDocument comes in. The DPlot_Command call in this case sets the tick mark interval to 365 (days, in this case) on the X axis and $1000 on the Y axis. For a rather lengthy list of all available DPlot_Command commands, see the Help file topic.

Moving Average

Adapted from the Forums:

Q: I need to know what mathematical algorithm is used for the moving average calculations.

Normally, a moving average will require that the "end regions" of the graph not be included in the graph of the moving average, since, for example, a centered 11-day moving average can't provide data for the first 5 days and therefore can't be graphed. The same is true of the last 5 days in this example. I believe this problem is inherent in all moving average calculation varieties (though I'm open to being educated).

I bring it up because graphing the end-point regions of a moving average generates quite a bit of controversy in the climate sciences where I work because the end regions normally excluded in a moving average have to be filled in based on some sort of (often unscientific) assumption--even a simple "continue the trend" assumption.

A: For versions 2.2.7.0 and earlier, the output of the Moving Average plugin is an average of the last N points, where N is the interval chosen. This is identical to the Moving Average function of Excel's Analysis ToolPak, with the exception that Excel gives "#N/A" for the first N-1 points, while the DPlot plugin gives the average of i points for i<N.

The new version of the Moving Average plugin includes three forms, two of them new: 1) Prior Moving Average (identical to the only previous option), 2) Central Moving Average (more like what you're talking about, with point i equal to the average of points i-(N-1)/2 through i+(N-1)/2, and 3) Cumulative Moving Average, which is the average of all points up to and including the i'th point.

In addition, the new plugin allows you to include (or not) points at the tails of your data. If Include tails is checked, then as before the i'th point for i<N is the average of the first i points for a Prior Moving Average. Similarly, for a Central Moving Average and i<(N+1)/2 or greater than NP-(N-1)/2 (where NP is the number of points in the input), the output is the average of the sampled input points. If Include tails is not checked, then the output will only include points for which i>N for a Prior Moving Average, or i>(N-1)/2 and i<NP-(N-1)/2 for a Central Moving Average.

The new input dialog for the Moving Average plugin (licensed version only):

Which method is appropriate for your use is dependent on what you are measuring. The Prior Moving Average is a well-established standard but for data that tends to increase or decrease over time, introduces a lag in the output: for a steadily increasing record the output will tend to be equal to the input N/2 measurements ago. The Central Moving Average eliminates this lag but may not be appropriate for your use if the current prediction should not be influenced by future data. The Cumulative Moving Average is mostly useful for data that doesn't increase/decrease monotonically over time and for which early data is significant for the present.

The new plugin is included with the 2.2.7.3 release of DPlot. Licensed users who are not on the update mailing list can get this version by selecting the Check for Updates command on the Help menu.

Example (with Include tails checked in all cases):

Help! DPlot's Help doesn't work!

In case you missed the recent addition to the FAQ and are having trouble with DPlot's Help:

When selecting Help>Contents in DPlot if you see a screen like this:

...then you almost certainly have DPlot installed on a network server and are running DPlot from a client system. Although the program will run fine in this configuration, the Help file will not. Unfortunately this is a Windows security "feature" introduced with a security patch for Windows XP, also affecting all Vista versions. If you are certain that you will not be viewing any "untrusted" CHM Help files then the good folks at EC Software have provided a fix. Otherwise, your best bet is to install DPlot to all client machines rather than running from a server.

Text Placeholders

A frequent support question is along the lines of "How do I automatically display the <min, max, mean, std. deviation, etc.> of a curve in a <title line, axis label, legend entry, note, etc.>"? Text placeholders have been available for a long while, but for whatever reason aren't noticed by many users. You'll probably find all the information you need in the linked Help topic, but this post will walk through an example of how this feature can be used. In the example below we'll work with a bubble plot because it has at least one oddity that isn't the case for most other plot types.

The particulars of this graph aren't really important in this case. The data consists of two "curves"; the bubble magnitudes come from the second curve. The rainfall rate values (bubble magnitudes) vary from ~2.1 - ~11.7. The user in this case wanted to display the rainfall rates in the legend entry for the plot. It would of course be easy enough to type those values for the legend, but we'd like something a bit more automatic that can be stored in a preferences file and/or set in a macro to get the same results for other data. If the legend entry is set to:

$YMIN(2) - $YMAX(2) inches/hr

...then the result looks something like:

The (2) index references the 2nd curve (the bubble magnitudes). If the (2) were not included, then the legend would use the minimum and maximum values of the "Runoff" curve.

This is fine, but displays more significant figures than we'd like. Text placeholders can include an equation, and the equation itself can contain other text placeholders. So we can use the MROUND function:

$=(MROUND($YMIN(2),0.1)) - $=(MROUND($YMAX(2),0.1)) inches/hr

... to round off the rainfall rate values to the nearest 0.1, and get:

Questions? Let us know.

Plot overlays

A frequent question in support e-mails is related to overlaying a plot with another, particularly for different plot types, e.g. overlaying a 3D surface plot with an XY plot. This feature has been a long time in making and still has a few issues (see below), but yes you can do this with the OverlayDocument macro command or, if you're a developer, by sending the OverlayDocument command to DPlot via the DPlot_Command function in dplotlib.dll. This feature is only available in DPlot versions 2.2 and later.

Syntax:
OverlayDocument(docnum|,alpha|maskcolor||) or
OverlayDocument("caption"|,alpha|,maskcolor||)

In the first form, docnum is the 1-based document index of the plot you want to draw on top of the currently active plot. This value corresponds to the indices you'll see for open documents on the Window menu.

In the second form, "caption" is the title of that plot, shown in the document title bar. If this form is used then the caption must be delineated with quotation marks, as shown.

The optional alpha parameter controls the transparency of the overlay. alpha=0 is completely transparent (invisible, in other words); alpha=255 is opaque (no transparency).

The maskcolor argument specifies a color (red+green*256+blue*65536, or hex 0x00bbggrr) in the overlay that will not be drawn if alpha is between 0 and 255. This is useful for making opaque portions of the overlay transparent: in particular an opaque legend box or contour line labels with an opaque background. This feature does not work particularly well with anti-aliased lines and/or smoothed screen fonts, since you can only specify a single color. Antialiased lines and fonts are generally less of a problem with printed plots.

If you aren't familiar with DPlot macros, there really isn't all that much to it, so don't be scared off just yet. To create a macro select the Macro command on the File menu. Type a name for the macro that you'll remember next week in the Macro Name box, then click Edit/Create. We'll start with something simple: if you have 2 open documents, document 1 is active and you want to overlay it with document 2, type:

OverlayDocument(2)

in the macro editor window, click the Save button, then the Run button.

Example:

Document 1 is a surface plot of the Vicksburg National Military Park area created from NASA SRTM data:

Document 2 is an XY plot created from a Garmin GPS device during a bike ride through that same park:

With document 1 active, the macro command OverlayDocument(2) results in:

This feature hasn't been added to the normal menu commands in DPlot simply because there are several quirks to work out yet. Namely: transparency may (or may not) work correctly in printed plots, depending on your printer. Multiple axes in the overlay plot are not transferred to the overlaid plot. Other than the data itself, most plot features for the overlay plot are ignored: title lines, axis labels, number formatting, etc. These are all necessary and won't likely change. But the legend for a surface plot is also omitted for (currently) the same reason, and there is no way to show that legend. At least some of these limitations will likely be worked out in a future release.

This feature is not intended for use with 3D views of 3D/4D data at this time, though DPlot will not prevent you from using it on those plot types.

Offsetting a curve

From support e-mail:

Q: I have created a log-prob curve and have fitted an equation to it that fits very nicely. Now I would like to create a parallel curve through another known data point How would I do this?

A: Nothing to it. You want your new curve to have the same shape (on this scale) as the original curve but offset by some value. So in your case the logarithms of the Y values in the new curve should all be proportional to the logarithms of the Y values in the original curve. First copy your curve fit to the Clipboard using Edit>Copy>Data Values and then paste using Edit>Paste so that you have a copy of that curve and can preserve the original.

It's good in your case that you have a known offset between two points with the same X values. Otherwise this would take a smidgen more work. But basically, you want the same shape curve to pass through the point X=4%, Y=553, where the original passes through X=4%, Y=2697. So the new curve should be something like Y'=10^(log10Y + log10553 - log102697. Remembering a bit of high school algebra, this reduces to Y'=Y*553/2697 (though either form will work). Use the Operate on Y command on the Edit menu on your pasted curve with Y=Y*553/2697 to get:

... which mimics the original curve fit with an offset in Y.

Finding 0.2% offset strain

This one is intended mostly for folks who break things. Specifically, structural engineers who pull on steel (or other ductile metal) bars until they fail. From Wikipedia:

Ductile metals do not have a well defined yield point. The yield strength is typically defined by the "0.2% offset strain". The yield strength at 0.2% offset is determined by finding the intersection of the stress-strain curve with a line parallel to the initial slope of the curve and which intercepts the abscissa at 0.2%.

The question, then, is "Is there an easy way to determine the 0.2% offset strain line, without a calculator handy?" Yes, there is. Shown below is a typical stress-strain curve, in this case for a rolled steel bar.

The first step is to find the slope of the elastic portion of the stress-strain curve. You can do that very easily with the Distance from... command on the Info menu. Ensure that Snap cursor to nearest data point is checked, then drag the mouse from a point near the origin along the initial (hopefully linear) slope:

To avoid retyping (and transcription errors), drag your mouse over the "slope" value, right-click and select "Copy". Now select the Y=f(X) command on the Generate menu. The equation we want is simply (in this case) "21.6569856057*(x-2000)" (2000 microinches/inch = 0.2% strain). For the from X value use 2000 (or any smaller value); for to X use a strain value large enough that the line will be certain to cross the stress-strain curve. You can then zoom in if you overshoot the curve by a lot. The with interval dX entry isn't critical, since you'll be generating a straight line.

Finally, select Find Intersections... on the Info menu. (If this command is disabled you likely need to sort the data points with the Sort command on the Edit menu.) Check the Insert data points... and Add labels at intersections boxes, and you get:

... indicating a yield stress of ~70.5ksi at a strain of ~0.005.

Thanks much to Stephen Rowell for his input on this one.

Ternary plot options for rocks

From support e-mail:

Q: I work with mineralogical data, some of which is used for rock classification. Is it possible to define field classification boundaries, other than those in the soil plot, so that rock classifications can be plotted? I am using the standard IUGS rock classification diagrams for felsic, mafic and ultramafic rocks which are based on three minerals each. I have tried plotting field boundary coordinates in X-Y space and then selecting triangle plot, but the line disappears.

A: In general you can plot any sort of boundaries and/or labels you want with a ternary plot (aka triangle plot, depending on what world you work in), and your "but the line disappears" is easily explained. But more on that below. First, yes you can plot mafic and ultramafic rock classifications starting with version 2.2.6.

If the plot you have on the screen is not a triangle plot, first right-click on the graph and select Triangle Plot (or, equivalently, click on the Options menu, then Linear/Log Scaling, then Triangle Plot). Then right-click again on the plot and select Triangle Plot Options (again, the same option is available on the Options menu). You'll be presented (with version 2.2.6 or later) with this dialog box:

Select the appropriate options and click OK. For an ultramafic scale and blue boundary lines, you should now see something that looks a bit like this (minus your data):

As to the more general question, you can create whatever boundaries and labels you want, save those to a DPlot file, then later open that file and import and/or add that data to your file. Your "but the line disappears" comment is due to a default setting with regard to triangle plots. Take a look at the General command on the Options menu. Most likely the Always force symbols on/lines off for triangle plots option is checked. That option only applies to new data; you can either uncheck that option or later select the Symbol/Line Styles command on the Options menu to use a line style other than "None".

An example is always nice, so here is an example:

You want to show the boundaries for tuffs and ashes from R. Schmid's Descriptive nomenclature and classification of pyroclastic deposits and fragments: Recommendations of the International Union of Geological Sciences Subcommission on the Systematics of Igneous Rocks.. Yes, I know you don't really want that, but follow along anyway. The boundaries between the 3 regions will be created with normal DPlot "curves". The X,Y endpoints are:

(50,0) - (33.333333,33.333333),
(0,50) - (33.333333,33.333333), and
(50,50) - (33.333333,33.333333)
You can easily enter these points using the Edit Data command on the Edit menu.

Labels for the various regions are added with the Add/Edit Note command on the Text menu (or by clicking the Note button on the toolbar). You can either drag the notes to the desired location, or specify their location in terms of data space initially (no dragging required). The anchor point in data space for the notations shown below, with the justification set to Center and Middle in all cases, is:

vitric tuff\nvitric ash: 16.67, 66.67
crystal ash\ncrystal tuff: 16.67, 16.67
lithic tuff\nlithic ash: 66.67, 16.67

Once done your plot should resemble something like this:

Once this graph is created you can add your own data with the Edit Data command on the Edit menu or, if your data exists in a file, with the Append command on the File menu (as opposed to the Open command).

The DPlot file for this picture is available here (right-click and select Save As).

Area enclosed by a polygon

From support e-mail:

Q: Will DPLOT compute the area and or acreage within a closed plotted polygon?

A: Yes. If you use the $AREA shortcut in any text entry (title lines, axis labels, legend, etc.) DPlot will report the area enclosed by your polygon. If the points are arranged in a clockwise direction the value will be positive; if counterclockwise the value will be negative. You can force a positive answer by using the absolute value operation, as in "Area=$=(ABS($AREA))". The initial "$=" tells DPlot an equation follows, which must be enclosed by parentheses.

If you have more than 1 curve then you'll need to use $AREA(n), where n is the 1-based index of the curve.

For most applications (and in particular since you asked about acreage) the value replacing $AREA is perfect. If you're trying to find the area enclosed by what should be a smooth curve, though, you might need to generate more points, if possible. The circle below was generated with X=f(t), Y=g(t) with X=2*cos(t), Y=2*sin(t) with the spacing in t set to 1 degree. The difference between the calculated area and the well-known area of a circle is due to the spacing between points.

In case anybody is wondering, the plain text for that text note is:

R	=2
Area	=$AREA
{\sp}r{\u2}	=$=(PI*4)

with the tab stop set to 5 characters.

For more information see How do I find the area under a curve? in the online manual.

Area between 2 curves

From support e-mail:

Q: 1. Can DPlot find the area between two curves? Area under a curve?

A:No and yes, mostly. You can embed the area under a curve in any text entry (title line, axis label, legend, etc.) with $AREA(n), where n is the index of the curve.

Since you can embed equations in those same entries and also embed text shortcuts like $AREA in equations, you can indirectly report the area between two curves with something like "$=($AREA(1)-$AREA(2))". This will work fine as long as the curves don't cross. If they DO cross then you can get the area with a bit of manipulation: Use Generate>Y=f(X,Y1,Y2,...) with Y=abs(Y1-Y2) (to find the area between curves 1 and 2), then $AREA() on that new curve to find your answer.

Q: 2. Why is DPlot so much cheaper than other programs such as UniPlot or SigmaPlot?

A: Good question :-). Both of those programs will do a few things DPlot will not. Whether that difference is worth 10+ times the price is something users will have to answer for themselves.

-->