BrightSide Workbench Full Report + Source Code
Chart.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.charts;
20 
21 import java.util.ArrayList;
22 import org.turro.string.Strings;
23 import org.turro.elephant.context.IConstructor;
24 import org.turro.marker.ElephantMarker;
25 
30 public class Chart {
31 
33  protected ArrayList<Series> series;
34 
36  this.constructor = constructor;
37  this.series = new ArrayList<>();
38  }
39 
40  public ArrayList<Series> getSeries() {
41  return series;
42  }
43 
44  public Series addSeries(String name) {
45  Series s = new Series(name);
46  if(series.add(s)) {
47  return s;
48  }
49  return null;
50  }
51 
52  public String getPieData() {
53  StringBuilder sb = new StringBuilder();
54  for(Series s : series) {
55  if(sb.length() > 0) {
56  sb.append(",");
57  }
58  sb.append("[");
59  boolean first = true;
60  for(Data d : s.getData()) {
61  if(first) { first = false; } else { sb.append(","); }
62  sb.append("{name:\"")
63  .append(d.key)
64  .append("\",y:")
65  .append(d.getValue(0.0));
66  if(!Strings.isBlank(d.getLink())) {
67  sb.append(",events:{click:function(){location.href=\"")
68  .append(d.getLink())
69  .append("\"}}");
70  }
71  sb.append("}");
72  }
73  sb.append("]");
74  }
75  return sb.toString();
76  }
77 
78  public String getLineData() {
79  StringBuilder sb = new StringBuilder();
80  for(Series s : series) {
81  if(sb.length() > 0) {
82  sb.append(",");
83  }
84  sb.append("[");
85  boolean first = true;
86  for(Data d : s.getData()) {
87  if(first) { first = false; } else { sb.append(","); }
88  sb.append("{name:\"")
89  .append(d.key)
90  .append("\",y:")
91  .append(d.getValue(0.0));
92  if(!Strings.isBlank(d.getLink())) {
93  sb.append(",events:{click:function(){location.href=\"")
94  .append(d.getLink())
95  .append("\"}}");
96  }
97  sb.append("}");
98  }
99  sb.append("]");
100  }
101  return sb.toString();
102  }
103 
104  public String parsePie(String identifier) {
105  return parsePie(identifier, "");
106  }
107 
108  public String parseLine(String identifier) {
109  return parseLine(identifier, "");
110  }
111 
112  public String parsePie(String identifier, String title) {
113  if(series.isEmpty()) loadData();
115  marker.put("identifier", identifier);
116  marker.put("title", title);
117  marker.put("data", getPieData());
118  return marker.parse("charts", "pie");
119  }
120 
121  public String parseLine(String identifier, String title) {
122  if(series.isEmpty()) loadData();
124  marker.put("identifier", identifier);
125  marker.put("title", title);
126  marker.put("data", getLineData());
127  return marker.parse("charts", "line");
128  }
129 
130  protected void loadData() {}
131 
132 }
String getLineData()
Definition: Chart.java:78
IConstructor constructor
Definition: Chart.java:32
ArrayList< Series > series
Definition: Chart.java:33
ArrayList< Series > getSeries()
Definition: Chart.java:40
String getPieData()
Definition: Chart.java:52
Series addSeries(String name)
Definition: Chart.java:44
String parsePie(String identifier)
Definition: Chart.java:104
String parseLine(String identifier, String title)
Definition: Chart.java:121
String parsePie(String identifier, String title)
Definition: Chart.java:112
String parseLine(String identifier)
Definition: Chart.java:108
Chart(IConstructor constructor)
Definition: Chart.java:35
String parse(String rootTmpl, String tmpl)
Object put(Object key, Object value)