Example usage for org.jfree.data.time TimeSeries getDataItem

List of usage examples for org.jfree.data.time TimeSeries getDataItem

Introduction

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

Prototype

public TimeSeriesDataItem getDataItem(RegularTimePeriod period) 

Source Link

Document

Returns the data item for a specific period.

Usage

From source file:com.dreikraft.axbo.timeseries.TimeSeriesUtil.java

private static double getValue(final TimeSeries series, final RegularTimePeriod t) {
    return series.getDataItem(t) != null && series.getDataItem(t).getValue() != null
            ? series.getDataItem(t).getValue().doubleValue()
            : 0;/*from  ww w  . j av  a  2  s .c  o  m*/
}

From source file:grafici.PrenotazioneTimeSeriesChart.java

/**
 * Creates a dataset, consisting of two series of monthly data.
 * /*  w  ww .  j  av  a2  s  .c  o m*/
 * @return The dataset.
 */
private static XYDataset createDataset(int tipo) {
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    System.out.println("grafico");
    if (tipo == 2) {
        // anno 2010
        TimeSeries s1 = new TimeSeries("2010");
        for (Prenotazione item : VisitaDAO.getPrenotazioni(2010)) {
            if (s1.getDataItem(new Day(item.getDataOra())) == null)
                s1.addOrUpdate(new Day(item.getDataOra()), 1);
            else
                s1.addOrUpdate(new Day(item.getDataOra()),
                        s1.getDataItem(new Day(item.getDataOra())).getValue().intValue() + 1);

        }
        // anno 2011
        TimeSeries s2 = new TimeSeries("2011");
        for (Prenotazione item : VisitaDAO.getPrenotazioni(2011)) {
            if (s2.getDataItem(new Day(item.getDataOra())) == null)
                s2.addOrUpdate(new Day(item.getDataOra()), 1);
            else
                s2.addOrUpdate(new Day(item.getDataOra()),
                        s2.getDataItem(new Day(item.getDataOra())).getValue().intValue() + 1);

        }
        dataset.addSeries(s1);
        dataset.addSeries(s2);
    } else
        alertGraficoNonDisp();
    return dataset;

}

From source file:grafici.MediciTimeSeriesChart.java

/**
 * Creates a dataset, consisting of two series of monthly data.
 * //  w ww  . j  ava2  s  . c o  m
 * @return The dataset.
 */
private static XYDataset createDataset(int tipo) {
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    System.out.println("medico time graph");
    // anno 2010
    try {
        TimeSeries s1 = new TimeSeries("2010");
        for (Object item : MedicoDAO.getPrestazioni(2010)) {
            Prestazione item1 = (Prestazione) ((Object[]) item)[0];
            if (s1.getDataItem(new Day(item1.getId().getDataTurno())) == null)
                s1.addOrUpdate(new Day(item1.getId().getDataTurno()), 1);
            else
                s1.addOrUpdate(new Day(item1.getId().getDataTurno()),
                        s1.getDataItem(new Day(item1.getId().getDataTurno())).getValue().intValue() + 1);

        }
        dataset.addSeries(s1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return dataset;

}

From source file:be.vds.jtbdive.client.view.core.stats.StatChartGenerator.java

private static JFreeChart buildChartForDive(StatQueryObject sqo) {
    Collection<StatSerie> s = sqo.getValues();
    String legend = I18nResourceManager.sharedInstance().getString("dive.times");
    TimeSeriesCollection collection = new TimeSeriesCollection();
    for (StatSerie statSerie : s) {
        TimeSeries ts = new TimeSeries(legend);
        for (StatPoint point : statSerie.getPoints()) {
            Date dd = (Date) point.getX();
            FixedMillisecond day = new FixedMillisecond(dd);
            Object index = ts.getDataItem(day);
            if (null != index) {
                GregorianCalendar gc = new GregorianCalendar();
                gc.setTime(dd);/*from   ww w.j  a v  a 2 s  . c  o  m*/
                gc.add(Calendar.MILLISECOND, 1);
                day = new FixedMillisecond(gc.getTime());
            }

            if (sqo.getStatYAxisParams().getStatYAxis().equals(StatYAxis.DEPTHS)) {
                ts.add(day, UnitsAgent.getInstance().convertLengthFromModel((Double) point.getY()));
            } else if (sqo.getStatYAxisParams().getStatYAxis().equals(StatYAxis.TEMPERATURES)) {
                ts.add(day, UnitsAgent.getInstance().convertTemperatureFromModel((Double) point.getY()));
            } else {
                ts.add(day, point.getY());
            }
        }
        collection.addSeries(ts);
    }

    JFreeChart chart = createLineChart(collection, getXLabel(sqo), getYLabel(sqo));

    if (sqo.getStatYAxisParams().getStatYAxis().equals(StatYAxis.DIVE_TIME)) {
        XYPlot xyp = (XYPlot) chart.getPlot();
        ((NumberAxis) xyp.getRangeAxis()).setNumberFormatOverride(new HoursMinutesNumberFormat());
    }

    return chart;
}

From source file:edu.fullerton.viewerplugin.PluginSupport.java

public static int scaleRange(TimeSeriesCollection mtds, Double miny, Double maxy) {
    int exp = PluginSupport.getExp(miny, maxy);
    double scale = Math.pow(10, exp);
    for (Iterator it = mtds.getSeries().iterator(); it.hasNext();) {
        TimeSeries ds = (TimeSeries) it.next();
        for (int item = 0; item < ds.getItemCount(); item++) {
            TimeSeriesDataItem dataItem = ds.getDataItem(item);
            RegularTimePeriod period = dataItem.getPeriod();

            double y = dataItem.getValue().doubleValue();
            y *= scale;/*w  w w  .j a v a  2  s.co  m*/
            ds.update(period, y);
        }
    }
    return exp;
}

From source file:edu.fullerton.viewerplugin.PluginSupport.java

public static void getRangeLimits(TimeSeriesCollection mtds, Double rng[]) {
    Double minx, miny, maxx, maxy;

    minx = miny = Double.MAX_VALUE;

    maxx = maxy = -Double.MAX_VALUE;
    for (Iterator it = mtds.getSeries().iterator(); it.hasNext();) {
        TimeSeries ds = (TimeSeries) it.next();
        for (int item = 1; item < ds.getItemCount() - 1; item++) {
            TimeSeriesDataItem dataItem = ds.getDataItem(item);
            RegularTimePeriod period = dataItem.getPeriod();

            double y = dataItem.getValue().doubleValue();
            double x = period.getFirstMillisecond();

            minx = Math.min(minx, x);
            miny = Math.min(miny, y);
            maxx = Math.max(maxx, x);
            maxy = Math.max(maxy, y);
        }/*from w w w  . j a  v  a  2s  .  c o  m*/
    }
    rng[0] = minx;
    rng[1] = miny;
    rng[2] = maxx;
    rng[3] = maxy;
}

From source file:net.sf.dynamicreports.test.jasper.chart.MultiAxisChartTest.java

@Override
public void test() {
    super.test();

    numberOfPagesTest(1);// ww  w  .  j a  v  a 2  s .c o m

    JFreeChart chart = getChart("summary.chart1", 0);
    XYItemRenderer renderer = chart.getXYPlot().getRenderer();
    Assert.assertEquals("renderer", XYLineAndShapeRenderer.class, renderer.getClass());
    TimeSeriesCollection dataset = (TimeSeriesCollection) chart.getXYPlot().getDataset(0);
    TimeSeries serie = (TimeSeries) dataset.getSeries().get(0);
    Assert.assertEquals("value", 1d, serie.getDataItem(0).getValue());
    Assert.assertEquals("value", 2d, serie.getDataItem(1).getValue());
    Assert.assertEquals("value", 3d, serie.getDataItem(2).getValue());
    Assert.assertEquals("value", 4d, serie.getDataItem(3).getValue());
    dataset = (TimeSeriesCollection) chart.getXYPlot().getDataset(1);
    serie = (TimeSeries) dataset.getSeries().get(0);
    Assert.assertEquals("value", 0d, serie.getDataItem(0).getValue());
    Assert.assertEquals("value", 1d, serie.getDataItem(1).getValue());
    Assert.assertEquals("value", 4d, serie.getDataItem(2).getValue());
    Assert.assertEquals("value", 9d, serie.getDataItem(3).getValue());

    JRChart chart2 = (JRChart) getJasperReport().getSummary().getElementByKey("summary.chart2");
    JRChartPlot plot = chart2.getPlot();
    Assert.assertTrue("plot", plot instanceof JRMultiAxisPlot);
    JRMultiAxisPlot multiAxisPlot = (JRMultiAxisPlot) plot;
    Assert.assertEquals("axes", 2, multiAxisPlot.getAxes().size());
    JRChartAxis chartAxis = multiAxisPlot.getAxes().get(0);
    Assert.assertEquals("position", AxisPositionEnum.LEFT_OR_TOP, chartAxis.getPositionValue());
    Assert.assertEquals("chart", JRChart.CHART_TYPE_TIMESERIES, chartAxis.getChart().getChartType());
    chartAxis = multiAxisPlot.getAxes().get(1);
    Assert.assertEquals("position", AxisPositionEnum.RIGHT_OR_BOTTOM, chartAxis.getPositionValue());
    Assert.assertEquals("chart", JRChart.CHART_TYPE_TIMESERIES, chartAxis.getChart().getChartType());
}

From source file:net.sf.dynamicreports.test.jasper.chart.MultiAxisChartDataTest.java

@Override
public void test() {
    super.test();

    numberOfPagesTest(1);/*ww w . j a  v  a 2  s . c  o  m*/

    JFreeChart chart = getChart("summary.chart1", 0);
    XYItemRenderer renderer = chart.getXYPlot().getRenderer();
    Assert.assertEquals("renderer", XYLineAndShapeRenderer.class, renderer.getClass());
    TimeSeriesCollection dataset = (TimeSeriesCollection) chart.getXYPlot().getDataset(0);
    TimeSeries serie = (TimeSeries) dataset.getSeries().get(0);
    Assert.assertEquals("value", 1d, serie.getDataItem(0).getValue());
    Assert.assertEquals("value", 2d, serie.getDataItem(1).getValue());
    Assert.assertEquals("value", 3d, serie.getDataItem(2).getValue());
    Assert.assertEquals("value", 4d, serie.getDataItem(3).getValue());
    dataset = (TimeSeriesCollection) chart.getXYPlot().getDataset(1);
    serie = (TimeSeries) dataset.getSeries().get(0);
    Assert.assertEquals("value", 0d, serie.getDataItem(0).getValue());
    Assert.assertEquals("value", 1d, serie.getDataItem(1).getValue());
    Assert.assertEquals("value", 4d, serie.getDataItem(2).getValue());
    Assert.assertEquals("value", 9d, serie.getDataItem(3).getValue());

    chart = getChart("summary.chart2", 0);
    renderer = chart.getXYPlot().getRenderer();
    Assert.assertEquals("renderer", XYLineAndShapeRenderer.class, renderer.getClass());
    dataset = (TimeSeriesCollection) chart.getXYPlot().getDataset(0);
    serie = (TimeSeries) dataset.getSeries().get(0);
    Assert.assertEquals("value", 2d, serie.getDataItem(0).getValue());
    Assert.assertEquals("value", 3d, serie.getDataItem(1).getValue());
    Assert.assertEquals("value", 4d, serie.getDataItem(2).getValue());
    Assert.assertEquals("value", 5d, serie.getDataItem(3).getValue());
    dataset = (TimeSeriesCollection) chart.getXYPlot().getDataset(1);
    serie = (TimeSeries) dataset.getSeries().get(0);
    Assert.assertEquals("value", 0d, serie.getDataItem(0).getValue());
    Assert.assertEquals("value", 1d, serie.getDataItem(1).getValue());
    Assert.assertEquals("value", 4d, serie.getDataItem(2).getValue());
    Assert.assertEquals("value", 9d, serie.getDataItem(3).getValue());
}

From source file:edu.jhuapl.graphs.jfreechart.TimeSeriesRenderer.java

private Map<String, Object> getMetadata(int row, int column) {
    TimeSeries series = collection.getSeries(row);
    if (series == null) {
        return null;
    }//from w  w  w.j  av a 2 s  .c om

    TimeSeriesDataItem item = series.getDataItem(column);
    if (!(item instanceof MetadataDataItem)) {
        return null;
    }

    return ((MetadataDataItem) item).getMetadata();
}

From source file:com.charts.FiveDayChart.java

public FiveDayChart(YStockQuote currentStock) {
    TimeSeries series = new TimeSeries(currentStock.get_name());
    ArrayList<String> fiveDayData = currentStock.get_five_day_data();
    int length = fiveDayData.size();
    for (int i = 22; i < length; i += 5) {
        String[] data = fiveDayData.get(i).split(",");
        Date time = new Date((long) Integer.parseInt(data[0]) * 1000);
        DateFormat df = new SimpleDateFormat("MM-dd-yyyy-h-m");
        series.addOrUpdate(new Minute(time), Double.parseDouble(data[1]));
    }/*from  ww w .  jav a  2  s  .c  o  m*/
    String[] data = fiveDayData.get(length - 1).split(",");
    Date time = new Date((long) Integer.parseInt(data[0]) * 1000);
    DateFormat df = new SimpleDateFormat("MM-dd-yyyy-h-m");
    series.addOrUpdate(new Minute(time), Double.parseDouble(data[1]));

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            currentStock.get_name() + "(" + currentStock.get_symbol() + ")" + " Five Day", "Date", "Price",
            dataset, true, true, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();

    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    Date now = new Date();
    SegmentedTimeline segmentedTimeline = SegmentedTimeline.newFifteenMinuteTimeline();
    segmentedTimeline.addBaseTimelineExclusions(segmentedTimeline.getStartTime(), now.getTime());
    Calendar[][] holidays = DayRange.getHolidayDates();
    for (int i = 0; i < holidays[0].length; i++) {
        Calendar day = Calendar.getInstance();
        day.set(Calendar.YEAR, holidays[0][i].get(Calendar.YEAR));
        day.set(Calendar.MONTH, holidays[0][i].get(Calendar.MONTH));
        day.set(Calendar.DAY_OF_MONTH, holidays[0][i].get(Calendar.DAY_OF_MONTH));
        day.set(Calendar.HOUR_OF_DAY, 9);
        segmentedTimeline.addException(day.getTimeInMillis(), day.getTimeInMillis() + 21600000);
    }
    xAxis.setTimeline(segmentedTimeline);
    xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    //xAxis.setVerticalTickLabels(true);
    xAxis.setDateFormatOverride(new SimpleDateFormat("MM-dd"));
    xAxis.setAutoTickUnitSelection(false);
    xAxis.setAutoRange(false);

    StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
    renderer1.setSeriesPaint(0, Color.BLUE);
    TimeSeries movingAverage5 = MovingAverage.createMovingAverage(series, "MA(5)", 30, 0);
    Double currMA5 = (Double) movingAverage5.getDataItem(movingAverage5.getItemCount() - 1).getValue();
    currMA5 = Math.round(currMA5 * 100.0) / 100.0;
    movingAverage5.setKey("MA(5): " + currMA5);
    TimeSeriesCollection collection = new TimeSeriesCollection();
    collection.addSeries(movingAverage5);
    plot.setDataset(1, collection);
    plot.setRenderer(1, renderer1);

    plot.setBackgroundPaint(Color.WHITE);

    chartPanel = new ChartPanel(chart);
    chart.setBackgroundPaint(chartPanel.getBackground());
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);
    chartPanel.setVisible(true);
    chartPanel.revalidate();
    chartPanel.repaint();
}