Commit 2cbb6532 by Alex Rhodes

cleaned up, jdoc comments, made demo etc.

parent 0512cdcc
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.compiler.source=1.7
......@@ -10,7 +10,7 @@
<h:head>
</h:head>
<h:body>
<gc:googleChart type="line" value="#{googleChartBean.lineChart}" rendered = "false"/>
<gc:googleChart type="combo" value="#{googleChartBean.chart}"/>
</h:body>
</html>
......@@ -29,8 +29,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
......
......@@ -11,19 +11,34 @@ import com.alexscottrhodes.constructionModel.ChartSeries;
import com.alexscottrhodes.enums.AxisType;
import com.alexscottrhodes.enums.SeriesType;
import com.alexscottrhodes.exceptions.AxesException;
import com.alexscottrhodes.exceptions.ChartException;
/**
* This class contains methods to render the chart object JS
* @author Alex Rhodes
*
*/
public class Builder {
public String buildComboChart(GoogleChartCore gcc) throws Exception{
/**
* Build the chart object and return the string to be rendered in the page
* @param gcc a GoogleChartCore object representing the chart
* @return a String to render
*/
public String buildComboChart(GoogleChartCore gcc){
//Verify at least one Axis is provided
if(gcc.getAxes().get(AxisType.X).size()==0){
throw new AxesException("No x axis provided.");
}
//Verify at least one data series provided
if(gcc.getSeriesList().size()<1){
throw new ChartException("No data provided.");
}
ArrayList<ChartSeries> data = new ArrayList<ChartSeries>();
data.addAll(gcc.getAxes().get(AxisType.X));
data.addAll(gcc.getSeriesList());
//Check for/generate an index line
if(gcc.getIndexLineVal() != 0){
ChartSeries index = new ChartSeries(SeriesType.NUMBER);
int iter = gcc.getAxes().get(AxisType.X).get(0).getDataset().size();
......@@ -34,6 +49,7 @@ public class Builder {
data.add(index);
}
//Script header dynamically checks for and loads required Google API JS files while avoiding duplicate call exceptions
int size = 0;
String header = ""+
"<script>"+
......@@ -75,6 +91,7 @@ public class Builder {
"callback : function(){"+
" google.charts.setOnLoadCallback(function(){";
//Data section of the chart
String dataRows="var data = google.visualization.arrayToDataTable([";
String colNames = "[";
for(ChartSeries s : data){
......@@ -108,6 +125,7 @@ public class Builder {
dataRows = dataRows.substring(0,dataRows.lastIndexOf(','));
dataRows += "]);";
//Options seciton of the chart
String options = "var options = {";
if(gcc.getTitle() != null && gcc.getTitle() != ""){
options += "title: '" + gcc.getTitle() + "',";
......@@ -132,6 +150,7 @@ public class Builder {
}
options += "hAxis: {title:'"+x.getLabel()+"'},";
options += "vAxis: {title:'"+yLabel+"'},";
options += "curveType: '" + gcc.getCurveType().toString() + "',";
options += "seriesType: '" + x.getDisplayType().toString() + "',";
String innerSeries ="series:{";
for(int i = 0; i < gcc.getSeriesList().size(); i++){
......@@ -144,7 +163,15 @@ public class Builder {
innerSeries = innerSeries.substring(0,innerSeries.lastIndexOf(','));
innerSeries +="}";
options += innerSeries + "};";
//Footer contains dynamically ID'd div to render the chart in
int height = gcc.getHeight();
int width = gcc.getWidth();
if(height == 0){
height = 500;
}
if(width == 0){
width = 1100;
}
String footer = "" +
"var chart = new google.visualization.ComboChart(document.getElementById('j_id_comboChartTarget'));"+
"chart.draw(data,options);"+
......@@ -152,7 +179,7 @@ public class Builder {
"} "+
"}); "+
"</script>"+
"<div style='width:1400;height:700;clear:both' id='j_id_comboChartTarget'></div>";
"<div style='width:"+width+"px;height:"+height+"px;clear:both' id='j_id_comboChartTarget'></div>";
String result = header + dataRows + options + footer;
return result;
}
......
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;
public class GoogleChartCore {
private String id;
......@@ -18,6 +22,7 @@ public class GoogleChartCore {
private String titleColor;
private boolean titleBold;
private String verticalAxisLabel;
private CurveType curveType = CurveType.DEFAULT;
public GoogleChartCore(){
seriesList = new ArrayList<ChartSeries>();
......@@ -25,15 +30,35 @@ public class GoogleChartCore {
axes.put(AxisType.X, new ArrayList<ChartSeries>());
axes.put(AxisType.Y, new ArrayList<ChartSeries>());
}
/**
* Create an index line to be displayed across the chart
* @param value an Int of the Y value to draw the index line
* @param label a String to display as the label for the index line
*/
public void setIndexLine(int value, String label) {
this.indexLineVal = value;
this.indexLineLabel = label;
}
/**
* Add a data series to the chart
* @param series a ChartSeries to be added to the chart
*/
public void addSeries(ChartSeries series){
seriesList.add(series);
}
/**
* Add a data series to be used as an axis
* @param series a ChartSeries object to be added as an axis
* @param axis an AxisType specifying which axis to use the series on
*/
public void addAxisSeries(ChartSeries series, AxisType axis){
axes.get(axis).add(series);
}
/**
* Getters and Setters
*/
public String getId() {
return id;
}
......@@ -96,12 +121,7 @@ public class GoogleChartCore {
public void setVerticalAxisLabel(String verticalAxisLabel) {
this.verticalAxisLabel = verticalAxisLabel;
}
public void setIndexLine(int value, String label) {
this.indexLineVal = value;
this.indexLineLabel = label;
}
public String getIndexLineLabel() {
return indexLineLabel;
}
......@@ -109,5 +129,11 @@ public class GoogleChartCore {
public int getIndexLineVal() {
return indexLineVal;
}
public CurveType getCurveType() {
return curveType;
}
public void setCurveType(CurveType curveType) {
this.curveType = curveType;
}
}
......@@ -11,18 +11,21 @@ import com.alexscottrhodes.enums.SeriesDisplayType;
import com.alexscottrhodes.enums.SeriesType;
/**
* An object representing a data series and related parameters to pass into a chart
* @author Alex Rhodes
*
*/
@SuppressWarnings("unchecked")
public class ChartSeries {
private String label = "";
private SeriesType type = SeriesType.STRING;
private SeriesDisplayType displayType = SeriesDisplayType.BAR;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
/**
* Constructor that requires definition of the data type being put into the chart series
* @param type a SeriesType representative of the data type of the series
*/
public ChartSeries(SeriesType type){
this.type = type;
}
......
package com.alexscottrhodes.demo;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import com.alexscottrhodes.chartModel.GoogleChartCore;
import com.alexscottrhodes.constructionModel.ChartSeries;
import com.alexscottrhodes.enums.AxisType;
import com.alexscottrhodes.enums.CurveType;
import com.alexscottrhodes.enums.SeriesDisplayType;
import com.alexscottrhodes.enums.SeriesType;
@ManagedBean
@ViewScoped
public class GoogleChartBean {
private GoogleChartCore gcc;
@PostConstruct
public void init(){
gcc = new GoogleChartCore();
gcc.setTitle("This is the Title");
gcc.setTitleBold(true); //Bolded title
gcc.setTitleColor("#59b300"); //Green title
gcc.setCurveType(CurveType.FUNCTION); //Smoothed curve
gcc.setHeight(800);
gcc.setWidth(1200);
gcc.setVerticalAxisLabel("Y-Axis vals"); //Label for Y-axis
gcc.setIndexLine(50, "Target Amount"); // add an index line
ChartSeries s = new ChartSeries(SeriesType.NUMBER); //Add a series of numbers
s.setDisplayType(SeriesDisplayType.LINE); //Make this series a line
s.setLabel("A line of values over time.");
for(Integer i : getRandVals()){
s.addPoint(i); //Populate with some random numbers
}
ChartSeries t = new ChartSeries(SeriesType.NUMBER); //Add a series of numbers
t.setDisplayType(SeriesDisplayType.BAR); //Make this series bars
t.setLabel("A second set of values over time");
for(Integer i : getRandVals()){
t.addPoint(i); //Populate with some random numbers
}
ChartSeries x = new ChartSeries(SeriesType.DATE); //Add a series of Dates
x.setDateFormat("yyyy-MM-dd"); //Format the date output
x.setLabel("Date - X-axis vals");
for(Date d : getDates()){
x.addPoint(d); //Populate with some dates
}
gcc.addSeries(s); //Add series S
gcc.addSeries(t);//Add series T
gcc.addAxisSeries(x,AxisType.X); //Add the X-axis
}
//Return the chart to render
public GoogleChartCore getChart(){
return gcc;
}
//Utility to get some random numbers
public ArrayList<Integer> getRandVals(){
Random r = new Random();
ArrayList<Integer> result = new ArrayList<Integer>();
for(int i = 0; i < 10; i++){
result.add(r.nextInt(100-10) + 10);
}
return result;
}
//Utility to get some dates
public ArrayList<Date> getDates(){
ArrayList<Date> result = new ArrayList<Date>();
Calendar c = Calendar.getInstance();
c.setTime(new Date());
for(int i = 0; i < 10; i++){
c.add(Calendar.DATE, 7);
result.add(c.getTime());
}
return result;
}
}
......@@ -4,6 +4,7 @@
package com.alexscottrhodes.enums;
/**
* An Enum representing the Axis types of the graph
* @author Alex Rhodes
*
*/
......
package com.alexscottrhodes.enums;
/**
* An Enum for the "curve type" parameter of the Google chart API;
* @author arhodes
*
*/
public enum CurveType {
FUNCTION,
DEFAULT;
FUNCTION("function"),
DEFAULT("line");
private String plainText;
CurveType(String pt){
this.plainText = pt;
}
@Override
public String toString(){
return plainText;
}
}
package com.alexscottrhodes.enums;
/**
* An Enum for the format to display the data of a chart series
* @author arhodes
*
*/
public enum SeriesDisplayType {
LINE("line"), BAR("bars"),AREA("area"), CANDLESTICKS("candlesticks"), STEPPED_AREA("steppedArea");
......
package com.alexscottrhodes.enums;
/**
* An enum to represent the type of data of a given chart series
* @author arhodes
*
*/
public enum SeriesType {
DATE("string"),STRING("string"),NUMBER("number");
......
......@@ -4,10 +4,11 @@
package com.alexscottrhodes.exceptions;
/**
* An exception thrown when problems are found with a chart's axes
* @author Alex Rhodes
*
*/
public class AxesException extends Exception{
public class AxesException extends RuntimeException{
private static final long serialVersionUID = 1L;
//No overrides
......
package com.alexscottrhodes.exceptions;
public class ChartException extends Exception{
/**
* A generic exception involving a chart object, designed to be thrown with an informative message
* @author arhodes
*
*/
public class ChartException extends RuntimeException{
private static final long serialVersionUID = 1L;
//No overrides
......
......@@ -16,7 +16,6 @@ import com.alexscottrhodes.exceptions.ChartException;
/**
* Tag handler for the google charts API wrapper.
*
* @author Alex Rhodes
*
*/
......@@ -36,18 +35,14 @@ public class GoogleChartTagHandler extends UIComponentBase{
try{
id = (String) this.getAttributes().get("id");
}catch(Exception e){
try{throw new Exception("No ID Specified");}catch(Exception ex){e.printStackTrace();}
throw new ChartException("No ID Specified");
}
//Collect parameters
String typeString = (String) getAttributes().get("type");
Object chartObject = getAttributes().get("value");
if(typeString == null){
try{
throw new ChartException("Invalid or missing chart type.");
}catch(Exception ex){
ex.printStackTrace();
}
throw new ChartException("Invalid or missing chart type.");
}
//Cast to appropriate chart model
......@@ -56,29 +51,20 @@ public class GoogleChartTagHandler extends UIComponentBase{
try{
gcc = (GoogleChartCore) chartObject;
gcc.setId(id);
}catch(Exception e){
try{
e.printStackTrace();
throw new ChartException("Invalid chart model.");
}catch(Exception ex){
ex.printStackTrace();
}
}catch(Exception e){
throw new ChartException("Invalid chart model.");
}
Builder b = new Builder();
try{
output = b.buildComboChart(gcc);
}catch(Exception e){
try{
e.printStackTrace();
throw e;}catch(Exception ex){}
throw new ChartException("Error rendering.");
}
}
else{
try{throw new ChartException("Invalid chart type:" + typeString);}catch(Exception e){}
throw new ChartException("Invalid chart type:" + typeString);
}
writer.write(output);
}
......
#Generated by Maven Integration for Eclipse
#Thu Jul 14 13:19:00 MST 2016
#Fri Jul 22 09:47:45 MST 2016
version=0.0.1-SNAPSHOT
groupId=com.alexscottrhodes
m2e.projectName=googlechartjsf
......
......@@ -29,8 +29,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</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