draw a line on a graph

Draws a vertical or horizontal line across the height or width of a graph at the chosen axis position
function ACW_DrawLine()
    string str_Axis="", str_AxisList="", str_layers="ProgBack;UserBack;ProgAxes;UserAxes;ProgFront;UserFront"
    variable pos, var_layer=2
   
    if (!strlen(WinList("*", ";","WIN:1")))
        doalert 0, "No graph window"
        return 0
    endif
   
    getaxis /Q left
    if (V_flag<1)
        str_AxisList+="left;"
    endif
    getaxis /Q bottom
    if (V_flag<1)
        str_AxisList+="bottom;"
    endif
    getaxis /Q right
    if (V_flag<1)
        str_AxisList+="right;"
    endif
    getaxis /Q top
    if (V_flag<1)
        str_AxisList+="top;"
    endif
   
    prompt str_Axis, "Choose an axis: ", popup, str_AxisList
    prompt pos, "Position: "
    prompt var_layer, "Choose drawing layer: ", popup, str_layers
    DoPrompt "Draw a line on the graph", str_Axis, pos, var_layer  

    if (V_Flag)
        return 0            // user canceled
    endif
   
    SetDrawLayer $stringfromlist(var_layer, str_layers)
    strswitch(str_Axis)
        case "left":
            setdrawenv xcoord=prel, ycoord=$str_Axis
            drawline 0,pos,1, pos
            break
        case "right":
            setdrawenv xcoord=prel, ycoord=$str_Axis
            drawline 0,pos,1, pos
            break
        default:
            setdrawenv ycoord=prel, xcoord=$str_Axis
            drawline pos,0,pos,1
    endswitch
   
   
end
Tony's version inspired me to write one which works on graphs with axes other than top/left/bottom/right.
And then I found that I had to add support for perpendicular axes that didn't completely span the plot area. :-)

#include <Readback ModifyStr>
#include <Axis Utilities>

Menu "Graph", hideable
    "Draw Axis Line...",/Q, JP_DrawAxisLine("")
End

Function JP_DrawAxisLine(graphName)
    String graphName
 
    if( strlen(graphName) == 0 )
        graphName= WinName(0,1)
    endif
    if( strlen(graphName) == 0 )
        DoAlert 0, "No graph window"
        return 0
    endif

    String str_AxisList= AxisList(graphName)
    if( ItemsInList(str_AxisList) < 2 )
        doalert 0, "no axes!"
        return 0
    endif
    String str_Axis= StringFromList(0,str_AxisList)
    Prompt str_Axis, "Line Intersecting: ", popup, str_AxisList
 
    String str_rangeList="entire plot area;over extent of other axis;"
    String str_range="entire plot area"
    Prompt str_range, "Extent of line: ", popup, str_rangeList
 
    Variable pos
    Prompt pos, "At this value: "
 
    String str_layers="ProgBack;UserBack;ProgAxes;UserAxes;ProgFront;UserFront;"
    String str_layer="ProgAxes"
    Prompt str_layer, "On this drawing layer: ", popup, str_layers
 
    DoPrompt "Draw Line Across Plot Area", str_Axis, str_range, pos, str_layer 
    if( V_Flag )
        return 0            // user cancelled
    endif
 
    String info=AxisInfo(graphName,str_Axis)
    String axisType= StringByKey("AXTYPE",info)
    Variable isHorizontal= (CmpStr(axisType,"bottom") == 0) || (CmpStr(axisType,"top") == 0)
 
    Variable pRelStart=0, pRelEnd=1
    String otherAxesList, otherAxis, otherInfo
    strswitch(str_range)
        case "over extent of other axis":
            // get a list of perpendicular axes. If there are more than 1, ask the user which one to use.
            otherAxesList= HVAxisList(graphName,!isHorizontal)
            if( ItemsInList(otherAxesList) == 1 )
                otherAxis= StringFromList(0,otherAxesList)
            else
                // ask the user which one he wants
                Prompt otherAxis, "Same extent as this axis:", popup, otherAxesList
                DoPrompt "Choose Perpendicular Axis for Extent", otherAxis 
                if( V_Flag )
                    return 0            // user cancelled
                endif
            endif
            otherInfo=AxisInfo(graphName,otherAxis)
            // parse axisEnab(x)={0.25,0.75}
            pRelStart= GetNumFromModifyStr(otherInfo,"axisEnab","{",0)
            pRelEnd= GetNumFromModifyStr(otherInfo,"axisEnab","{",1)
        break
    endswitch
 
    SetDrawLayer $str_layer
    if( isHorizontal )
        SetDrawEnv/W=$graphName xcoord=$str_Axis, ycoord=prel
        DrawLine/W=$graphName pos,pRelStart,pos,pRelEnd
    else
        SetDrawEnv/W=$graphName xcoord=prel, ycoord=$str_Axis
        DrawLine/W=$graphName pRelStart,pos,pRelEnd, pos
    endif
    return 1    // success
End

--Jim Prouty
Software Engineer, WaveMetrics, Inc.

Forum

Support

Gallery

Igor Pro 8

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More