Zoom Plot

 

Version Info

Minimum Origin Version Required: Origin 8.0

Remark

This example shows how to set script with event mode on one retangle graph object. The Script and Event Mode are Script, Run After and Ctrl-TAB for TAB options in Programming Control... dialog. Right click on graph object and choose Programming Control... to open this dialog.

This example just simulate Origin Zoom graph. Move rectangle on the top layer to zoom the selected area and display in the bottom layer.

Before Running

To generate ZoomSource template for the top layer:

  1. Click New Graph button to new a graph.
  2. Double-click on X axis to open dialog, choose Grid Lines tab, choose Major Grids checkbox and Opposite checkbox, click OK button to close dialog.
  3. The same step as above on Y axis to show grid line.
  4. Delete X Axis Title, Y Axis Title and Legend.
  5. Right-click graph window title, choose Save Template As..., type "ZoomSource" for Template Name and click OK button twice time.

To genereate ZoomDest template for the bottom layer:

  1. Click New Graph button to new a graph.
  2. Double-click on graph layer to open Plot Details dialog, in Background tab, change Color to Cyan, click OK button.
  3. Delete X Axis Title, Y Axis Title and Legend.
  4. Right-click graph window title, choose Save Template As..., type "ZoomDest" for Template Name and click OK button twice time.

Example Codes

void zoom_plot()
{
        GraphPage       gp;
        gp.Create("ZoomSource");
        int nZoomLayer = gp.AddLayer("Layer2", 0, "ZoomDest"); // return the plot index of newly created layer
        graph_arrange_layers(gp, 2, 1); // 2 rows 1 column
        
        Worksheet       wks;
        wks.Create("Origin", CREATE_HIDDEN);
        
        string          strFile = GetAppPath(true) + "Samples\\Spectroscopy\\HiddenPeaks.dat";
        ASCIMP          ai;
        if( AscImpReadFileStruct(strFile, &ai) != 0 )
        {
                out_str("Fail to read ASCII file struct.");
                return;
        }
        
        if( 0 != wks.ImportASCII(strFile, ai) )
        {
                out_str("Fail to import data");
                return;
        }
        
        // plot source data to the first layer and second layer
        DataRange drIn;
        drIn.Add(wks, 0, "X");
        drIn.Add(wks, 1, "Y");
        
        GraphLayer glSource = gp.Layers(0);
        glSource.AddPlot(drIn, IDM_PLOT_LINE);
        
        DataRange dr2;
        dr2.Add(wks, 0, "X");
        dr2.Add(wks, 1, "Y");        
        GraphLayer glZoom = gp.Layers(nZoomLayer);
        glZoom.AddPlot(dr2, IDM_PLOT_LINE);      
        
        // add rectangle to first layer
        GraphObject goRect = glSource.CreateGraphObject(GROT_RECT);
        goRect.Attach = 2;
        
        Tree tr;
        tr.Root.Border.Color.nVal = SYSCOLOR_BLUE;
        tr.Root.Border.Width.dVal = 0.2;
        tr.Root.Fill.Color.nVal = SYSCOLOR_CYAN;
        tr.Root.BehindData.nVal = 1;
        tr.Root.Dimension.Left.dVal = glSource.X.From + (glSource.X.To - glSource.X.From) * 0.2;
        tr.Root.Dimension.Top.dVal = glSource.Y.To - (glSource.Y.To - glSource.Y.From) * 0.4;
        tr.Root.Dimension.Width.dVal = (glSource.X.To - glSource.X.From) * 0.2;
        tr.Root.Dimension.Height.dVal = (glSource.Y.To - glSource.Y.From) * 0.2;  
        tr.Root.Script.strVal = "string name$=this.name$; ZoomByRect(name$)";
        tr.Root.Event.nVal = GRCT_MOVE;
        if( 0 == goRect.UpdateThemeIDs(tr.Root) )
        {
                goRect.ApplyFormat(tr, true, true);
        }
        ZoomByRect(goRect.GetName()); // call this once in first time to init zoom layer according to the init postion of rectangle
}

// This function always assume the first layer is source data layer, the second layer is zoom layer.
void ZoomByRect(string strRectName)
{     
        GraphPage gp = Project.GraphPages(-1); // to get active graph page          
        GraphLayer glSource = gp.Layers(0);    
        GraphLayer glZoom = gp.Layers(1);
        
        GraphObject goRect = glSource.GraphObjects(strRectName);
        if( !goRect )
                return;
        
        double dLeft = goRect.X - goRect.DX / 2.0;
        double dRight = goRect.X + goRect.DX / 2.0;
        double dTop = goRect.Y + goRect.DY / 2.0;
        double dBottom = goRect.Y - goRect.DY / 2.0;
        printf("%.2f, %.2f, %.2f, %.2f\n", dLeft, dRight, dTop, dBottom);  

        glZoom.X.From = dLeft;
        glZoom.X.To = dRight;
        glZoom.Y.From = dBottom;
        glZoom.Y.To = dTop;      
}