File size: 6,653 Bytes
b04beb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from pystata.config import stlib, stconfig, get_encode_str
from IPython.display import SVG, display, Image
from pystata.ipython.ipy_utils import get_ipython_stata_cache_dir
import sfi
import os

pdf_counter = 0

def _get_graphs_info():
    stlib.StataSO_Execute(get_encode_str("qui _gr_list list"), False)
    gnamelist = sfi.Macro.getGlobal("r(_grlist)")

    graphs_info = []
    for gname in gnamelist.split():
        graphs_info.append(gname)

    return graphs_info


class _Pdf_Display_Obj:
    def __init__(self, pdf, width, height):
        self.pdf = pdf
        self.width = width
        self.height = height

    def _repr_html_(self):
        return '<iframe src={0} width={1} height={2} frameBorder="0"></iframe>'.format(self.pdf, self.width, self.height)

    def _repr_latex_(self):
        return r'\includegraphics[width=1.0\textwidth,keepaspectratio]{{{0}}}'.format(self.pdf)

    def __repr__(self):
        return '(file ' + self.pdf + ' written in PDF format)'


def display_stata_graph():
    global pdf_counter

    grformat = stconfig['grformat']
    grwidth = stconfig['grwidth']
    grheight = stconfig['grheight']
    if grformat=="svg":
        if grwidth[0]=='default':
            gwidth_str = ""
        else:
            if grwidth[1]=='cm':
                gwidth_str = str(round(grwidth[0]/2.54, 3)) + 'in'
            else:
                gwidth_str = str(grwidth[0]) + grwidth[1]

        if grheight[0]=='default':
            gheight_str = ""
        else:
            if grheight[1]=='cm':
                gheight_str = str(round(grheight[0]/2.54, 3)) + 'in'
            else:	    
                gheight_str = str(grheight[0]) + grheight[1]
    elif grformat=="png":
        if grwidth[0]=='default':
            gwidth_str = ""
        else:	    
            if grwidth[1]=='px':
                gwidth_str = str(grwidth[0])
            elif grwidth[1]=='cm':
                gwidth_str = str(int(round(grwidth[0]*96/2.54)))
            else:
                gwidth_str = str(int(round(grwidth[0]*96)))

        if grheight[0]=='default':
            gheight_str = ""
        else:
            if grheight[1]=='px':
                gheight_str = str(grheight[0])
            elif grheight[1]=='cm':
                gheight_str = str(int(round(grheight[0]*96/2.54)))
            else:
                gheight_str = str(int(round(grheight[0]*96)))
    else:
        gwidth = -1
        gheight = -1
        if grwidth[0]=='default':
            gwidth_str = ""
        else:	 
            if grwidth[1]=='px':
                gwidth_str = str(grwidth[0]*1.0/96)
                gwidth = grwidth[0]
            elif grwidth[1]=='cm':
                gwidth_str = str(grwidth[0]*1.0/2.54)
                gwidth = grwidth[0]*96/2.54               
            else:
                gwidth_str = str(grwidth[0])
                gwidth = grwidth[0]*96

        if grheight[0]=='default':
            gheight_str = ""
        else:
            if grheight[1]=='px':
                gheight_str = str(grheight[0]*1.0/96)
                gheight = grheight[0]
            elif grheight[1]=='cm':
                gheight_str = str(grheight[0]*1.0/2.54)
                gheight = grheight[0]*96/2.54
            else:
                gheight_str = str(grheight[0])
                gheight = grheight[0]*96

        if gwidth==-1 and gheight==-1:
            gwidth = 528
            gheight = 384
        elif gwidth==-1:
            gwidth = gheight*528.0/384
        elif gheight==-1:
            gheight = gwidth*384.0/528

    graphs_info = _get_graphs_info()
    gdir = get_ipython_stata_cache_dir()
    glen = len(graphs_info)
    if glen > 0:
        for i in range(glen):
            gph_disp = 'qui graph display %s' % graphs_info[i]
            rc = stlib.StataSO_Execute(get_encode_str(gph_disp), False)
            if rc!=0:
                continue

            if grformat=='svg':
                graph_out = os.path.join(gdir, 'temp_graph'+str(i)+'.svg')
                if gwidth_str!="" and gheight_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace width(%s) height(%s) ' % (graph_out, graphs_info[i], gwidth_str, gheight_str)		
                elif gwidth_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace width(%s) ' % (graph_out, graphs_info[i], gwidth_str)		
                elif gheight_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace height(%s) ' % (graph_out, graphs_info[i], gheight_str)
                else:
                    gph_exp = 'qui graph export "%s", name(%s) replace width(528) height(384)' % (graph_out, graphs_info[i])

                stlib.StataSO_Execute(get_encode_str(gph_exp), False)
                display(SVG(filename=graph_out))
            elif grformat=='png':
                graph_out = os.path.join(gdir, 'temp_graph'+str(i)+'.png')
                if gwidth_str!="" and gheight_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace width(%s) height(%s) ' % (graph_out, graphs_info[i], gwidth_str, gheight_str)
                elif gwidth_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace width(%s) ' % (graph_out, graphs_info[i], gwidth_str)
                elif gheight_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace height(%s) ' % (graph_out, graphs_info[i], gheight_str)
                else:
                    gph_exp = 'qui graph export "%s", name(%s) replace ' % (graph_out, graphs_info[i])

                stlib.StataSO_Execute(get_encode_str(gph_exp), False)
                display(Image(filename=graph_out))
            else:
                graph_out = os.path.join(os.getcwd(), str(pdf_counter)+'.pdf')
                if gwidth_str!="" and gheight_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace xsize(%s) ysize(%s) ' % (graph_out, graphs_info[i], gwidth_str, gheight_str)
                elif gwidth_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace xsize(%s) ' % (graph_out, graphs_info[i], gwidth_str)
                elif gheight_str!="":
                    gph_exp = 'qui graph export "%s", name(%s) replace ysize(%s) ' % (graph_out, graphs_info[i], gheight_str)
                else:
                    gph_exp = 'qui graph export "%s", name(%s) replace ' % (graph_out, graphs_info[i])

                stlib.StataSO_Execute(get_encode_str(gph_exp), False)
                display(_Pdf_Display_Obj(os.path.relpath(graph_out), int(gwidth*1.1), int(gheight*1.1)))
                pdf_counter += 1