001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package jsx3.chart;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * Base chart class for charts that render on a cartesian plane with x and y axes. Currently only supports
024: primary x and y axes, even though there are methods pertaining to secondary axes.
025:
026: Cartesian charts can have the following additional children:
027:
028: {0,n} GridLines, will render lines aligned with the axes below or above the data series
029: {2,n} Axis, required to define the coordinate space of the cartesian plane
030: * @author Joe Walker [joe at getahead dot org]
031: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
032: */
033: public class CartesianChart extends jsx3.chart.Chart {
034: /**
035: * All reverse ajax proxies need context to work from
036: * @param scriptProxy The place we are writing scripts to
037: * @param context The script that got us to where we are now
038: */
039: public CartesianChart(Context context, String extension,
040: ScriptProxy scriptProxy) {
041: super (context, extension, scriptProxy);
042: }
043:
044: /**
045: * The instance initializer.
046: * @param name the GI name of the instance
047: * @param left left position (in pixels) of the chart relative to its parent container
048: * @param top top position (in pixels) of the chart relative to its parent container
049: * @param width width (in pixels) of the chart
050: * @param height height (in pixels) of the chart
051: */
052: public CartesianChart(String name, int left, int top, int width,
053: int height) {
054: super ((Context) null, (String) null, (ScriptProxy) null);
055: ScriptBuffer script = new ScriptBuffer();
056: script.appendCall("new CartesianChart", name, left, top, width,
057: height);
058: setInitScript(script);
059: }
060:
061: /**
062: * Returns the array of children GridLines instances.
063: * @param callback gridLines
064: */
065: @SuppressWarnings("unchecked")
066: public void getGridLines(
067: org.directwebremoting.proxy.Callback<Object[]> callback) {
068: ScriptBuffer script = new ScriptBuffer();
069: String callbackPrefix = "";
070:
071: if (callback != null) {
072: callbackPrefix = "var reply = ";
073: }
074:
075: script.appendCall(callbackPrefix + getContextPath()
076: + "getGridLines");
077:
078: if (callback != null) {
079: String key = org.directwebremoting.extend.CallbackHelper
080: .saveCallback(callback, Object[].class);
081: script
082: .appendCall("__System.activateCallback", key,
083: "reply");
084: }
085:
086: getScriptProxy().addScript(script);
087: }
088:
089: /**
090: * Returns the primary x axis, if any.
091: * @return primaryXAxis
092: */
093: @SuppressWarnings("unchecked")
094: public jsx3.chart.Axis getPrimaryXAxis() {
095: String extension = "getPrimaryXAxis().";
096: try {
097: java.lang.reflect.Constructor<jsx3.chart.Axis> ctor = jsx3.chart.Axis.class
098: .getConstructor(Context.class, String.class,
099: ScriptProxy.class);
100: return ctor.newInstance(this , extension, getScriptProxy());
101: } catch (Exception ex) {
102: throw new IllegalArgumentException("Unsupported type: "
103: + jsx3.chart.Axis.class.getName());
104: }
105: }
106:
107: /**
108: * Returns the primary x axis, if any.
109: * @param returnType The expected return type
110: * @return primaryXAxis
111: */
112: @SuppressWarnings("unchecked")
113: public <T> T getPrimaryXAxis(Class<T> returnType) {
114: String extension = "getPrimaryXAxis().";
115: try {
116: java.lang.reflect.Constructor<T> ctor = returnType
117: .getConstructor(Context.class, String.class,
118: ScriptProxy.class);
119: return ctor.newInstance(this , extension, getScriptProxy());
120: } catch (Exception ex) {
121: throw new IllegalArgumentException(
122: "Unsupported return type: " + returnType.getName());
123: }
124: }
125:
126: /**
127: * Returns the primary y axis, if any.
128: * @return primaryYAxis
129: */
130: @SuppressWarnings("unchecked")
131: public jsx3.chart.Axis getPrimaryYAxis() {
132: String extension = "getPrimaryYAxis().";
133: try {
134: java.lang.reflect.Constructor<jsx3.chart.Axis> ctor = jsx3.chart.Axis.class
135: .getConstructor(Context.class, String.class,
136: ScriptProxy.class);
137: return ctor.newInstance(this , extension, getScriptProxy());
138: } catch (Exception ex) {
139: throw new IllegalArgumentException("Unsupported type: "
140: + jsx3.chart.Axis.class.getName());
141: }
142: }
143:
144: /**
145: * Returns the primary y axis, if any.
146: * @param returnType The expected return type
147: * @return primaryYAxis
148: */
149: @SuppressWarnings("unchecked")
150: public <T> T getPrimaryYAxis(Class<T> returnType) {
151: String extension = "getPrimaryYAxis().";
152: try {
153: java.lang.reflect.Constructor<T> ctor = returnType
154: .getConstructor(Context.class, String.class,
155: ScriptProxy.class);
156: return ctor.newInstance(this , extension, getScriptProxy());
157: } catch (Exception ex) {
158: throw new IllegalArgumentException(
159: "Unsupported return type: " + returnType.getName());
160: }
161: }
162:
163: /**
164: * Returns the range for axis, delegates to getXRange() or getYRange().
165: * @param axis
166: * @param callback [min,max] or null if no range can be found
167: */
168: @SuppressWarnings("unchecked")
169: public void getRangeForAxis(jsx3.chart.Axis axis,
170: org.directwebremoting.proxy.Callback<Object[]> callback) {
171: ScriptBuffer script = new ScriptBuffer();
172: String callbackPrefix = "";
173:
174: if (callback != null) {
175: callbackPrefix = "var reply = ";
176: }
177:
178: script.appendCall(callbackPrefix + getContextPath()
179: + "getRangeForAxis", axis);
180:
181: if (callback != null) {
182: String key = org.directwebremoting.extend.CallbackHelper
183: .saveCallback(callback, Object[].class);
184: script
185: .appendCall("__System.activateCallback", key,
186: "reply");
187: }
188:
189: getScriptProxy().addScript(script);
190: }
191:
192: /**
193: * Returns the range of x values in the data provider, subclasses must implement.
194: * @param series the series to consider
195: * @param callback [min,max] or null if no range can be found
196: */
197: @SuppressWarnings("unchecked")
198: public void getXRange(Object[] series,
199: org.directwebremoting.proxy.Callback<Object[]> callback) {
200: ScriptBuffer script = new ScriptBuffer();
201: String callbackPrefix = "";
202:
203: if (callback != null) {
204: callbackPrefix = "var reply = ";
205: }
206:
207: script.appendCall(callbackPrefix + getContextPath()
208: + "getXRange", series);
209:
210: if (callback != null) {
211: String key = org.directwebremoting.extend.CallbackHelper
212: .saveCallback(callback, Object[].class);
213: script
214: .appendCall("__System.activateCallback", key,
215: "reply");
216: }
217:
218: getScriptProxy().addScript(script);
219: }
220:
221: /**
222: * Returns the range of y values in the data provider, subclasses must implement.
223: * @param series the series to consider
224: * @param callback [min,max] or null if no range can be found
225: */
226: @SuppressWarnings("unchecked")
227: public void getYRange(Object[] series,
228: org.directwebremoting.proxy.Callback<Object[]> callback) {
229: ScriptBuffer script = new ScriptBuffer();
230: String callbackPrefix = "";
231:
232: if (callback != null) {
233: callbackPrefix = "var reply = ";
234: }
235:
236: script.appendCall(callbackPrefix + getContextPath()
237: + "getYRange", series);
238:
239: if (callback != null) {
240: String key = org.directwebremoting.extend.CallbackHelper
241: .saveCallback(callback, Object[].class);
242: script
243: .appendCall("__System.activateCallback", key,
244: "reply");
245: }
246:
247: getScriptProxy().addScript(script);
248: }
249:
250: }
|