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