Charting in Retool with Plotly. Part I: Line graphs step-by-step beginners guide
In this first part of the Plotly in Retool series, we’re going back to basics with the line/scatter chart and how to add more useful features like trend lines and annotations.
At Bold Tech, we think that Plotly Charts are a criminally underrated option in Retool. For that reason, we’ve spent some time digging into all the options available with Plotly and have come up with a whole series of design choices, ranging from simple to complex, to help Retool users integrate better data into their apps.
In this first part of the series, we’ll strip it back to basics with the line/scatter chart and add various features to make it more useful. This will also help to illustrate the structure of the data/layout objects in Plotly, which is often the biggest struggle for developers using it in Retool.
In the rest of this series of Plotly guides, we’ll show you how to make more advanced graphs and charts that might just rival those of your favorite BI tools.
Plotly in Retool: Pros and Cons
Pros
Plotly is hugely flexible and with a little tweaking you can get most of the results that you are looking for. This is very useful for consolidating your tech stack and removing costly BI seats for your organization.
You can build out niche use cases for your team based on interactive data already available in your apps.
Simple charts are really easy to build - and these tend to be the majority of use cases we see. For presenting these simple charts, paying for PowerBI or similar products might be overkill and Retool is a great alternative.
Annotations help add a lot of contextual information to chart formats.
The flexibility of Retool means you can build your charts specifically for the end user. For example, you might see that operations teams want to see data in a more action-focused format than team leaders, or even marketing teams, who might prefer detailed charts. With Retool you can present the data exactly how it suits these teams.
Cons
Development speed using Plotly is the major limitation compared to standard BI tools, and is typically slower than the speed of development you’d be used to in Retool too.
Nevertheless, Business Intelligence tools like Tableau are usually very expensive, so if you only use a limited selection of charts, you might find using Plotly in Retool a good way to save money.
Plotly can take a little time to get into and truly understand, and there is a steep learning curve to mastering the system that may not pay off if you are only looking to make a single chart.
Plotly Essentials
Retool has built in a UI layer for working with Plotly in Retool, but they are mostly limited to simple charts like bar graphs, line and pie charts, and scatter plots. Working with these options is simple and Retool does a lot of the heavy lifting for you, automatically offering the data as available in your data resource, and allowing you to sum, average, median your data, and more.
With more complex datasets, however, it can be tricky to format the data correctly in the spaces provided. In this case it can be more straightforward to format into a JavaScript query. The Retool UI also doesn’t leave much space for customization with the built-in options.
The key to learning how to use Plotly in Retool is understanding how to transfer their detailed documentation into the data and layout objects used in Retool.
Luckily, there is a simple trick that can help you get started with this for all the demo charts and their code that are available in the Plotly docs. All you need to do is copy and paste the example code into a JavaScript query and change the final line from Plotly.newPlot('myDiv', data, layout); to return {data, layout};.
You can then refer to these objects in the respective value boxes in the Retool chart component - switching the option to PlotlyJSON instead of the UI form, and referring to the attributes in your query.
This gives you a great starting point from which to understand how to manipulate the example charts that Plotly provides.
Here is an example of how the Plotly attributes are organized within the object.
Something that many developers using Plotly are unaware of, is the ‘Annotations’ options it offers. These annotations need to go in the ‘Layout’ object of the Plotly code as you can see in the example above. This can be helpful for adding useful contextual customizations to any chart.
Features such as subplot annotations can be particularly useful for highlighting certain data points. You can read more in their annotations section.
Scatter plot/line graph step-by-step
For this demonstration, we’ve used the scatter plot option as an illustration, as this trace type includes line charts, scatter charts, text charts, and bubble charts and the code can easily be used to create any of these examples.
Line charts are set by changing the ‘mode’ type from ‘markers’, a scatter plot, to ‘line’, or ‘lines+markers’ to combine the two. Bubble charts are achieved by setting ‘marker.size’ and/or ‘marker.color’ to numerical arrays.
Chart 1: Basic chart
To get started, we set up a simple line chart with some social media analytics data. In the code below, we have annotated the purpose and some of the customization options of this chart.
As you can see, the data object is where you’ll format your X and Y values, the type of graph and some of the color scheme (including the color of the line/scatter plots), as well as the format of the text in the hover label.
Data object:
Now, in the layout object, we can define more customization options. These typically include the title, legend, labels and their respective design options, such as font and color, and define the margins of the plot.
You can also define whether it’s possible to select points on the chart. This can be useful when using a chart to filter data in a table for instance, which is possible in Retool using the chart.selectedPoints attribute. Note, this is straightforward with scatter plots and bubble charts with clearly-defined markers, but requires some more complex code for line charts.
Layout object:
Chart 2: Basic chart with trend line
For this chart version we have the same chart but have added a trendline that summarizes the values as an average of each month. To set up the trendline, you need to make these changes in the data object. In this chart, the layout object is exactly the same as in the first.
This trendline is just another line on the chart, so this method can also be used for adding a second line to the chart to represent different data.
In our case, we needed to first group months and calculate averages of each, so we initially had to define these variables and number formats and calculate the averages:
Data object:
Chart 3: Annotations
For this chart, we have replaced the trendline with an annotation that alerts the user to an event on the chart. For the sake of clarity in this first code, we manually coded in the annotation label and value, but you could use the annotations options to set dynamic values to automatically indicate values such as the highest or lowest with some JavaScript calculations.
The data object is the same as in the first chart, but we’ve changed the layout object by adding the ‘Annotations’ object within it. This ‘Annotations’ object comes in right at the end of our layout object code, after all the other code.
This is the annotations object to be added to the layout:
To take these annotations one step further, we’ve made some dynamic options based on some calculations. We included a multi-select component (named selectedAnnotations here) where users can decide which features to highlight.
When any of these are selected, a marker will show on the chart to point out the relevant value.
These are calculated in JavaScript queries that look for the minimum and maximum values using _.minBy and _.maxBy. These queries can be triggered to run on the value selection.
Min:
Max:
In the Plotly code, we then check if the multiselect (selectedAnnotations) includes the selected value, and then if so, sets that annotation on the chart. As before, this code is right at the end of our layout object.
Chart 4: Color scale
In chart 4, we’re getting a little funky with our color scheme and have used the built-in color scale to add some interest to the chart. It’s not a major lift, but color can go a long way to making data easier to understand with a single glance. This might be particularly useful for bubble maps.
The color scale is defined in the data object under "colorscale": "Viridis".
Chart 5: Range slider and time buttons
For our last addition, we’re adding a handy range-slider feature and some buttons to adjust the time span.
The data object is the same as in chart 1. For the layout object, everything we need here is under the ‘x-axis’ object. The range slider is a built-in option, and can be activated simply by setting ‘rangeslider’ to visible.
The integrated buttons are an additional UI feature that can save some screen real estate by keeping buttons on a Retool app to a minimum. It’s important to note however that the state of these buttons are not exposed to Retool, so can’t be used to filter more data in the app if needed. To filter data in both a chart and table, for instance, the buttons would need to be configured in Retool itself.
Candlestick chart
Another similar option to the line and scatter graphs is the Candlestick chart. This chart gives you a clear overall trend line of values, and up close users can get a better understanding of the positive and negative values and their min/max values.
It’s a particularly common format in financial charts.
To illustrate this chart, we used the publicly available Bitcoin price data.
Data
Layout
In this case, our layout object is very straightforward and mainly defines the labels, legends and color scheme. The range slider is set to visible as default, but can be added as in chart 5 and set to false if not required.
There you have several variations to improve your scatter/line charts in Retool with just a few simple tweaks! For our subscribers we’ve included links to these charts in Retool below.
In the next part of this series, we’ll be sharing how we built out a complex Sankey diagram in Retool.
💥
At Bold Tech, we specialize in building great internal tools, fast. We are obsessed with building apps that make your teams happier and more productive. In our blog, you can learn all about how to build better business software for more satisfied employees, or get in touch to chat to us about what you're looking to build.
In this tutorial, learn to build an AI-supported customer chatbot app, connected to the Intercom.io API, which helps customer support agents by generating accurate, editable responses fast.