好用的java web图表组件──chartdirector

ChartDirector是非免费的,但是有破解方法就是将一个授权文件放在web工程的WEB-INF/CLASSES下面。这里只给出一个例子的代码,如果大家想试试可以到资源中搜索chartdirector下载它的示例工程及api,还有授权文件(在web工程的WEB-INF/CLASSES下面)。
1.曲线图(可点击):
  1. <%@page import="ChartDirector.*" %> 
  2. <% // 曲线图数据 
  3. double[] data0 = {50, 55, 47, 36, 42, 49, 63, 62, 73, 59, 56, 50, 64, 60, 67, 67, 58, 59, 73, 77, 84, 82, 80, 84}; 
  4. double[] data1 = {36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24, 28, 36, 30, 45, 46, 42, 48, 45, 43, 52, 64, 70}; 
  5. // 图形的横坐标 
  6. String[] labels = {"Jan-04", "Feb-04", "Mar-04", "Apr-04", "May-04", "Jun-04", "Jul-04", "Aug-04", "Sep-04", "Oct-04", "Nov-04", "Dec-04", "Jan-05", "Feb-05", "Mar-05", "Apr-05", "May-05", "Jun-05", "Jul-05", "Aug-05", "Sep-05", "Oct-05", "Nov-05", "Dec-05"}; 
  7. // 创建一个大小 600 x 360的图形 
  8. XYChart c = new XYChart(600, 360, 0xeeeeff, 0x000000, 1); c.setRoundedFrame(); //设置图形圆角
  9. // 设置绘图区的大小及格式
  10. c.setPlotArea(55, 60, 520, 240, 0xffffff, -1, -1, 0xcccccc, 0xcccccc); 
  11. //添加图例
  12. LegendBox legendBox = c.addLegend(55, 58, false, "Arial Bold", 9); legendBox.setBackground(Chart.Transparent);//图例背景色透明
  13. //设置纵坐标的刻度间隔
  14. c.yAxis().setAutoScale(0.1); 
  15. // 添加图形标题
  16. ChartDirector.TextBox title = c.addTitle("Monthly Revenue for Year 2000/2001", "Times New Roman Bold Italic", 15, 0xffffff); title.setBackground(0x0000cc, 0x000000, Chart.glassEffect(Chart.ReducedGlare)); 
  17. //为y轴添加标题 
  18. c.yAxis().setTitle("Month Revenue (USD millions)"); 
  19. // 为x轴添加坐标
  20. c.xAxis().setLabels(labels).setFontAngle(90); //90是角度,设置横坐标的显示格式
  21. //在 x = 17 的位置添加一个标记
  22. Mark mark = c.xAxis2().addMark(17, 0x809933ff, "Merge with Star Tech", "Arial Bold"); 
  23. mark.setFontColor(0x9933ff); 
  24. // 添加图形右下角显示信息(这里是版权信息)
  25. ChartDirector.TextBox ccopyRight = c.addText(575, 295, "(c) Copyright Space Travel Ltd", "Arial Bold"); copyRight.setAlignment(Chart.BottomRight); 
  26. // 添加绘图区到图形中(LineLayer是曲线图) 
  27. LineLayer layer = c.addLineLayer(); 
  28. // 设置曲线宽度 
  29. layer.setLineWidth(3); 
  30. // 添加数据 
  31. layer.addDataSet(data0, -1, "Enterprise"); 
  32. layer.addDataSet(data1, -1, "Consumer"); 
  33. // 创建图形文件
  34. String cchart1URL = c.makeSession(request, "chart1");

  35. String cchartImageMap = c.getHTMLImageMap("xystub.jsp", "", "title='{dataSetName} @ {xLabel} = USD {value|0} millions'"); 

  36. String legendImageMap = legendBox.getHTMLImageMap( "javascript:popMsg('the legend key [{dataSetName}]');", " ", "title='This legend key is clickable!'"); 

  37. String titletitleCoor = title.getImageCoor(); 
  38. String markmarkCoor = mark.getImageCoor(); 
  39. String copyRightcopyRightCoor = copyRight.getImageCoor(); %> 
  40. <html> 
  41. <body topmargin="5" leftmargin="5" rightmargin="0" marginwidth="5" marginheight="5"> 
  42. <div style="font-size:18pt; font-family:verdana; font-weight:bold">  Custom Clickable Objects div> 
  43. <hr color="#000080"> <div style="font-size:10pt; font-family:verdana; margin-bottom:20"> 
  44. <div style="font-size:10pt; font-family:verdana; width:600px; margin-bottom:20"> In the following chart, the lines, legend keys, title, copyright, and the "Merge with Star Tech" text are all clickable! div> 
  45. <img src="<%=response.encodeURL("getchart.jsp?"+chart1URL)%>border="0" usemap="#map1"> 
  46. <map name="map1"> 
  47. <%=chartImageMap%> 
  48. <%=legendImageMap%> 
  49. <area <%=titleCoor%> href='javascript:popMsg("the chart title");' title='The title is clickable!'> 
  50. <area <%=markCoor%> href='javascript:popMsg("the Merge with Star Tech mark");' title='The "Merge with Star Tech" text is clickable!'> 
  51. <area <%=copyRightCoor%> href='javascript:popMsg("the copyright message");' title='The copyright text is clickable!'> 
  52. map> 
  53. <SCRIPT> 
  54. function popMsg(msg) { alert("You have clicked on " + msg + "."); } SCRIPT> 
  55. body> 
  56. html> 

你可能感兴趣的:(java,web开发)