From: G?nther B. <br...@us...> - 2002-05-19 08:36:46
|
Update of /cvsroot/xpg-xml/edu/iicm/xpg/transitions/simple In directory usw-pr-cvs1:/tmp/cvs-serv20099/transitions/simple Added Files: WriteChartTransition.java Log Message: new staff for piechart-example --- NEW FILE: WriteChartTransition.java --- /*********************************************************************** * * Copyright (c) 2001 IICM, Graz University of Technology * Schiesstattgasse 4a, A-8010 Graz, Austria. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License (LGPL) * as published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ***********************************************************************/ package edu.iicm.xpg.transitions.simple; import edu.iicm.xpg.statemachine.Transition; import edu.iicm.xpg.statemachine.State; import edu.iicm.xpg.statemachine.StateMachine; import edu.iicm.xpg.statemachine.Input; import edu.iicm.xpg.statemachine.XMLInput; import edu.iicm.xpg.statemachine.XMLElement; import edu.iicm.xpg.statemachine.DataObject; import edu.iicm.xpg.statemachine.Const; import edu.iicm.xpg.util.Debug; import net.sourceforge.chart2d.Chart2DProperties; import net.sourceforge.chart2d.GraphProperties; import net.sourceforge.chart2d.GraphChart2DProperties; import net.sourceforge.chart2d.Dataset; import net.sourceforge.chart2d.PieChart2DProperties; import net.sourceforge.chart2d.PieChart2D; import net.sourceforge.chart2d.LBChart2D; import java.util.Vector; import java.util.Enumeration; import java.io.File; import javax.imageio.ImageIO; import javax.swing.JFrame; import java.awt.Dimension; import java.awt.Toolkit; //---------------------------------------------------------------------- /** * @author Guenther Brand * @version $Revision: 1.1 $ */ public class WriteChartTransition implements Transition { //---------------------------------------------------------------------- /** * @param input the input that triggered this transition * @param from_state the start state for this transition * @param to_state the destination state for this transition * @param machine the state machine that this transition belongs to * @param data user defined data * @return * @exception Exception whatever an implementation can throw */ public String transitionTriggered(Input input,State from_state,State to_state, StateMachine machine,DataObject data) throws Exception { StringBuffer result = (StringBuffer)data.getObject(Const.RESULT_BUFFER); Vector list_items = (Vector)data.removeObject("list items"); String list_title = ((XMLElement)data.removeObject("title")).getValue(); String chart_type = data.getXMLElement().getAttributes().getValue("chart"); String chart_filename = list_title.trim(); result.append("\n<h2>" + list_title + "<h2>\n"); //<-- Begin Chart2D configuration --> //Configure chart properties Chart2DProperties chart2DProps = new Chart2DProperties(); chart2DProps.setChartTitleText(list_title); if (chart_type.equals("pie")) { //Configure graph properties Dataset dataset = new Dataset (list_items.size(), 1, 1); String[] legendLabels = new String[list_items.size()]; for (int idx = 0; idx < list_items.size(); idx++) { String[] item = (String[])list_items.get(idx); legendLabels[idx] = item[0]; String value = item[1]; dataset.set(idx, 0, 0, new Float(value).floatValue()); } PieChart2DProperties pieChart2DProps = new PieChart2DProperties (chart2DProps, dataset); pieChart2DProps.setLegendLabelsTexts (legendLabels); PieChart2D chart2D = new PieChart2D (pieChart2DProps); //<-- End Chart2D configuration --> JFrame frame = new JFrame(); frame.getContentPane().add (chart2D); //Add Chart2D to GUI frame.setTitle (list_title); frame.pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation ( (screenSize.width - frame.getSize().width) / 2, (screenSize.height - frame.getSize().height) / 2 ); ImageIO.write(chart2D.getImage(), "PNG", new File(chart_filename)); } else if (chart_type.equals("line")) { //Configure graph properties Dataset dataset = new Dataset(1, list_items.size(), 1); String[] axisLabels = new String[list_items.size()]; for (int idx = 0; idx < list_items.size(); idx++) { String[] item = (String[])list_items.get(idx); axisLabels[idx] = item[0]; String value = item[1]; dataset.set(0, idx, 0, new Float(value).floatValue()); } GraphProperties graphProps = new GraphProperties (dataset); graphProps.setGraphBarsExistence (false); graphProps.setGraphLinesExistence (true); graphProps.setGraphLinesFillInterior (true); graphProps.setGraphAllowComponentAlignment (true); graphProps.setGraphBetweenComponentsGapExistence (false); //Configure graph chart properties GraphChart2DProperties graphChart2DProps = new GraphChart2DProperties (chart2DProps, graphProps); graphChart2DProps.setLabelsAxisLabelsTexts (axisLabels); graphChart2DProps.setLabelsAxisTicksAlignment (graphChart2DProps.CENTERED); //Configure chart LBChart2D chart2D = new LBChart2D (graphChart2DProps); //<-- End Chart2D configuration --> JFrame frame = new JFrame(); frame.getContentPane().add (chart2D); //Add Chart2D to GUI frame.setTitle (list_title); frame.pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation ( (screenSize.width - frame.getSize().width) / 2, (screenSize.height - frame.getSize().height) / 2 ); ImageIO.write(chart2D.getImage(), "PNG", new File(chart_filename)); } result.append("\n<img href=\"" + chart_filename + "\">\n"); return(null); } } |