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.

GoogleChart.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import com.gitblit.utils.StringUtils;
  21. /**
  22. * Abstract parent class for Google Charts built with the Visualization API.
  23. *
  24. * @author James Moger
  25. *
  26. */
  27. public abstract class GoogleChart implements Serializable {
  28. private static final long serialVersionUID = 1L;
  29. final String tagId;
  30. final String dataName;
  31. final String title;
  32. final String keyName;
  33. final String valueName;
  34. final List<ChartValue> values;
  35. int width;
  36. int height;
  37. boolean showLegend;
  38. public GoogleChart(String tagId, String title, String keyName, String valueName) {
  39. this.tagId = tagId;
  40. this.dataName = StringUtils.getSHA1(title).substring(0, 8);
  41. this.title = title;
  42. this.keyName = keyName;
  43. this.valueName = valueName;
  44. values = new ArrayList<ChartValue>();
  45. showLegend = true;
  46. }
  47. public void setWidth(int width) {
  48. this.width = width;
  49. }
  50. public void setHeight(int height) {
  51. this.height = height;
  52. }
  53. public void setShowLegend(boolean val) {
  54. this.showLegend = val;
  55. }
  56. public void addValue(String name, int value) {
  57. values.add(new ChartValue(name, value));
  58. }
  59. public void addValue(String name, float value) {
  60. values.add(new ChartValue(name, value));
  61. }
  62. public void addValue(String name, double value) {
  63. values.add(new ChartValue(name, (float) value));
  64. }
  65. protected abstract void appendChart(StringBuilder sb);
  66. protected void line(StringBuilder sb, String line) {
  67. sb.append(line);
  68. sb.append('\n');
  69. }
  70. protected class ChartValue implements Serializable, Comparable<ChartValue> {
  71. private static final long serialVersionUID = 1L;
  72. final String name;
  73. final float value;
  74. ChartValue(String name, float value) {
  75. this.name = name;
  76. this.value = value;
  77. }
  78. @Override
  79. public int compareTo(ChartValue o) {
  80. // sorts the dataset by largest value first
  81. if (value > o.value) {
  82. return -1;
  83. } else if (value < o.value) {
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. }
  89. }