Google Chart using Struts 2

We know that Google chart basically displays different statistical data on different chart types such as column chart, bar chart, line chart, pie chart etc. You can have a look at the URL for more information.

You can integrate Google chart with any server side technology but here I will show you how to integrate Google chart using Struts 2. This tutorial shows step by step so that we can understand how it happens. It displays the data for area-wise top seven countries in pie chart. You can also use other charts for other purposes. In this example I have used static sample data and the actual data should come from database or other sources. You can change the source of data as per your requirements.

If you have any query please feel free to leave a comment.


JDK 1.6
Knowledge of Struts 2, Java
Apache Tomcat v7
Eclipse IDE

Google chart using Struts2
Final Output

When chart renders on the view

Google Chart API

When mouse hover happens on chart

Google Chart API

Now we will see how it happens step by step.

Step 1. Create the Dynamic Web Project in Eclipse IDE. The required project structure gets created in the IDE.

Step 2. Put all jar files under WEB-INF/lib directory.

Step 3. Now modify deployment descriptor – web.xml. Paste the below code in your web.xml file.


Step 4. Create Struts Action Class

Step 5. Then we will create sample data for showing on Google Pie Chart. So create a class as shown below. This file contains area-wise top seven countries in the world.


Step 6. Create a view called struts.jsp which will render the Google Pie Chart along with the data from Struts Action class. In view you can initialize data to chart in two ways


In the above code look carefully we simply access the struts action bean and add the data to the Google Pie Chart.


Step 7. If everything is done please run the application and see yourself. Hit the URL http://<host>:<port>/<context-path>/struts.action. For example  http://localhost:8080/google-chart/struts.action

Thanks for your reading. Please do not forget to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.