/testability-explorer/src/main/java/com/google/test/metric/report/chart/GoogleChartAPI.java

http://testability-explorer.googlecode.com/ · Java · 142 lines · 109 code · 18 blank · 15 comment · 14 complexity · f0c6ce284c2ac1d608c1deaefe3fbbc2 MD5 · raw file

  1. /*
  2. * Copyright 2007 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.test.metric.report.chart;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.TreeSet;
  20. public class GoogleChartAPI {
  21. public static final String BASE_URL = "http://chart.apis.google.com/chart";
  22. protected final Map<String, String> keys = new HashMap<String, String>();
  23. public static final String GREEN = "00AA00";
  24. public static final String YELLOW = "FFFF00";
  25. public static final String RED = "D22222";
  26. private int width = 100;
  27. private int height = 100;
  28. public Map<String,String> getMap() {
  29. return keys;
  30. }
  31. @Override
  32. public String toString() {
  33. StringBuilder buf = new StringBuilder();
  34. buf.append(BASE_URL);
  35. String seperator = "?";
  36. for (String key : new TreeSet<String>(keys.keySet())) {
  37. String value = keys.get(key);
  38. buf.append(seperator);
  39. buf.append(key);
  40. buf.append("=");
  41. buf.append(value.replace(' ', '+'));
  42. seperator = "&";
  43. }
  44. return buf.toString();
  45. }
  46. protected String toList(String separator, String... items) {
  47. StringBuilder buf = new StringBuilder();
  48. String sep = "";
  49. for (String label : items) {
  50. buf.append(sep);
  51. buf.append(label);
  52. sep = separator;
  53. }
  54. return buf.toString();
  55. }
  56. protected String toList(String separator, int... items) {
  57. StringBuilder buf = new StringBuilder();
  58. String sep = "";
  59. for (int label : items) {
  60. buf.append(sep);
  61. buf.append(label);
  62. sep = separator;
  63. }
  64. return buf.toString();
  65. }
  66. protected String encodeT(int... values) {
  67. StringBuilder buf = new StringBuilder();
  68. String separator = "t:";
  69. for (int i : values) {
  70. buf.append(separator);
  71. buf.append(Integer.toString(i));
  72. separator = ",";
  73. }
  74. return buf.toString();
  75. }
  76. public void setSize(int width, int height) {
  77. this.width = width;
  78. this.height = height;
  79. keys.put("chs", width + "x" + height);
  80. }
  81. public void setTitle(String title) {
  82. keys.put("chtt", title);
  83. }
  84. public void setItemLabel(String... labels) {
  85. keys.put("chl", toList("|", labels));
  86. }
  87. public void setChartLabel(String... labels) {
  88. keys.put("chdl", toList("|", labels));
  89. }
  90. public void setColors(String... colors) {
  91. keys.put("chco", toList(",", colors));
  92. }
  93. public void setValues(int ... values) {
  94. keys.put("chd", encodeT(values));
  95. }
  96. public void setValues(int[]...values) {
  97. StringBuilder buf = new StringBuilder();
  98. String seperator = "s:";
  99. for (int[] dataSetValues : values) {
  100. buf.append(seperator);
  101. for (int value : dataSetValues) {
  102. buf.append(encodeS(value));
  103. }
  104. seperator = ",";
  105. }
  106. keys.put("chd", buf.toString());
  107. }
  108. protected char encodeS(int value) {
  109. if (value <= 0) {
  110. return 'A';
  111. } else if (value <= 25) {
  112. return (char) ('A' + value);
  113. } else if (value <= 51) {
  114. return (char) ('a' + value - 26);
  115. } else if (value <= 61) {
  116. return (char) ('0' + value - 52);
  117. } else {
  118. return '9';
  119. }
  120. }
  121. public String getHtml() {
  122. return String.format("<img src='%s' width='%d' height='%d'/>", toString(), width, height);
  123. }
  124. }