Skip to content

charts

Charts engine for output window.

Attributes

CHARTS_ENGINE = 'Chart.bundle.js' module-attribute

LINE_CHART = 'line' module-attribute

BAR_CHART = 'bar' module-attribute

RADAR_CHART = 'radar' module-attribute

POLAR_CHART = 'polarArea' module-attribute

PIE_CHART = 'pie' module-attribute

DOUGHNUT_CHART = 'doughnut' module-attribute

BUBBLE_CHART = 'bubble' module-attribute

CHARTS_JS_PATH = 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/{version}/Chart.min.js' module-attribute

SCRIPT_TEMPLATE = "var ctx = document.getElementById('{canvas_id}').getContext('2d');var chart = new Chart(ctx, {canvas_code});" module-attribute

Classes

PyRevitOutputChartOptions()

Bases: object

Chart options wrapper object.

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def __init__(self):
    pass

PyRevitOutputChartDataset(label)

Bases: object

Chart dataset wrapper object.

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def __init__(self, label):
    self.label = label
    self.data = []
    self.backgroundColor = ''

Attributes

label = label instance-attribute
data = [] instance-attribute
backgroundColor = '' instance-attribute

Functions

set_color(*args)

Set dataset color.

Arguments are expected to be R, G, B, A values.

Examples:

dataset_obj.set_color(0xFF, 0x8C, 0x8D, 0.8)
Source code in pyrevitlib/pyrevit/coreutils/charts.py
def set_color(self, *args):
    """Set dataset color.

    Arguments are expected to be R, G, B, A values.

    Examples:
        ```python
        dataset_obj.set_color(0xFF, 0x8C, 0x8D, 0.8)
        ```
    """
    if len(args) == 4:
        self.backgroundColor = 'rgba({},{},{},{})'.format(args[0],
                                                          args[1],
                                                          args[2],
                                                          args[3])
    elif len(args) == 1:
        self.backgroundColor = '{}'.format(args[0])

PyRevitOutputChartData()

Bases: object

Chart data wrapper object.

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def __init__(self):
    self.labels = ''
    self.datasets = []

Attributes

labels = '' instance-attribute
datasets = [] instance-attribute

Functions

new_dataset(dataset_label)

Create new data set.

Parameters:

Name Type Description Default
dataset_label str

dataset label

required

Returns:

Type Description
PyRevitOutputChartDataset

dataset wrapper object

Examples:

chart.data.new_dataset('set_a')
Source code in pyrevitlib/pyrevit/coreutils/charts.py
def new_dataset(self, dataset_label):
    """Create new data set.

    Args:
        dataset_label (str): dataset label

    Returns:
        (PyRevitOutputChartDataset): dataset wrapper object

    Examples:
        ```python
        chart.data.new_dataset('set_a')
        ```
    """
    new_dataset = PyRevitOutputChartDataset(dataset_label)
    self.datasets.append(new_dataset)
    return new_dataset

PyRevitOutputChart(output, chart_type=LINE_CHART, version=None)

Bases: object

Chart wrapper object for output window.

Attributes:

Name Type Description
output PyRevitOutputWindow

output window wrapper object

chart_type str

chart type name

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def __init__(self, output, chart_type=LINE_CHART, version=None):
    self._output = output
    self._style = None
    self._width = self._height = None
    self._version = version or '2.8.0'

    self.type = chart_type
    self.data = PyRevitOutputChartData()

    self.options = PyRevitOutputChartOptions()

Attributes

type = chart_type instance-attribute
data = PyRevitOutputChartData() instance-attribute
options = PyRevitOutputChartOptions() instance-attribute

Functions

randomize_colors()

Randomize chart datasets colors.

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def randomize_colors(self):
    """Randomize chart datasets colors."""
    if self.type in [POLAR_CHART, PIE_CHART, DOUGHNUT_CHART]:
        for dataset in self.data.datasets:
            dataset.backgroundColor = [random_rgba_color()
                                       for _ in range(0, len(dataset.data))]
    else:
        for dataset in self.data.datasets:
            dataset.backgroundColor = random_rgba_color()
set_width(width)

Set chart width on output window.

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def set_width(self, width):
    """Set chart width on output window."""
    self._width = width
set_height(height)

Set chart height on output window.

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def set_height(self, height):
    """Set chart height on output window."""
    self._height = height
set_style(html_style)

Set chart styling.

Parameters:

Name Type Description Default
html_style str

inline html css styling string

required

Examples:

chart.set_style('height:150px')
Source code in pyrevitlib/pyrevit/coreutils/charts.py
def set_style(self, html_style):
    """Set chart styling.

    Args:
        html_style (str): inline html css styling string

    Examples:
        ```python
        chart.set_style('height:150px')
        ```
    """
    self._style = html_style
draw()

Request chart to draw itself on output window.

Source code in pyrevitlib/pyrevit/coreutils/charts.py
def draw(self):
    """Request chart to draw itself on output window."""
    self._setup_charts()
    # setup canvas
    canvas_id = self._make_canvas_unique_id()
    canvas_code = self._make_canvas_code(canvas_id)
    self._output.print_html(canvas_code)
    # make the code
    js_code = self._make_charts_script(canvas_id)
    self._output.inject_script(js_code, body=True)

Functions