52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
# -*- encoding: utf-8 -*-
|
|
|
|
from ..element import element
|
|
|
|
|
|
class generic_plot(element):
|
|
""" Class representing a pgfplots plot. """
|
|
|
|
templates = {
|
|
"options": "+[{content}]",
|
|
"content": """\\addplot{options} {data};{legend}""",
|
|
"legend": """\\addlegendentry{{{content}}}""",
|
|
}
|
|
|
|
def __init__(self, legend, data, precision=6, label=None, options=[]):
|
|
super().__init__(label=label)
|
|
self.legend = legend
|
|
self.data = data
|
|
self.options = options
|
|
self.precision = precision
|
|
|
|
def format_options(self, options):
|
|
if not options:
|
|
return ""
|
|
opts = []
|
|
for opt in options:
|
|
if type(opt) is not str:
|
|
opt = "{}={}".format(*opt)
|
|
opts.append(str(opt))
|
|
return self.fmt().format("options", {"content": ", ".join(opts)})
|
|
|
|
def format_value(self, value):
|
|
return "{:.{prec}f}".format(value, prec=self.precision)
|
|
|
|
def format_values(self, values):
|
|
return [self.format_value(value) for value in values]
|
|
|
|
def format_legend(self, legend):
|
|
if not legend:
|
|
return ""
|
|
return self.fmt().format("legend", {"content": legend})
|
|
|
|
def content(self):
|
|
return self.fmt().format(
|
|
"content",
|
|
{
|
|
"data": self.format_data(self.data),
|
|
"options": self.format_options(self.options),
|
|
"legend": self.format_legend(self.legend),
|
|
},
|
|
)
|