--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.wicket.charting;\r
+\r
+import java.io.Serializable;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import com.gitblit.utils.StringUtils;\r
+\r
+/**\r
+ * Abstract parent class for Google Charts built with the Visualization API.\r
+ * \r
+ * @author James Moger\r
+ * \r
+ */\r
+public abstract class GoogleChart implements Serializable {\r
+\r
+ private static final long serialVersionUID = 1L;\r
+ final String tagId;\r
+ final String dataName;\r
+ final String title;\r
+ final String keyName;\r
+ final String valueName;\r
+ final List<ChartValue> values;\r
+ int width;\r
+ int height;\r
+\r
+ public GoogleChart(String tagId, String title, String keyName, String valueName) {\r
+ this.tagId = tagId;\r
+ this.dataName = StringUtils.getSHA1(title).substring(0, 8);\r
+ this.title = title;\r
+ this.keyName = keyName;\r
+ this.valueName = valueName;\r
+ values = new ArrayList<ChartValue>();\r
+ }\r
+\r
+ public void setWidth(int width) {\r
+ this.width = width;\r
+ }\r
+\r
+ public void setHeight(int height) {\r
+ this.height = height;\r
+ }\r
+\r
+ public void addValue(String name, int value) {\r
+ values.add(new ChartValue(name, value));\r
+ }\r
+\r
+ public void addValue(String name, float value) {\r
+ values.add(new ChartValue(name, value));\r
+ }\r
+\r
+ public void addValue(String name, double value) {\r
+ values.add(new ChartValue(name, (float) value));\r
+ }\r
+\r
+ protected abstract void appendChart(StringBuilder sb);\r
+\r
+ protected void line(StringBuilder sb, String line) {\r
+ sb.append(line);\r
+ sb.append('\n');\r
+ }\r
+\r
+ protected class ChartValue implements Serializable, Comparable<ChartValue> {\r
+\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ final String name;\r
+ final float value;\r
+\r
+ ChartValue(String name, float value) {\r
+ this.name = name;\r
+ this.value = value;\r
+ }\r
+\r
+ @Override\r
+ public int compareTo(ChartValue o) {\r
+ // sorts the dataset by largest value first\r
+ if (value > o.value) {\r
+ return -1;\r
+ } else if (value < o.value) {\r
+ return 1;\r
+ }\r
+ return 0;\r
+ }\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ Copyright 2011 comSysto GmbH\r
+\r
+ Licensed under the Apache License, Version 2.0 (the "License");\r
+ you may not use this file except in compliance with the License.\r
+ You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+ */\r
+\r
+package com.gitblit.wicket.charting;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.apache.wicket.markup.html.IHeaderContributor;\r
+import org.apache.wicket.markup.html.IHeaderResponse;\r
+\r
+/**\r
+ * The Google Visualization API provides interactive JavaScript based charts and\r
+ * graphs. This class implements the JavaScript header necessary to display\r
+ * complete graphs and charts.\r
+ * \r
+ * @author James Moger\r
+ * \r
+ */\r
+public class GoogleCharts implements IHeaderContributor {\r
+\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ private List<GoogleChart> charts = new ArrayList<GoogleChart>();\r
+\r
+ public void addChart(GoogleChart chart) {\r
+ charts.add(chart);\r
+ }\r
+\r
+ @Override\r
+ public void renderHead(IHeaderResponse response) {\r
+ // add Google Chart JS API reference\r
+ response.renderJavascriptReference("https://www.google.com/jsapi");\r
+\r
+ // prepare draw chart function\r
+ StringBuilder sb = new StringBuilder();\r
+ line(sb, "google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});");\r
+ line(sb, "google.setOnLoadCallback(drawChart);");\r
+ line(sb, "function drawChart() {");\r
+\r
+ // add charts to header\r
+ for (GoogleChart chart : charts) {\r
+ chart.appendChart(sb);\r
+ }\r
+\r
+ // end draw chart function\r
+ line(sb, "}");\r
+ response.renderJavascript(sb.toString(), null);\r
+ }\r
+\r
+ private void line(StringBuilder sb, String line) {\r
+ sb.append(line);\r
+ sb.append('\n');\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.wicket.charting;\r
+\r
+import java.text.MessageFormat;\r
+\r
+/**\r
+ * Builds an interactive line chart using the Visualization API.\r
+ * \r
+ * @author James Moger\r
+ * \r
+ */\r
+public class GoogleLineChart extends GoogleChart {\r
+\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ public GoogleLineChart(String tagId, String title, String keyName, String valueName) {\r
+ super(tagId, title, keyName, valueName);\r
+ }\r
+\r
+ @Override\r
+ protected void appendChart(StringBuilder sb) {\r
+ String dName = "data_" + dataName;\r
+ line(sb, MessageFormat.format("var {0} = new google.visualization.DataTable();", dName));\r
+ line(sb, MessageFormat.format("{0}.addColumn(''string'', ''{1}'');", dName, keyName));\r
+ line(sb, MessageFormat.format("{0}.addColumn(''number'', ''{1}'');", dName, valueName));\r
+ line(sb, MessageFormat.format("{0}.addRows({1,number,0});", dName, values.size()));\r
+\r
+ for (int i = 0; i < values.size(); i++) {\r
+ ChartValue value = values.get(i);\r
+ line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 0, ''{2}'');", dName, i,\r
+ value.name));\r
+ line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 1, {2,number,0.0});", dName,\r
+ i, value.value));\r
+ }\r
+\r
+ String cName = "chart_" + dataName;\r
+ line(sb, MessageFormat.format(\r
+ "var {0} = new google.visualization.LineChart(document.getElementById(''{1}''));",\r
+ cName, tagId));\r
+ line(sb,\r
+ MessageFormat\r
+ .format("{0}.draw({1}, '{'width: {2,number,0}, height: {3,number,0}, pointSize: 4, chartArea:'{'left:20,top:20'}', legend: ''none'', title: ''{4}'' '}');",\r
+ cName, dName, width, height, title));\r
+ line(sb, "");\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.wicket.charting;\r
+\r
+import java.text.MessageFormat;\r
+import java.util.Collections;\r
+\r
+/**\r
+ * Builds an interactive pie chart using the Visualization API.\r
+ * \r
+ * @author James Moger\r
+ * \r
+ */\r
+public class GooglePieChart extends GoogleChart {\r
+\r
+ private static final long serialVersionUID = 1L;\r
+\r
+ public GooglePieChart(String tagId, String title, String keyName, String valueName) {\r
+ super(tagId, title, keyName, valueName);\r
+ }\r
+\r
+ @Override\r
+ protected void appendChart(StringBuilder sb) {\r
+ // create dataset\r
+ String dName = "data_" + dataName;\r
+ line(sb, MessageFormat.format("var {0} = new google.visualization.DataTable();", dName));\r
+ line(sb, MessageFormat.format("{0}.addColumn(''string'', ''{1}'');", dName, keyName));\r
+ line(sb, MessageFormat.format("{0}.addColumn(''number'', ''{1}'');", dName, valueName));\r
+ line(sb, MessageFormat.format("{0}.addRows({1,number,0});", dName, values.size()));\r
+\r
+ Collections.sort(values);\r
+\r
+ for (int i = 0; i < values.size(); i++) {\r
+ ChartValue value = values.get(i);\r
+ line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 0, ''{2}'');", dName, i,\r
+ value.name));\r
+ line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 1, {2,number,0.0});", dName,\r
+ i, value.value));\r
+ }\r
+\r
+ // instantiate chart\r
+ String cName = "chart_" + dataName;\r
+ line(sb, MessageFormat.format(\r
+ "var {0} = new google.visualization.PieChart(document.getElementById(''{1}''));",\r
+ cName, tagId));\r
+ line(sb,\r
+ MessageFormat\r
+ .format("{0}.draw({1}, '{'width: {2,number,0}, height: {3,number,0}, chartArea:'{'left:20,top:20'}', title: ''{4}'' '}');",\r
+ cName, dName, width, height, title));\r
+ line(sb, "");\r
+ }\r
+}
\ No newline at end of file