📄 chart.java
字号:
} /** * Set the foreground alpha (transparency, 0 ~ 255). * @param alpha the transparency of foreground color (0 ~ 255, default to 255 opaque). */ public void setFgAlpha(int alpha) { if (alpha == _fgAlpha) { return; } if (alpha > 255 || alpha < 0) { alpha = 255; } _fgAlpha = alpha; smartDrawChart(); } /** * Get the foreground alpha (transparency, 0 ~ 255, opacue). */ public int getFgAlpha() { return _fgAlpha; } /** * Set the background alpha (transparency, 0 ~ 255). * @param alpha the transparency of background color (0 ~ 255, default to 255 opaque). */ public void setBgAlpha(int alpha) { if (alpha == _bgAlpha) { return; } if (alpha > 255 || alpha < 0) { alpha = 255; } _bgAlpha = alpha; smartDrawChart(); } /** * Get the background alpha (transparency, 0 ~ 255, opacue). */ public int getBgAlpha() { return _bgAlpha; } /** * Set the background color of the chart. * @param color in #RRGGBB format (hexdecimal). */ public void setBgColor(String color) { if (Objects.equals(color, _bgColor)) { return; } _bgColor = color; if (_bgColor == null) { _bgRGB = null; } else { _bgRGB = new int[3]; decode(_bgColor, _bgRGB); } smartDrawChart(); } /** * Get the background color of the chart (in string as #RRGGBB). * null means default. */ public String getBgColor() { return _bgColor; } /** * Get the background color in int array (0: red, 1: green, 2:blue). * null means default. */ public int[] getBgRGB() { return _bgRGB; } /** * Set the chart orientation. * @param orient vertical or horizontal (default to vertical) */ public void setOrient(String orient) { if (Objects.equals(orient, _orient)) { return; } _orient = orient; smartDrawChart(); } /** * Get the chart orientation (vertical or horizontal) */ public String getOrient() { return _orient; } /** Returns the time zone that this Time Series Chart belongs to, or null if * the default time zone is used. * <p>The default time zone is determined by {@link org.zkoss.util.TimeZones#getCurrent}. */ public TimeZone getTimeZone() { return _tzone; } /** Sets the time zone that this Time Series Chart belongs to, or null if * the default time zone is used. * <p>The default time zone is determined by {@link org.zkoss.util.TimeZones#getCurrent}. */ public void setTimeZone(TimeZone tzone) { if (Objects.equals(tzone, _tzone)) { return; } _tzone = tzone; smartDrawChart(); } /** Returns the period used in Time Series Chart. The value can be * "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year". * default is "millisecond" if not specified. */ public String getPeriod() { return _period; } /** Sets the period used in Time Series Chart. The value can be * "millisecond", "second", "minute", "hour", "day", "week", "month", "quarter", and "year". */ public void setPeriod(String period) { if (Objects.equals(period, _period)) { return; } _period = period; smartDrawChart(); } /** Returns the chart model associated with this chart, or null * if this chart is not associated with any chart data model. */ public ChartModel getModel() { return _model; } /** Sets the chart model associated with this chart. * If a non-null model is assigned, no matter whether it is the same as * the previous, it will always cause re-render. * * @param model the chart model to associate, or null to dis-associate * any previous model. * @exception UiException if failed to initialize with the model */ public void setModel(ChartModel model) { if (_model != model) { initDataListener(); if (_model != null) { _model.removeChartDataListener(_dataListener); } if (model != null) { model.addChartDataListener(_dataListener); } _model = model; } //Always redraw smartDrawChart(); } /** Sets the model by use of a class name. * It creates an instance automatically. */ public void setModel(String clsnm) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, java.lang.reflect.InvocationTargetException { if (clsnm != null) { setModel((ChartModel)Classes.newInstanceByThread(clsnm)); } } /** Returns the implemetation chart engine. * @exception UiException if failed to load the engine. */ public ChartEngine getEngine() throws UiException { if (_engine == null) _engine = newChartEngine(); return _engine; } /** Instantiates the default chart engine. * It is called, if {@link #setEngine} is not called with non-null * engine. * * <p>By default, it looks up the component attribute called * chart-engine. If found, the value is assumed to be the class * or the class name of the default engine (it must implement * {@link ChartEngine}). * If not found, {@link UiException} is thrown. * * <p>Derived class might override this method to provide your * own default class. * * @exception UiException if failed to instantiate the engine * @since 3.0.0 */ protected ChartEngine newChartEngine() throws UiException { Object v = getAttribute("chart-engine"); if (v == null) v = "org.zkoss.zkex.zul.impl.JFreeChartEngine"; try { final Class cls; if (v instanceof String) { cls = Classes.forNameByThread((String)v); } else if (v instanceof Class) { cls = (Class)v; } else { throw new UiException(v != null ? "Unknown chart-engine, "+v: "The chart-engine attribute is not defined"); } v = cls.newInstance(); } catch (Exception ex) { throw UiException.Aide.wrap(ex); } if (!(v instanceof ChartEngine)) throw new UiException(ChartEngine.class + " must be implemented by "+v); return (ChartEngine)v; } /** Sets the chart engine. */ public void setEngine(ChartEngine engine) { if (_engine != engine) { _engine = engine; } //Always redraw smartDrawChart(); } /** Sets the chart engine by use of a class name. * It creates an instance automatically. */ public void setEngine(String clsnm) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, java.lang.reflect.InvocationTargetException { if (clsnm != null) { setEngine((ChartEngine)Classes.newInstanceByThread(clsnm)); } } private void initDataListener() { if (_dataListener == null) { _dataListener = new ChartDataListener() { public void onChange(ChartDataEvent event) { smartDrawChart(); } }; } } /** Returns the renderer to render each area, or null if the default * renderer is used. */ public ChartAreaListener getAreaListener() { return _areaListener; } /** Sets the renderer which is used to render each area. * * <p>Note: changing a render will not cause the chart to re-render. * If you want it to re-render, you could call smartDraw. * * @param listener the area listener, or null to ignore it. * @exception UiException if failed to initialize. */ public void setAreaListener(ChartAreaListener listener) { if (_areaListener != listener) { _areaListener = listener; } } /** Sets the renderer by use of a class name. * It creates an instance automatically. */ public void setAreaListener(String clsnm) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, java.lang.reflect.InvocationTargetException { if (clsnm != null) { setAreaListener((ChartAreaListener)Classes.newInstanceByThread(clsnm)); } } /** * mark a draw flag to inform that this Chart needs update. */ protected void smartDrawChart() { if (_smartDrawChart) { //already mark smart draw return; } _smartDrawChart = true; if (_smartDrawChartListener == null) { _smartDrawChartListener = new EventListener() { public void onEvent(Event event) { if (Strings.isBlank(getType())) throw new UiException("chart must specify type (pie, bar, line, ...)"); if (_model == null) throw new UiException("chart must specify a data model"); if (Strings.isBlank(getWidth())) throw new UiException("chart must specify width"); if (Strings.isBlank(getHeight())) throw new UiException("chart must specify height"); try { final String title = getTitle(); final AImage image = new AImage("chart"+new Date().getTime(), getEngine().drawChart(Chart.this)); setContent(image); } catch(java.io.IOException ex) { throw UiException.Aide.wrap(ex); } finally { _smartDrawChart = false; } } }; addEventListener("onSmartDrawChart", _smartDrawChartListener); } Events.postEvent("onSmartDrawChart", this, null); } //-- utilities --// /*package*/ static void decode(String color, int[] rgb) { if (color == null) { return; } if (color.length() != 7 || !color.startsWith("#")) { throw new UiException("Incorrect color format (#RRGGBB) : "+color); } rgb[0] = Integer.parseInt(color.substring(1, 3), 16); rgb[1] = Integer.parseInt(color.substring(3, 5), 16); rgb[2] = Integer.parseInt(color.substring(5, 7), 16); } /*package*/ static int stringToInt(String str) { int j = str.lastIndexOf("px"); if (j > 0) { final String num = str.substring(0, j); return Integer.parseInt(num); } j = str.lastIndexOf("pt"); if (j > 0) { final String num = str.substring(0, j); return (int) (Integer.parseInt(num) * 1.3333); } j = str.lastIndexOf("em"); if (j > 0) { final String num = str.substring(0, j); return (int) (Integer.parseInt(num) * 13.3333); } return Integer.parseInt(str); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -