Example #1
0
    def write_preamble(self, fp):
        bbox = [self.__xmin-1, self.__ymin-1, self.__xmax+1, self.__ymax+1]
        fp.write('%!PS-Adobe-2.0 EPSF-1.2\n')
        fp.write('%%Title: ' + self.title + '\n')
        fp.write('%%Creator: ' + self.creator + '\n')
        if self.author:
            fp.write('%%Author: ' + self.author + '\n')
        fp.write('%%CreationDate: ' + self.creation_date + '\n')
        fp.write('%%DocumentFonts: ' + ' '.join(self.__font_ids.keys()) + '\n')
        fp.write('%%Pages: 1\n')

        bbox = theme.adjust_bounding_box(bbox)

        fp.write('%%%%BoundingBox: %d %d %d %d\n' % \
                 (round(xscale(bbox[0])),
                  round(yscale(bbox[1])),
                  round(xscale(bbox[2])),
                  round(yscale(bbox[3]))))
        fp.write('%%EndComments\n')
        if self.aux_comments != '':
            for line in self.aux_comments.split('\n'):
                fp.write('% ' + line + '\n')

        fp.write(preamble_text)
        for name, font_id in self.__font_ids.items():
            fp.write('/%s {/%s findfont SF} def\n' % (font_id, name))
        fp.write('%%EndProlog\n%%Page: 1 1\n')
    def start_gs(self, arg):
        self.bbox = theme.adjust_bounding_box([xscale(self.__xmin),
                                               yscale(self.__ymin),
                                               xscale(self.__xmax),
                                               yscale(self.__ymax)])
        
        gs_path = get_gs_path()
        self.pipe_fp = None
	if self.__output_lines == []:
	    return

        if sys.platform != "win32" and hasattr(os, "popen"):
            # UNIX-like systems
            cmdline = "\"%s\" -q %s -g%dx%d -q >/dev/null 2>&1" % \
            (gs_path, arg,
             self.bbox[2] - self.bbox[0],
             self.bbox[3] - self.bbox[1])
            self.pipe_fp = os.popen(cmdline, "w")
            self.__write_contents(self.pipe_fp)
        else:
            # XXX should use mktemp, but need to support python<=2.2 as well.
            fname = tempfile.mktemp("xxx")
            fp = open(fname, "wb")
            self.__write_contents(fp)
            fp.close()
            cmdline = "\"%s\" -q %s -g%dx%d -q <%s >NUL" % \
            (gs_path, arg,
             self.bbox[2] - self.bbox[0],
             self.bbox[3] - self.bbox[1], fname)
            os.system(cmdline)
            os.unlink(fname)
Example #3
0
    def close(self):
        basecanvas.T.close(self)
        self.grestore()           # matching the gsave in __init__
        if (self.__cur_element.nodeName != 'svg') :
            raise ValueError, "Incomplete document at close!"

        # Don't bother to output an empty document - this can happen
        # when we get close()d immediately by theme reinit
        if (len(self.__svg.childNodes[-1].childNodes) == 0) :
            return

        fp, need_close = self.open_output(self.__out_fname)
        bbox = theme.adjust_bounding_box([self.__xmin, self.__ymin,
                                          self.__xmax, self.__ymax])
        self.__svg.setAttribute('viewBox','%g %g %g %g'
                                % (xscale(bbox[0]),
                                   -yscale(bbox[3]),
                                   xscale(bbox[2])-xscale(bbox[0]),
                                   yscale(bbox[3])-yscale(bbox[1])))
        self.__svg.setAttribute('xmlns','http://www.w3.org/2000/svg')
        self.__svg.setAttribute('xmlns:xlink','http://www.w3.org/1999/xlink')

        self.__doc.writexml(fp,'','  ','\n')
        if need_close:
            fp.close()
Example #4
0
    def start_gs(self, arg):
        self.bbox = theme.adjust_bounding_box([
            xscale(self.__xmin),
            yscale(self.__ymin),
            xscale(self.__xmax),
            yscale(self.__ymax)
        ])

        gs_path = _get_gs_path()
        self.pipe_fp = None
        if self.__output_lines == []:
            return

        if sys.platform != "win32" and hasattr(os, "popen"):
            # UNIX-like systems
            cmdline = "\"%s\" -q %s -g%dx%d -q >/dev/null 2>&1" % \
            (gs_path, arg,
             self.bbox[2] - self.bbox[0],
             self.bbox[3] - self.bbox[1])
            self.pipe_fp = os.popen(cmdline, "w")
            self.__write_contents(self.pipe_fp)
        else:
            # XXX should use mktemp, but need to support python<=2.2 as well.
            fname = tempfile.mktemp("xxx")
            fp = open(fname, "wb")
            self.__write_contents(fp)
            fp.close()
            cmdline = "\"%s\" -q %s -g%dx%d -q <%s >NUL" % \
            (gs_path, arg,
             self.bbox[2] - self.bbox[0],
             self.bbox[3] - self.bbox[1], fname)
            os.system(cmdline)
            os.unlink(fname)
Example #5
0
    def draw(self, ar, can):

        # Draw the line

        clipbox = theme.adjust_bounding_box([ar.loc[0], ar.loc[1], ar.loc[0] + ar.size[0], ar.loc[1] + ar.size[1]])

        can.clip(clipbox[0], clipbox[1], clipbox[2], clipbox[3])
        if self.line_style:
            points = []
            for pair in self.data:
                yval = pychart_util.get_sample_val(pair, self.ycol)
                xval = pair[self.xcol]
                if None not in (xval, yval):
                    points.append((ar.x_pos(xval), ar.y_pos(yval)))
            can.lines(self.line_style, points)
        can.endclip()

        # Draw tick marks and error bars
        can.clip(ar.loc[0] - 10, ar.loc[1] - 10, ar.loc[0] + ar.size[0] + 10, ar.loc[1] + ar.size[1] + 10)
        for pair in self.data:
            x = pair[self.xcol]
            y = pychart_util.get_sample_val(pair, self.ycol)
            if None in (x, y):
                continue

            x_pos = ar.x_pos(x)
            y_pos = ar.y_pos(y)

            if self.error_bar:
                plus = pair[self.y_error_plus_col or self.y_error_minus_col]
                minus = pair[self.y_error_minus_col or self.y_error_plus_col]
                if self.y_qerror_minus_col or self.y_qerror_plus_col:
                    q_plus = pair[self.y_qerror_plus_col or self.y_qerror_minus_col]
                    q_minus = pair[self.y_qerror_minus_col or self.y_qerror_plus_col]
                    if None not in (minus, plus, q_minus, q_plus):
                        self.error_bar.draw(
                            can,
                            (x_pos, y_pos),
                            ar.y_pos(y - minus),
                            ar.y_pos(y + plus),
                            ar.y_pos(y - q_minus),
                            ar.y_pos(y + q_plus),
                        )
                else:
                    if None not in (minus, plus):  # PDS
                        self.error_bar.draw(can, (x_pos, y_pos), ar.y_pos(y - minus), ar.y_pos(y + plus))

            if self.tick_mark:
                self.tick_mark.draw(can, x_pos, y_pos)
            if self.data_label_format:
                can.show(
                    x_pos + self.data_label_offset[0],
                    y_pos + self.data_label_offset[1],
                    "/hC" + pychart_util.apply_format(self.data_label_format, (x, y), 1),
                )

        can.endclip()
Example #6
0
    def draw(self, ar, can):

        # Draw the line

        clipbox = theme.adjust_bounding_box([ar.loc[0], ar.loc[1],
                                             ar.loc[0] + ar.size[0],
                                             ar.loc[1] + ar.size[1]]);
        
        can.clip(clipbox[0],clipbox[1],clipbox[2],clipbox[3])
        if self.line_style:
            points = []
            for pair in self.data:
                yval = pychart_util.get_sample_val(pair, self.ycol)
                xval = pair[self.xcol]
                if None not in (xval, yval):
                    points.append((ar.x_pos(xval), ar.y_pos(yval)))
            can.lines(self.line_style, points)
        can.endclip()
        
        # Draw tick marks and error bars
        can.clip(ar.loc[0] - 10, ar.loc[1] - 10,
                ar.loc[0] + ar.size[0] + 10,
                ar.loc[1] + ar.size[1] + 10)
        for pair in self.data:
            x = pair[self.xcol]
            y = pychart_util.get_sample_val(pair, self.ycol)
            if None in (x, y): continue
            
            x_pos = ar.x_pos(x)
            y_pos = ar.y_pos(y)

            if self.error_bar:
                plus = pair[self.y_error_plus_col or self.y_error_minus_col]
                minus = pair[self.y_error_minus_col or self.y_error_plus_col]
                if self.y_qerror_minus_col or self.y_qerror_plus_col:
                    q_plus = pair[self.y_qerror_plus_col or self.y_qerror_minus_col]
                    q_minus = pair[self.y_qerror_minus_col or self.y_qerror_plus_col]
                    if None not in (minus,plus,q_minus,q_plus):
                        self.error_bar.draw(can, (x_pos, y_pos),
                                            ar.y_pos(y - minus),
                                            ar.y_pos(y + plus),
                                            ar.y_pos(y - q_minus),
                                            ar.y_pos(y + q_plus))
                else:
                    if None not in (minus,plus): #PDS
                        self.error_bar.draw(can, (x_pos, y_pos),
                                            ar.y_pos(y - minus),
                                            ar.y_pos(y + plus))
                        
            if self.tick_mark:
                self.tick_mark.draw(can, x_pos, y_pos)
            if self.data_label_format:
                can.show(x_pos + self.data_label_offset[0],
                         y_pos + self.data_label_offset[1],
                         '/hC' + pychart_util.apply_format(self.data_label_format, (x, y), 1))

        can.endclip()
Example #7
0
    def close(self):
        basecanvas.T.close(self)
	if self.__lines == []:
	    return

        _fp, need_close = self.open_output(self.__out_fname)
        fp = pdf_stream(_fp)

        fp.write("%PDF-1.2\n")

        stream_obj_id = self.__define_stream_obj(fp, " ".join(self.__lines))

        fontstr = ""
        for font_name, font_id in self.__registered_fonts.items():
            obj_id = self.__define_font_obj(fp, font_name, font_id)
            fontstr += "/F%d %d 0 R " % (font_id, obj_id)

        pages_obj_id = self.__define_obj(fp, " <</Type/Pages /Kids [%d 0 R] /Count 1 >>" % (self.__next_obj_id + 1))

        bbox = theme.adjust_bounding_box([xscale(self.__xmin), yscale(self.__ymin),
                                          xscale(self.__xmax), yscale(self.__ymax)])

        page_obj_id = self.__define_obj(fp, """  <</Type/Page
\t/Parent %d 0 R
\t/Contents %d 0 R
\t/MediaBox [%d %d %d %d]
\t/Resources << /ProcSet [/PDF /Text]
\t\t/Font << %s >>
>> >>""" % (pages_obj_id, stream_obj_id,
            bbox[0], bbox[1], bbox[2], bbox[3], fontstr))

        info_str = "/Producer (%s)\n/CreationDate (%s)" % (self.creator, self.creation_date)

        if self.title:
            info_str += "\n/Title (%s)" % (self.title, )
        if self.author:
            info_str += "\n/Author (%s)" % (self.author, )

        info_obj_id = self.__define_obj(fp, """<<%s>>""" % info_str)
        catalog_obj_id = self.__define_obj(fp, """  <</Type/Catalog/Pages %d 0 R>>""" % (pages_obj_id))

        xref_offset = fp.tell()
        fp.write("xref\n0 %d\n" % (len(self.__obj_offsets)+1))
        fp.write("0000000000 65535 f \n")
        id = 1
        while id <= len(self.__obj_offsets):
            fp.write("%010d 00000 n \n" % (self.__obj_offsets[id]))
            id += 1
        fp.write("trailer << /Size %d /Root %d 0 R /Info %d 0 R\n>>\n" % (len(self.__obj_offsets)+1, catalog_obj_id, info_obj_id))
        fp.write("startxref\n%d\n%%%%EOF\n" % xref_offset)

        if need_close:
            _fp.close()
Example #8
0
    def close(self):
        basecanvas.T.close(self)
        self.grestore()  # matching the gsave in __init__
        if (self.__currElt.nodeName != 'svg'):
            raise ValueError, "Incomplete document at close!"

        # Don't bother to output an empty document - this can happen
        # when we get close()d immediately by theme reinit
        if (len(self.__svg.childNodes[-1].childNodes) == 0):
            return

        fp, need_close = self.open_output(self.__out_fname)
        bbox = theme.adjust_bounding_box(
            [self.__xmin, self.__ymin, self.__xmax, self.__ymax])
        self.__svg.setAttribute(
            'viewBox', '%g %g %g %g' %
            (xscale(bbox[0]), -yscale(bbox[3]), xscale(bbox[2]) -
             xscale(bbox[0]), yscale(bbox[3]) - yscale(bbox[1])))
        self.__doc.writexml(fp, '', '  ', '\n')
        if need_close:
            fp.close()
Example #9
0
    def draw(self, can = None):
        "Draw the charts."

        if can == None:
            can = canvas.default_canvas()
            
        assert self.check_integrity()
        
        for plot in self.__plots:
            plot.check_integrity()
            
        self.x_range, self.x_grid_interval = \
                      self.__get_data_range(self.x_range, 'X',
                                            self.x_coord,
                                            self.x_grid_interval)
            
        self.y_range, self.y_grid_interval = \
                      self.__get_data_range(self.y_range, 'Y',
                                            self.y_coord,
                                            self.y_grid_interval)
        
        can.rectangle(self.border_line_style, self.bg_style,
                      self.loc[0], self.loc[1],
                      self.loc[0] + self.size[0], self.loc[1] + self.size[1])

        if not self.x_grid_over_plot:
            self.__draw_x_grid_and_axis(can)

        if not self.y_grid_over_plot:
            self.__draw_y_grid_and_axis(can)

        clipbox = theme.adjust_bounding_box([self.loc[0], self.loc[1],
                                             self.loc[0] + self.size[0],
                                             self.loc[1] + self.size[1]])

        can.clip(clipbox[0], clipbox[1],
                 clipbox[2], clipbox[3])

        for plot in self.__plots:
            plot.draw(self, can)
            
        can.endclip()
            
        if self.x_grid_over_plot:
            self.__draw_x_grid_and_axis(can)
        if self.y_grid_over_plot:
            self.__draw_y_grid_and_axis(can)

        if self.legend == _dummy_legend:
            self.legend = legend.T()
            
        if self.legend:
            legends = []
            for plot in self.__plots:
                entry = plot.get_legend_entry()
                if entry == None:
                    pass
                elif type(entry) != ListType:
                    legends.append(entry)
                else:
                    for e in entry:
                        legends.append(e)
            self.legend.draw(self, legends, can)
Example #10
0
    def draw(self, can=None):
        "Draw the charts."

        if can == None:
            can = canvas.default_canvas()

        assert self.check_integrity()

        for plot in self.__plots:
            plot.check_integrity()

        self.x_range, self.x_grid_interval = \
                      self.__get_data_range(self.x_range, 'X',
                                            self.x_coord,
                                            self.x_grid_interval)

        self.y_range, self.y_grid_interval = \
                      self.__get_data_range(self.y_range, 'Y',
                                            self.y_coord,
                                            self.y_grid_interval)

        can.rectangle(self.border_line_style, self.bg_style, self.loc[0],
                      self.loc[1], self.loc[0] + self.size[0],
                      self.loc[1] + self.size[1])

        if not self.x_grid_over_plot:
            self.__draw_x_grid_and_axis(can)

        if not self.y_grid_over_plot:
            self.__draw_y_grid_and_axis(can)

        clipbox = theme.adjust_bounding_box([
            self.loc[0], self.loc[1], self.loc[0] + self.size[0],
            self.loc[1] + self.size[1]
        ])

        can.clip(clipbox[0], clipbox[1], clipbox[2], clipbox[3])

        for plot in self.__plots:
            plot.draw(self, can)

        can.endclip()

        if self.x_grid_over_plot:
            self.__draw_x_grid_and_axis(can)
        if self.y_grid_over_plot:
            self.__draw_y_grid_and_axis(can)

        if self.legend == _dummy_legend:
            self.legend = legend.T()

        if self.legend:
            legends = []
            for plot in self.__plots:
                entry = plot.get_legend_entry()
                if entry == None:
                    pass
                elif type(entry) != ListType:
                    legends.append(entry)
                else:
                    for e in entry:
                        legends.append(e)
            self.legend.draw(self, legends, can)