0
|
1 /*
|
|
2 * PieChartWidget.js
|
|
3 *
|
|
4 * Copyright (c) 2013, Sebastian Kruse. All rights reserved.
|
|
5 *
|
|
6 * This library is free software; you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
8 * License as published by the Free Software Foundation; either
|
|
9 * version 3 of the License, or (at your option) any later version.
|
|
10 *
|
|
11 * This library is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * Lesser General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
17 * License along with this library; if not, write to the Free Software
|
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
19 * MA 02110-1301 USA
|
|
20 */
|
|
21
|
|
22 /**
|
|
23 * @class PieChartWidget
|
|
24 * PieChartWidget Implementation
|
|
25 * @author Sebastian Kruse (skruse@mpiwg-berlin.mpg.de)
|
|
26 *
|
|
27 * @param {WidgetWrapper} core wrapper for interaction to other widgets
|
|
28 * @param {HTML object} div parent div to append the PieChart widget div
|
|
29 * @param {JSON} options user specified configuration that overwrites options in PieChartConfig.js
|
|
30 */
|
|
31 PieChartWidget = function(core, div, options) {
|
|
32
|
|
33 this.datasets;
|
|
34 this.selected;
|
|
35 this.core = core;
|
|
36 this.core.setWidget(this);
|
|
37
|
|
38 this.options = (new PieChartConfig(options)).options;
|
|
39 this.gui = new PieChartGui(this, div, this.options);
|
|
40
|
|
41 this.pieCharts = [];
|
|
42 }
|
|
43
|
|
44 PieChartWidget.prototype = {
|
|
45
|
|
46 addCategorizedPieChart : function(watchedDataset, watchedColumn, type, categories){
|
|
47 var selectionFunction;
|
|
48 if (type === "text"){
|
|
49 //create selection function for the pie chart
|
|
50 var selectionFunction = function(columnData){
|
|
51 var categoryLabel;
|
|
52 $(categories).each(function(){
|
|
53 if ($.inArray(columnData,this.values) != -1){
|
|
54 categoryLabel = this.label;
|
|
55 //exit .each
|
|
56 return false;
|
|
57 }
|
|
58 if (typeof categoryLabel !== "undefined")
|
|
59 return false;
|
|
60 });
|
|
61
|
|
62 if (typeof categoryLabel === "undefined")
|
|
63 categoryLabel = "unknown";
|
|
64
|
|
65 return categoryLabel;
|
|
66 };
|
|
67 } else if (type === "numeral"){
|
|
68 //create selection function for the pie chart
|
|
69 var selectionFunction = function(columnData){
|
|
70 var categoryLabel;
|
|
71 var columnDataNumeric = parseFloat(columnData);
|
|
72 for (var i = 0; i < categories.length; i++){
|
|
73 if (columnDataNumeric<=categories[i]){
|
|
74 categoryLabel = pieChartCategoryChooser.columnName + "<=" + categories[i];
|
|
75 break;
|
|
76 }
|
|
77 }
|
|
78
|
|
79 if (typeof categoryLabel === "undefined")
|
|
80 categoryLabel = "unknown";
|
|
81
|
|
82 return categoryLabel;
|
|
83 };
|
|
84 } else
|
|
85 return;
|
|
86
|
|
87 //make categories easy accessible for later usage
|
|
88 selectionFunction.type = type;
|
|
89 selectionFunction.categories = categories;
|
|
90
|
|
91 this.addPieChart(watchedDataset, watchedColumn, selectionFunction);
|
|
92 },
|
|
93
|
|
94 addPieChart : function(watchedDataset, watchedColumn, selectionFunction){
|
|
95 var newPieChart = new PieChart(this, watchedDataset, watchedColumn, selectionFunction);
|
|
96 this.pieCharts.push(newPieChart);
|
|
97 if ( (typeof GeoTemConfig.datasets !== "undefined") &&
|
|
98 (GeoTemConfig.datasets.length > watchedDataset) )
|
|
99 newPieChart.initPieChart(GeoTemConfig.datasets);
|
|
100 this.redrawPieCharts(this.selected);
|
|
101 },
|
|
102
|
|
103 initWidget : function(data) {
|
|
104 var piechart = this;
|
|
105 this.datasets = data;
|
|
106 piechart.selected = [];
|
|
107 $(this.datasets).each(function(){
|
|
108 piechart.selected.push(this.objects);
|
|
109 })
|
|
110
|
|
111 this.gui.refreshColumnSelector();
|
|
112
|
|
113 $(this.pieCharts).each(function(){
|
|
114 if (this instanceof PieChart)
|
|
115 this.initPieChart(data);
|
|
116 });
|
|
117 },
|
|
118
|
|
119 redrawPieCharts : function(objects, overwrite) {
|
|
120 $(this.pieCharts).each(function(){
|
|
121 if (this instanceof PieChart){
|
|
122 if ( (typeof overwrite !== "undefined") && overwrite)
|
|
123 this.preHighlightObjects = objects;
|
|
124 this.redrawPieChart(objects);
|
|
125 }
|
|
126 });
|
|
127 },
|
|
128
|
|
129 highlightChanged : function(objects) {
|
|
130 if( !GeoTemConfig.highlightEvents ){
|
|
131 return;
|
|
132 }
|
|
133 if ( (typeof objects === "undefined") || (objects.length == 0) ){
|
|
134 return;
|
|
135 }
|
|
136 this.redrawPieCharts(objects, false);
|
|
137 },
|
|
138
|
|
139 selectionChanged : function(selection) {
|
|
140 if( !GeoTemConfig.selectionEvents ){
|
|
141 return;
|
|
142 }
|
|
143 if (!selection.valid()){
|
|
144 selection.loadAllObjects();
|
|
145 }
|
|
146 var objects = selection.objects;
|
|
147 this.selected = objects;
|
|
148 this.redrawPieCharts(objects, true);
|
|
149 },
|
|
150
|
|
151 getElementData : function(dataObject, watchedColumn, selectionFunction) {
|
|
152 var columnData;
|
|
153 if (watchedColumn.indexOf("[") === -1){
|
|
154 columnData = dataObject[watchedColumn];
|
|
155 if (typeof columnData === "undefined"){
|
|
156 columnData = dataObject.tableContent[watchedColumn];
|
|
157 };
|
|
158 } else {
|
|
159 try {
|
|
160 var columnName = watchedColumn.split("[")[0];
|
|
161 var IndexAndAttribute = watchedColumn.split("[")[1];
|
|
162 if (IndexAndAttribute.indexOf("]") != -1){
|
|
163 var arrayIndex = IndexAndAttribute.split("]")[0];
|
|
164 var attribute = IndexAndAttribute.split("]")[1];
|
|
165
|
|
166 if (typeof attribute === "undefined")
|
|
167 columnData = dataObject[columnName][arrayIndex];
|
|
168 else{
|
|
169 attribute = attribute.split(".")[1];
|
|
170 columnData = dataObject[columnName][arrayIndex][attribute];
|
|
171 }
|
|
172 }
|
|
173 } catch(e) {
|
|
174 if (typeof console !== undefined)
|
|
175 console.error(e);
|
|
176
|
|
177 delete columnData;
|
|
178 }
|
|
179 }
|
|
180
|
|
181 if ( (typeof columnData !== "undefined") && (typeof selectionFunction !== "undefined") )
|
|
182 columnData = selectionFunction(columnData);
|
|
183
|
|
184 return(columnData);
|
|
185 },
|
|
186
|
|
187 getElementsByValue : function(columnValue, watchedDataset, watchedColumn, selectionFunction) {
|
|
188 var elements = [];
|
|
189 var pieChart = this;
|
|
190
|
|
191 $(this.datasets[watchedDataset].objects).each(function(){
|
|
192 var columnData = pieChart.getElementData(this, watchedColumn, selectionFunction);
|
|
193 if (columnData === columnValue)
|
|
194 elements.push(this);
|
|
195 });
|
|
196
|
|
197 return elements;
|
|
198 },
|
|
199 };
|