Commit 10ab8c48 by Alex Rhodes

Updated commenting/cleaned up code

parent 463789de
......@@ -2,12 +2,7 @@
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>GoogleChartsJSF</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
......
......@@ -5,11 +5,12 @@
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:gc="http://alexscottrhodes.com/facelets">
<!-- This page demonstrates how the two demo charts look when rendered -->
<h:head>
</h:head>
<h:body>
<!-- Charts are passed the type corresponding to the type of chart being rendered, and the chart object -->
<gc:googleChart type="combo" value="#{googleChartBean.combo}"/>
<gc:googleChart type="pie" value="#{googleChartBean.pie}"/>
</h:body>
......
......@@ -7,13 +7,6 @@
<packaging>war</packaging>
<name>Google Charts JSF API</name>
<description>JSF tag wrapper for google charts objects</description>
<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
......
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
version="2.0">
<namespace>http://alexscottrhodes.com/facelets</namespace>
<tag>
<tag-name>dataExporter</tag-name>
<handler-class>
com.alexscottrhodes.handler.GoogleChartTagHandler
</handler-class>
</tag>
</facelet-taglib>
\ No newline at end of file
/**
*
*/
package com.alexscottrhodes.builder;
import java.util.ArrayList;
......@@ -25,7 +22,7 @@ public class Builder {
/**
* Build the chart object and return the string to be rendered in the page
* @param gcc a GoogleChartCore object representing the chart
* @param gcc a GoogleComboChart object representing the chart
* @return a String to render
*/
public String buildComboChart(GoogleComboChart gcc){
......@@ -54,46 +51,6 @@ public class Builder {
}
//Script header dynamically checks for and loads required Google API JS files while avoiding duplicate call exceptions
int size = 0;
String header = ""+
"<script>"+
"function loader(){"+
"var s = document.createElement(\"script\");"+
"s.src = \"https://www.gstatic.com/charts/loader.js\";"+
"if (s.addEventListener){"+
"window.gChartLoaded = true;"+
"s.addEventListener(\"load\", initGlobal, false);"+
"}else if (s.readyState){"+
"window.gChartLoaded = true;"+
"s.onreadystatechange = initGlobal;"+
"}"+
"document.getElementsByTagName('head')[0].appendChild(s);"+
"}"+
"if(typeof gChartLoaded == \"undefined\"){"+
"window.gChartLoaded = false;"+
"}"+
"if(typeof google == \"undefined\" && !window.gChartLoaded){"+
"loader();"+
"}"+
"function initGlobal(){"+
"window.gChartPackageLoaded = true;"+
"google.charts.load('current', {'packages':['corechart']});"+
"}"+
"</script>"+
"<script>"+
"var asyncLoopA = function(o){"+
"var loop = function(){"+
"if(typeof google != \"undefined\"){o.callback(); return;}"+
"o.asyncLoopFunc(loop);"+
"}\n"+
"loop();"+
"}\n"+
"asyncLoopA({"+
"asyncLoopFunc : function(loop){"+
" setTimeout(function(){loop();},0);"+
"},"+
"callback : function(){"+
" google.charts.setOnLoadCallback(function(){";
//Data section of the chart
String dataRows="var data = google.visualization.arrayToDataTable([";
String colNames = "[";
......@@ -187,7 +144,11 @@ public class Builder {
return result;
}
/**
* Build the chart object and return the string to be rendered in the page
* @param gcc a GooglePieChart object representing the chart
* @return a String to render
*/
public String buildPieChart(GooglePieChart gpc){
//verify data was named
if(gpc.getDataName()==null){
......@@ -202,49 +163,6 @@ public class Builder {
if(gpc.getDonutHoleSize()!=0 && (gpc.getDonutHoleSize()<0.2 || gpc.getDonutHoleSize() > 1)){
throw new PieChartException("Donut hole size invalid.");
}
//Script header dynamically checks for and loads required Google API JS files while avoiding duplicate call exceptions
int size = 0;
String header = ""+
"<script>"+
"function loader(){"+
"var s = document.createElement(\"script\");"+
"s.src = \"https://www.gstatic.com/charts/loader.js\";"+
"if (s.addEventListener){"+
"window.gChartLoaded = true;"+
"s.addEventListener(\"load\", initGlobal, false);"+
"}else if (s.readyState){"+
"window.gChartLoaded = true;"+
"s.onreadystatechange = initGlobal;"+
"}"+
"document.getElementsByTagName('head')[0].appendChild(s);"+
"}"+
"if(typeof gChartLoaded == \"undefined\"){"+
"window.gChartLoaded = false;"+
"}"+
"if(typeof google == \"undefined\" && !window.gChartLoaded){"+
"loader();"+
"}"+
"function initGlobal(){"+
"window.gChartPackageLoaded = true;"+
"google.charts.load('current', {'packages':['corechart']});"+
"}"+
"</script>"+
"<script>"+
"var asyncLoopA = function(o){"+
"var loop = function(){"+
"if(typeof google != \"undefined\"){o.callback(); return;}"+
"o.asyncLoopFunc(loop);"+
"}\n"+
"loop();"+
"}\n"+
"asyncLoopA({"+
"asyncLoopFunc : function(loop){"+
" setTimeout(function(){loop();},0);"+
"},"+
"callback : function(){"+
" google.charts.setOnLoadCallback(function(){";
//Data section of the chart
String dataRows="var data = google.visualization.arrayToDataTable([";
String colNames = "['" + gpc.getDataName() + "','"+gpc.getDataType() + "'],";
......@@ -261,7 +179,7 @@ public class Builder {
dataRows = dataRows.substring(0,dataRows.lastIndexOf(','));
dataRows += "]);";
//Options seciton of the chart
//Options section of the chart
String options = "var options = {";
if(gpc.getTitle() != null && gpc.getTitle() != ""){
options += "title: '" + gpc.getTitle() + "',";
......@@ -277,10 +195,20 @@ public class Builder {
options+="pieHole:" + gpc.getDonutHoleSize() +",";
}
options+= "is3d:" + gpc.isIs3d() + ",";
if(!gpc.isShowSliceText()){
options+= "pieSliceText : 'none'";
}
String slices = "slices : {";
for(int i = 0; i < gpc.getSlices().size();i++){
PieSlice p = gpc.getSlices().get(i);
slices+= i+": {offset:" + p.getOffset() + "},";
if(p.isVisible()==false){
p.setColor("transparent");
}
slices+= i+": {offset:" + p.getOffset();
if(p.getColor()!="" && p.getColor() != null){
slices += ", color:'" + p.getColor() + "'";
}
slices += "},";
}
slices = slices.substring(0, slices.lastIndexOf(","));
slices += "}";
......@@ -305,11 +233,50 @@ public class Builder {
"</script>"+
"<div style='width:"+width+"px;height:"+height+"px;clear:both' id='"+gpc.getId()+"_pieChartTarget'></div>";
String result = header + dataRows + options + footer;
System.out.println(result);
return result;
}
//Large vars stored here for code readability
String header = ""+
"<script>"+
"function loader(){"+
"var s = document.createElement(\"script\");"+
"s.src = \"https://www.gstatic.com/charts/loader.js\";"+
"if (s.addEventListener){"+
"window.gChartLoaded = true;"+
"s.addEventListener(\"load\", initGlobal, false);"+
"}else if (s.readyState){"+
"window.gChartLoaded = true;"+
"s.onreadystatechange = initGlobal;"+
"}"+
"document.getElementsByTagName('head')[0].appendChild(s);"+
"}"+
"if(typeof gChartLoaded == \"undefined\"){"+
"window.gChartLoaded = false;"+
"}"+
"if(typeof google == \"undefined\" && !window.gChartLoaded){"+
"loader();"+
"}"+
"function initGlobal(){"+
"window.gChartPackageLoaded = true;"+
"google.charts.load('current', {'packages':['corechart']});"+
"}"+
"</script>"+
"<script>"+
"var asyncLoopA = function(o){"+
"var loop = function(){"+
"if(typeof google != \"undefined\"){o.callback(); return;}"+
"o.asyncLoopFunc(loop);"+
"}\n"+
"loop();"+
"}\n"+
"asyncLoopA({"+
"asyncLoopFunc : function(loop){"+
" setTimeout(function(){loop();},0);"+
"},"+
"callback : function(){"+
" google.charts.setOnLoadCallback(function(){";
......
package com.alexscottrhodes.chartModel;
/**
* Model representing a google chart
*/
import java.util.ArrayList;
import java.util.HashMap;
import com.alexscottrhodes.constructionModel.ChartSeries;
import com.alexscottrhodes.enums.AxisType;
import com.alexscottrhodes.enums.CurveType;
/**
* Model of the Google Combo chart object
* @author Alex Rhodes
*
*/
public class GoogleComboChart {
private String id;
private String title;
......
......@@ -3,7 +3,11 @@ package com.alexscottrhodes.chartModel;
import java.util.ArrayList;
import com.alexscottrhodes.chartModel.components.PieSlice;
/**
* Model of the Google Pie Chart object
* @author Alex Rhodes
*
*/
public class GooglePieChart {
String id;
private String title;
......@@ -15,6 +19,7 @@ public class GooglePieChart {
private int width;
private int height;
private boolean is3d;
private boolean showSliceText = true;
private double donutHoleSize;
private int rotation;
......@@ -100,6 +105,14 @@ public class GooglePieChart {
return donutHoleSize;
}
public boolean isShowSliceText() {
return showSliceText;
}
public void setShowSliceText(boolean showSliceText) {
this.showSliceText = showSliceText;
}
}
package com.alexscottrhodes.chartModel.components;
/**
* Model of a Google Pie Slice object
* @author Alex Rhodes
*
*/
public class PieSlice {
private double value;
private String name;
private boolean visible = true;
private String color;
private boolean showText = true;
private double offset;
public PieSlice(String name, double value){
......@@ -28,12 +32,6 @@ public class PieSlice {
public void setColor(String color) {
this.color = color;
}
public boolean isShowText() {
return showText;
}
public void setShowText(boolean showText) {
this.showText = showText;
}
public void setOffset(double offset) {
this.offset = offset;
}
......
/**
*
*/
package com.alexscottrhodes.constructionModel;
import java.text.SimpleDateFormat;
......
package com.alexscottrhodes.demo;
import java.util.ArrayList;
......@@ -13,8 +14,16 @@ import com.alexscottrhodes.enums.AxisType;
import com.alexscottrhodes.enums.CurveType;
import com.alexscottrhodes.enums.SeriesDisplayType;
import com.alexscottrhodes.enums.SeriesType;
/**
* Demonstration of the Google Chart object construction
* @author Alex Rhodes
*
*/
public class Charts {
/**
* Build a Combo chart
* @return a GoogleComboChart
*/
public static GoogleComboChart getComboChart() {
GoogleComboChart gcc;
gcc = new GoogleComboChart();
......@@ -57,7 +66,10 @@ public class Charts {
return gcc;
}
/**
* Build a Google Pie Chart
* @return a Google Pie Chart object
*/
public static GooglePieChart getPieChart(){
GooglePieChart gpc = new GooglePieChart("Activity","Time Spent");
gpc.setDataName("Activity");
......
......@@ -6,13 +6,20 @@ import javax.faces.bean.ViewScoped;
import com.alexscottrhodes.chartModel.GoogleComboChart;
import com.alexscottrhodes.chartModel.GooglePieChart;
/**
* A demo of the Google Chart JSF wrapper
* @author Alex
*
*/
@ManagedBean
@ViewScoped
public class GoogleChartBean {
private GoogleComboChart gcc;
private GooglePieChart gpc;
/**
* Build some charts
*/
@PostConstruct
public void init(){
gcc = Charts.getComboChart();
......@@ -20,7 +27,7 @@ public class GoogleChartBean {
}
//Return the chart to render
//Return the charts to render
public GoogleComboChart getCombo(){
return gcc;
}
......
......@@ -2,7 +2,7 @@ package com.alexscottrhodes.enums;
/**
* An Enum for the format to display the data of a chart series
* @author arhodes
* @author Alex Rhodes
*
*/
public enum SeriesDisplayType {
......
package com.alexscottrhodes.enums;
/**
* An enum to represent the type of data of a given chart series
* @author arhodes
* @author Alex Rhodes
*
*/
public enum SeriesType {
......
/**
*
*/
package com.alexscottrhodes.exceptions;
/**
......
package com.alexscottrhodes.exceptions;
/**
* A generic exception involving a chart object, designed to be thrown with an informative message
* @author arhodes
* @author Alex Rhodes
*
*/
public class ChartException extends RuntimeException{
......
/**
*
*/
package com.alexscottrhodes.handler;
import java.io.IOException;
......
#Generated by Maven Integration for Eclipse
#Mon Jul 25 17:13:43 MST 2016
#Wed Jul 27 17:24:09 MST 2016
version=0.0.1-SNAPSHOT
groupId=com.alexscottrhodes
m2e.projectName=googlechartjsf
......
......@@ -7,13 +7,6 @@
<packaging>war</packaging>
<name>Google Charts JSF API</name>
<description>JSF tag wrapper for google charts objects</description>
<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.3</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment