commit | author | age
|
44bbc4
|
1 |
/*
|
JM |
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 |
|
|
18 |
import java.text.MessageFormat;
|
|
19 |
import java.util.Collections;
|
|
20 |
|
db91a3
|
21 |
import com.gitblit.utils.StringUtils;
|
JM |
22 |
|
44bbc4
|
23 |
/**
|
JM |
24 |
* Builds an interactive pie chart using the Visualization API.
|
|
25 |
*
|
|
26 |
* @author James Moger
|
|
27 |
*
|
|
28 |
*/
|
|
29 |
public class GooglePieChart extends GoogleChart {
|
|
30 |
|
|
31 |
private static final long serialVersionUID = 1L;
|
|
32 |
|
|
33 |
public GooglePieChart(String tagId, String title, String keyName, String valueName) {
|
|
34 |
super(tagId, title, keyName, valueName);
|
|
35 |
}
|
|
36 |
|
|
37 |
@Override
|
|
38 |
protected void appendChart(StringBuilder sb) {
|
|
39 |
// create dataset
|
|
40 |
String dName = "data_" + dataName;
|
|
41 |
line(sb, MessageFormat.format("var {0} = new google.visualization.DataTable();", dName));
|
|
42 |
line(sb, MessageFormat.format("{0}.addColumn(''string'', ''{1}'');", dName, keyName));
|
|
43 |
line(sb, MessageFormat.format("{0}.addColumn(''number'', ''{1}'');", dName, valueName));
|
|
44 |
line(sb, MessageFormat.format("{0}.addRows({1,number,0});", dName, values.size()));
|
|
45 |
|
|
46 |
Collections.sort(values);
|
|
47 |
|
db91a3
|
48 |
StringBuilder colors = new StringBuilder("colors:[");
|
44bbc4
|
49 |
for (int i = 0; i < values.size(); i++) {
|
JM |
50 |
ChartValue value = values.get(i);
|
db91a3
|
51 |
colors.append('\'');
|
JM |
52 |
colors.append(StringUtils.getColor(value.name));
|
|
53 |
colors.append('\'');
|
|
54 |
if (i < values.size() - 1) {
|
|
55 |
colors.append(',');
|
|
56 |
}
|
44bbc4
|
57 |
line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 0, ''{2}'');", dName, i,
|
JM |
58 |
value.name));
|
|
59 |
line(sb, MessageFormat.format("{0}.setValue({1,number,0}, 1, {2,number,0.0});", dName,
|
|
60 |
i, value.value));
|
|
61 |
}
|
db91a3
|
62 |
colors.append(']');
|
44bbc4
|
63 |
|
JM |
64 |
// instantiate chart
|
|
65 |
String cName = "chart_" + dataName;
|
|
66 |
line(sb, MessageFormat.format(
|
|
67 |
"var {0} = new google.visualization.PieChart(document.getElementById(''{1}''));",
|
|
68 |
cName, tagId));
|
|
69 |
line(sb,
|
|
70 |
MessageFormat
|
db91a3
|
71 |
.format("{0}.draw({1}, '{'width: {2,number,0}, height: {3,number,0}, chartArea:'{'left:20,top:20'}', title: ''{4}'', {5} '}');",
|
JM |
72 |
cName, dName, width, height, title, colors.toString()));
|
44bbc4
|
73 |
line(sb, "");
|
JM |
74 |
}
|
|
75 |
} |