0001: /* JFreeChartEngine.java
0002:
0003: {{IS_NOTE
0004: Purpose:
0005:
0006: Description:
0007:
0008: History:
0009: Tue Aug 1 10:30:48 2006, Created by henrichen
0010: }}IS_NOTE
0011:
0012: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
0013:
0014: {{IS_RIGHT
0015: This program is distributed under GPL Version 2.0 in the hope that
0016: it will be useful, but WITHOUT ANY WARRANTY.
0017: }}IS_RIGHT
0018: */
0019: package org.zkoss.zkex.zul.impl;
0020:
0021: import org.zkoss.zul.*;
0022: import org.zkoss.zul.impl.ChartEngine;
0023: import org.zkoss.zk.ui.Component;
0024: import org.zkoss.zk.ui.UiException;
0025: import org.zkoss.image.AImage;
0026: import org.zkoss.util.TimeZones;
0027: import org.zkoss.lang.Strings;
0028: import org.zkoss.lang.Objects;
0029:
0030: import org.jfree.chart.*;
0031: import org.jfree.chart.encoders.*;
0032: import org.jfree.chart.plot.*;
0033: import org.jfree.chart.entity.*;
0034: import org.jfree.data.general.*;
0035: import org.jfree.data.category.*;
0036: import org.jfree.data.xy.*;
0037: import org.jfree.data.time.RegularTimePeriod;
0038: import org.jfree.data.time.TimeSeriesCollection;
0039: import org.jfree.util.TableOrder;
0040:
0041: import java.util.List;
0042: import java.util.Date;
0043: import java.util.Map;
0044: import java.util.HashMap;
0045: import java.util.TimeZone;
0046: import java.io.ByteArrayOutputStream;
0047: import java.awt.image.BufferedImage;
0048: import java.awt.Paint;
0049: import java.awt.Color;
0050: import java.util.Iterator;
0051:
0052: /**
0053: * A chart engine implemented with JFreeChart.
0054: *
0055: * <p>This is the JFreeChart base chart engine implementation. All chart would
0056: * support drilldown by providing Area hot spots. Each Area would callback to
0057: * {@link org.zkoss.zul.event.ChartAreaListener} class that application developers
0058: * can do processing on each area.</p>
0059: *
0060: * <p>Note that useful {@link org.zkoss.zul.ChartModel} information is put in
0061: * Area's custom attribute Map so you can retrieve them by calling Area's
0062: * getAttribute(key) method and use them in drilldown function. Following is
0063: * the table of keys for different ChartModel.</p>
0064: *
0065: * <table>
0066: * <tr><th>model</th><th>key</th></tr>
0067: * <tr><td>{@link PieModel}</td><td>entity</td></tr>
0068: * <tr><td></td><td>category</td></tr>
0069: * <tr><td></td><td>value</td></tr>
0070: *
0071: * <tr><td>{@link CategoryModel}</td><td>entity</td></tr>
0072: * <tr><td></td><td>series</td></tr>
0073: * <tr><td></td><td>category</td></tr>
0074: * <tr><td></td><td>value</td></tr>
0075: *
0076: * <tr><td>{@link XYModel}</td><td>entity</td></tr>
0077: * <tr><td></td><td>series</td></tr>
0078: * <tr><td></td><td>x</td></tr>
0079: * <tr><td></td><td>y</td></tr>
0080: *
0081: * <tr><td>{@link HiLoModel}</td><td>entity</td></tr>
0082: * <tr><td></td><td>series</td></tr>
0083: * <tr><td></td><td>date</td></tr>
0084: * <tr><td></td><td>open</td></tr>
0085: * <tr><td></td><td>high</td></tr>
0086: * <tr><td></td><td>low</td></tr>
0087: * <tr><td></td><td>close</td></tr>
0088: * <tr><td></td><td>volumn</td></tr>
0089: * </table>
0090: * <p>Following is the explanation for each key:</p>
0091: * <ul>
0092: * <li>entity: entity is used to distinguish the differnt entities on a
0093: * chart. It might be TITLE, CATEGORY, DATA, LEGEND. The most important
0094: * entity might be DATA entity where the real chart data point is located.
0095: * For example, the DATA Area is each slice in a Pie chart.</li>
0096: * <li>category: category name of the associated data.</li>
0097: * <li>value: value of the associated data.</li>
0098: * <li>series: series name of the associated data.</li>
0099: * <li>x: x value of the XYModel</li>
0100: * <li>y: y value of the XYModel</li>
0101: * <li>date, open, high, low, close, volumn: data of the HiLoModel</li>
0102: * </ul>
0103: *
0104: * <p>See also <a href="http://www.jfree.org/jfreechart">jFreeChart</a>.
0105: * @author henrichen
0106: * @since 3.0.0
0107: */
0108: public class JFreeChartEngine implements ChartEngine,
0109: java.io.Serializable {
0110: //as long as the series name is not set
0111: private static final String DEFAULT_HI_LO_SERIES = "High Low Data";
0112:
0113: //caching chartImpl if type and 3d are the same
0114: private transient boolean _threeD;
0115: private transient String _type;
0116: private transient ChartImpl _chartImpl; //chart implementaion
0117:
0118: /**
0119: * create specific type of chart drawing engine. This implementation use
0120: * JFreeChart engine.
0121: */
0122: private ChartImpl getChartImpl(Chart chart) {
0123: if (Objects.equals(chart.getType(), _type)
0124: && _threeD == chart.isThreeD()) {
0125: return _chartImpl;
0126: }
0127:
0128: if (Chart.PIE.equals(chart.getType()))
0129: _chartImpl = chart.isThreeD() ? new Pie3d() : new Pie();
0130:
0131: else if (Chart.RING.equals(chart.getType()))
0132: _chartImpl = new Ring();
0133:
0134: else if (Chart.BAR.equals(chart.getType()))
0135: _chartImpl = chart.isThreeD() ? new Bar3d() : new Bar();
0136:
0137: else if (Chart.LINE.equals(chart.getType()))
0138: _chartImpl = chart.isThreeD() ? new Line3d() : new Line();
0139:
0140: else if (Chart.AREA.equals(chart.getType()))
0141: _chartImpl = new AreaImpl();
0142:
0143: else if (Chart.STACKED_BAR.equals(chart.getType()))
0144: _chartImpl = chart.isThreeD() ? new StackedBar3d()
0145: : new StackedBar();
0146:
0147: else if (Chart.STACKED_AREA.equals(chart.getType()))
0148: _chartImpl = new StackedArea();
0149:
0150: else if (Chart.WATERFALL.equals(chart.getType()))
0151: _chartImpl = new Waterfall();
0152:
0153: else if (Chart.POLAR.equals(chart.getType()))
0154: _chartImpl = new Polar();
0155:
0156: else if (Chart.SCATTER.equals(chart.getType()))
0157: _chartImpl = new Scatter();
0158:
0159: else if (Chart.TIME_SERIES.equals(chart.getType()))
0160: _chartImpl = new TimeSeries();
0161:
0162: else if (Chart.STEP_AREA.equals(chart.getType()))
0163: _chartImpl = new StepArea();
0164:
0165: else if (Chart.STEP.equals(chart.getType()))
0166: _chartImpl = new Step();
0167:
0168: else if (Chart.HISTOGRAM.equals(chart.getType()))
0169: _chartImpl = new Histogram();
0170:
0171: else if (Chart.CANDLESTICK.equals(chart.getType()))
0172: _chartImpl = new Candlestick();
0173:
0174: else if (Chart.HIGHLOW.equals(chart.getType()))
0175: _chartImpl = new Highlow();
0176:
0177: else
0178: throw new UiException("Unsupported chart type yet: "
0179: + chart.getType());
0180:
0181: _threeD = chart.isThreeD();
0182: _type = chart.getType();
0183: return _chartImpl;
0184: }
0185:
0186: /**
0187: * Developers with special needs can override this method to apply own rendering properties on the created JFreeChart. The
0188: * original implementation of this method simply return false to tell the engine to apply its default rendering properties. Return true
0189: * tells the engine NOT to apply its default rendering properties.
0190: *
0191: * @param jfchart the created JFreeChart
0192: * @param chart the ZK chart component associated with this ChartEngine.
0193: * @return true to tell this engine NOT to apply its default rendering properties; false to tell this engine TO apply its default rendering properties.
0194: * @since 3.0.1
0195: */
0196: protected boolean prepareJFreeChart(JFreeChart jfchart, Chart chart) {
0197: return false; //request original engine to also do the setting.
0198: }
0199:
0200: //-- ChartEngine --//
0201: public byte[] drawChart(Object data) {
0202: Chart chart = (Chart) data;
0203: ChartImpl impl = getChartImpl(chart);
0204:
0205: JFreeChart jfchart = impl.createChart(chart);
0206:
0207: //feature#1814621
0208: //call prepareJFreeChart so developer can override it and do their own parameter settings
0209: if (!prepareJFreeChart(jfchart, chart)) {
0210: Plot plot = (Plot) jfchart.getPlot();
0211: float alpha = (float) (((float) chart.getFgAlpha()) / 255);
0212: plot.setForegroundAlpha(alpha);
0213:
0214: alpha = (float) (((float) chart.getBgAlpha()) / 255);
0215: plot.setBackgroundAlpha(alpha);
0216:
0217: int[] bgRGB = chart.getBgRGB();
0218: if (bgRGB != null) {
0219: plot.setBackgroundPaint(new Color(bgRGB[0], bgRGB[1],
0220: bgRGB[2], chart.getBgAlpha()));
0221: }
0222:
0223: int[] paneRGB = chart.getPaneRGB();
0224: if (paneRGB != null) {
0225: jfchart.setBackgroundPaint(new Color(paneRGB[0],
0226: paneRGB[1], paneRGB[2], chart.getPaneAlpha()));
0227: }
0228: }
0229:
0230: //callbacks for each area
0231: ChartRenderingInfo jfinfo = new ChartRenderingInfo();
0232: BufferedImage bi = jfchart.createBufferedImage(chart
0233: .getIntWidth(), chart.getIntHeight(),
0234: BufferedImage.TRANSLUCENT, jfinfo);
0235:
0236: //remove old areas
0237: chart.getChildren().clear();
0238:
0239: int j = 0;
0240: String preUrl = null;
0241: for (Iterator it = jfinfo.getEntityCollection().iterator(); it
0242: .hasNext();) {
0243: ChartEntity ce = (ChartEntity) it.next();
0244: final String url = ce.getURLText();
0245:
0246: //workaround JFreeChart's bug (skip replicate areas)
0247: if (url != null) {
0248: if (preUrl == null) {
0249: preUrl = url;
0250: } else if (url.equals(preUrl)) { //start replicate, skip
0251: break;
0252: }
0253: }
0254:
0255: Area area = new Area();
0256: area.setParent(chart);
0257: area.setCoords(ce.getShapeCoords());
0258: area.setShape(ce.getShapeType());
0259: area.setId("area_" + chart.getId() + '_' + (j++));
0260: if (chart.isShowTooltiptext()
0261: && ce.getToolTipText() != null) {
0262: area.setTooltiptext(ce.getToolTipText());
0263: }
0264: area.setAttribute("url", ce.getURLText());
0265: impl.render(chart, area, ce);
0266: if (chart.getAreaListener() != null) {
0267: try {
0268: chart.getAreaListener().onRender(area, ce);
0269: } catch (Exception ex) {
0270: throw UiException.Aide.wrap(ex);
0271: }
0272: }
0273: }
0274:
0275: //clean up the "LEGEND_SEQ"
0276: //used for workaround LegendItemEntity.getSeries() always return 0
0277: //used for workaround TickLabelEntity no information
0278: chart.removeAttribute("LEGEND_SEQ");
0279: chart.removeAttribute("TICK_SEQ");
0280:
0281: try {
0282: //encode into png image format byte array
0283: return EncoderUtil.encode(bi, ImageFormat.PNG, true);
0284: } catch (java.io.IOException ex) {
0285: throw UiException.Aide.wrap(ex);
0286: }
0287: }
0288:
0289: //-- utilities --//
0290: /**
0291: * transfer a PieModel into JFreeChart PieDataset.
0292: */
0293: private PieDataset PieModelToPieDataset(PieModel model) {
0294: DefaultPieDataset dataset = new DefaultPieDataset();
0295: for (final Iterator it = model.getCategories().iterator(); it
0296: .hasNext();) {
0297: Comparable category = (Comparable) it.next();
0298: Number value = model.getValue(category);
0299: dataset.setValue(category, value);
0300: }
0301: return dataset;
0302: }
0303:
0304: /**
0305: * transfer a CategoryModel into JFreeChart PieDataset.
0306: */
0307: private PieDataset CategoryModelToPieDataset(CategoryModel model) {
0308: DefaultPieDataset dataset = new DefaultPieDataset();
0309: Comparable defaultSeries = null;
0310: int max = 0;
0311: for (final Iterator it = model.getKeys().iterator(); it
0312: .hasNext();) {
0313: final List key = (List) it.next();
0314: Comparable series = (Comparable) key.get(0);
0315: if (defaultSeries == null) {
0316: defaultSeries = series;
0317: max = model.getCategories().size();
0318: }
0319: if (!Objects.equals(defaultSeries, series)) {
0320: continue;
0321: }
0322: Comparable category = (Comparable) key.get(1);
0323: Number value = (Number) model.getValue(series, category);
0324: dataset.setValue(category, value);
0325:
0326: if (--max == 0)
0327: break; //no more in this series
0328: }
0329: return dataset;
0330: }
0331:
0332: /**
0333: * transfer a CategoryModel into JFreeChart CategoryDataset.
0334: */
0335: private CategoryDataset CategoryModelToCategoryDataset(
0336: CategoryModel model) {
0337: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
0338: for (final Iterator it = model.getKeys().iterator(); it
0339: .hasNext();) {
0340: final List key = (List) it.next();
0341: Comparable series = (Comparable) key.get(0);
0342: Comparable category = (Comparable) key.get(1);
0343: Number value = (Number) model.getValue(series, category);
0344: dataset.setValue(value, series, category);
0345: }
0346: return dataset;
0347: }
0348:
0349: /**
0350: * transfer a XYModel into JFreeChart XYSeriesCollection.
0351: */
0352: private XYDataset XYModelToXYDataset(XYModel model) {
0353: XYSeriesCollection dataset = new XYSeriesCollection();
0354: for (final Iterator it = model.getSeries().iterator(); it
0355: .hasNext();) {
0356: final Comparable series = (Comparable) it.next();
0357: XYSeries xyser = new XYSeries(series, model.isAutoSort());
0358: final int size = model.getDataCount(series);
0359: for (int j = 0; j < size; ++j) {
0360: xyser.add(model.getX(series, j), model.getY(series, j),
0361: false);
0362: }
0363: dataset.addSeries(xyser);
0364: }
0365: return dataset;
0366: }
0367:
0368: /**
0369: * transfer a XYModel into JFreeChart DefaultTableXYDataset.
0370: */
0371: private TableXYDataset XYModelToTableXYDataset(XYModel model) {
0372: DefaultTableXYDataset dataset = new DefaultTableXYDataset();
0373: for (final Iterator it = model.getSeries().iterator(); it
0374: .hasNext();) {
0375: final Comparable series = (Comparable) it.next();
0376: XYSeries xyser = new XYSeries(series, false, false);
0377: final int size = model.getDataCount(series);
0378: for (int j = 0; j < size; ++j) {
0379: xyser.add(model.getX(series, j), model.getY(series, j),
0380: false);
0381: }
0382: dataset.addSeries(xyser);
0383: }
0384: return dataset;
0385: }
0386:
0387: /**
0388: * transfer a XYModel into JFreeChart TimeSeriesCollection.
0389: */
0390: private XYDataset XYModelToTimeDataset(XYModel model, Chart chart) {
0391: TimeZone tz = chart.getTimeZone();
0392: if (tz == null)
0393: tz = TimeZones.getCurrent();
0394: String p = chart.getPeriod();
0395: if (p == null)
0396: p = Chart.MILLISECOND;
0397: Class pclass = (Class) _periodMap.get(p);
0398: if (pclass == null) {
0399: throw new UiException(
0400: "Unsupported period for Time Series chart: " + p);
0401: }
0402: TimeSeriesCollection dataset = new TimeSeriesCollection(tz);
0403:
0404: for (final Iterator it = model.getSeries().iterator(); it
0405: .hasNext();) {
0406: final Comparable series = (Comparable) it.next();
0407: final org.jfree.data.time.TimeSeries tser = new org.jfree.data.time.TimeSeries(
0408: (String) series, pclass);
0409: final int size = model.getDataCount(series);
0410: for (int j = 0; j < size; ++j) {
0411: final RegularTimePeriod period = RegularTimePeriod
0412: .createInstance(pclass, new Date(model.getX(
0413: series, j).longValue()), tz);
0414: tser.addOrUpdate(period, model.getY(series, j));
0415: }
0416: dataset.addSeries(tser);
0417: }
0418: return dataset;
0419: }
0420:
0421: private static Map _periodMap = new HashMap(10);
0422: static {
0423: _periodMap.put(Chart.MILLISECOND,
0424: org.jfree.data.time.Millisecond.class);
0425: _periodMap.put(Chart.SECOND, org.jfree.data.time.Second.class);
0426: _periodMap.put(Chart.MINUTE, org.jfree.data.time.Minute.class);
0427: _periodMap.put(Chart.HOUR, org.jfree.data.time.Hour.class);
0428: _periodMap.put(Chart.DAY, org.jfree.data.time.Day.class);
0429: _periodMap.put(Chart.WEEK, org.jfree.data.time.Week.class);
0430: _periodMap.put(Chart.MONTH, org.jfree.data.time.Month.class);
0431: _periodMap
0432: .put(Chart.QUARTER, org.jfree.data.time.Quarter.class);
0433: _periodMap.put(Chart.YEAR, org.jfree.data.time.Year.class);
0434: }
0435:
0436: /**
0437: * transfer a HiLoModel into JFreeChart DefaultOHLCDataset
0438: */
0439: private OHLCDataset HiLoModelToOHLCDataset(HiLoModel model) {
0440: final int size = model.getDataCount();
0441: final OHLCDataItem[] items = new OHLCDataItem[size];
0442:
0443: for (int j = 0; j < size; ++j) {
0444: Date date = model.getDate(j);
0445: Number open = model.getOpen(j);
0446: Number high = model.getHigh(j);
0447: Number low = model.getLow(j);
0448: Number close = model.getClose(j);
0449: Number volume = model.getVolume(j);
0450:
0451: OHLCDataItem item = new OHLCDataItem(date,
0452: doubleValue(open), doubleValue(high),
0453: doubleValue(low), doubleValue(close),
0454: doubleValue(volume));
0455: items[j] = item;
0456: }
0457:
0458: Comparable series = model.getSeries();
0459: if (series == null) {
0460: series = DEFAULT_HI_LO_SERIES;
0461: }
0462: return new DefaultOHLCDataset(series, items);
0463: }
0464:
0465: private double doubleValue(Number n) {
0466: return n == null ? 0.0 : n.doubleValue();
0467: }
0468:
0469: /**
0470: * decode LegendItemEntity into key-value pair of Area's componentScope.
0471: * @param area the Area where the final attribute is set
0472: * @param info the LegendItemEntity to be decoded.
0473: */
0474: private void decodeLegendInfo(Area area, LegendItemEntity info,
0475: Chart chart) {
0476: if (info == null) {
0477: return;
0478: }
0479: final ChartModel model = chart.getModel();
0480: final int seq = ((Integer) chart.getAttribute("LEGEND_SEQ"))
0481: .intValue();
0482:
0483: if (model instanceof PieModel) {
0484: Comparable category = ((PieModel) model).getCategory(seq);
0485: area.setAttribute("category", category);
0486: area.setAttribute("value", ((PieModel) model)
0487: .getValue(category));
0488: if (chart.isShowTooltiptext()
0489: && info.getToolTipText() == null) {
0490: area.setTooltiptext(category.toString());
0491: }
0492: } else if (model instanceof CategoryModel) {
0493: Comparable series = ((CategoryModel) model).getSeries(seq);
0494: area.setAttribute("series", series);
0495: if (chart.isShowTooltiptext()
0496: && info.getToolTipText() == null) {
0497: area.setTooltiptext(series.toString());
0498: }
0499: } else if (model instanceof XYModel) {
0500: Comparable series = ((XYModel) model).getSeries(seq);
0501: area.setAttribute("series", series);
0502: if (chart.isShowTooltiptext()
0503: && info.getToolTipText() == null) {
0504: area.setTooltiptext(series.toString());
0505: }
0506: } else if (model instanceof HiLoModel) {
0507: Comparable series = ((HiLoModel) model).getSeries();
0508: if (series == null) {
0509: series = DEFAULT_HI_LO_SERIES;
0510: }
0511: area.setAttribute("series", series);
0512: if (chart.isShowTooltiptext()
0513: && info.getToolTipText() == null) {
0514: area.setTooltiptext(series.toString());
0515: }
0516: }
0517: }
0518:
0519: /**
0520: * decode TickLabelEntity into key-value pair of Area's componentScope.
0521: * @param area the Area where the final attribute is set
0522: * @param info the TickLabelEntity to be decoded.
0523: */
0524: private void decodeTickLabelInfo(Area area, TickLabelEntity info,
0525: Chart chart) {
0526: if (info == null) {
0527: return;
0528: }
0529: final ChartModel model = chart.getModel();
0530: final int seq = ((Integer) chart.getAttribute("TICK_SEQ"))
0531: .intValue();
0532:
0533: if (model instanceof CategoryModel) {
0534: Comparable category = ((CategoryModel) model)
0535: .getCategory(seq);
0536: area.setAttribute("category", category);
0537: if (chart.isShowTooltiptext()
0538: && info.getToolTipText() == null) {
0539: area.setTooltiptext(category.toString());
0540: }
0541: }
0542: }
0543:
0544: /**
0545: * decode PieSectionEntity into key-value pair of Area's componentScope.
0546: * @param area the Area where the final attribute is set
0547: * @param info the PieSectionEntity to be decoded.
0548: */
0549: private void decodePieInfo(Area area, PieSectionEntity info) {
0550: if (info == null) {
0551: return;
0552: }
0553:
0554: PieDataset dataset = info.getDataset();
0555: Comparable category = info.getSectionKey();
0556: area.setAttribute("category", category);
0557: area.setAttribute("value", dataset.getValue(category));
0558: }
0559:
0560: /**
0561: * decode CategoryItemEntity into key-value pair of Area's componentScope.
0562: * @param area the Area where the final attribute is set
0563: * @param info the CategoryItemEntity to be decoded.
0564: */
0565: private void decodeCategoryInfo(Area area, CategoryItemEntity info) {
0566: if (info == null) {
0567: return;
0568: }
0569:
0570: CategoryDataset dataset = info.getDataset();
0571: Comparable category = info.getColumnKey();
0572: Comparable series = info.getRowKey();
0573:
0574: area.setAttribute("series", series);
0575: area.setAttribute("category", category);
0576: area.setAttribute("value", dataset.getValue(series, category));
0577: }
0578:
0579: /**
0580: * decode XYItemEntity into key-value pair of Area's componentScope.
0581: * @param area the Area where the final attribute is set
0582: * @param info the XYItemEntity to be decoded.
0583: */
0584: private void decodeXYInfo(Area area, XYItemEntity info) {
0585: if (info == null) {
0586: return;
0587: }
0588:
0589: XYDataset dataset = info.getDataset();
0590: int si = info.getSeriesIndex();
0591: int ii = info.getItem();
0592:
0593: area.setAttribute("series", dataset.getSeriesKey(si));
0594:
0595: if (dataset instanceof OHLCDataset) {
0596: OHLCDataset ds = (OHLCDataset) dataset;
0597: area.setAttribute("date", new Date(ds.getX(si, ii)
0598: .longValue()));
0599: area.setAttribute("open", ds.getOpen(si, ii));
0600: area.setAttribute("high", ds.getHigh(si, ii));
0601: area.setAttribute("low", ds.getLow(si, ii));
0602: area.setAttribute("close", ds.getClose(si, ii));
0603: area.setAttribute("volume", ds.getVolume(si, ii));
0604: } else {
0605: area.setAttribute("x", dataset.getX(si, ii));
0606: area.setAttribute("y", dataset.getY(si, ii));
0607: }
0608:
0609: }
0610:
0611: //-- Chart specific implementation --//
0612: /** base chart */
0613: abstract private class ChartImpl {
0614: abstract void render(Chart chart, Area area, ChartEntity info);
0615:
0616: abstract JFreeChart createChart(Chart chart);
0617: }
0618:
0619: /** pie chart */
0620: private class Pie extends ChartImpl {
0621: public void render(Chart chart, Area area, ChartEntity info) {
0622: if (info instanceof LegendItemEntity) {
0623: area.setAttribute("entity", "LEGEND");
0624: Integer seq = (Integer) chart
0625: .getAttribute("LEGEND_SEQ");
0626: seq = seq == null ? new Integer(0) : new Integer(seq
0627: .intValue() + 1);
0628: chart.setAttribute("LEGEND_SEQ", seq);
0629: decodeLegendInfo(area, (LegendItemEntity) info, chart);
0630: } else if (info instanceof PieSectionEntity) {
0631: area.setAttribute("entity", "DATA");
0632: decodePieInfo(area, (PieSectionEntity) info);
0633: } else {
0634: area.setAttribute("entity", "TITLE");
0635: if (chart.isShowTooltiptext()) {
0636: area.setTooltiptext(chart.getTitle());
0637: }
0638: }
0639: }
0640:
0641: protected PieDataset getDataset(ChartModel model) {
0642: if (model instanceof CategoryModel) {
0643: return CategoryModelToPieDataset((CategoryModel) model);
0644: } else if (model instanceof PieModel) {
0645: return PieModelToPieDataset((PieModel) model);
0646: } else {
0647: throw new UiException(
0648: "model must be a org.zkoss.zul.PieModel or a org.zkoss.zul.CategoryModel");
0649: }
0650: }
0651:
0652: public JFreeChart createChart(Chart chart) {
0653: ChartModel model = (ChartModel) chart.getModel();
0654: return ChartFactory.createPieChart(chart.getTitle(),
0655: getDataset(model), chart.isShowLegend(), chart
0656: .isShowTooltiptext(), true);
0657: }
0658: }
0659:
0660: /** pie3d chart */
0661: private class Pie3d extends Pie {
0662: public JFreeChart createChart(Chart chart) {
0663: ChartModel model = (ChartModel) chart.getModel();
0664: return ChartFactory.createPieChart3D(chart.getTitle(),
0665: getDataset(model), chart.isShowLegend(), chart
0666: .isShowTooltiptext(), true);
0667: }
0668: }
0669:
0670: /** ring chart */
0671: private class Ring extends Pie {
0672: public JFreeChart createChart(Chart chart) {
0673: ChartModel model = (ChartModel) chart.getModel();
0674: return ChartFactory.createRingChart(chart.getTitle(),
0675: getDataset(model), chart.isShowLegend(), chart
0676: .isShowTooltiptext(), true);
0677: }
0678: }
0679:
0680: /** bar chart */
0681: private class Bar extends ChartImpl {
0682: public void render(Chart chart, Area area, ChartEntity info) {
0683: if (info instanceof LegendItemEntity) {
0684: area.setAttribute("entity", "LEGEND");
0685: Integer seq = (Integer) chart
0686: .getAttribute("LEGEND_SEQ");
0687: seq = seq == null ? new Integer(0) : new Integer(seq
0688: .intValue() + 1);
0689: chart.setAttribute("LEGEND_SEQ", seq);
0690: decodeLegendInfo(area, (LegendItemEntity) info, chart);
0691: } else if (info instanceof CategoryItemEntity) {
0692: area.setAttribute("entity", "DATA");
0693: decodeCategoryInfo(area, (CategoryItemEntity) info);
0694: } else if (info instanceof XYItemEntity) {
0695: area.setAttribute("entity", "DATA");
0696: decodeXYInfo(area, (XYItemEntity) info);
0697: } else if (info instanceof TickLabelEntity) {
0698: area.setAttribute("entity", "CATEGORY");
0699: Integer seq = (Integer) chart.getAttribute("TICK_SEQ");
0700: seq = seq == null ? new Integer(0) : new Integer(seq
0701: .intValue() + 1);
0702: chart.setAttribute("TICK_SEQ", seq);
0703: decodeTickLabelInfo(area, (TickLabelEntity) info, chart);
0704: } else {
0705: area.setAttribute("entity", "TITLE");
0706: if (chart.isShowTooltiptext()) {
0707: area.setTooltiptext(chart.getTitle());
0708: }
0709: }
0710: }
0711:
0712: public JFreeChart createChart(Chart chart) {
0713: ChartModel model = (ChartModel) chart.getModel();
0714: if (model instanceof CategoryModel) {
0715: return ChartFactory
0716: .createBarChart(
0717: chart.getTitle(),
0718: chart.getXAxis(),
0719: chart.getYAxis(),
0720: CategoryModelToCategoryDataset((CategoryModel) model),
0721: getOrientation(chart.getOrient()),
0722: chart.isShowLegend(), chart
0723: .isShowTooltiptext(), true);
0724: } else if (model instanceof XYModel) {
0725: return ChartFactory
0726: .createXYBarChart(
0727: chart.getTitle(),
0728: chart.getXAxis(),
0729: false,
0730: chart.getYAxis(),
0731: (IntervalXYDataset) XYModelToXYDataset((XYModel) model),
0732: getOrientation(chart.getOrient()),
0733: chart.isShowLegend(), chart
0734: .isShowTooltiptext(), true);
0735: } else {
0736: throw new UiException(
0737: "model must be a org.zkoss.zul.CategoryModel or a org.zkoss.zul.XYModel");
0738: }
0739: }
0740: }
0741:
0742: /** bar3d chart */
0743: private class Bar3d extends Bar {
0744: public JFreeChart createChart(Chart chart) {
0745: ChartModel model = (ChartModel) chart.getModel();
0746: if (!(model instanceof CategoryModel)) {
0747: throw new UiException(
0748: "model must be a org.zkoss.zul.CategoryModel");
0749: }
0750: return ChartFactory
0751: .createBarChart3D(
0752: chart.getTitle(),
0753: chart.getXAxis(),
0754: chart.getYAxis(),
0755: CategoryModelToCategoryDataset((CategoryModel) model),
0756: getOrientation(chart.getOrient()), chart
0757: .isShowLegend(), chart
0758: .isShowTooltiptext(), true);
0759: }
0760: }
0761:
0762: /** area chart */
0763: private class AreaImpl extends ChartImpl {
0764: public void render(Chart chart, Area area, ChartEntity info) {
0765: if (info instanceof LegendItemEntity) {
0766: area.setAttribute("entity", "LEGEND");
0767: Integer seq = (Integer) chart
0768: .getAttribute("LEGEND_SEQ");
0769: seq = seq == null ? new Integer(0) : new Integer(seq
0770: .intValue() + 1);
0771: chart.setAttribute("LEGEND_SEQ", seq);
0772: decodeLegendInfo(area, (LegendItemEntity) info, chart);
0773: } else if (info instanceof CategoryItemEntity) {
0774: area.setAttribute("entity", "DATA");
0775: decodeCategoryInfo(area, (CategoryItemEntity) info);
0776: } else if (info instanceof XYItemEntity) {
0777: area.setAttribute("entity", "DATA");
0778: decodeXYInfo(area, (XYItemEntity) info);
0779: } else if (info instanceof TickLabelEntity) {
0780: area.setAttribute("entity", "CATEGORY");
0781: Integer seq = (Integer) chart.getAttribute("TICK_SEQ");
0782: seq = seq == null ? new Integer(0) : new Integer(seq
0783: .intValue() + 1);
0784: chart.setAttribute("TICK_SEQ", seq);
0785: decodeTickLabelInfo(area, (TickLabelEntity) info, chart);
0786: } else {
0787: area.setAttribute("entity", "TITLE");
0788: if (chart.isShowTooltiptext()) {
0789: area.setTooltiptext(chart.getTitle());
0790: }
0791: }
0792: }
0793:
0794: public JFreeChart createChart(Chart chart) {
0795: ChartModel model = (ChartModel) chart.getModel();
0796: if (model instanceof CategoryModel) {
0797: return ChartFactory
0798: .createAreaChart(
0799: chart.getTitle(),
0800: chart.getXAxis(),
0801: chart.getYAxis(),
0802: CategoryModelToCategoryDataset((CategoryModel) model),
0803: getOrientation(chart.getOrient()),
0804: chart.isShowLegend(), chart
0805: .isShowTooltiptext(), true);
0806: } else if (model instanceof XYModel) {
0807: return ChartFactory.createXYAreaChart(chart.getTitle(),
0808: chart.getXAxis(), chart.getYAxis(),
0809: XYModelToXYDataset((XYModel) model),
0810: getOrientation(chart.getOrient()), chart
0811: .isShowLegend(), chart
0812: .isShowTooltiptext(), true);
0813: } else {
0814: throw new UiException(
0815: "model must be a org.zkoss.zul.CategoryModel or a org.zkoss.zul.XYModel");
0816: }
0817:
0818: }
0819: }
0820:
0821: /** line chart */
0822: private class Line extends ChartImpl {
0823: public void render(Chart chart, Area area, ChartEntity info) {
0824: if (info instanceof LegendItemEntity) {
0825: area.setAttribute("entity", "LEGEND");
0826: Integer seq = (Integer) chart
0827: .getAttribute("LEGEND_SEQ");
0828: seq = seq == null ? new Integer(0) : new Integer(seq
0829: .intValue() + 1);
0830: chart.setAttribute("LEGEND_SEQ", seq);
0831: decodeLegendInfo(area, (LegendItemEntity) info, chart);
0832: } else if (info instanceof CategoryItemEntity) {
0833: area.setAttribute("entity", "DATA");
0834: decodeCategoryInfo(area, (CategoryItemEntity) info);
0835: } else if (info instanceof XYItemEntity) {
0836: area.setAttribute("entity", "DATA");
0837: decodeXYInfo(area, (XYItemEntity) info);
0838: } else if (info instanceof TickLabelEntity) {
0839: area.setAttribute("entity", "CATEGORY");
0840: Integer seq = (Integer) chart.getAttribute("TICK_SEQ");
0841: seq = seq == null ? new Integer(0) : new Integer(seq
0842: .intValue() + 1);
0843: chart.setAttribute("TICK_SEQ", seq);
0844: decodeTickLabelInfo(area, (TickLabelEntity) info, chart);
0845: } else {
0846: area.setAttribute("entity", "TITLE");
0847: if (chart.isShowTooltiptext()) {
0848: area.setTooltiptext(chart.getTitle());
0849: }
0850: }
0851: }
0852:
0853: public JFreeChart createChart(Chart chart) {
0854: ChartModel model = (ChartModel) chart.getModel();
0855: if (model instanceof CategoryModel) {
0856: return ChartFactory
0857: .createLineChart(
0858: chart.getTitle(),
0859: chart.getXAxis(),
0860: chart.getYAxis(),
0861: CategoryModelToCategoryDataset((CategoryModel) model),
0862: getOrientation(chart.getOrient()),
0863: chart.isShowLegend(), chart
0864: .isShowTooltiptext(), true);
0865: } else if (model instanceof XYModel) {
0866: return ChartFactory.createXYLineChart(chart.getTitle(),
0867: chart.getXAxis(), chart.getYAxis(),
0868: XYModelToXYDataset((XYModel) model),
0869: getOrientation(chart.getOrient()), chart
0870: .isShowLegend(), chart
0871: .isShowTooltiptext(), true);
0872: } else {
0873: throw new UiException(
0874: "model must be a org.zkoss.zul.CategoryModel or a org.zkoss.zul.XYModel");
0875: }
0876: }
0877: }
0878:
0879: /** line3d chart */
0880: private class Line3d extends Line {
0881: public JFreeChart createChart(Chart chart) {
0882: ChartModel model = (ChartModel) chart.getModel();
0883: if (!(model instanceof CategoryModel)) {
0884: throw new UiException(
0885: "model must be a org.zkoss.zul.CategoryModel");
0886: }
0887: return ChartFactory
0888: .createLineChart3D(
0889: chart.getTitle(),
0890: chart.getXAxis(),
0891: chart.getYAxis(),
0892: CategoryModelToCategoryDataset((CategoryModel) model),
0893: getOrientation(chart.getOrient()), chart
0894: .isShowLegend(), chart
0895: .isShowTooltiptext(), true);
0896: }
0897: }
0898:
0899: /** stackedbar chart */
0900: private class StackedBar extends ChartImpl {
0901: public void render(Chart chart, Area area, ChartEntity info) {
0902: if (info instanceof LegendItemEntity) {
0903: area.setAttribute("entity", "LEGEND");
0904: Integer seq = (Integer) chart
0905: .getAttribute("LEGEND_SEQ");
0906: seq = seq == null ? new Integer(0) : new Integer(seq
0907: .intValue() + 1);
0908: chart.setAttribute("LEGEND_SEQ", seq);
0909: decodeLegendInfo(area, (LegendItemEntity) info, chart);
0910: } else if (info instanceof CategoryItemEntity) {
0911: area.setAttribute("entity", "DATA");
0912: decodeCategoryInfo(area, (CategoryItemEntity) info);
0913: } else if (info instanceof TickLabelEntity) {
0914: area.setAttribute("entity", "CATEGORY");
0915: Integer seq = (Integer) chart.getAttribute("TICK_SEQ");
0916: seq = seq == null ? new Integer(0) : new Integer(seq
0917: .intValue() + 1);
0918: chart.setAttribute("TICK_SEQ", seq);
0919: decodeTickLabelInfo(area, (TickLabelEntity) info, chart);
0920: } else {
0921: area.setAttribute("entity", "TITLE");
0922: if (chart.isShowTooltiptext()) {
0923: area.setTooltiptext(chart.getTitle());
0924: }
0925: }
0926: }
0927:
0928: public JFreeChart createChart(Chart chart) {
0929: ChartModel model = (ChartModel) chart.getModel();
0930: if (!(model instanceof CategoryModel)) {
0931: throw new UiException(
0932: "model must be a org.zkoss.zul.CategoryModel");
0933: }
0934: return ChartFactory
0935: .createStackedBarChart(
0936: chart.getTitle(),
0937: chart.getXAxis(),
0938: chart.getYAxis(),
0939: CategoryModelToCategoryDataset((CategoryModel) model),
0940: getOrientation(chart.getOrient()), chart
0941: .isShowLegend(), chart
0942: .isShowTooltiptext(), true);
0943: }
0944: }
0945:
0946: /** stackedbar3d chart */
0947: private class StackedBar3d extends StackedBar {
0948: public JFreeChart createChart(Chart chart) {
0949: ChartModel model = (ChartModel) chart.getModel();
0950: if (!(model instanceof CategoryModel)) {
0951: throw new UiException(
0952: "model must be a org.zkoss.zul.CategoryModel");
0953: }
0954: return ChartFactory
0955: .createStackedBarChart3D(
0956: chart.getTitle(),
0957: chart.getXAxis(),
0958: chart.getYAxis(),
0959: CategoryModelToCategoryDataset((CategoryModel) model),
0960: getOrientation(chart.getOrient()), chart
0961: .isShowLegend(), chart
0962: .isShowTooltiptext(), true);
0963: }
0964: }
0965:
0966: /** stackedarea chart */
0967: //note: cutting area coordinate is not correct.
0968: private class StackedArea extends ChartImpl {
0969: public void render(Chart chart, Area area, ChartEntity info) {
0970: if (info instanceof LegendItemEntity) {
0971: area.setAttribute("entity", "LEGEND");
0972: Integer seq = (Integer) chart
0973: .getAttribute("LEGEND_SEQ");
0974: seq = seq == null ? new Integer(0) : new Integer(seq
0975: .intValue() + 1);
0976: chart.setAttribute("LEGEND_SEQ", seq);
0977: decodeLegendInfo(area, (LegendItemEntity) info, chart);
0978: } else if (info instanceof CategoryItemEntity) {
0979: area.setAttribute("entity", "DATA");
0980: decodeCategoryInfo(area, (CategoryItemEntity) info);
0981: } else if (info instanceof XYItemEntity) {
0982: area.setAttribute("entity", "DATA");
0983: decodeXYInfo(area, (XYItemEntity) info);
0984: } else if (info instanceof TickLabelEntity) {
0985: area.setAttribute("entity", "CATEGORY");
0986: Integer seq = (Integer) chart.getAttribute("TICK_SEQ");
0987: seq = seq == null ? new Integer(0) : new Integer(seq
0988: .intValue() + 1);
0989: chart.setAttribute("TICK_SEQ", seq);
0990: decodeTickLabelInfo(area, (TickLabelEntity) info, chart);
0991: } else {
0992: area.setAttribute("entity", "TITLE");
0993: if (chart.isShowTooltiptext()) {
0994: area.setTooltiptext(chart.getTitle());
0995: }
0996: }
0997: }
0998:
0999: public JFreeChart createChart(Chart chart) {
1000: ChartModel model = (ChartModel) chart.getModel();
1001: if (model instanceof CategoryModel) {
1002: return ChartFactory
1003: .createStackedAreaChart(
1004: chart.getTitle(),
1005: chart.getXAxis(),
1006: chart.getYAxis(),
1007: CategoryModelToCategoryDataset((CategoryModel) model),
1008: getOrientation(chart.getOrient()),
1009: chart.isShowLegend(), chart
1010: .isShowTooltiptext(), true);
1011: } else if (model instanceof XYModel) {
1012: return ChartFactory.createStackedXYAreaChart(chart
1013: .getTitle(), chart.getXAxis(),
1014: chart.getYAxis(),
1015: XYModelToTableXYDataset((XYModel) model),
1016: getOrientation(chart.getOrient()), chart
1017: .isShowLegend(), chart
1018: .isShowTooltiptext(), true);
1019: } else {
1020: throw new UiException(
1021: "model must be a org.zkoss.zul.CategoryModel or a org.zkoss.zul.XYModel");
1022: }
1023: }
1024: }
1025:
1026: /** waterfall chart */
1027: //note: cuttin area corrdinate is not correct.
1028: private class Waterfall extends ChartImpl {
1029: public void render(Chart chart, Area area, ChartEntity info) {
1030: if (info instanceof LegendItemEntity) {
1031: area.setAttribute("entity", "LEGEND");
1032: Integer seq = (Integer) chart
1033: .getAttribute("LEGEND_SEQ");
1034: seq = seq == null ? new Integer(0) : new Integer(seq
1035: .intValue() + 1);
1036: chart.setAttribute("LEGEND_SEQ", seq);
1037: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1038: } else if (info instanceof CategoryItemEntity) {
1039: area.setAttribute("entity", "DATA");
1040: decodeCategoryInfo(area, (CategoryItemEntity) info);
1041: } else if (info instanceof TickLabelEntity) {
1042: area.setAttribute("entity", "CATEGORY");
1043: Integer seq = (Integer) chart.getAttribute("TICK_SEQ");
1044: seq = seq == null ? new Integer(0) : new Integer(seq
1045: .intValue() + 1);
1046: chart.setAttribute("TICK_SEQ", seq);
1047: decodeTickLabelInfo(area, (TickLabelEntity) info, chart);
1048: } else {
1049: area.setAttribute("entity", "TITLE");
1050: if (chart.isShowTooltiptext()) {
1051: area.setTooltiptext(chart.getTitle());
1052: }
1053: }
1054: }
1055:
1056: public JFreeChart createChart(Chart chart) {
1057: ChartModel model = (ChartModel) chart.getModel();
1058: if (!(model instanceof CategoryModel)) {
1059: throw new UiException(
1060: "model must be a org.zkoss.zul.CategoryModel");
1061: }
1062: return ChartFactory
1063: .createWaterfallChart(
1064: chart.getTitle(),
1065: chart.getXAxis(),
1066: chart.getYAxis(),
1067: CategoryModelToCategoryDataset((CategoryModel) model),
1068: getOrientation(chart.getOrient()), chart
1069: .isShowLegend(), chart
1070: .isShowTooltiptext(), true);
1071: }
1072: }
1073:
1074: /** polar chart */
1075: private class Polar extends ChartImpl {
1076: public void render(Chart chart, Area area, ChartEntity info) {
1077: if (info instanceof LegendItemEntity) {
1078: area.setAttribute("entity", "LEGEND");
1079: Integer seq = (Integer) chart
1080: .getAttribute("LEGEND_SEQ");
1081: seq = seq == null ? new Integer(0) : new Integer(seq
1082: .intValue() + 1);
1083: chart.setAttribute("LEGEND_SEQ", seq);
1084: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1085: } else {
1086: area.setAttribute("entity", "TITLE");
1087: if (chart.isShowTooltiptext()) {
1088: area.setTooltiptext(chart.getTitle());
1089: }
1090: }
1091: }
1092:
1093: public JFreeChart createChart(Chart chart) {
1094: ChartModel model = (ChartModel) chart.getModel();
1095: if (!(model instanceof XYModel)) {
1096: throw new UiException(
1097: "model must be a org.zkoss.zul.XYModel");
1098: }
1099: return ChartFactory.createPolarChart(chart.getTitle(),
1100: XYModelToXYDataset((XYModel) model), chart
1101: .isShowLegend(), chart.isShowTooltiptext(),
1102: true);
1103: }
1104: }
1105:
1106: /** scatter chart */
1107: private class Scatter extends ChartImpl {
1108: public void render(Chart chart, Area area, ChartEntity info) {
1109: if (info instanceof LegendItemEntity) {
1110: area.setAttribute("entity", "LEGEND");
1111: Integer seq = (Integer) chart
1112: .getAttribute("LEGEND_SEQ");
1113: seq = seq == null ? new Integer(0) : new Integer(seq
1114: .intValue() + 1);
1115: chart.setAttribute("LEGEND_SEQ", seq);
1116: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1117: } else if (info instanceof XYItemEntity) {
1118: area.setAttribute("entity", "DATA");
1119: decodeXYInfo(area, (XYItemEntity) info);
1120: } else {
1121: area.setAttribute("entity", "TITLE");
1122: if (chart.isShowTooltiptext()) {
1123: area.setTooltiptext(chart.getTitle());
1124: }
1125: }
1126: }
1127:
1128: public JFreeChart createChart(Chart chart) {
1129: ChartModel model = (ChartModel) chart.getModel();
1130: if (!(model instanceof XYModel)) {
1131: throw new UiException(
1132: "model must be a org.zkoss.zul.XYModel");
1133: }
1134: return ChartFactory.createScatterPlot(chart.getTitle(),
1135: chart.getXAxis(), chart.getYAxis(),
1136: XYModelToXYDataset((XYModel) model),
1137: getOrientation(chart.getOrient()), chart
1138: .isShowLegend(), chart.isShowTooltiptext(),
1139: true);
1140: }
1141: }
1142:
1143: /** timeseries chart */
1144: private class TimeSeries extends ChartImpl {
1145: public void render(Chart chart, Area area, ChartEntity info) {
1146: if (info instanceof LegendItemEntity) {
1147: area.setAttribute("entity", "LEGEND");
1148: Integer seq = (Integer) chart
1149: .getAttribute("LEGEND_SEQ");
1150: seq = seq == null ? new Integer(0) : new Integer(seq
1151: .intValue() + 1);
1152: chart.setAttribute("LEGEND_SEQ", seq);
1153: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1154: } else if (info instanceof XYItemEntity) {
1155: area.setAttribute("entity", "DATA");
1156: decodeXYInfo(area, (XYItemEntity) info);
1157: } else {
1158: area.setAttribute("entity", "TITLE");
1159: if (chart.isShowTooltiptext()) {
1160: area.setTooltiptext(chart.getTitle());
1161: }
1162: }
1163: }
1164:
1165: public JFreeChart createChart(Chart chart) {
1166: ChartModel model = (ChartModel) chart.getModel();
1167: if (!(model instanceof XYModel)) {
1168: throw new UiException(
1169: "model must be a org.zkoss.zul.XYModel");
1170: }
1171: return ChartFactory.createTimeSeriesChart(chart.getTitle(),
1172: chart.getXAxis(), chart.getYAxis(),
1173: XYModelToTimeDataset((XYModel) model, chart), chart
1174: .isShowLegend(), chart.isShowTooltiptext(),
1175: true);
1176: }
1177: }
1178:
1179: /** steparea chart */
1180: private class StepArea extends ChartImpl {
1181: public void render(Chart chart, Area area, ChartEntity info) {
1182: if (info instanceof LegendItemEntity) {
1183: area.setAttribute("entity", "LEGEND");
1184: Integer seq = (Integer) chart
1185: .getAttribute("LEGEND_SEQ");
1186: seq = seq == null ? new Integer(0) : new Integer(seq
1187: .intValue() + 1);
1188: chart.setAttribute("LEGEND_SEQ", seq);
1189: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1190: } else if (info instanceof XYItemEntity) {
1191: area.setAttribute("entity", "DATA");
1192: decodeXYInfo(area, (XYItemEntity) info);
1193: } else {
1194: area.setAttribute("entity", "TITLE");
1195: if (chart.isShowTooltiptext()) {
1196: area.setTooltiptext(chart.getTitle());
1197: }
1198: }
1199: }
1200:
1201: public JFreeChart createChart(Chart chart) {
1202: ChartModel model = (ChartModel) chart.getModel();
1203: if (!(model instanceof XYModel)) {
1204: throw new UiException(
1205: "model must be a org.zkoss.zul.XYModel");
1206: }
1207: return ChartFactory.createXYStepAreaChart(chart.getTitle(),
1208: chart.getXAxis(), chart.getYAxis(),
1209: XYModelToXYDataset((XYModel) model),
1210: getOrientation(chart.getOrient()), chart
1211: .isShowLegend(), chart.isShowTooltiptext(),
1212: true);
1213: }
1214: }
1215:
1216: /** step chart */
1217: private class Step extends ChartImpl {
1218: public void render(Chart chart, Area area, ChartEntity info) {
1219: if (info instanceof LegendItemEntity) {
1220: area.setAttribute("entity", "LEGEND");
1221: Integer seq = (Integer) chart
1222: .getAttribute("LEGEND_SEQ");
1223: seq = seq == null ? new Integer(0) : new Integer(seq
1224: .intValue() + 1);
1225: chart.setAttribute("LEGEND_SEQ", seq);
1226: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1227: } else if (info instanceof XYItemEntity) {
1228: area.setAttribute("entity", "DATA");
1229: decodeXYInfo(area, (XYItemEntity) info);
1230: } else {
1231: area.setAttribute("entity", "TITLE");
1232: if (chart.isShowTooltiptext()) {
1233: area.setTooltiptext(chart.getTitle());
1234: }
1235: }
1236: }
1237:
1238: public JFreeChart createChart(Chart chart) {
1239: ChartModel model = (ChartModel) chart.getModel();
1240: if (!(model instanceof XYModel)) {
1241: throw new UiException(
1242: "model must be a org.zkoss.zul.XYModel");
1243: }
1244: return ChartFactory.createXYStepChart(chart.getTitle(),
1245: chart.getXAxis(), chart.getYAxis(),
1246: XYModelToXYDataset((XYModel) model),
1247: getOrientation(chart.getOrient()), chart
1248: .isShowLegend(), chart.isShowTooltiptext(),
1249: true);
1250: }
1251: }
1252:
1253: /** histogram */
1254: private class Histogram extends ChartImpl {
1255: public void render(Chart chart, Area area, ChartEntity info) {
1256: if (info instanceof LegendItemEntity) {
1257: area.setAttribute("entity", "LEGEND");
1258: Integer seq = (Integer) chart
1259: .getAttribute("LEGEND_SEQ");
1260: seq = seq == null ? new Integer(0) : new Integer(seq
1261: .intValue() + 1);
1262: chart.setAttribute("LEGEND_SEQ", seq);
1263: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1264: } else if (info instanceof XYItemEntity) {
1265: area.setAttribute("entity", "DATA");
1266: decodeXYInfo(area, (XYItemEntity) info);
1267: } else {
1268: area.setAttribute("entity", "TITLE");
1269: if (chart.isShowTooltiptext()) {
1270: area.setTooltiptext(chart.getTitle());
1271: }
1272: }
1273: }
1274:
1275: public JFreeChart createChart(Chart chart) {
1276: ChartModel model = (ChartModel) chart.getModel();
1277: if (!(model instanceof XYModel)) {
1278: throw new UiException(
1279: "model must be a org.zkoss.zul.XYModel");
1280: }
1281: return ChartFactory
1282: .createHistogram(
1283: chart.getTitle(),
1284: chart.getXAxis(),
1285: chart.getYAxis(),
1286: (IntervalXYDataset) XYModelToXYDataset((XYModel) model),
1287: getOrientation(chart.getOrient()), chart
1288: .isShowLegend(), chart
1289: .isShowTooltiptext(), true);
1290: }
1291: }
1292:
1293: /** candlestick */
1294: private class Candlestick extends ChartImpl {
1295: public void render(Chart chart, Area area, ChartEntity info) {
1296: if (info instanceof LegendItemEntity) {
1297: area.setAttribute("entity", "LEGEND");
1298: Integer seq = (Integer) chart
1299: .getAttribute("LEGEND_SEQ");
1300: seq = seq == null ? new Integer(0) : new Integer(seq
1301: .intValue() + 1);
1302: chart.setAttribute("LEGEND_SEQ", seq);
1303: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1304: } else if (info instanceof XYItemEntity) {
1305: area.setAttribute("entity", "DATA");
1306: decodeXYInfo(area, (XYItemEntity) info);
1307: } else {
1308: area.setAttribute("entity", "TITLE");
1309: if (chart.isShowTooltiptext()) {
1310: area.setTooltiptext(chart.getTitle());
1311: }
1312: }
1313: }
1314:
1315: public JFreeChart createChart(Chart chart) {
1316: ChartModel model = (ChartModel) chart.getModel();
1317: if (!(model instanceof HiLoModel)) {
1318: throw new UiException(
1319: "model must be a org.zkoss.zul.HiLoModel");
1320: }
1321: return ChartFactory.createCandlestickChart(
1322: chart.getTitle(), chart.getXAxis(), chart
1323: .getYAxis(),
1324: HiLoModelToOHLCDataset((HiLoModel) model), chart
1325: .isShowLegend());
1326: }
1327: }
1328:
1329: /** highlow */
1330: private class Highlow extends ChartImpl {
1331: public void render(Chart chart, Area area, ChartEntity info) {
1332: if (info instanceof LegendItemEntity) {
1333: area.setAttribute("entity", "LEGEND");
1334: Integer seq = (Integer) chart
1335: .getAttribute("LEGEND_SEQ");
1336: seq = seq == null ? new Integer(0) : new Integer(seq
1337: .intValue() + 1);
1338: chart.setAttribute("LEGEND_SEQ", seq);
1339: decodeLegendInfo(area, (LegendItemEntity) info, chart);
1340: } else if (info instanceof XYItemEntity) {
1341: area.setAttribute("entity", "DATA");
1342: decodeXYInfo(area, (XYItemEntity) info);
1343: } else {
1344: area.setAttribute("entity", "TITLE");
1345: if (chart.isShowTooltiptext()) {
1346: area.setTooltiptext(chart.getTitle());
1347: }
1348: }
1349: }
1350:
1351: public JFreeChart createChart(Chart chart) {
1352: ChartModel model = (ChartModel) chart.getModel();
1353: if (!(model instanceof HiLoModel)) {
1354: throw new UiException(
1355: "model must be a org.zkoss.zul.HiLoModel");
1356: }
1357: return ChartFactory.createHighLowChart(chart.getTitle(),
1358: chart.getXAxis(), chart.getYAxis(),
1359: HiLoModelToOHLCDataset((HiLoModel) model), chart
1360: .isShowLegend());
1361: }
1362: }
1363:
1364: private PlotOrientation getOrientation(String orient) {
1365: return "horizontal".equals(orient) ? PlotOrientation.HORIZONTAL
1366: : PlotOrientation.VERTICAL;
1367: }
1368: }
|