Example usage for org.jfree.chart ChartFactory createXYStepChart

List of usage examples for org.jfree.chart ChartFactory createXYStepChart

Introduction

In this page you can find the example usage for org.jfree.chart ChartFactory createXYStepChart.

Prototype

public static JFreeChart createXYStepChart(String title, String xAxisLabel, String yAxisLabel,
        XYDataset dataset, PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls) 

Source Link

Document

Creates a stepped XY plot with default settings.

Usage

From source file:playground.johannes.snowball.Histogram.java

public void plot(String filename, String title) throws IOException {
    fillBins();/*from  w  w  w . jav  a 2s  .  c o m*/
    final XYSeriesCollection data = new XYSeriesCollection();
    final XYSeries wave = new XYSeries(title, false, true);

    double min, max, width;
    //      int size;

    if (bounds != null) {
        min = bounds[0];
        max = bounds[1];
    } else {
        double minmax[] = getMinMax();
        min = minmax[0];
        max = minmax[1];
    }
    if (binWidth > 0) {
        //         size = (int)Math.ceil((max - min)/(double)binWidth);
        width = binWidth;
    } else {
        //         size = bincount;
        width = (max - min) / (double) bincount;
    }

    int cnt = bins.size();
    for (int i = 0; i < cnt; i++) {
        wave.add(i * width + min, bins.get(i));
    }

    data.addSeries(wave);

    final JFreeChart chart = ChartFactory.createXYStepChart("title", "x", "y", data, PlotOrientation.VERTICAL,
            true, // legend
            false, // tooltips
            false // urls
    );

    XYPlot plot = chart.getXYPlot();

    final CategoryAxis axis1 = new CategoryAxis("x");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(new NumberAxis("y"));
    ChartUtilities.saveChartAsPNG(new File(filename), chart, 1024, 768);
}

From source file:playground.christoph.evacuation.analysis.AgentsInEvacuationAreaActivityWriter.java

/**
 * @return a graphic showing the number of agents in the evacuated area
 *//*w w w  .jav  a 2  s. c o m*/
private JFreeChart getGraphic(int[] activities, int[] activitiesParticipatingAtHome,
        int[] activitiesParticipatingNotAtHome, int[] activitiesNotParticipatingAtHome,
        int[] activitiesNotParticipatingNotAtHome) {

    final XYSeriesCollection xyData = new XYSeriesCollection();
    XYSeries dataSerie;

    dataSerie = new XYSeries("total activity performing agents in evacuated area", false, true);
    for (int i = 0; i < activities.length; i++) {
        double hour = i * this.binSize / 60.0 / 60.0;
        dataSerie.add(hour, activities[i]);
    }
    xyData.addSeries(dataSerie);

    dataSerie = new XYSeries("participating agents performing a home activity in evacuated area", false, true);
    for (int i = 0; i < activitiesParticipatingAtHome.length; i++) {
        double hour = i * this.binSize / 60.0 / 60.0;
        dataSerie.add(hour, activitiesParticipatingAtHome[i]);
    }
    xyData.addSeries(dataSerie);

    dataSerie = new XYSeries("participating agents performing an other activity in evacuated area", false,
            true);
    for (int i = 0; i < activitiesParticipatingNotAtHome.length; i++) {
        double hour = i * this.binSize / 60.0 / 60.0;
        dataSerie.add(hour, activitiesParticipatingNotAtHome[i]);
    }
    xyData.addSeries(dataSerie);

    dataSerie = new XYSeries("not participating agents performing a home activity in evacuated area", false,
            true);
    for (int i = 0; i < activitiesNotParticipatingAtHome.length; i++) {
        double hour = i * this.binSize / 60.0 / 60.0;
        dataSerie.add(hour, activitiesNotParticipatingAtHome[i]);
    }
    xyData.addSeries(dataSerie);

    dataSerie = new XYSeries("not participating agents performing an other activity in evacuated area", false,
            true);
    for (int i = 0; i < activitiesNotParticipatingNotAtHome.length; i++) {
        double hour = i * this.binSize / 60.0 / 60.0;
        dataSerie.add(hour, activitiesNotParticipatingNotAtHome[i]);
    }
    xyData.addSeries(dataSerie);

    final JFreeChart chart = ChartFactory.createXYStepChart(
            "activity performing agents in evacuated area, it." + this.iteration, "time", "# agents", xyData,
            PlotOrientation.VERTICAL, true, // legend
            false, // tooltips
            false // urls
    );

    XYPlot plot = chart.getXYPlot();

    final CategoryAxis axis1 = new CategoryAxis("hour");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(new NumberAxis("time"));
    return chart;
}

From source file:playground.christoph.evacuation.analysis.AgentsInEvacuationAreaWriter.java

/**
 * @return a graphic showing the number of agents in the evacuated area
 *//*  w w  w  .  j a  va  2s.  c om*/
private JFreeChart getGraphic(String title, String legend, String modeName, int inputData[]) {

    /*
     * Write only the number of defined picture bins to the plot.
     */
    int data[];
    if (inputData.length > this.nofPictureBins) {
        data = Arrays.copyOfRange(inputData, 0, this.nofPictureBins);
    } else
        data = inputData;

    final XYSeriesCollection xyData = new XYSeriesCollection();
    final XYSeries dataSerie = new XYSeries(legend, false, true);

    for (int i = 0; i < data.length; i++) {
        double hour = i * this.binSize / 60.0 / 60.0;
        dataSerie.add(hour, data[i]);
    }

    xyData.addSeries(dataSerie);
    final JFreeChart chart = ChartFactory.createXYStepChart(title + ", " + modeName + ", it." + this.iteration,
            "time", "# agents", xyData, PlotOrientation.VERTICAL, true, // legend
            false, // tooltips
            false // urls
    );

    XYPlot plot = chart.getXYPlot();

    final CategoryAxis axis1 = new CategoryAxis("hour");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(new NumberAxis("time"));
    return chart;
}

From source file:playground.dgrether.events.handlers.DgGeoFilteredLegHistogram.java

private JFreeChart getGraphic(final ModeData modeData, final String modeName) {
    final XYSeriesCollection xyData = new XYSeriesCollection();
    final XYSeries departuresSerie = new XYSeries("departures", false, true);
    final XYSeries arrivalsSerie = new XYSeries("arrivals", false, true);
    final XYSeries onRouteSerie = new XYSeries("en route", false, true);
    int onRoute = 0;
    for (int i = 0; i < modeData.countsDep.length; i++) {
        onRoute = onRoute + modeData.countsDep[i] - modeData.countsArr[i] - modeData.countsStuck[i];
        double hour = i * this.binSizeSeconds / 60.0 / 60.0;
        departuresSerie.add(hour, modeData.countsDep[i]);
        arrivalsSerie.add(hour, modeData.countsArr[i]);
        onRouteSerie.add(hour, onRoute);
    }//from ww  w.j  a v  a  2s.c  o m

    xyData.addSeries(departuresSerie);
    xyData.addSeries(arrivalsSerie);
    xyData.addSeries(onRouteSerie);

    final JFreeChart chart = ChartFactory.createXYStepChart(
            "Leg Histogram, " + modeName + ", it." + this.iteration, "time", "# vehicles", xyData,
            PlotOrientation.VERTICAL, true, // legend
            false, // tooltips
            false // urls
    );

    XYPlot plot = chart.getXYPlot();
    plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f));
    plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f));
    plot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0f));
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.gray);
    plot.setDomainGridlinePaint(Color.gray);

    final CategoryAxis axis1 = new CategoryAxis("hour");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(new NumberAxis("time"));
    return chart;
}

From source file:org.hxzon.demo.jfreechart.XYDatasetDemo.java

private static JFreeChart createXYStepChart(XYDataset dataset) {

    JFreeChart chart = ChartFactory.createXYStepChart("Legal & General Unit Trust Prices", // title
            "Date", // x-axis label
            "Price Per Unit", // y-axis label
            dataset, // data
            PlotOrientation.VERTICAL, true, // include legend
            true, // tooltips?
            false // URLs?
    );/*from w w w .j av  a  2  s  .c  o  m*/

    chart.setBackgroundPaint(Color.white);

    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);

    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("MM-yyyy"));

    return chart;
}

From source file:playground.christoph.evacuation.analysis.AgentsInEvacuationAreaWriter.java

/**
 * @return a graphic showing the number of agents in the evacuated area
 */// w  ww .  j  a va 2s  .  co  m
private JFreeChart getGraphic(String[] modeNames, int inputData[][]) {

    /*
     * Write only the number of defined picture bins to the plot.
     */
    int data[][];
    data = new int[inputData.length][];
    for (int i = 0; i < inputData.length; i++) {
        if (inputData[i].length > this.nofPictureBins) {
            data[i] = Arrays.copyOfRange(inputData[i], 0, this.nofPictureBins);
        } else
            data[i] = inputData[i];
    }

    final XYSeriesCollection xyData = new XYSeriesCollection();

    for (int j = 0; j < modeNames.length; j++) {
        String modeName = modeNames[j];
        int[] d = data[j];

        XYSeries dataSerie = new XYSeries(modeName, false, true);

        for (int i = 0; i < d.length; i++) {
            double hour = i * this.binSize / 60.0 / 60.0;
            dataSerie.add(hour, d[i]);
        }

        xyData.addSeries(dataSerie);
    }

    final JFreeChart chart = ChartFactory.createXYStepChart(
            "agents in evacuated area, all modes, it." + this.iteration, "time", "# agents", xyData,
            PlotOrientation.VERTICAL, true, // legend
            false, // tooltips
            false // urls
    );

    XYPlot plot = chart.getXYPlot();

    final CategoryAxis axis1 = new CategoryAxis("hour");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(new NumberAxis("time"));
    return chart;
}

From source file:org.matsim.contrib.freight.usecases.analysis.LegHistogram.java

private JFreeChart getGraphic(final ModeData modeData, final String modeName) {
    final XYSeriesCollection xyData = new XYSeriesCollection();
    final XYSeries departuresSerie = new XYSeries("departures", false, true);
    final XYSeries arrivalsSerie = new XYSeries("arrivals", false, true);
    final XYSeries onRouteSerie = new XYSeries("en route", false, true);
    int onRoute = 0;
    for (int i = 0; i < modeData.countsDep.length; i++) {
        onRoute = onRoute + modeData.countsDep[i] - modeData.countsArr[i] - modeData.countsStuck[i];
        double hour = i * this.binSize / 60.0 / 60.0;
        departuresSerie.add(hour, modeData.countsDep[i]);
        arrivalsSerie.add(hour, modeData.countsArr[i]);
        onRouteSerie.add(hour, onRoute);
    }/*from   ww  w.j  a v a2s .  co  m*/

    xyData.addSeries(departuresSerie);
    xyData.addSeries(arrivalsSerie);
    xyData.addSeries(onRouteSerie);

    final JFreeChart chart = ChartFactory.createXYStepChart(
            "Leg Histogram, " + modeName + ", it." + this.iteration, "time", "# vehicles", xyData,
            PlotOrientation.VERTICAL, true, // legend
            false, // tooltips
            false // urls
    );

    XYPlot plot = chart.getXYPlot();

    final CategoryAxis axis1 = new CategoryAxis("hour");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(new NumberAxis("time"));

    plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f));
    plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f));
    plot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0f));
    plot.setBackgroundPaint(Color.white);
    plot.setRangeGridlinePaint(Color.gray);
    plot.setDomainGridlinePaint(Color.gray);

    return chart;
}

From source file:src.gui.LifelinePanel.java

/**
 * this method created and set the graph for showing the timing diagram based
 * on the variable diagram/* ww  w . j  a  va2s  . c  om*/
 */
public void buildLifeLine() {

    //1. get type and context
    String dtype = diagram.getChildText("type");
    String context = diagram.getChildText("context");
    //the frame and lifeline nodes
    Element frame = diagram.getChild("frame");
    String durationStr = frame.getChildText("duration");
    lifelineName = "";
    String objectClassName = "";
    String yAxisName = "";

    float lastIntervalDuration = 0;

    //condition lifeline
    if (dtype.equals("condition")) {

        //check if the context is a action
        if (context.equals("action")) {

            //get action/operator
            Element operatorRef = diagram.getChild("action");
            Element operator = null;
            try {
                XPath path = new JDOMXPath(
                        "elements/classes/class[@id='" + operatorRef.getAttributeValue("class")
                                + "']/operators/operator[@id='" + operatorRef.getAttributeValue("id") + "']");
                operator = (Element) path.selectSingleNode(project);
            } catch (JaxenException e2) {
                e2.printStackTrace();
            }

            if (operator != null) {
                // System.out.println(operator.getChildText("name"));
                //System.out.println("Life line id "+ lifeline.getAttributeValue("id"));

                //get the object (can be a parametr. literal, or object)
                Element objRef = lifeline.getChild("object");
                Element attrRef = lifeline.getChild("attribute");

                //get object class
                Element objClass = null;
                try {
                    XPath path = new JDOMXPath(
                            "elements/classes/class[@id='" + objRef.getAttributeValue("class") + "']");
                    objClass = (Element) path.selectSingleNode(project);
                } catch (JaxenException e2) {
                    e2.printStackTrace();
                }

                Element attribute = null;
                try {
                    XPath path = new JDOMXPath(
                            "elements/classes/class[@id='" + attrRef.getAttributeValue("class")
                                    + "']/attributes/attribute[@id='" + attrRef.getAttributeValue("id") + "']");
                    attribute = (Element) path.selectSingleNode(project);
                } catch (JaxenException e2) {
                    e2.printStackTrace();
                }

                yAxisName = attribute.getChildText("name");

                //if (objClass!=null)
                Element object = null;

                //check what is this object (parameterof an action, object, literal)
                if (objRef.getAttributeValue("element").equals("parameter")) {
                    //get parameter in the action

                    try {
                        XPath path = new JDOMXPath(
                                "parameters/parameter[@id='" + objRef.getAttributeValue("id") + "']");
                        object = (Element) path.selectSingleNode(operator);
                    } catch (JaxenException e2) {
                        e2.printStackTrace();
                    }
                    String parameterStr = object.getChildText("name");

                    lifelineName = parameterStr + ":" + objClass.getChildText("name");
                    objectClassName = parameterStr + ":" + objClass.getChildText("name");
                }
                //

                //set suround border
                Border etchedBdr = BorderFactory.createEtchedBorder();
                Border titledBdr = BorderFactory.createTitledBorder(etchedBdr,
                        "lifeline(" + lifelineName + ")");
                //Border titledBdr = BorderFactory.createTitledBorder(etchedBdr, "");
                Border emptyBdr = BorderFactory.createEmptyBorder(10, 10, 10, 10);
                Border compoundBdr = BorderFactory.createCompoundBorder(titledBdr, emptyBdr);
                this.setBorder(compoundBdr);

                //Boolean attribute
                if (attribute.getChildText("type").equals("1")) {
                    lifelineName += " - " + attribute.getChildText("name");

                    Element timeIntervals = lifeline.getChild("timeIntervals");

                    XYSeriesCollection dataset = new XYSeriesCollection();
                    XYSeries series = new XYSeries("Boolean");
                    for (Iterator<Element> it1 = timeIntervals.getChildren().iterator(); it1.hasNext();) {
                        Element timeInterval = it1.next();
                        boolean insertPoint = true;

                        Element durationConstratint = timeInterval.getChild("durationConstratint");
                        Element lowerbound = durationConstratint.getChild("lowerbound");
                        Element upperbound = durationConstratint.getChild("upperbound");
                        Element value = timeInterval.getChild("value");

                        //Add for both lower and upper bound

                        //lower bound
                        float lowerTimePoint = 0;
                        try {
                            lowerTimePoint = Float.parseFloat(lowerbound.getAttributeValue("value"));
                            lastIntervalDuration = lowerTimePoint;
                        } catch (Exception e) {
                            insertPoint = false;
                        }
                        //System.out.println("    > point     x= "+ Float.toString(lowerTimePoint)+ " ,  y= "+ lowerbound.getAttributeValue("value"));
                        if (insertPoint) {
                            series.add(lowerTimePoint, (value.getText().equals("false") ? 0 : 1));
                        }

                        //upper bound
                        float upperTimePoint = 0;
                        try {
                            upperTimePoint = Float.parseFloat(upperbound.getAttributeValue("value"));
                            lastIntervalDuration = upperTimePoint;
                        } catch (Exception e) {
                            insertPoint = false;
                        }
                        //System.out.println("    > point     x= "+ Float.toString(upperTimePoint)+ " ,  y= "+ lowerbound.getAttributeValue("value"));
                        if (insertPoint && upperTimePoint != lowerTimePoint) {
                            series.add(upperTimePoint, (value.getText().equals("false") ? 0 : 1));
                        }

                    }
                    dataset.addSeries(series);

                    //chart = ChartFactory.createXYStepChart(lifelineName, "time", "value", dataset, PlotOrientation.VERTICAL, false, true, false);
                    chart = ChartFactory.createXYStepChart(attribute.getChildText("name"), "time", "value",
                            dataset, PlotOrientation.VERTICAL, false, true, false);
                    chart.setBackgroundPaint(Color.WHITE);

                    XYPlot plot = (XYPlot) chart.getPlot();
                    plot.setBackgroundPaint(Color.WHITE);

                    NumberAxis domainAxis = new NumberAxis("Time");
                    domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
                    domainAxis.setAutoRangeIncludesZero(false);

                    //set timing ruler
                    if (durationStr.trim().equals("")) {
                        if (lastIntervalDuration > 0)
                            domainAxis.setUpperBound(lastIntervalDuration + timingRulerAdditional);
                        else
                            domainAxis.setUpperBound(10.0);
                    } else {
                        try {
                            float dur = Float.parseFloat(durationStr);
                            if (dur >= lastIntervalDuration) {
                                domainAxis.setUpperBound(dur + timingRulerAdditional);
                            } else {
                                domainAxis.setUpperBound(lastIntervalDuration + timingRulerAdditional);
                            }
                        } catch (Exception e) {
                            if (lastIntervalDuration > 0)
                                domainAxis.setUpperBound(lastIntervalDuration + timingRulerAdditional);
                            else
                                domainAxis.setUpperBound(10.0);
                        }

                    }

                    plot.setDomainAxis(domainAxis);

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

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

                    JLabel title = new JLabel("<html><b><u>" + objectClassName + "</u></b></html>");
                    title.setBackground(Color.WHITE);

                    this.add(title, BorderLayout.WEST);
                    this.add(chartPanel, BorderLayout.CENTER);

                }

            }

        }
        //if this is a possible sequence of action being modeled to a condition
        else if (context.equals("general")) {

        }

    } else if (dtype.equals("state")) {

    }

}

From source file:SciTK.PlotXYStep.java

/** Initialization routine (common to both constructors) */
private void init(String x_label, String y_label, String window_title, double x_min, double x_max) {
    chart = ChartFactory.createXYStepChart("", x_label, y_label, data, PlotOrientation.VERTICAL, false, true,
            false);/*from  w  w  w  .  j  ava2 s .  c  om*/

    chart.setBackgroundPaint(Color.white);
    XYPlot plot = chart.getXYPlot(); // the plot itself

    // Use a step renderer for this type of chart:
    XYStepRenderer renderer = new XYStepRenderer();
    renderer.setBaseStroke(new BasicStroke(2.0f));
    renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
    renderer.setDefaultEntityRadius(6);
    renderer.setLegendItemLabelGenerator(new MultipleXYSeriesLabelGenerator());
    // need to tell the plot to use this renderer
    plot.setRenderer(renderer);

    // create new axis with range set by dataset max/min:
    NumberAxis domainAxis = new NumberAxis(x_label);
    domainAxis.setRange(x_min, x_max);
    plot.setDomainAxis(domainAxis);

    // for some reason default is white, change it to black:
    setGridlineColor(Color.BLACK);

    super.window_title = window_title;
    super.initUI();
}

From source file:playground.johannes.socialnets.GraphStatistics.java

public static JFreeChart makeChart(Histogram1D hist, String title) {
    final XYSeriesCollection data = new XYSeriesCollection();
    final XYSeries wave = new XYSeries(title, false, true);

    for (int i = 0; i < 100; i++) {
        wave.add(hist.xAxis().binCentre(i), hist.binHeight(i));
    }/*from w w  w . j  ava2 s  . c  o  m*/

    data.addSeries(wave);

    final JFreeChart chart = ChartFactory.createXYStepChart("title", "x", "y", data, PlotOrientation.VERTICAL,
            true, // legend
            false, // tooltips
            false // urls
    );

    XYPlot plot = chart.getXYPlot();

    final CategoryAxis axis1 = new CategoryAxis("x");
    axis1.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 7));
    plot.setDomainAxis(new NumberAxis("y"));
    return chart;
}