001: package xui.samples.carousel.components;
002:
003: import com.xoetrope.export.image.XImageExportHelper;
004: import net.xoetrope.xui.XPage;
005: import com.xoetrope.swing.XGraph;
006: import java.awt.Component;
007: import java.io.IOException;
008: import javax.swing.JFileChooser;
009: import net.xoetrope.xui.XProjectManager;
010: import net.xoetrope.xui.data.XModel;
011:
012: /**
013: * Demonstarte some features of the charting component
014: * <p>Copyright: Xoetrope Ltd. (c) 2001-2006</p>
015: * <p>License: see license.txt</p>
016: * <p>$Revision: 1.6 $</p>
017: */
018: public class LineChart extends XPage {
019: private XGraph chart;
020:
021: private double[] capacityPoints;
022: private int numSeries;
023: private String[] seriesNames;
024:
025: /** Creates a new instance of LineChart */
026: public LineChart() {
027: }
028:
029: /**
030: * Do some chart setup
031: */
032: public void pageCreated() {
033: chart = (XGraph) findComponent("lineChart");
034:
035: String[] names = { "Series 1", "Series 2", "Series 3",
036: "Series 4", "Series 5" };
037: seriesNames = names;
038: setupChart(false, false);
039: }
040:
041: /**
042: * A response handler for the radio buttons. Uses the mode attribute to
043: * determine the chart setting
044: */
045: public void setMode() {
046: Component comp = (Component) getCurrentEvent().getSource();
047: int mode = Integer
048: .parseInt(getAttribute("mode", comp.getName())
049: .toString());
050: chart.setMode(mode);
051: }
052:
053: /**
054: * A response handler for the fade button - toggles the chart animation
055: */
056: public void toggleFade() {
057: chart.toggleYAxisAnimation();
058: }
059:
060: /**
061: * Setup the chart
062: * @param chartType the type of chart e.g. performance, current etc..
063: * @param seriesNames the names of the data series
064: * @param animate true to animate
065: */
066: public void showChart(int chartType, String[] seriesNames,
067: boolean animate) {
068: double[] points;
069: chart.setTitle("Line Chart Demo");
070: chart.setXScale(-45, 20, 0, "Evaporating temperature (°C)",
071: null);
072: chart.setYScale(chart.LOGARITHMIC, "Cooling capacity (W)");
073: points = capacityPoints;
074: chart.setData(points, numSeries, seriesNames, animate);
075: chart.repaint();
076: }
077:
078: /**
079: * Setup the chart data
080: * @param animate true to animate
081: * @param outputTable true to output data to tables instead of charts
082: */
083: protected void setupChart(boolean animate, boolean outputTable) {
084: XModel root = XProjectManager.getModel();
085: XModel chartData = (XModel) root.get("Outcome");
086: if (chartData != null) {
087: chart.setTitle("Opportunities by Outcome");
088: chart.setXScale(-45, 20, 0, "Outcome", null);
089: chart.setYScale(chart.LOGARITHMIC, "Value");
090: chart.setModelStructure(chart.SERIES, 1);
091: chart.setModelStructure(chart.LABELS, 0);
092: chart.setModelStructure(chart.VALUES, 2);
093: chart.setModel(chartData);
094: } else {
095: numSeries = 5;
096: int numPoints = 14;
097: int seriesPts = numPoints * 2 * numSeries;
098: capacityPoints = new double[seriesPts];
099:
100: double te[] = { -45.0, -40.0, -35.0, -30.0, -25.0, -20.0,
101: -15.0, -10.0, -5.0, 0.0, 5.0, 10.0, 15.0, 20.0 };
102: double ts[] = { 0.9, 1.0, 1.1, 2.0, 2.1 };
103:
104: int idx = 0;
105: for (int j = 0; j < numSeries; j++) {
106: for (int i = 0; i < numPoints; i++) {
107: capacityPoints[idx++] = te[i];
108: capacityPoints[idx++] = te[i] * te[i] * ts[j];
109: }
110: }
111: showChart(1, seriesNames, animate);
112: }
113: }
114:
115: /**
116: * Print the chart
117: */
118: public void printChart() {
119: try {
120: JFileChooser fc = new JFileChooser();
121: int rc = fc.showOpenDialog(this );
122: if (rc == fc.APPROVE_OPTION)
123: XImageExportHelper.writePng(chart, fc.getSelectedFile()
124: .getCanonicalPath(), -1, -1);
125: } catch (IOException ioe) {
126: ioe.printStackTrace();
127: }
128: }
129: }
|