<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b84a1133-09de-434c-a36e-3eee68618b84/alert.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b84a1133-09de-434c-a36e-3eee68618b84/alert.png" width="40px" /> The Charts engine in pyRevit uses the Charts.js module as the underlying renderer. See the Charts.js docs for detail info on the chart configuration and options.
</aside>
pyRevit has an internal charts engine (See Charts Engine ). This feature can easily be used thought the output window wrapper object, to create beautiful and interactive charts in the output window of your scripts.
Get the wapper object for the output window, and see the examples below on creating the most popular chart types
from pyrevit import script
output = script.get_output()
Create a chart object for the chart type that you want. We’ll add data to this later…
from pyrevit import script
output = script.get_output()
# Line chart
chart = output.make_line_chart()
# Bar chart
chart = output.make_bar_chart()
# Bubble chart
chart = output.make_bubble_chart()
# Radar chart
chart = output.make_radar_chart()
# Polar chart
chart = output.make_polar_chart()
# Pie chart
chart = output.make_pie_chart()
# Doughnut chart
chart = output.make_doughnut_chart()
Setup the chart title, and other options. the full list of options for every chart is available on Charts.js Documentation page. Some of the properties have their own sub-properties, for example the title
option for the charts has multiple sub-properties as shown below. The value for these type of properties should be a dictionary of the sub-properties you’d like to set.
chart.set_style('height:150px')
chart.options.title = {'display': True, 'text':'Chart Title', 'fontSize': 18, 'fontColor': '#000', 'fontStyle': 'bold'}
Now let’s add data to the chart. Every chart object has a data property chart.data
that we can interact with to add datasets to the chart. Different types of charts need different types of data sets in terms of how data is organized, so the chart can present multiple data sets correctly. I’m providing two examples here, one for a simple line chart (showing 3 different data sets) and another for a radial chart (also showing 3 different data sets within the same chart). They’re all very similar to each other though.
# setting the charts x line data labels
chart.data.labels = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# Let's add the first dataset to the chart object
# we'll give it a name: set_a
set_a = chart.data.new_dataset('set_a')
# And let's add data to it.
# These are the data for the Y axis of the graph
# The data length should match the length of data for the X axis
set_a.data = [12, 19, 3, 17, 6, 3, 7]
# Set the color for this graph
set_a.set_color(0xFF, 0x8C, 0x8D, 0.8)
The last step is to ask the chart object to draw itself.
# Before drawing the chart you can randomize the colors
# if you have not added any color to the datasets.
chart.randomize_colors()
# Finally let's draw the chart
chart.draw()