lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
 Copyright 2011 gitblit.com.
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http://www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
 
package com.gitblit.wicket.charting;
 
import java.util.ArrayList;
import java.util.List;
 
import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
 
/**
 * The Google Visualization API provides interactive JavaScript based charts and
 * graphs. This class implements the JavaScript header necessary to display
 * complete graphs and charts.
 * 
 * @author James Moger
 * 
 */
public class GoogleCharts implements IHeaderContributor {
 
    private static final long serialVersionUID = 1L;
 
    public final List<GoogleChart> charts = new ArrayList<GoogleChart>();
 
    public void addChart(GoogleChart chart) {
        charts.add(chart);
    }
 
    @Override
    public void renderHead(IHeaderResponse response) {
        // add Google Chart JS API reference
        response.renderJavascriptReference("https://www.google.com/jsapi");
 
        // prepare draw chart function
        StringBuilder sb = new StringBuilder();
        line(sb, "google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});");
        line(sb, "google.setOnLoadCallback(drawChart);");
        line(sb, "function drawChart() {");
 
        // add charts to header
        for (GoogleChart chart : charts) {
            chart.appendChart(sb);
        }
 
        // end draw chart function
        line(sb, "}");
        response.renderJavascript(sb.toString(), null);
    }
 
    private void line(StringBuilder sb, String line) {
        sb.append(line);
        sb.append('\n');
    }
}