

Draw FusionCharts with Python
source link: https://dev.to/bcm628/draw-fusioncharts-with-python-kc1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Draw beautiful charts with FusionCharts and Anvil
FusionCharts is a library for building beautiful dashboards in the browser, and with Anvil you can build your FusionCharts dashboard entirely in Python, no HTML or JS required!
Let's jump in and build the standard FusionCharts demo plot in Anvil. Here's the plan:
- Create an Anvil app
- Add the FusionCharts library
- Create a FusionChart plot in Python
- Bonus: Add an event handler to the plot
- Deploy your app to the web
1. Create an Anvil app
We're going to start with a blank app, built from Anvil's Material Design theme:
2. Add the FusionCharts library
FusionCharts is distributed as a Javascript library, so we'll add that to our app in the Native Libraries section:
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
<script type="text/javascript" src="https://cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
Enter fullscreen mode
Exit fullscreen mode
That's all the HTML we have to write - the rest of our app will be written entirely from Python!
3. Create a FusionCharts plot in Python
The blank app we created already has a form (Form1
) that will load when someone visits our app, so let's add a placeholder component to hold the plot. A Spacer
works nicely. Drag and drop one from the Toolbox onto the form, set the height to something suitable, and rename it to chart_placeholder
:
We want to display the chart when the form is opened, so select the form and then scroll down to the bottom of the Properties panel to create an event handler for the show
event:
Anvil has created the form_show
event handler for you, and this is where we want to write code to display our chart. Before we get to that, let's import the things we need. We'll be using the anvil.js
module to interact with the FusionCharts Javascript, so start by importing that at the top of the file. At the same time, let's add the data we want to plot:
...
from anvil import *
import anvil.js # Add this import
chart_data = [{
"label": "Venezuela",
"value": "290"
},{
"label": "Saudi",
"value": "260"
},{
"label": "Canada",
"value": "180"
},{
"label": "Iran",
"value": "140"
},{
"label": "Russia",
"value": "115"
},{
"label": "UAE",
"value": "100"
},{
"label": "US",
"value": "30"
},{
"label": "China",
"value": "30"
}
]
#
class Form1(Form1Template):
...
Enter fullscreen mode
Exit fullscreen mode
Now update your new form_show
event handler to create the chart:
def form_show(self, **event_args):
"""This method is called when the form is shown on the screen"""
chart = anvil.js.window.FusionCharts({
"renderAt": anvil.js.get_dom_node(self.chart_placeholder),
"type": "column2d",
"width": "100%",
"height": "100%",
"dataSource": {
"chart": {
"caption": "Countries With Most Oil Reserves [2017-18]",
"subCaption": "In MMbbl = One Million barrels",
"xAxisName": "Country",
"yAxisName": "Reserves (MMbbl)",
"numberSuffix": "K",
"theme": "fusion",
},
"data": chart_data,
},
})
chart.render()
Enter fullscreen mode
Exit fullscreen mode
And that's it! Hit "Run" to see your chart. It should look something like this:
4. Bonus step: Add an event handler to your plot
FusionCharts are fully interactive, and we can easily wire up their events to Python methods in our app. Let's pop up an alert when the user clicks one of the bars on the plot.
First, we'll add a method to our form to handle the event:
def data_plot_click(self, event, data):
alert(f"You clicked on {data.categoryLabel}, which has a value of {data.displayValue}")
Enter fullscreen mode
Exit fullscreen mode
Then we need to modify our chart generation code to pass the method to FusionCharts:
def form_show(self, **event_args):
"""This method is called when the form is shown on the screen"""
chart = anvil.js.window.FusionCharts({
"renderAt": anvil.js.get_dom_node(self.spacer_1),
"type": "column2d",
## Add this section to register our new event handler
"events": {
"dataplotclick": self.data_plot_click
}
"width": "100%",
"height": "100%",
...
Enter fullscreen mode
Exit fullscreen mode
That's all we need. Run your app again, and try clicking on one of the bars to see the popup.
5. Deploy your app to the web
Anvil makes it really easy to publish your new FusionCharts dashboard to the web. Deploying the app is as simple as clicking 'Publish App' in the app's settings and choosing a URL:
Now our app is available to anyone on the web!
That's it!
We've just created a web app with nothing but Python, even though the FusionCharts library usually requires Javascript!
Check out the full source code of the app here
More about Anvil
If you're new here, welcome! Anvil is a platform for building full-stack web apps with nothing but Python. No need to wrestle with JS, HTML, CSS, Python, SQL and all their frameworks – just build it all in Python.
Recommend
-
64
Shell + Lisp = <3 SHCL is a shell written in Common Lisp. It tries to adhere to the POSIX standard as closely as it can. Unlike those new-fangled shells, SHCL doesn't try to strap new features onto the POSIX Shell language to...
-
84
流程图软件 draw.io 值得你拥有
-
38
All that noise, and all that sound, all those places I have found (Speed of Sound, Coldplay) Some days ago, my friend Jorge showed me one of the coolest datasets I’...
-
101
Doodle Master The Doodle Master seeks to turn your UI mockups into real code. Currently this repository just serves to demonstrate a Proof Of Concept of Artificially Intelligent Design Tools. The demo...
-
47
Draw Your Galaxy- HTML,CSS,JS Hey Guys, This is just another creation that can be done using these 3 languages. Actually the...
-
56
tart - terminal art program _____ _ ____ _____ _ (_ _)/ \ | (_ _)| | | | / ^ \ | O || | |_| | |/ ___ \| _ < | | _ |_|_/ \_|_| \_\|_| |_| Tart is a program that provid...
-
4
使用FusionCharts_Evaluation心得小记 近日开发需要,做服务器监控统计,zabbix做的二次开发,统计图部分原来是图片的,没flash效果好,应需求,改成flash的.遇到点小问题,调试了好久才解决,现在,写在这里,供其他遇到该问题的朋友阅读尝试解决此类问题!...
-
7
Draw the Mandelbrot Set in Python This tutorial will gu...
-
2
Ranked #5 for todayFusionGrid by FusionChartsJavaScript data grid component for web & mobile applicationsPayment Required
-
6
Python program to draw star using turtle graphicsSkip to content
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK