If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!
Marked:
Package type:
plugin

Repository

  • Project:Google Charts plugin
  • Type:Git
  • Project's site
  • Root location:https://github.com/timrichardson/google_charts_plugin/

Links

License


This is a simple plugin to use Google Charts.

Based on web2py 2.6.4

Usage

You need to install the plugin, write a controller function which returns the data to be charted, and set up any options to pass to the chart.

There are examples included. Multiple charts per page is ok.

Usage in a view looks like this two-chart view:

{{extend 'layout.html'}}
<!-- need to source this in any view which has a LOAD chart -->
<script src="https://www.google.com/jsapi" type="text/javascript"></script>

<!-- Here, data and options same for both charts -->
<!-- user_signature works if using logged-in users ... not well tested -->
{{ data_url = URL('plugin_google_chart','plugin_return_data.json',user_signature=True)
  #options are documented at https://developers.google.com/chart/
  options ={'title':'Sales vs Expenses','width':0,'height':0}
          }}

<div class="span6">
{{=LOAD('plugin_google_chart','plugin_google_chart.load',ajax=True,
        user_signature=True,vars={'type':'bar','data_url':data_url,
        'options_dict':options})
}}
</div>

<div class="span6">
{{=LOAD('plugin_google_chart','plugin_google_chart.load',user_signature=True,ajax=True,
    vars={'type':'column','data_url':data_url,'options_dict':options})
}}
</div>

 

This example above uses a function plugin_return_data in the plugin controller so you can look at that to see how to send data to a chart. In your own apps, you need to provide this function, which means replacing data_url with your own.

The type of the chart is provided in the LOAD url in vars; look at the plugin_google_chart.load to see which types I have included. 

Installing the plugin

A plugin is simply files in directories; by uploading a plugin to an app, the web2py admin application installs those files in a given application as if you had put them there yourself. The plugin file is on the releases section of the GitHub repository. Use the Download link above to get the .w2p file

 

In this case the plugin will add one controller and one view to your app.

The controller includes the function plugin_google_chart() which works with the view plugin_google_chart.load to display the chart. 

The other content in the controller is just example/tutorial content. 

Improving and extending it (add more chart types)

 

The view plugin_google_chart.load has the important logic.

There is a switch which handles the different chart types. You can easily extend it.

 

Related slices

Comments (1)

  • Login to post



  • 0
    m 8 years ago

    Hi, does anyone know of a way to generate multiple charts with different datasets on the same page with the Google Charts plugin?  I can't seem to find a way.

     

    Thanks!


Hosting graciously provided by:
Python Anywhere