001    package railo.runtime.tag;
002    
003    import java.awt.Color;
004    
005    import javax.servlet.jsp.JspException;
006    import javax.servlet.jsp.tagext.Tag;
007    
008    import railo.commons.color.ColorCaster;
009    import railo.commons.lang.StringUtil;
010    import railo.runtime.exp.ApplicationException;
011    import railo.runtime.exp.ExpressionException;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.ext.tag.BodyTagImpl;
014    import railo.runtime.op.Caster;
015    import railo.runtime.type.List;
016    import railo.runtime.type.Query;
017    
018    public final class Chartseries extends BodyTagImpl {
019            
020            private ChartSeriesBean series=new ChartSeriesBean();
021            private String itemColumn;
022            private Query query;
023            private String valueColumn;
024            
025            /**
026             *
027             * @see railo.runtime.ext.tag.BodyTagImpl#release()
028             */
029            public void release() {
030                    super.release();
031                    series=new ChartSeriesBean();
032                    itemColumn=null;
033                    valueColumn=null;
034                    query=null;
035            }
036            /**
037             * @param colorlist the colorlist to set
038             * @throws ExpressionException 
039             */
040            public void setColorlist(String strColorlist) throws ExpressionException {
041                    String[] arr=List.listToStringArray(strColorlist.trim(),',');
042                    Color[] colorlist=new Color[arr.length];
043                    for(int i=0;i<arr.length;i++) {
044                            colorlist[i]=ColorCaster.toColor(arr[i]);
045                    }
046                    series.setColorlist(colorlist);
047            }
048            /**
049             * @param dataLabelStyle the dataLabelStyle to set
050             * @throws ExpressionException 
051             */
052            public void setDatalabelstyle(String strDataLabelStyle) throws ExpressionException {
053                    strDataLabelStyle=strDataLabelStyle.trim().toLowerCase();
054                    
055                    if("none".equals(strDataLabelStyle))                    series.setDataLabelStyle(ChartSeriesBean.DATA_LABEL_STYLE_NONE);
056                    else if("value".equals(strDataLabelStyle))              series.setDataLabelStyle(ChartSeriesBean.DATA_LABEL_STYLE_VALUE);
057                    else if("rowlabel".equals(strDataLabelStyle))   series.setDataLabelStyle(ChartSeriesBean.DATA_LABEL_STYLE_ROWLABEL);
058                    else if("columnlabel".equals(strDataLabelStyle))series.setDataLabelStyle(ChartSeriesBean.DATA_LABEL_STYLE_COLUMNLABEL);
059                    else if("pattern".equals(strDataLabelStyle))    series.setDataLabelStyle(ChartSeriesBean.DATA_LABEL_STYLE_PATTERN);
060                    
061                    else throw new ExpressionException("invalid value ["+strDataLabelStyle+"] for attribute dataLabelStyle, for this attribute only the following values are supported " +
062                                    "[none, value, rowlabel, columnlabel, pattern]");
063            }
064            /**
065             * @param itemColumn the itemColumn to set
066             */
067            public void setItemcolumn(String itemColumn) {
068                    this.itemColumn=itemColumn;
069            }
070            /**
071             * @param markerStyle the markerStyle to set
072             * @throws ExpressionException 
073             */
074            public void setMarkerstyle(String strMarkerStyle) throws ExpressionException {
075                    strMarkerStyle=strMarkerStyle.trim().toLowerCase();
076                    
077                    if("circle".equals(strMarkerStyle))                     series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_CIRCLE);
078                    else if("diamond".equals(strMarkerStyle))       series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_DIAMOND);
079                    else if("letter".equals(strMarkerStyle))        series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_LETTER);
080                    else if("mcross".equals(strMarkerStyle))        series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_MCROSS);
081                    else if("rcross".equals(strMarkerStyle))        series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_RCROSS);
082                    else if("rectangle".equals(strMarkerStyle))     series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_RECTANGLE);
083                    else if("snow".equals(strMarkerStyle))          series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_SNOW);
084                    else if("triangle".equals(strMarkerStyle))      series.setMarkerStyle(ChartSeriesBean.MARKER_STYLE_TRIANGLE);
085                    
086                    else throw new ExpressionException("invalid value ["+strMarkerStyle+"] for attribute markerStyle, for this attribute only the following values are supported " +
087                                    "[circle, diamond, letter, mcross, rcross, rectangle, snow, triangle]");
088            }
089            /**
090             * @param paintStyle the paintStyle to set
091             * @throws ExpressionException 
092             */
093            public void setPaintstyle(String strPaintStyle) throws ExpressionException {
094                    strPaintStyle=strPaintStyle.trim().toLowerCase();
095                    
096                    if("light".equals(strPaintStyle))               series.setPaintStyle(ChartSeriesBean.PAINT_STYLE_LIGHT);
097                    else if("plain".equals(strPaintStyle))  series.setPaintStyle(ChartSeriesBean.PAINT_STYLE_PLAIN);
098                    else if("raise".equals(strPaintStyle))  series.setPaintStyle(ChartSeriesBean.PAINT_STYLE_RAISE);
099                    else if("shade".equals(strPaintStyle))  series.setPaintStyle(ChartSeriesBean.PAINT_STYLE_SHADE);
100                    
101                    else throw new ExpressionException("invalid value ["+strPaintStyle+"] for attribute paintStyle, for this attribute only the following values are supported " +
102                                    "[light, plain, raise, shade]");
103            }
104            /**
105             * @param query the query to set
106             * @throws PageException 
107             */
108            public void setQuery(Object oQuery) throws PageException {
109                    if(oQuery instanceof Query) this.query=(Query)oQuery;
110                    else if(oQuery instanceof String) this.query=pageContext.getQuery((String)oQuery);
111                    else query=Caster.toQuery(oQuery);
112            }
113            /**
114             * @param seriesColor the seriesColor to set
115             * @throws ExpressionException 
116             */
117            public void setSeriescolor(String strSeriesColor) throws ExpressionException {
118                    series.setSeriesColor(ColorCaster.toColor(strSeriesColor));
119            }
120            /**
121             * @param seriesLabel the seriesLabel to set
122             */
123            public void setSerieslabel(String seriesLabel) {
124                    series.setSeriesLabel(seriesLabel);
125            }
126            /**
127             * @param type the type to set
128             * @throws ExpressionException 
129             */
130            public void setType(String strType) throws ExpressionException {
131                    strType=strType.trim().toLowerCase();
132                    
133                    if("area".equals(strType))                                      series.setType(ChartSeriesBean.TYPE_AREA);
134                    else if("bar".equals(strType))                          series.setType(ChartSeriesBean.TYPE_BAR);
135                    else if("cone".equals(strType))                         series.setType(ChartSeriesBean.TYPE_CONE);
136                    else if("curve".equals(strType))                        series.setType(ChartSeriesBean.TYPE_CURVE);
137                    else if("cylinder".equals(strType))                     series.setType(ChartSeriesBean.TYPE_CYLINDER);
138                    else if("horizontalbar".equals(strType))        series.setType(ChartSeriesBean.TYPE_HORIZONTALBAR);
139                    else if("line".equals(strType))                         series.setType(ChartSeriesBean.TYPE_LINE);
140                    else if("timeline".equals(strType))                             series.setType(ChartSeriesBean.TYPE_TIME);
141                    else if("time".equals(strType))                         series.setType(ChartSeriesBean.TYPE_TIME);
142                    else if("pie".equals(strType))                          series.setType(ChartSeriesBean.TYPE_PIE);
143                    else if("pyramid".equals(strType))                      series.setType(ChartSeriesBean.TYPE_PYRAMID);
144                    else if("scatter".equals(strType))                      series.setType(ChartSeriesBean.TYPE_SCATTER);
145                    else if("scatte".equals(strType))                       series.setType(ChartSeriesBean.TYPE_SCATTER);
146                    else if("step".equals(strType))                         series.setType(ChartSeriesBean.TYPE_STEP);
147                    
148                    else throw new ExpressionException("invalid value ["+strType+"] for attribute type, for this attribute only the following values are supported " +
149                                    "[area, bar, cone, curve, cylinder, horizontalbar, line,pie,pyramid,scatter,step,timeline]");
150            }
151            /**
152             * @param valueColumn the valueColumn to set
153             */
154            public void setValuecolumn(String valueColumn) {
155                    this.valueColumn=valueColumn;
156            }
157    
158            public void addChartData(ChartDataBean data) {
159                    series.addChartData(data);
160            }
161            
162    
163            public int doStartTag() {
164                    return EVAL_BODY_INCLUDE;
165            }
166    
167            /**
168             *
169             * @see railo.runtime.ext.tag.TagImpl#doEndTag()
170             */
171            public int doEndTag() throws JspException {
172    
173                    ChartDataBean data;
174                    
175                    if(query!=null) {
176                            if(StringUtil.isEmpty(itemColumn)) throw new ApplicationException("attribute itemColumn is required for tag cfchartseries when attribute query is defined");
177                            if(StringUtil.isEmpty(valueColumn)) throw new ApplicationException("attribute valueColumn is required for tag cfchartseries when attribute query is defined");
178                            
179                            int rowCount = query.getRecordcount();
180                            for(int i=1;i<=rowCount;i++) {
181                                    data=new ChartDataBean();
182                                    data.setValue(Caster.toDoubleValue(query.getAt(valueColumn, i, new Double(0))));
183                                    data.setItem(pageContext,query.getAt(itemColumn, i, ""));
184                                    //data.setItem(itemToString(query.getAt(itemColumn, i, "")));
185                                    addChartData(data);
186                            }
187                    }
188                    // get parent chart
189                    Tag parent=this;
190                    do{
191                            parent = parent.getParent();
192                            if(parent instanceof Chart) {
193                                    ((Chart)parent).addChartSeries(series);
194                                    break;
195                            }
196                    }
197                    while(parent!=null);
198                    return EVAL_PAGE;
199            }
200    }