As a newbie in the world of JSON and AJAX, I find myself struggling to create a chart using chart.js. I have a servlet that reads SQL statements and a JSP that processes these requests, but I lack experience in this type of programming. If anyone out there knows how to solve this problem, please lend me a hand. Even a small idea could point me in the right direction. Thank you.
ChartJsonDataServlet
public void dbConn(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
final String DB_DRIVER = readConfig.getProperties("dbDriver");
final String DB_URL = readConfig.getProperties("conUrl");
final String USER = readConfig.getProperties("dbUser");
final String PASSWORD = readConfig.getProperties("dbPass");
try {
Class.forName(DB_DRIVER);
// loads driver
Connection c = DriverManager.getConnection(DB_URL, USER, PASSWORD); // gets a new connection
PreparedStatement ps = c.prepareStatement("SELECT m.month, IFNULL(x.sales, 0) AS sales FROM (SELECT 1 AS month UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12) AS m LEFT JOIN (SELECT DATE_FORMAT(date, '%c') as month, COUNT(*) AS sales FROM ssl_sales where YEAR(date)=2017 AND status_ssl='new' GROUP BY month) AS x ON m.month = x.month ORDER BY m.month ASC");
ResultSet rs = ps.executeQuery();
ArrayList<Integer> month = new ArrayList<Integer>();
ArrayList<Integer> sales = new ArrayList<Integer>();
while(rs.next())
{
month.add(rs.getInt("month"));
sales.add(rs.getInt("sales"));
}
String jmonth=new Gson().toJson(month);
System.out.println("***sini***:-=1----------------------"+jmonth);
String jsales=new Gson().toJson(sales);
System.out.println("##sale## " +jsales);
rs.close();
return;
}
chart.js
<!-- start: BAR CHART -->
<div class="container-fluid container-fullw bg-white">
<div class="row">
<div class="col-sm-12">
<h5 class="over-title margin-bottom-15">Bar <span class="text-bold">Chart</span></h5>
<div class="row">
<div class="col-sm-6">
<canvas id="barChart" class="full-width"></canvas>
</div>
<div class="col-sm-6">
<p class="margin-top-20">
<div id="barLegend" class="chart-legend"></div>
</p>
<span id="sana" ></span>
</div>
</div>
</div>
</div>
</div>
<!-- end: BAR CHART -->
<script type="text/javascript">
var Charts = function() {"use strict";
var barChartHandler = function() {
var options = {
// Sets the chart to be responsive
responsive: true,
//Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
scaleBeginAtZero: true,
//Boolean - Whether grid lines are shown across the chart
scaleShowGridLines: true,
//String - Colour of the grid lines
scaleGridLineColor: "rgba(0,0,0,.05)",
//Number - Width of the grid lines
scaleGridLineWidth: 1,
//Boolean - If there is a stroke on each bar
barShowStroke: true,
//Number - Pixel width of the bar stroke
barStrokeWidth: 2,
//Number - Spacing between each of the X value sets
barValueSpacing: 5,
//Number - Spacing between data sets within X values
barDatasetSpacing: 1,
//String - A legend template
legendTemplate: '<ul class=\"name.toLowerCase()-legend\"> for (var i=0; i<datasets.length; i++){<li><span style="background-color:datasets[i].fillColor"></span>if(datasets[i].label){datasets[i].label%>}</li>}</ul>'
};
var dataarray = [65];
dataarray.push(59);
dataarray.push(27);
var data = {
labels: jmonth,
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: jsales
}, ]
};
// Get context with jQuery - using jQuery's .get() method.
var ctx = $("#barChart").get(0).getContext("2d");
// This will get the first returned node in the jQuery collection.
var barChart = new Chart(ctx).Bar(data, options);
;
//generate the legend
var legend = barChart.generateLegend();
//and append it to your page somewhere
$('#barLegend').append(legend);
};
return {
init: function() {
barChartHandler();
}
};
}();
</script>
If you have any insights on how to extract JSON data from the servlet into the JSP file, please share your wisdom with me. Your help would be greatly appreciated. Let's rock this programming challenge together!