在jsp中,可以使用jsp通过servlet中的代码读取数据库数据。
下面给出了jstl读取数据库数据并使用JavaScript画出饼图方法的jsp代码。
首先在该类的Servlet中加入如下方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public String tree(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ File file=CommonUtils.toBean(request.getParameterMap(),File.class); encoding(file); //处理GET请求方式编码问题 int pageNum=getPageNum(request); int Record=999999; pageBean<File> pb=fileService.query(file,pageNum,Record); //获取路径url,保存到pb中 pb.setUrl(getUrl(request)); request.setAttribute("pb",pb); return "/controller.jsp"; } } |
另外对应的controller.jsp页面如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>客户列表</title> <script type="text/javascript" src="<%= request.getContextPath()%>/plugin/layui/layui.all.js"></script> <link rel="stylesheet" href="<%= request.getContextPath()%>/plugin/layui/css/layui.css"> <script src="jquery-1.4.4.min.js"></script> <!--[if IE]> <script src="http://explorercanvas.googlecode.com/svn/trunk/excanvas.js"></script> <![endif]--> <script> // Run the code when the DOM is ready $( pieChart ); function pieChart() { // Config settings var chartSizePercent = 55; // The chart radius relative to the canvas width/height (in percent) var sliceBorderWidth = 1; // Width (in pixels) of the border around each slice var sliceBorderStyle = "#fff"; // Colour of the border around each slice var sliceGradientColour = "#ddd"; // Colour to use for one end of the chart gradient var maxPullOutDistance = 25; // How far, in pixels, to pull slices out when clicked var pullOutFrameStep = 4; // How many pixels to move a slice with each animation frame var pullOutFrameInterval = 40; // How long (in ms) between each animation frame var pullOutLabelPadding = 65; // Padding between pulled-out slice and its label var pullOutLabelFont = "bold 16px 'Trebuchet MS', Verdana, sans-serif"; // Pull-out slice label font var pullOutValueFont = "bold 12px 'Trebuchet MS', Verdana, sans-serif"; // Pull-out slice value font var pullOutValuePrefix = "$"; // Pull-out slice value prefix var pullOutShadowColour = "rgba( 0, 0, 0, .5 )"; // Colour to use for the pull-out slice shadow var pullOutShadowOffsetX = 5; // X-offset (in pixels) of the pull-out slice shadow var pullOutShadowOffsetY = 5; // Y-offset (in pixels) of the pull-out slice shadow var pullOutShadowBlur = 5; // How much to blur the pull-out slice shadow var pullOutBorderWidth = 2; // Width (in pixels) of the pull-out slice border var pullOutBorderStyle = "#333"; // Colour of the pull-out slice border var chartStartAngle = -.5 * Math.PI; // Start the chart at 12 o'clock instead of 3 o'clock // Declare some variables for the chart var canvas; // The canvas element in the page var currentPullOutSlice = -1; // The slice currently pulled out (-1 = no slice) var currentPullOutDistance = 0; // How many pixels the pulled-out slice is currently pulled out in the animation var animationId = 0; // Tracks the interval ID for the animation created by setInterval() var chartData = []; // Chart data (labels, values, and angles) var chartColours = []; // Chart colours (pulled from the HTML table) var totalValue = 0; // Total of all the values in the chart var canvasWidth; // Width of the canvas, in pixels var canvasHeight; // Height of the canvas, in pixels var centreX; // X-coordinate of centre of the canvas/chart var centreY; // Y-coordinate of centre of the canvas/chart var chartRadius; // Radius of the pie chart, in pixels // Set things up and draw the chart init(); /** * Set up the chart data and colours, as well as the chart and table click handlers, * and draw the initial pie chart */ function init() { // Get the canvas element in the page canvas = document.getElementById('chart'); // Exit if the browser isn't canvas-capable if ( typeof canvas.getContext === 'undefined' ) return; // Initialise some properties of the canvas and chart canvasWidth = canvas.width; canvasHeight = canvas.height; centreX = canvasWidth / 2; centreY = canvasHeight / 2; chartRadius = Math.min( canvasWidth, canvasHeight ) / 2 * ( chartSizePercent / 100 ); // Grab the data from the table, // and assign click handlers to the table data cells var currentRow = -1; var currentCell = 0; $('#chartData td').each( function() { currentCell++; if ( currentCell % 2 != 0 ) { currentRow++; chartData[currentRow] = []; chartData[currentRow]['label'] = $(this).text(); } else { var value = parseFloat($(this).text()); totalValue += value; value = value.toFixed(2); chartData[currentRow]['value'] = value; } // Store the slice index in this cell, and attach a click handler to it $(this).data( 'slice', currentRow ); $(this).click( handleTableClick ); // Extract and store the cell colour if ( rgb = $(this).css('color').match( /rgb\((\d+), (\d+), (\d+)/) ) { chartColours[currentRow] = [ rgb[1], rgb[2], rgb[3] ]; } else if ( hex = $(this).css('color').match(/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/) ) { chartColours[currentRow] = [ parseInt(hex[1],16) ,parseInt(hex[2],16), parseInt(hex[3], 16) ]; } else { alert( "Error: Colour could not be determined! Please specify table colours using the format '#xxxxxx'" ); return; } } ); // Now compute and store the start and end angles of each slice in the chart data var currentPos = 0; // The current position of the slice in the pie (from 0 to 1) for ( var slice in chartData ) { chartData[slice]['startAngle'] = 2 * Math.PI * currentPos; chartData[slice]['endAngle'] = 2 * Math.PI * ( currentPos + ( chartData[slice]['value'] / totalValue ) ); currentPos += chartData[slice]['value'] / totalValue; } // All ready! Now draw the pie chart, and add the click handler to it drawChart(); $('#chart').click ( handleChartClick ); } /** * Process mouse clicks in the chart area. * * If a slice was clicked, toggle it in or out. * If the user clicked outside the pie, push any slices back in. * * @param Event The click event */ function handleChartClick ( clickEvent ) { // Get the mouse cursor position at the time of the click, relative to the canvas var mouseX = clickEvent.pageX - this.offsetLeft; var mouseY = clickEvent.pageY - this.offsetTop; // Was the click inside the pie chart? var xFromCentre = mouseX - centreX; var yFromCentre = mouseY - centreY; var distanceFromCentre = Math.sqrt( Math.pow( Math.abs( xFromCentre ), 2 ) + Math.pow( Math.abs( yFromCentre ), 2 ) ); if ( distanceFromCentre <= chartRadius ) { // Yes, the click was inside the chart. // Find the slice that was clicked by comparing angles relative to the chart centre. var clickAngle = Math.atan2( yFromCentre, xFromCentre ) - chartStartAngle; if ( clickAngle < 0 ) clickAngle = 2 * Math.PI + clickAngle; for ( var slice in chartData ) { if ( clickAngle >= chartData[slice]['startAngle'] && clickAngle <= chartData[slice]['endAngle'] ) { // Slice found. Pull it out or push it in, as required. toggleSlice ( slice ); return; } } } // User must have clicked outside the pie. Push any pulled-out slice back in. pushIn(); } /** * Process mouse clicks in the table area. * * Retrieve the slice number from the jQuery data stored in the * clicked table cell, then toggle the slice * * @param Event The click event */ function handleTableClick ( clickEvent ) { var slice = $(this).data('slice'); toggleSlice ( slice ); } /** * Push a slice in or out. * * If it's already pulled out, push it in. Otherwise, pull it out. * * @param Number The slice index (between 0 and the number of slices - 1) */ function toggleSlice ( slice ) { if ( slice == currentPullOutSlice ) { pushIn(); } else { startPullOut ( slice ); } } /** * Start pulling a slice out from the pie. * * @param Number The slice index (between 0 and the number of slices - 1) */ function startPullOut ( slice ) { // Exit if we're already pulling out this slice if ( currentPullOutSlice == slice ) return; // Record the slice that we're pulling out, clear any previous animation, then start the animation currentPullOutSlice = slice; currentPullOutDistance = 0; clearInterval( animationId ); animationId = setInterval( function() { animatePullOut( slice ); }, pullOutFrameInterval ); // Highlight the corresponding row in the key table $('#chartData td').removeClass('highlight'); var labelCell = $('#chartData td:eq(' + (slice*2) + ')'); var valueCell = $('#chartData td:eq(' + (slice*2+1) + ')'); labelCell.addClass('highlight'); valueCell.addClass('highlight'); } /** * Draw a frame of the pull-out animation. * * @param Number The index of the slice being pulled out */ function animatePullOut ( slice ) { // Pull the slice out some more currentPullOutDistance += pullOutFrameStep; // If we've pulled it right out, stop animating if ( currentPullOutDistance >= maxPullOutDistance ) { clearInterval( animationId ); return; } // Draw the frame drawChart(); } /** * Push any pulled-out slice back in. * * Resets the animation variables and redraws the chart. * Also un-highlights all rows in the table. */ function pushIn() { currentPullOutSlice = -1; currentPullOutDistance = 0; clearInterval( animationId ); drawChart(); $('#chartData td').removeClass('highlight'); } /** * Draw the chart. * * Loop through each slice of the pie, and draw it. */ function drawChart() { // Get a drawing context var context = canvas.getContext('2d'); // Clear the canvas, ready for the new frame context.clearRect ( 0, 0, canvasWidth, canvasHeight ); // Draw each slice of the chart, skipping the pull-out slice (if any) for ( var slice in chartData ) { if ( slice != currentPullOutSlice ) drawSlice( context, slice ); } // If there's a pull-out slice in effect, draw it. // (We draw the pull-out slice last so its drop shadow doesn't get painted over.) if ( currentPullOutSlice != -1 ) drawSlice( context, currentPullOutSlice ); } /** * Draw an individual slice in the chart. * * @param Context A canvas context to draw on * @param Number The index of the slice to draw */ function drawSlice ( context, slice ) { // Compute the adjusted start and end angles for the slice var startAngle = chartData[slice]['startAngle'] + chartStartAngle; var endAngle = chartData[slice]['endAngle'] + chartStartAngle; if ( slice == currentPullOutSlice ) { // We're pulling (or have pulled) this slice out. // Offset it from the pie centre, draw the text label, // and add a drop shadow. var midAngle = (startAngle + endAngle) / 2; var actualPullOutDistance = currentPullOutDistance * easeOut( currentPullOutDistance/maxPullOutDistance, .8 ); startX = centreX + Math.cos(midAngle) * actualPullOutDistance; startY = centreY + Math.sin(midAngle) * actualPullOutDistance; context.fillStyle = 'rgb(' + chartColours[slice].join(',') + ')'; context.textAlign = "center"; context.font = pullOutLabelFont; context.fillText( chartData[slice]['label'], centreX + Math.cos(midAngle) * ( chartRadius + maxPullOutDistance + pullOutLabelPadding ), centreY + Math.sin(midAngle) * ( chartRadius + maxPullOutDistance + pullOutLabelPadding ) ); context.font = pullOutValueFont; context.fillText( pullOutValuePrefix + chartData[slice]['value'] + " (" + ( parseInt( chartData[slice]['value'] / totalValue * 100 + .5 ) ) + "%)", centreX + Math.cos(midAngle) * ( chartRadius + maxPullOutDistance + pullOutLabelPadding ), centreY + Math.sin(midAngle) * ( chartRadius + maxPullOutDistance + pullOutLabelPadding ) + 20 ); context.shadowOffsetX = pullOutShadowOffsetX; context.shadowOffsetY = pullOutShadowOffsetY; context.shadowBlur = pullOutShadowBlur; } else { // This slice isn't pulled out, so draw it from the pie centre startX = centreX; startY = centreY; } // Set up the gradient fill for the slice var sliceGradient = context.createLinearGradient( 0, 0, canvasWidth*.75, canvasHeight*.75 ); sliceGradient.addColorStop( 0, sliceGradientColour ); sliceGradient.addColorStop( 1, 'rgb(' + chartColours[slice].join(',') + ')' ); // Draw the slice context.beginPath(); context.moveTo( startX, startY ); context.arc( startX, startY, chartRadius, startAngle, endAngle, false ); context.lineTo( startX, startY ); context.closePath(); context.fillStyle = sliceGradient; context.shadowColor = ( slice == currentPullOutSlice ) ? pullOutShadowColour : "rgba( 0, 0, 0, 0 )"; context.fill(); context.shadowColor = "rgba( 0, 0, 0, 0 )"; // Style the slice border appropriately if ( slice == currentPullOutSlice ) { context.lineWidth = pullOutBorderWidth; context.strokeStyle = pullOutBorderStyle; } else { context.lineWidth = sliceBorderWidth; context.strokeStyle = sliceBorderStyle; } // Draw the slice border context.stroke(); } /** * Easing function. * * A bit hacky but it seems to work! (Note to self: Re-read my school maths books sometime) * * @param Number The ratio of the current distance travelled to the maximum distance * @param Number The power (higher numbers = more gradual easing) * @return Number The new ratio */ function easeOut( ratio, power ) { return ( Math.pow ( 1 - ratio, power ) + 1 ); } }; </script> <style> .wideBox { clear: both; text-align: center; margin-bottom: 50px; padding: 10px; background: #ebedf2; border: 1px solid #333; line-height: 80%; } #container { width: 900px; margin: 0 auto; } #chart, #chartData { border: 1px solid #333; background: #ebedf2 url("img/gradient.png") repeat-x 0 0; } #chart { display: block; margin: 0 0 50px 0; float: left; cursor: pointer; } #chartData { width: 200px; margin: 0 40px 0 0; float: right; border-collapse: collapse; box-shadow: 0 0 1em rgba(0, 0, 0, 0.5); -moz-box-shadow: 0 0 1em rgba(0, 0, 0, 0.5); -webkit-box-shadow: 0 0 1em rgba(0, 0, 0, 0.5); background-position: 0 -100px; } #chartData th, #chartData td { padding: 0.5em; border: 1px dotted #666; text-align: left; } #chartData th { border-bottom: 2px solid #333; text-transform: uppercase; } #chartData td { cursor: pointer; } #chartData td.highlight { background: #e8e8e8; } #chartData tr:hover td { background: #f0f0f0; } </style> </head> <body> <c:set var="docx" value="0"> </c:set> <c:set var="pptx" value="0"> </c:set> <c:set var="xlsx" value="0"> </c:set> <c:set var="code" value="0"> </c:set> <c:set var="other" value="0"> </c:set> <c:forEach items="${pb.beanList}" var="cstm"> <c:if test="${cstm.category eq 'docx'}"> <c:set var="docx" value="${docx+1}"/> </c:if> <c:if test="${cstm.category eq 'doc'}"> <c:set var="docx" value="${docx+1}"/> </c:if> <c:if test="${cstm.category eq 'txt'}"> <c:set var="docx" value="${docx+1}"/> </c:if> <c:if test="${cstm.category eq 'pptx'}"> <c:set var="pptx" value="${pptx+1}"/> </c:if> <c:if test="${cstm.category eq 'ppt'}"> <c:set var="pptx" value="${pptx+1}"/> </c:if> <c:if test="${cstm.category eq 'xlsx'}"> <c:set var="xlsx" value="${xlsx+1}"/> </c:if> <c:if test="${cstm.category eq 'xls'}"> <c:set var="xlsx" value="${xlsx+1}"/> </c:if> <c:if test="${cstm.category eq 'java'}"> <c:set var="code" value="${code+1}"/> </c:if> <c:if test="${cstm.category eq 'py'}"> <c:set var="code" value="${code+1}"/> </c:if> <c:if test="${cstm.category eq 'cpp'}"> <c:set var="code" value="${code+1}"/> </c:if> <c:if test="${cstm.category eq 'c'}"> <c:set var="code" value="${code+1}"/> </c:if> <c:if test="${cstm.category eq 'exe'}"> <c:set var="other" value="${other+1}"/> </c:if> <c:if test="${cstm.category eq 'mat'}"> <c:set var="other" value="${other+1}"/> </c:if> <c:if test="${cstm.category eq 'm'}"> <c:set var="other" value="${other+1}"/> </c:if> <c:if test="${cstm.category eq 'html'}"> <c:set var="other" value="${other+1}"/> </c:if> </c:forEach> <div id="container"> <br> <br> <canvas id="chart" width="600" height="500"></canvas> <table id="chartData"> <tr> <th>文档类型</th><th>数量</th> </tr> <tr style="color: #0DA068"> <td>文本</td><td>${docx}</td> </tr> <tr style="color: #194E9C"> <td>幻灯片</td><td>${pptx}</td> </tr> <tr style="color: #723d17"> <td>数据表</td><td>${xlsx}</td> </tr> <tr style="color: #973f9c"> <td>代码类</td><td>${code}</td> </tr> <tr style="color: #9c0055"> <td>其它</td><td>${other}</td> </tr> </table> </div> </body> </html> |
效果: