summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/wicket/charting/Flotr2PieChart.java
blob: ea04db943fbee824bbad5240f39050a0968c2c47 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * 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.text.MessageFormat;

import com.gitblit.utils.StringUtils;

/**
 * Implementation of a Pie chart using the flotr2 library
 *
 * @author Tim Ryan
 *
 */
public class Flotr2PieChart extends Chart {

	private static final long serialVersionUID = 1L;

	public Flotr2PieChart(String tagId, String title, String keyName, String valueName) {
		super(tagId, title, keyName, valueName);
	}

	@Override
	protected void appendChart(StringBuilder sb) {
			
		String dName = "data_" + dataName;
		line(sb, "var selected_" + dataName + " = null;");
		line(sb, MessageFormat.format("var {0} = Flotr.draw(document.getElementById(''{1}''),", dName, tagId));
		
		// Add the data
		line(sb, "[");
		for (int i = 0; i < values.size(); i++) {
			ChartValue value = values.get(i);
			if(i > 0){
				sb.append(",");
			}
			line(sb, MessageFormat.format("'{'data : [ [0, {0}] ], label : ''{1}'', color: ''{2}'' '}'", Float.toString(value.value), value.name, StringUtils.getColor(value.name)));
		}
		line(sb, "]");
		
		// Add the options
		line(sb, ", {");
		line(sb, MessageFormat.format("title : ''{0}'',", title));
		line(sb, "fontSize : 2,");
		line(sb, "pie : {");
		line(sb, "  show : true,");
		line(sb, "  labelFormatter : function (pie, slice) {");
		line(sb, "    if(slice / pie > .05)");
		line(sb, "      return Math.round(slice / pie * 100).toString() + \"%\";");
		line(sb, "  }");
		line(sb, "}");
		line(sb, ", mouse: {");
		line(sb, "  track: true,");
		line(sb, "  lineColor: '#002060',");
		line(sb, "  trackFormatter: function (obj)");
		line(sb, "  {");
		line(sb, "    selected_" + dataName + " = obj.series.label;");
		line(sb, "    return obj.series.label + \": \" + parseInt(obj.y) + \" (\" + Math.round(obj.fraction * 100) + \"%)\";" );
		line(sb, "  }");
		line(sb, "}");
		line(sb, ", xaxis: {");
		line(sb, "  margin: false,");
		line(sb, "  showLabels: false,");
		line(sb, "  showMinorLabels: false");
		line(sb, "}");
		line(sb, ", yaxis: {");
		line(sb, "  margin: false,");
		line(sb, "  min: 20,");
		line(sb, "  showLabels: false,");
		line(sb, "  showMinorLabels: false");
		line(sb, "}");
		line(sb, ", grid: {");
		line(sb, "  verticalLines: false,");
		line(sb, "  minorVerticalLines: null,");
		line(sb, "  horizontalLines: false,");
		line(sb, "  minorHorizontalLines: null,");
		line(sb, "  outlineWidth: 0");
		line(sb, "}");
		line(sb, ", legend: {");
		if(showLegend){		
			line(sb, "  show: true");
		}
		else {
			line(sb, "  show: false");
		}
		line(sb, "}");
		line(sb, "});");
		
		if(clickUrl != null && clickUrl.isEmpty() == false){
			line(sb, MessageFormat.format("Flotr.EventAdapter.observe(document.getElementById(''{0}''), ''flotr:click'', function (mouse, a, b, c) '{'", tagId));
			line(sb, "  window.location.href = \"" + clickUrl + "\" + selected_" + dataName + ";");
			line(sb, "});"); 
		}
		 
	   
		
	}

}