1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.charts.plots

File TimeSeriesCollectionFactory.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

24
47
3
1
125
92
17
0.36
15.67
3
5.67

Classes

Class Line # Actions
TimeSeriesCollectionFactory 38 47 0% 17 74
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.plugin.charts.plots;
21   
22    import java.lang.reflect.Constructor;
23    import java.text.DateFormat;
24    import java.text.SimpleDateFormat;
25    import java.util.Date;
26   
27    import org.jfree.data.time.Day;
28    import org.jfree.data.time.RegularTimePeriod;
29    import org.jfree.data.time.TimeSeries;
30    import org.jfree.data.time.TimeSeriesCollection;
31    import org.jfree.data.xy.XYDataset;
32   
33    import com.xpn.xwiki.plugin.charts.exceptions.DataSourceException;
34    import com.xpn.xwiki.plugin.charts.exceptions.GenerateException;
35    import com.xpn.xwiki.plugin.charts.params.ChartParams;
36    import com.xpn.xwiki.plugin.charts.source.DataSource;
37   
 
38    public class TimeSeriesCollectionFactory
39    {
40    private static TimeSeriesCollectionFactory uniqueInstance = new TimeSeriesCollectionFactory();
41   
 
42  0 toggle private TimeSeriesCollectionFactory()
43    {
44    // empty
45    }
46   
 
47  0 toggle public static TimeSeriesCollectionFactory getInstance()
48    {
49  0 return uniqueInstance;
50    }
51   
 
52  0 toggle public XYDataset create(DataSource dataSource, ChartParams params) throws GenerateException, DataSourceException
53    {
54  0 String dataSeries = params.getString(ChartParams.SERIES);
55   
56  0 TimeSeriesCollection dataset = new TimeSeriesCollection();
57  0 Class timePeriodClass = params.getClass(ChartParams.TIME_PERIOD_CLASS);
58  0 if (timePeriodClass == null) {
59  0 timePeriodClass = Day.class;
60    }
61  0 DateFormat format = params.getDateFormat(ChartParams.DATE_FORMAT);
62  0 if (format == null) {
63  0 format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
64    }
65   
66  0 if (dataSeries.equals("columns")) {
67  0 if (!dataSource.hasHeaderColumn()) {
68  0 throw new GenerateException("Header column required");
69    }
70  0 for (int column = 0; column < dataSource.getColumnCount(); column++) {
71  0 String seriesName;
72  0 if (dataSource.hasHeaderRow()) {
73  0 seriesName = dataSource.getHeaderRowValue(column);
74    } else {
75  0 seriesName = "Series " + (column + 1);
76    }
77   
78  0 TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
79   
80  0 for (int row = 0; row < dataSource.getRowCount(); row++) {
81  0 RegularTimePeriod period;
82  0 try {
83  0 Date date = format.parse(dataSource.getHeaderColumnValue(row));
84  0 Constructor ctor = timePeriodClass.getConstructor(new Class[] {Date.class});
85  0 period = (RegularTimePeriod) ctor.newInstance(new Object[] {date});
86    } catch (Exception e) {
87  0 throw new GenerateException(e);
88    }
89  0 series.add(period, dataSource.getCell(row, column));
90    }
91  0 dataset.addSeries(series);
92    }
93  0 } else if (dataSeries.equals("rows")) {
94  0 if (!dataSource.hasHeaderRow()) {
95  0 throw new GenerateException("Header row required");
96    }
97  0 for (int row = 0; row < dataSource.getRowCount(); row++) {
98  0 String seriesName;
99  0 if (dataSource.hasHeaderColumn()) {
100  0 seriesName = dataSource.getHeaderColumnValue(row);
101    } else {
102  0 seriesName = "Series " + (row + 1);
103    }
104   
105  0 TimeSeries series = new TimeSeries(seriesName, timePeriodClass);
106   
107  0 for (int column = 0; column < dataSource.getColumnCount(); column++) {
108  0 RegularTimePeriod period;
109  0 try {
110  0 Date date = format.parse(dataSource.getHeaderRowValue(column));
111  0 Constructor ctor = timePeriodClass.getConstructor(new Class[] {Date.class});
112  0 period = (RegularTimePeriod) ctor.newInstance(new Object[] {date});
113    } catch (Exception e) {
114  0 throw new GenerateException(e);
115    }
116  0 series.add(period, dataSource.getCell(row, column));
117    }
118  0 dataset.addSeries(series);
119    }
120    } else {
121  0 throw new GenerateException("Invalid series parameter:" + dataSeries);
122    }
123  0 return dataset;
124    }
125    }