Example usage for org.jfree.data.time TimeSeriesCollection TimeSeriesCollection

List of usage examples for org.jfree.data.time TimeSeriesCollection TimeSeriesCollection

Introduction

In this page you can find the example usage for org.jfree.data.time TimeSeriesCollection TimeSeriesCollection.

Prototype

public TimeSeriesCollection() 

Source Link

Document

Constructs an empty dataset, tied to the default timezone.

Usage

From source file:com.jonas.testing.jfreechart.demos.DemoDatasetFactory.java

/**
 * Creates a time series collection containing JPY/GBP exchange rates.
 *
 * @return a sample time series collection.
 *//*from w ww. j  a  v  a  2 s.  c om*/
public static TimeSeriesCollection createTimeSeriesCollection2() {

    final TimeSeriesCollection data = new TimeSeriesCollection();
    data.addSeries(createJPYTimeSeries());
    return data;

}

From source file:org.jstockchart.plot.TimeseriesPlot.java

private XYPlot createPricePlot() {
    Font axisFont = new Font("Arial", 0, 12);
    Stroke stroke = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.CAP_SQUARE, 0.0f,
            new float[] { 1.0f, 1.0f }, 1.0f);
    PriceArea priceArea = timeseriesArea.getPriceArea();
    Color averageColor = new Color(243, 182, 117);
    priceArea.setAverageColor(averageColor);
    priceArea.setPriceColor(Color.BLUE);
    TimeSeriesCollection priceDataset = new TimeSeriesCollection();
    priceDataset.addSeries(dataset.getPriceTimeSeries().getTimeSeries());
    if (priceArea.isAverageVisible()) {
        priceDataset.addSeries(dataset.getAverageTimeSeries().getTimeSeries());
    }/*w w  w.ja  v a 2 s.  com*/

    CentralValueAxis logicPriceAxis = priceArea.getLogicPriceAxis();

    logicPriceAxis.setTickCount(7);

    CFXNumberAxis priceAxis = new CFXNumberAxis(logicPriceAxis.getLogicTicks());
    priceAxis.setShowUD(true);
    priceAxis.setOpenPrice(logicPriceAxis.getCentralValue().doubleValue());
    priceAxis.setTickMarksVisible(false);
    XYLineAndShapeRenderer priceRenderer = new XYLineAndShapeRenderer(true, false);
    priceAxis.setUpperBound(logicPriceAxis.getUpperBound());
    priceAxis.setLowerBound(logicPriceAxis.getLowerBound());
    priceAxis.setAxisLineVisible(false);
    priceAxis.setTickLabelFont(axisFont);
    priceRenderer.setSeriesPaint(0, priceArea.getPriceColor());
    priceRenderer.setSeriesPaint(1, priceArea.getAverageColor());

    CFXNumberAxis rateAxis = new CFXNumberAxis(logicPriceAxis.getRatelogicTicks());
    rateAxis.setShowUD(true);
    rateAxis.setOpenPrice(logicPriceAxis.getCentralValue().doubleValue());
    rateAxis.setTickMarksVisible(false);
    ;
    rateAxis.setTickLabelFont(axisFont);
    rateAxis.setAxisLineVisible(false);
    rateAxis.setUpperBound(logicPriceAxis.getUpperBound());
    rateAxis.setLowerBound(logicPriceAxis.getLowerBound());
    XYPlot plot = new XYPlot(priceDataset, null, priceAxis, priceRenderer);
    plot.setBackgroundPaint(priceArea.getBackgroudColor());
    plot.setOrientation(priceArea.getOrientation());
    plot.setRangeAxisLocation(priceArea.getPriceAxisLocation());
    plot.setRangeMinorGridlinesVisible(false);

    Stroke outLineStroke = new BasicStroke(1f, BasicStroke.CAP_BUTT, BasicStroke.CAP_SQUARE, 0.0f,
            new float[] { 1.0f, 1.0f }, 1.0f);
    Stroke gridLineStroke = new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0.0f,
            new float[] { 2.0f, 2.0f }, 1.0f);

    plot.setRangeGridlineStroke(gridLineStroke);
    plot.setDomainGridlineStroke(gridLineStroke);
    plot.setRangeGridlinesVisible(true);
    plot.setDomainGridlinesVisible(true);
    plot.setOutlineVisible(true);
    plot.setOutlineStroke(outLineStroke);
    plot.setOutlinePaint(Color.BLACK);

    if (priceArea.isRateVisible()) {
        plot.setRangeAxis(1, rateAxis);
        plot.setRangeAxisLocation(1, priceArea.getRateAxisLocation());
        plot.setDataset(1, null);
        plot.mapDatasetToRangeAxis(1, 1);
    }

    if (priceArea.isMarkCentralValue()) {
        Number centralPrice = logicPriceAxis.getCentralValue();
        if (centralPrice != null) {
            plot.addRangeMarker(new ValueMarker(centralPrice.doubleValue(), priceArea.getCentralPriceColor(),
                    new BasicStroke()));
        }
    }
    return plot;
}

From source file:org.jfree.chart.demo.TimeSeriesDemo11.java

/**
 * Creates a sample dataset./* w ww  .  j  a va2s .c o m*/
 * 
 * @param name  the dataset name.
 * @param base  the starting value.
 * @param start  the starting period.
 * @param count  the number of values to generate.
 *
 * @return The dataset.
 */
private XYDataset createDataset(final String name, final double base, final RegularTimePeriod start,
        final int count) {

    final TimeSeries series = new TimeSeries(name, start.getClass());
    RegularTimePeriod period = start;
    double value = base;
    for (int i = 0; i < count; i++) {
        series.add(period, value);
        period = period.previous();
        value = value * (1 + (Math.random() - 0.495) / 10.0);
    }

    final TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);

    return dataset;

}

From source file:gui.DemoDatasetFactory.java

/**
 * Creates a time series collection containing JPY/GBP exchange rates.
 *
 * @return a sample time series collection.
 *///from ww w  .j a va 2  s .  c  o  m
public static TimeSeriesCollection createTimeSeriesCollection2() {

    TimeSeriesCollection data = new TimeSeriesCollection();
    data.addSeries(createJPYTimeSeries());
    return data;

}

From source file:de.dal33t.powerfolder.ui.information.stats.StatsInformationCard.java

private JPanel getAveragePanel() {
    DateAxis domain = new DateAxis(Translation.getTranslation("stats_information_card.date"));
    TimeSeriesCollection series = new TimeSeriesCollection();
    NumberAxis axis = new NumberAxis(Translation.getTranslation("stats_information_card.percentage"));

    series.addSeries(percentageBandwidthSeries);

    XYItemRenderer renderer = new StandardXYItemRenderer();
    XYPlot plot = new XYPlot(series, domain, axis, renderer);
    JFreeChart graph = new JFreeChart(plot);
    ChartPanel cp = new ChartPanel(graph);

    FormLayout layout = new FormLayout("3dlu, fill:pref:grow, 3dlu",
            "3dlu, pref , 3dlu, pref, 3dlu, fill:pref:grow, 3dlu");
    DefaultFormBuilder builder = new DefaultFormBuilder(layout);
    CellConstraints cc = new CellConstraints();

    JPanel p = buildPercentStatsControlPanel();

    builder.add(p, cc.xy(2, 2));//  w  w  w  . j  a  v  a2s . com
    builder.addSeparator(null, cc.xyw(1, 4, 3));
    builder.add(cp, cc.xy(2, 6));
    return builder.getPanel();
}

From source file:org.sunzoft.sunstock.StockMain.java

protected XYDataset initIndexData() {
    TimeSeries ts1 = new TimeSeries("");
    float c = profits.get(0).capital;
    for (AccountStatus td : profits) {
        ts1.add(new Day(Integer.parseInt(td.date.substring(6)), Integer.parseInt(td.date.substring(4, 6)),
                Integer.parseInt(td.date.substring(0, 4))), 1 + (td.market - td.capital) / c);
    }/*from  w  w  w.  j ava  2s.  co  m*/
    TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
    localTimeSeriesCollection.addSeries(ts1);
    return localTimeSeriesCollection;
}

From source file:eu.hydrologis.jgrass.charting.impl.JGrassXYTimeBarChart.java

public JGrassXYTimeBarChart(String[] chartTitles, double[][][] chartValues, Class<?> timeClass,
        double barWidth) {
    try {/*from   www  .  j  a v  a  2  s .c o m*/
        chartSeries = new TimeSeries[chartValues.length];

        constructor = (Constructor<RegularTimePeriod>) timeClass.getConstructor(Date.class);

        for (int i = 0; i < chartTitles.length; i++) {
            final String title = chartTitles[i];
            final double[][] values = chartValues[i];

            chartSeries[i] = new TimeSeries(title, timeClass);
            for (int j = 0; j < values[0].length; j++) {
                // important: the data matrix has to be passed as two rows (not
                // two columns)
                double val = values[1][j];
                if (isNovalue(val))
                    continue;
                chartSeries[i].add(constructor.newInstance(new Date((long) values[0][j])), val);
            }
        }

        lineDataset = new TimeSeriesCollection();
        for (int i = 0; i < chartSeries.length; i++) {
            lineDataset.addSeries(chartSeries[i]);
        }
        lineDataset.setXPosition(TimePeriodAnchor.MIDDLE);
        lineDataset.setDomainIsPointsInTime(true);

        if (barWidth != -1)
            dataset = new XYBarDataset(lineDataset, barWidth);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.mwc.cmap.LiveDataMonitor.views.LiveDataMonitor.java

/**
 * a watchable item has been selected, better show it
 * //from   www  . j a va  2s.  c  o m
 * @param attribute
 *          what we're going to watch
 */
private void storeDataset(final IAttribute attribute, final Object index) {
    final Vector<DataDoublet> data = attribute.getHistoricValues(index);

    // is there any data in it?
    if (data.size() == 0) {
        _chart.setTitle(attribute.getName());
        _chart.getXYPlot().setDataset(null);
    } else {
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        final TimeSeries series = new TimeSeries(attribute.getName());

        for (final Iterator<DataDoublet> iterator = data.iterator(); iterator.hasNext();) {
            final DataDoublet thisD = (DataDoublet) iterator.next();
            final Object thisVal = thisD.getValue();
            if (thisVal instanceof Number) {
                series.addOrUpdate(new Millisecond(new Date(thisD.getTime())), (Number) thisD.getValue());
            }
        }
        // did it work?
        if (!series.isEmpty()) {
            dataset.addSeries(series);
            _chart.getXYPlot().setDataset(dataset);
            _chart.setTitle(attribute.getName());
            _chart.getXYPlot().getRangeAxis().setLabel(attribute.getUnits());
        }
    }
}

From source file:ChartUsingJava.CombinedXYPlotDemo1.java

/**
 * Creates a sample dataset.  You wouldn't normally hard-code the
 * population of a dataset in this way (it would be better to read the
 * values from a file or a database query), but for a self-contained demo
 * this is the least complicated solution.
 *
 * @return A sample dataset.//from  ww w.  ja v a  2s.co  m
 */
private static IntervalXYDataset createDataset2() {
    TimeSeriesCollection dataset = new TimeSeriesCollection();

    TimeSeries series1 = new TimeSeries("Series 2");
    series1.add(new Month(1, 2005), 1200);
    series1.add(new Month(2, 2005), 1400);
    series1.add(new Month(3, 2005), 1500);
    series1.add(new Month(4, 2005), 1700);
    series1.add(new Month(5, 2005), 1600);
    series1.add(new Month(6, 2005), 2400);
    series1.add(new Month(7, 2005), 2100);
    series1.add(new Month(8, 2005), 2200);
    series1.add(new Month(9, 2005), 800);
    series1.add(new Month(10, 2005), 2350);
    series1.add(new Month(11, 2005), 500);
    series1.add(new Month(12, 2005), 700);
    series1.add(new Month(1, 2006), 900);
    series1.add(new Month(2, 2006), 1500);
    series1.add(new Month(3, 2006), 2100);
    series1.add(new Month(4, 2006), 2200);
    series1.add(new Month(5, 2006), 1900);
    series1.add(new Month(6, 2006), 3000);
    series1.add(new Month(7, 2006), 3780);
    series1.add(new Month(8, 2006), 4000);
    series1.add(new Month(9, 2006), 4500);
    series1.add(new Month(10, 2006), 7000);
    series1.add(new Month(11, 2006), 5500);
    series1.add(new Month(12, 2006), 6000);
    series1.add(new Month(1, 2007), 6500);
    dataset.addSeries(series1);
    return dataset;

}

From source file:org.openmrs.web.controller.ConceptStatsFormController.java

/**
 * Called prior to form display. Allows for data to be put in the request to be used in the view
 *
 * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
 *//*from w w w  .  j  a  va  2  s  .c  o  m*/
protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {

    Map<String, Object> map = new HashMap<String, Object>();
    if (!Context.hasPrivilege("View Observations")) {
        return map;
    }

    MessageSourceAccessor msa = getMessageSourceAccessor();
    Locale locale = Context.getLocale();
    ConceptService cs = Context.getConceptService();
    String conceptId = request.getParameter("conceptId");
    //List<Obs> obs = new Vector<Obs>();
    //List<Obs> obsAnswered = new Vector<Obs>();

    if (conceptId != null) {
        Concept concept = cs.getConcept(Integer.valueOf(conceptId));
        ObsService obsService = Context.getObsService();

        if (concept != null) {

            // previous/next ids for links
            map.put("previousConcept", cs.getPrevConcept(concept));
            map.put("nextConcept", cs.getNextConcept(concept));

            //obs = obsService.getObservations(concept, "valueNumeric, obsId");
            //obsAnswered = obsService.getObservationsAnsweredByConcept(concept);

            if (ConceptDatatype.NUMERIC.equals(concept.getDatatype().getHl7Abbreviation())) {
                map.put("displayType", "numeric");

                List<Obs> numericAnswers = obsService.getObservations(null, null,
                        Collections.singletonList(concept), null,
                        Collections.singletonList(OpenmrsConstants.PERSON_TYPE.PERSON), null,
                        Collections.singletonList("valueNumeric"), null, null, null, null, false);

                if (numericAnswers.size() > 0) {
                    Double min = numericAnswers.get(0).getValueNumeric();
                    Double max = (Double) numericAnswers.get(numericAnswers.size() - 1).getValueNumeric();
                    Double median = (Double) numericAnswers.get(numericAnswers.size() / 2).getValueNumeric();

                    Map<Double, Integer> counts = new HashMap<Double, Integer>(); // counts for the histogram
                    Double total = 0.0; // sum of values. used for mean

                    // dataset setup for lineChart
                    TimeSeries timeSeries = new TimeSeries(concept.getName().getName(), Day.class);
                    TimeSeriesCollection timeDataset = new TimeSeriesCollection();
                    Calendar calendar = Calendar.getInstance();

                    // array for histogram
                    double[] obsNumerics = new double[(numericAnswers.size())];

                    Integer i = 0;
                    for (Obs obs : numericAnswers) {
                        Date date = (Date) obs.getObsDatetime();
                        Double value = (Double) obs.getValueNumeric();

                        // for mean calculation
                        total += value;

                        // for histogram
                        obsNumerics[i++] = value;
                        Integer count = counts.get(value);
                        counts.put(value, count == null ? 1 : count + 1);

                        // for line chart
                        calendar.setTime(date);
                        Day day = new Day(calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH) + 1, // January = 0 
                                calendar.get(Calendar.YEAR) < 1900 ? 1900 : calendar.get(Calendar.YEAR) // jfree chart doesn't like the 19th century
                        );
                        timeSeries.addOrUpdate(day, value);
                    }

                    Double size = new Double(numericAnswers.size());
                    Double mean = total / size;

                    map.put("size", numericAnswers.size());
                    map.put("min", min);
                    map.put("max", max);
                    map.put("mean", mean);
                    map.put("median", median);

                    // create histogram chart
                    HistogramDataset histDataset = new HistogramDataset(); // dataset for histogram
                    histDataset.addSeries(concept.getName().getName(), obsNumerics, counts.size());

                    JFreeChart histogram = ChartFactory.createHistogram(concept.getName().getName(),
                            msa.getMessage("Concept.stats.histogramDomainAxisTitle"),
                            msa.getMessage("Concept.stats.histogramRangeAxisTitle"), histDataset,
                            PlotOrientation.VERTICAL, false, true, false);
                    map.put("histogram", histogram);

                    if (size > 25) {
                        // calculate 98th percentile of the data:
                        Double x = 0.98;
                        Integer xpercentile = (int) (x * size);
                        Double upperQuartile = numericAnswers.get(xpercentile).getValueNumeric();
                        Double lowerQuartile = numericAnswers.get((int) (size - xpercentile)).getValueNumeric();
                        Double innerQuartile = upperQuartile - lowerQuartile;
                        Double innerQuartileLimit = innerQuartile * 1.5; // outliers will be greater than this from the upper/lower quartile
                        Double upperQuartileLimit = upperQuartile + innerQuartileLimit;
                        Double lowerQuartileLimit = lowerQuartile - innerQuartileLimit;

                        List<Obs> outliers = new Vector<Obs>();

                        // move outliers to the outliers list
                        // removing lower quartile outliers
                        for (i = 0; i < size - xpercentile; i++) {
                            Obs possibleOutlier = numericAnswers.get(i);
                            if (possibleOutlier.getValueNumeric() >= lowerQuartileLimit) {
                                break; // quit if this value is greater than the lower limit
                            }
                            outliers.add(possibleOutlier);
                        }

                        // removing upper quartile outliers
                        for (i = size.intValue() - 1; i >= xpercentile; i--) {
                            Obs possibleOutlier = numericAnswers.get(i);
                            if (possibleOutlier.getValueNumeric() <= upperQuartileLimit) {
                                break; // quit if this value is less than the upper limit
                            }
                            outliers.add(possibleOutlier);
                        }
                        numericAnswers.removeAll(outliers);

                        double[] obsNumericsOutliers = new double[(numericAnswers.size())];
                        i = 0;
                        counts.clear();
                        for (Obs values : numericAnswers) {
                            Double value = values.getValueNumeric();
                            obsNumericsOutliers[i++] = value;
                            Integer count = counts.get(value);
                            counts.put(value, count == null ? 1 : count + 1);
                        }

                        // create outlier histogram chart
                        HistogramDataset outlierHistDataset = new HistogramDataset();
                        outlierHistDataset.addSeries(concept.getName().getName(), obsNumericsOutliers,
                                counts.size());

                        JFreeChart histogramOutliers = ChartFactory.createHistogram(concept.getName().getName(),
                                msa.getMessage("Concept.stats.histogramDomainAxisTitle"),
                                msa.getMessage("Concept.stats.histogramRangeAxisTitle"), outlierHistDataset,
                                PlotOrientation.VERTICAL, false, true, false);
                        map.put("histogramOutliers", histogramOutliers);
                        map.put("outliers", outliers);

                    }

                    // create line graph chart
                    timeDataset.addSeries(timeSeries);
                    JFreeChart lineChart = ChartFactory.createTimeSeriesChart(concept.getName().getName(),
                            msa.getMessage("Concept.stats.lineChartDomainAxisLabel"),
                            msa.getMessage("Concept.stats.lineChartRangeAxisLabel"), timeDataset, false, true,
                            false);
                    map.put("timeSeries", lineChart);

                }
            } else if (ConceptDatatype.BOOLEAN.equals(concept.getDatatype().getHl7Abbreviation())) {
                // create bar chart for boolean answers
                map.put("displayType", "boolean");

                List<Obs> obs = obsService.getObservations(null, null, Collections.singletonList(concept), null,
                        Collections.singletonList(OpenmrsConstants.PERSON_TYPE.PERSON), null, null, null, null,
                        null, null, false);

                DefaultPieDataset pieDataset = new DefaultPieDataset();

                // count the number of unique answers
                Map<String, Integer> counts = new HashMap<String, Integer>();
                for (Obs o : obs) {
                    Boolean answer = o.getValueAsBoolean();
                    if (answer == null) {
                        answer = false;
                    }
                    String name = answer.toString();
                    Integer count = counts.get(name);
                    counts.put(name, count == null ? 1 : count + 1);
                }

                // put the counts into the dataset
                for (Map.Entry<String, Integer> entry : counts.entrySet()) {
                    pieDataset.setValue(entry.getKey(), entry.getValue());
                }

                JFreeChart pieChart = ChartFactory.createPieChart(concept.getName().getName(), pieDataset, true,
                        true, false);
                map.put("pieChart", pieChart);

            } else if (ConceptDatatype.CODED.equals(concept.getDatatype().getHl7Abbreviation())) {
                // create pie graph for coded answers
                map.put("displayType", "coded");

                List<Obs> obs = obsService.getObservations(null, null, Collections.singletonList(concept), null,
                        Collections.singletonList(OpenmrsConstants.PERSON_TYPE.PERSON), null, null, null, null,
                        null, null, false);

                DefaultPieDataset pieDataset = new DefaultPieDataset();

                // count the number of unique answers
                Map<String, Integer> counts = new HashMap<String, Integer>();
                for (Obs o : obs) {
                    Concept value = o.getValueCoded();
                    String name;
                    if (value == null) {
                        name = "[value_coded is null]";
                    } else {
                        name = value.getName().getName();
                    }
                    Integer count = counts.get(name);
                    counts.put(name, count == null ? 1 : count + 1);
                }

                // put the counts into the dataset
                for (Map.Entry<String, Integer> entry : counts.entrySet()) {
                    pieDataset.setValue(entry.getKey(), entry.getValue());
                }

                JFreeChart pieChart = ChartFactory.createPieChart(concept.getName().getName(), pieDataset, true,
                        true, false);
                map.put("pieChart", pieChart);

            }
        }

    }

    //map.put("obs", obs);
    //map.put("obsAnswered", obsAnswered);

    map.put("locale", locale.getLanguage().substring(0, 2));

    return map;
}