Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. <%@page import="java.sql.*" %>
  2. <%@page import="java.util.*" %>
  3. <%@page import="com.google.gson.*" %>
  4.  
  5.  
  6. <!--
  7.         Include the `fusioncharts.php` file that contains functions  to embed the charts.
  8. -->
  9.         <%@page import="fusioncharts.FusionCharts" %>
  10.  
  11. <%
  12.    
  13. /*
  14.     The following 4 code lines contain the database connection information.
  15.     Alternatively, you can move these code lines to a separate file and
  16.     include the file here. You can also modify this code based on your
  17.     database connection.
  18. */
  19.  
  20. String hostdb = "localhost:3306";  // MySQl host
  21. String userdb = "root";  // MySQL username
  22. String passdb = "";  // MySQL password
  23. String namedb = "fusioncharts_jspsample";  // MySQL database name
  24.  
  25.     // Establish a connection to the database
  26.     DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  27.     Connection con = DriverManager.getConnection("jdbc:mysql://" + hostdb + "/" + namedb , userdb , passdb);
  28.    
  29.     %>
  30.     <div id="chart"></div>
  31.  
  32.    
  33.      
  34.     <%
  35.      /*
  36.         google-gson
  37.  
  38.         Gson is a Java library that can be used to convert Java Objects into
  39.         their JSON representation. It can also be used to convert a JSON string to
  40.         an equivalent Java object. Gson can work with arbitrary Java objects including
  41.         pre-existing objects that you do not have source-code of.
  42.         link : https://github.com/google/gson    
  43.      */
  44.  
  45.         Gson gson = new Gson();
  46.        
  47.        
  48.         // Form the SQL query that returns the top 10 most populous countries
  49.         String sql="SELECT Name, Population FROM Country ORDER BY Population DESC LIMIT 10";
  50.  
  51.         // Execute the query.
  52.         PreparedStatement pt=con.prepareStatement(sql);    
  53.         ResultSet rs=pt.executeQuery();
  54.        
  55.         // The 'chartobj' map object holds the chart attributes and data.
  56.         Map<String, String> chartobj = new HashMap<String, String>();
  57.        
  58.         chartobj.put("caption" , "Top 10 Most Populous Countries");
  59.         chartobj.put("paletteColors" , "#0075c2");
  60.         chartobj.put("bgColor" , "#ffffff");
  61.         chartobj.put("borderAlpha", "20");
  62.         chartobj.put("canvasBorderAlpha", "0");
  63.         chartobj.put("usePlotGradientColor", "0");
  64.         chartobj.put("plotBorderAlpha", "10");
  65.         chartobj.put("showXAxisLine", "1");
  66.         chartobj.put("xAxisLineColor" , "#999999");
  67.         chartobj.put("showValues" , "0");
  68.         chartobj.put("divlineColor" , "#999999");
  69.         chartobj.put("divLineIsDashed" , "1");
  70.         chartobj.put("showAlternateHGridColor" , "0");
  71.  
  72.         // Push the data into the array using map object.
  73.         ArrayList arrData = new ArrayList();
  74.         while(rs.next())
  75.         {
  76.             Map<String, String> lv = new HashMap<String, String>();
  77.             lv.put("label", rs.getString("Name"));
  78.             lv.put("value", rs.getString("Population"));
  79.             arrData.add(lv);            
  80.         }
  81.        
  82.         //close the connection.
  83.         rs.close();
  84.  
  85.         //create 'dataMap' map object to make a complete FC datasource.
  86.        
  87.          Map<String, String> dataMap = new LinkedHashMap<String, String>();  
  88.     /*
  89.         gson.toJson() the data to retrieve the string containing the
  90.         JSON representation of the data in the array.
  91.     */
  92.          dataMap.put("chart", gson.toJson(chartobj));
  93.          dataMap.put("data", gson.toJson(arrData));
  94. /*
  95.     Create an object for the column chart using the FusionCharts JSP class constructor.
  96.     Syntax for the constructor -
  97.   new FusionCharts("type of chart",
  98.   "unique chart id",
  99.   "width of chart",
  100.   "height of chart",
  101.   "div id to render the chart",
  102.   "type of data",
  103.   "actual data in string format")
  104.          
  105.     Because we are using JSON data to render the chart, the data format will be `json`.
  106.     The object `dataMap` holds all the JSON data for the chart,
  107.     and will be passed as the value for the data source parameter of the constructor.
  108. */
  109.         FusionCharts column2DChart= new FusionCharts(
  110.                     "column2d",// chartType
  111.                     "chart1",// chartId
  112.                     "600","400",// chartWidth, chartHeight
  113.                     "chart",// chartContainer
  114.                     "json",// dataFormat
  115.                     gson.toJson(dataMap) //dataSource
  116.                 );
  117.        
  118.         %>
  119.        
  120. <!--    Step 5: Render the chart    -->                
  121.         <%=column2DChart.render()%>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement