1.2.1 Plotting
Plotting Matrix as Contour Plot
Excel VBA
In this example, we take a range from excel sheet1, put the data into an Origin MatrixObject and create a new contour plot.
Private Sub CommandButton1_Click()
Const nrows = 5
Const ncols = 4
Dim app As Origin.Application
Dim mobj As Origin.MatrixObject
Dim matrixLayer As Origin.matrixSheet
Dim excelRange As Excel.range
Set app = New Origin.ApplicationSI
'Add a new MatrixPage which will have by default a MtrixSteet with one MatrixObject
Set matrixLayer = app.MatrixPages.Add("Origin").Layers(0)
Set mobj = matrixLayer.MatrixObjects(0)
matrixLayer.Cols = ncols
matrixLayer.Rows = nrows
'Copy a range to the new Matrix
Set excelRange = Worksheets("Sheet1").range("A1:D5")
mobj.SetData (excelRange.Value)
Dim dp As Origin.DataPlot
Dim dr As Origin.DataRange
Dim glay As Origin.GraphLayer
'plot the data
Set dr = matrixLayer.NewDataRange(0)
Set glay = app.GraphPages.Add("Contour").Layers(0) 'Add a graph page with the template "Contour"
Set dp = glay.DataPlots.Add(dr, IDM_PLOT_CONTOUR) 'Plot the range as a contour plot
End Sub
C#
In the program, we generate a datarange from matrixobject and create a new contour plot on a new graphlayer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Origin;
namespace COMExampleCSharp
{
class Program
{
public void check()
{
Origin.IOApplication pOrigin;
pOrigin = new Origin.ApplicationCOMSIClass(); //connect to COM-exclusive Origin instance
pOrigin.Visible = Origin.MAINWND_VISIBLE.MAINWND_SHOW; //make it visible
Origin.MatrixSheet ms;
Origin.MatrixObject mo;
Origin.GraphLayer gl;
ms = (Origin.MatrixSheet)pOrigin.MatrixPages.Add("Origin", null).Layers.Add(null, null, null, null, null);
mo = ms.MatrixObjects[0];
int nCols = ms.Cols;
int nRows = ms.Rows;
double [,] data = new double[nRows, nCols];
for ( int ii = 0; ii < nRows; ii++)
{
for( int jj = 0; jj < nCols; jj++)
data[ii,jj] = Math.Sin(ii/3.14) + Math.Cos(jj/2.03);
}
mo.SetData(data, 0, 0);
Origin.DataRange dr;
dr = ms.NewDataRange(0); //create datarange from the first matrixobject
gl = (Origin.GraphLayer)pOrigin.GraphPages.Add("Contour", null).Layers.Add("", null, null, null, null);
gl.DataPlots.Add(dr, Origin.PLOTTYPES.IDM_PLOT_CONTOUR);
gl.Activate(); //activate it for view
System.Console.ReadKey(); //press key will disconnect from Origin
System.Runtime.InteropServices.Marshal.ReleaseComObject(pOrigin);
return;
}
static void Main(string[] args)
{
Program p = new Program();
p.check();
}
}
}
Plotting Worksheet XYZ columns as a Contour Plot
Excel VBA
In this example, you will fill A,B,C columns in the Excel sheet with XYZ data and we will transfer those data to an Origin worksheet and plot a triangular contour graph.
Private Sub CommandButton1_Click()
Dim app As Origin.Application
Dim wks As Origin.Worksheet
Set app = New Origin.ApplicationSI
'Add a WorksheetPage, which will have one Worksheet by default
Set wks = app.WorksheetPages.Add().Layers(0)
Dim bRet As Boolean
Dim ii As Integer
Dim exlRange As Excel.range
'Copy col A,B,C from excel to Origin Wks as XYZ columns
'col A1 in Excel -> col(1) in Origin
For ii = 1 To 3
Set exlRange = Worksheets("Sheet1").Columns(ii)
bRet = wks.SetData(exlRange.Value, 0, ii - 1)
Next
'set column designations in Origin first before we can plot them
wks.Columns(0).Type = COLTYPE_X
wks.Columns(1).Type = COLTYPE_Y
wks.Columns(2).Type = COLTYPE_Z
Dim glay As Origin.GraphLayer
Dim dr As Origin.DataRange
Dim dp As Origin.DataPlot
'create a new graph page using TriContour template
Set glay = wks.Application.GraphPages.Add("TriContour").Layers(0)
Set dr = wks.NewDataRange(0, 0, -1, 2) 'create range to be plotted, all rows,A,B,C
Set dp = glay.DataPlots.Add(dr, IDM_PLOT_XYZ_CONTOUR)
End Sub
|