Example usage for org.jfree.chart.axis SymbolAxis setStandardTickUnits

List of usage examples for org.jfree.chart.axis SymbolAxis setStandardTickUnits

Introduction

In this page you can find the example usage for org.jfree.chart.axis SymbolAxis setStandardTickUnits.

Prototype

public void setStandardTickUnits(TickUnitSource source) 

Source Link

Document

Sets the source for obtaining standard tick units for the axis and sends an AxisChangeEvent to all registered listeners.

Usage

From source file:src.planning.PlanSimulator.java

public static List<ChartPanel> drawCharts(Element analysis, Element problem) {
    List<ChartPanel> charts = new ArrayList<ChartPanel>();

    // draw the charts
    List<?> variables = analysis.getChild("variables").getChildren("variable");
    for (Iterator<?> iter = variables.iterator(); iter.hasNext();) {
        Element variable = (Element) iter.next();

        Element domainObject = null;
        try {/* w  ww  .j a va  2s . c  o m*/
            XPath path = new JDOMXPath("elements/objects/object[@id='"
                    + variable.getChild("object").getAttributeValue("id") + "']");
            domainObject = (Element) path.selectSingleNode(problem.getParentElement().getParentElement());
        } catch (JaxenException e) {
            e.printStackTrace();
        }

        Element attribute = variable.getChild("object").getChild("attribute");
        Element classAttr = null;
        try {
            XPath path = new JDOMXPath(
                    "project/elements/classes/class[@id='" + attribute.getAttributeValue("class")
                            + "']/attributes/attribute[@id='" + attribute.getAttributeValue("id") + "']");
            classAttr = (Element) path.selectSingleNode(problem.getDocument());
        } catch (JaxenException e) {
            e.printStackTrace();
        }

        String chartTitle = domainObject.getChildText("name") + "." + classAttr.getChildText("name");

        if (variable.getAttributeValue("type").equals("attr")) {
            // attribute
            String attrType = variable.getChild("object").getChild("attribute").getAttributeValue("type");
            if (attrType.equals("1")) { //BOOLEAN attribute

                XYSeriesCollection dataset = new XYSeriesCollection();
                XYSeries series = new XYSeries("Boolean");
                int stepIndex = 0;
                for (Iterator<?> iterator = variable.getChild("values").getChildren("value")
                        .iterator(); iterator.hasNext();) {
                    Element value = (Element) iterator.next();
                    series.add(stepIndex++, (value.getText().equals("false") ? 0 : 1));
                }
                dataset.addSeries(series);

                JFreeChart chart = ChartFactory.createXYStepChart(chartTitle, "Values", "Steps", dataset,
                        PlotOrientation.VERTICAL, false, true, false);

                XYPlot plot = (XYPlot) chart.getPlot();
                NumberAxis domainAxis = new NumberAxis("Steps");
                domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                domainAxis.setAutoRangeIncludesZero(false);
                plot.setDomainAxis(domainAxis);

                String[] values = { "false", "true" };
                SymbolAxis rangeAxis = new SymbolAxis("Values", values);
                plot.setRangeAxis(rangeAxis);

                ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setPreferredSize(new Dimension(chartPanel.getSize().width, 175));

                charts.add(chartPanel);

            } else if (attrType.equals("2") || attrType.equals("3")) { //NUMERIC attributes

                XYSeriesCollection dataset = new XYSeriesCollection();
                XYSeries series = new XYSeries("variable");
                int stepIndex = 0;
                for (Iterator<?> iterator = variable.getChild("values").getChildren("value")
                        .iterator(); iterator.hasNext();) {
                    Element value = (Element) iterator.next();
                    series.add(stepIndex++, Double.parseDouble(value.getText()));
                }
                dataset.addSeries(series);

                JFreeChart chart = ChartFactory.createXYLineChart(chartTitle, "Steps", "Values", dataset,
                        PlotOrientation.VERTICAL, false, true, false);

                XYPlot xyPlot = (XYPlot) chart.getPlot();
                XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyPlot.getRenderer();
                renderer.setShapesVisible(true);
                renderer.setShapesFilled(true);

                NumberAxis rangeAxis = (NumberAxis) xyPlot.getRangeAxis();
                rangeAxis.setAutoRangeIncludesZero(true);
                rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                if (attrType.equals("2")) {
                    NumberAxis domainAxis = (NumberAxis) xyPlot.getDomainAxis();
                    domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                }

                ChartPanel chartPanel = new ChartPanel(chart);

                charts.add(chartPanel);
            } else if (!attrType.equals("4")) {//NOT PRIMITIVE attributes

                Element attrClass = null;
                try {
                    XPath path = new JDOMXPath(
                            "project/elements/classes/class[@id='" + classAttr.getChildText("type") + "']");
                    attrClass = (Element) path.selectSingleNode(problem.getDocument());
                } catch (JaxenException e) {
                    e.printStackTrace();
                }

                if (attrClass != null) {

                    List<Element> classes = XMLUtilities.getClassDescendents(attrClass);
                    String query = "elements/objects/object[";
                    for (Iterator<?> iterator = classes.iterator(); iterator.hasNext();) {
                        Element childClass = (Element) iterator.next();
                        query += "class='" + childClass.getAttributeValue("id") + "'";

                        query += " or ";// the last or is for the parent class                     

                    }
                    query += "class='" + attrClass.getAttributeValue("id") + "']";

                    // get all the objects of all descendant classes, including the parent class
                    List<?> objects = null;
                    try {
                        XPath path = new JDOMXPath(query);
                        objects = path.selectNodes(problem.getParentElement().getParentElement());
                    } catch (JaxenException e) {
                        e.printStackTrace();
                    }
                    if (objects.size() > 0) {
                        //build a list with all the objects names                     
                        String[] names = new String[objects.size() + 1];// the array is for the axis
                        names[0] = "null";// default null value

                        List<String> objectNames = new ArrayList<String>();
                        int i = 1;
                        for (Iterator<?> iterator = objects.iterator(); iterator.hasNext();) {
                            Element object = (Element) iterator.next();
                            names[i++] = object.getChildText("name");
                            objectNames.add(object.getChildText("name").toLowerCase());
                        }

                        XYSeriesCollection dataset = new XYSeriesCollection();
                        XYSeries series = new XYSeries("Objects");
                        int stepIndex = 0;
                        for (Iterator<?> iterator = variable.getChild("values").getChildren("value")
                                .iterator(); iterator.hasNext();) {
                            Element value = (Element) iterator.next();

                            series.add(stepIndex++, objectNames.indexOf(value.getText().toLowerCase()) + 1);
                        }
                        dataset.addSeries(series);

                        // draw the chart
                        JFreeChart chart = ChartFactory.createXYStepChart(chartTitle, "Objects", "Steps",
                                dataset, PlotOrientation.VERTICAL, false, true, false);

                        XYPlot plot = (XYPlot) chart.getPlot();
                        NumberAxis domainAxis = new NumberAxis("Steps");
                        domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                        domainAxis.setAutoRangeIncludesZero(false);
                        plot.setDomainAxis(domainAxis);

                        SymbolAxis rangeAxis = new SymbolAxis("Objects", names);
                        plot.setRangeAxis(rangeAxis);

                        ChartPanel chartPanel = new ChartPanel(chart);

                        charts.add(chartPanel);
                    }
                }
            }
        }
    }

    return charts;
}