Create Charts in Java using Database
In addition to specifying the chart data (or the URL for the file in which the chart data is stored) directly in the JSON/XML code, you can also fetch data for the chart from a database.
This section showcases how you can do this using the FusionCharts JSP wrapper.
Before you proceed, make sure you have installed and set up the plugin correctly. Although you can create all kinds of charts using this method, in this section we will focus on only these two examples.
Creating a Column 2D Chart
Assume that you have a fusioncharts_jspsample database with the Country table that stores the population of all countries in the world. Using the data in this database, let's say you want to plot a column 2D chart showing the top ten most populous countries in the world.
The resultant column 2D chart looks like this:
The data structure to create this chart, by fetching chart data from a database, is given below:
The code above uses the
Gson is a Java library that can be used to convert:
- Java objects into their corresponding JSON representation
- JSON strings into their corresponding Java objects.
Gson allows working with arbitrary Java objects that include pre-existing objects for which you may not have the source code.
The google-gsn library :
- Provides the
fromJson()methods for converting Java objects to JSON and the other way round
- Allows conversion of the already existing unmodifiable objects to and from JSON
- Supports Java Generics extensively
- Allows custom representations of objects
- Supports arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)
The code above imports the
com.google.gsonpackage that enables the conversion from JSON to Java and Java to JSON.
Click here for more information on google-gson.