You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Flotr2LineChart.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.charting;
  17. import java.text.MessageFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. /**
  21. * Implementation of a Line chart using the flotr2 library
  22. *
  23. * @author Tim Ryan
  24. *
  25. */
  26. public class Flotr2LineChart extends Chart {
  27. private static final long serialVersionUID = 1L;
  28. boolean xAxisIsDate = false;
  29. public Flotr2LineChart(String tagId, String title, String keyName,
  30. String valueName) {
  31. super(tagId, title, keyName, valueName);
  32. }
  33. @Override
  34. protected void appendChart(StringBuilder sb) {
  35. String dName = "data_" + dataName;
  36. sb.append("var labels_" + dataName + " = [");
  37. if(xAxisIsDate){
  38. // Generate labels for the dates
  39. SimpleDateFormat df = new SimpleDateFormat(dateFormat);
  40. df.setTimeZone(getTimeZone());
  41. for (int i = 0; i < values.size(); i++) {
  42. ChartValue value = values.get(i);
  43. Date date = new Date(Long.parseLong(value.name));
  44. String label = df.format(date);
  45. if(i > 0){
  46. sb.append(",");
  47. }
  48. sb.append("\"" + label + "\"");
  49. }
  50. }
  51. else {
  52. for (int i = 0; i < values.size(); i++) {
  53. ChartValue value = values.get(i);
  54. if(i > 0){
  55. sb.append(",");
  56. }
  57. sb.append("\"" + value.name + "\"");
  58. }
  59. }
  60. line(sb, "];");
  61. line(sb, MessageFormat.format("var {0} = Flotr.draw(document.getElementById(''{1}''),", dName, tagId));
  62. // Add the data
  63. line(sb, "[");
  64. line(sb, "{ data : [ ");
  65. for (int i = 0; i < values.size(); i++) {
  66. ChartValue value = values.get(i);
  67. if(i > 0){
  68. sb.append(",");
  69. }
  70. line(sb, MessageFormat.format("[{0}, {1}] ", value.name, Float.toString(value.value)));
  71. }
  72. line(sb, MessageFormat.format(" ], label : ''{0}'', lines : '{' show : true '}', color: ''#ff9900'' '}'", valueName));
  73. if(highlights.size() > 0){
  74. // get the highlights
  75. line(sb, ", { data : [ ");
  76. for (int i = 0; i < highlights.size(); i++) {
  77. ChartValue value = highlights.get(i);
  78. if(i > 0){
  79. sb.append(",");
  80. }
  81. line(sb, MessageFormat.format("[{0}, {1}] ", value.name, Float.toString(value.value)));
  82. }
  83. line(sb, MessageFormat.format(" ], label : ''{0}'', points : '{' show : true, fill: true, fillColor:''#002060'' '}', color: ''#ff9900'' '}'", valueName));
  84. }
  85. line(sb, "]");
  86. // Add the options
  87. line(sb, ", {");
  88. if(title != null && title.isEmpty() == false){
  89. line(sb, MessageFormat.format("title : ''{0}'',", title));
  90. }
  91. line(sb, "mouse: {");
  92. line(sb, " track: true,");
  93. line(sb, " lineColor: '#002060',");
  94. line(sb, " position: 'ne',");
  95. line(sb, " trackFormatter: function (obj) {");
  96. line(sb, " return labels_" + dataName + "[obj.index] + ': ' + parseInt(obj.y) + ' ' + obj.series.label;");
  97. line(sb, " }");
  98. line(sb, "},");
  99. line(sb, "xaxis: {");
  100. line(sb, " showLabels: false,");
  101. line(sb, " showMinorLabels: false,");
  102. line(sb, " autoscale: true,");
  103. line(sb, " autoscaleMargin: 0,");
  104. line(sb, " margin: 10");
  105. line(sb, "},");
  106. line(sb, "yaxis: {");
  107. line(sb, " showLabels: false,");
  108. line(sb, " showMinorLabels: false,");
  109. line(sb, " tickDecimals: 0,");
  110. line(sb, " autoscale: true,");
  111. line(sb, " autoscaleMargin: 1,");
  112. line(sb, " margin: 10");
  113. line(sb, "},");
  114. line(sb, "grid: {");
  115. line(sb, " verticalLines: false,");
  116. line(sb, " minorVerticalLines: null,");
  117. line(sb, " horizontalLines: true,");
  118. line(sb, " minorHorizontalLines: null,");
  119. line(sb, " outlineWidth: 1,");
  120. line(sb, " outline: 's'");
  121. line(sb, "},");
  122. line(sb, "legend: {");
  123. line(sb, " show: false");
  124. line(sb, "}");
  125. line(sb, "});");
  126. }
  127. @Override
  128. public void addValue(Date date, int value) {
  129. xAxisIsDate = true;
  130. super.addValue(date, value);
  131. }
  132. }