PageRenderTime 46ms CodeModel.GetById 11ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/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 */
 16package com.google.test.metric.report.chart;
 17
 18import java.util.HashMap;
 19import java.util.Map;
 20import java.util.TreeSet;
 21
 22public class GoogleChartAPI {
 23
 24  public static final String BASE_URL = "http://chart.apis.google.com/chart";
 25  protected final Map<String, String> keys = new HashMap<String, String>();
 26  public static final String GREEN = "00AA00";
 27  public static final String YELLOW = "FFFF00";
 28  public static final String RED = "D22222";
 29  private int width = 100;
 30  private int height = 100;
 31
 32  public Map<String,String> getMap() {
 33    return keys;
 34  }
 35
 36  @Override
 37  public String toString() {
 38    StringBuilder buf = new StringBuilder();
 39    buf.append(BASE_URL);
 40    String seperator = "?";
 41    for (String key : new TreeSet<String>(keys.keySet())) {
 42      String value = keys.get(key);
 43      buf.append(seperator);
 44      buf.append(key);
 45      buf.append("=");
 46      buf.append(value.replace(' ', '+'));
 47      seperator = "&";
 48    }
 49    return buf.toString();
 50  }
 51
 52  protected String toList(String separator, String... items) {
 53    StringBuilder buf = new StringBuilder();
 54    String sep = "";
 55    for (String label : items) {
 56      buf.append(sep);
 57      buf.append(label);
 58      sep = separator;
 59    }
 60    return buf.toString();
 61  }
 62
 63  protected String toList(String separator, int... items) {
 64    StringBuilder buf = new StringBuilder();
 65    String sep = "";
 66    for (int label : items) {
 67      buf.append(sep);
 68      buf.append(label);
 69      sep = separator;
 70    }
 71    return buf.toString();
 72  }
 73
 74  protected String encodeT(int... values) {
 75    StringBuilder buf = new StringBuilder();
 76    String separator = "t:";
 77    for (int i : values) {
 78      buf.append(separator);
 79      buf.append(Integer.toString(i));
 80      separator = ",";
 81    }
 82    return buf.toString();
 83  }
 84
 85  public void setSize(int width, int height) {
 86    this.width = width;
 87    this.height = height;
 88    keys.put("chs", width + "x" + height);
 89  }
 90
 91  public void setTitle(String title) {
 92    keys.put("chtt", title);
 93  }
 94
 95  public void setItemLabel(String... labels) {
 96    keys.put("chl", toList("|", labels));
 97  }
 98
 99  public void setChartLabel(String... labels) {
100    keys.put("chdl", toList("|", labels));
101  }
102
103  public void setColors(String... colors) {
104    keys.put("chco", toList(",", colors));
105  }
106
107  public void setValues(int ... values) {
108    keys.put("chd", encodeT(values));
109  }
110
111  public void setValues(int[]...values) {
112    StringBuilder buf = new StringBuilder();
113    String seperator = "s:";
114    for (int[] dataSetValues : values) {
115      buf.append(seperator);
116      for (int value : dataSetValues) {
117        buf.append(encodeS(value));
118      }
119      seperator = ",";
120    }
121    keys.put("chd", buf.toString());
122  }
123
124  protected char encodeS(int value) {
125    if (value <= 0) {
126      return 'A';
127    } else if (value <= 25) {
128      return (char) ('A' + value);
129    } else if (value <= 51) {
130      return (char) ('a' + value - 26);
131    } else if (value <= 61) {
132      return (char) ('0' + value - 52);
133    } else {
134      return '9';
135    }
136  }
137
138  public String getHtml() {
139    return String.format("<img src='%s' width='%d' height='%d'/>", toString(), width, height);
140  }
141
142}