use of org.jfree.data.time.TimeSeriesCollection in project GULLI by rsaemann.
the class TimelinePanel method updateShownTimeSeries.
public void updateShownTimeSeries() {
if (this.collection == null) {
return;
}
XYPlot plot = panelChart.getChart().getXYPlot();
for (int i = 0; i < plot.getDatasetCount(); i++) {
plot.setDataset(i, null);
}
plot.clearRangeAxes();
numberUsedDataSetSlots = 0;
yAxisMap.clear();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
int indexDataset = 0;
int indexSeries = 0;
for (int i = 0; i < collection.getSeriesCount(); i++) {
SeriesKey key = (SeriesKey) collection.getSeries(i).getKey();
/**
* Baue neues Dataset wenn keine Wiederekennung zu finden ist
*/
TimeSeriesCollection dataset = null;
if (key.axisKey == null || key.axisKey.name == null) {
/*
* No recognition (mapping to other dataset) required.
* Build a new Dataset+Yaxis for this TimeSeries
*/
indexDataset = numberUsedDataSetSlots;
numberUsedDataSetSlots++;
dataset = new TimeSeriesCollection(this.collection.getSeries(i));
plot.setDataset(indexDataset, dataset);
renderer = new XYLineAndShapeRenderer(true, false);
plot.setRenderer(indexDataset, renderer);
NumberAxis axis2 = new NumberAxis(checkboxes[i].getText());
yAxisMap.put(axis2.getLabel(), indexDataset);
axis2.setAutoRangeIncludesZero(false);
plot.setRangeAxis(indexDataset, axis2);
plot.mapDatasetToRangeAxis(indexDataset, indexDataset);
} else {
NumberAxis yAxis;
if (yAxisMap.containsKey(key.axisKey.name)) {
indexDataset = yAxisMap.get(key.axisKey.name);
yAxis = (NumberAxis) plot.getRangeAxis(indexDataset);
dataset = (TimeSeriesCollection) plot.getDataset(indexDataset);
indexSeries = dataset.getSeriesCount();
dataset.addSeries(this.collection.getSeries(i));
renderer = (XYLineAndShapeRenderer) plot.getRenderer(indexDataset);
renderer.setSeriesStroke(indexSeries, key.stroke);
} else {
// Axis key not yet in use. Build new Dataset for this Yaxis
indexDataset = numberUsedDataSetSlots;
numberUsedDataSetSlots++;
yAxisMap.put(key.axisKey.name, indexDataset);
indexSeries = 0;
if (key.axisKey.label != null) {
yAxis = new NumberAxis(key.axisKey.label);
} else {
yAxis = new NumberAxis("[" + key.unit + "]");
}
if (key.axisKey != null) {
if (key.axisKey.manualBounds) {
yAxis.setLowerBound(key.axisKey.lowerBound);
yAxis.setUpperBound(key.axisKey.upperBound);
} else {
key.axisKey.lowerBound = yAxis.getLowerBound();
key.axisKey.upperBound = yAxis.getUpperBound();
yAxis.setAutoRange(true);
}
}
yAxisMap.put(yAxis.getLabel(), indexDataset);
renderer = new XYLineAndShapeRenderer(true, false);
renderer.setSeriesStroke(indexSeries, key.stroke);
plot.setRenderer(indexDataset, renderer);
yAxis.setAutoRangeIncludesZero(false);
plot.setRangeAxis(indexDataset, yAxis);
plot.mapDatasetToRangeAxis(indexDataset, indexDataset);
dataset = new TimeSeriesCollection(this.collection.getSeries(i));
plot.setDataset(indexDataset, dataset);
}
plot.mapDatasetToRangeAxis(indexDataset, indexDataset);
}
renderer = (XYLineAndShapeRenderer) plot.getRenderer(indexDataset);
renderer.setDrawSeriesLineAsPath(true);
if (key.lineColor != null) {
renderer.setSeriesPaint(indexSeries, key.lineColor);
}
if (key.stroke != null) {
renderer.setSeriesStroke(indexSeries, key.stroke);
renderer.setSeriesLinesVisible(indexSeries, true);
} else {
renderer.setSeriesLinesVisible(indexSeries, false);
}
if (key.shape != null && key.shape.getShape() != null) {
renderer.setSeriesShape(indexSeries, key.shape.getShape());
renderer.setSeriesShapesFilled(indexSeries, key.shapeFilled);
renderer.setSeriesShapesVisible(indexSeries, true);
} else {
renderer.setSeriesShape(indexSeries, null);
renderer.setSeriesShapesVisible(indexSeries, false);
}
indexDataset++;
}
if (matlabStyle) {
MatlabLayout.layoutToMatlab(this.panelChart.getChart());
}
}
use of org.jfree.data.time.TimeSeriesCollection in project jpsonic by tesshucom.
the class StatusChartController method handleRequest.
// (Millisecond, Date) Not reusable
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override
@GetMapping
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
String type = request.getParameter(Attributes.Request.TYPE.value());
int index = ServletRequestUtils.getIntParameter(request, Attributes.Request.INDEX.value(), 0);
List<TransferStatus> statuses = Collections.emptyList();
if ("stream".equals(type)) {
statuses = statusService.getAllStreamStatuses();
} else if ("download".equals(type)) {
statuses = statusService.getAllDownloadStatuses();
} else if ("upload".equals(type)) {
statuses = statusService.getAllUploadStatuses();
}
if (index < 0 || index >= statuses.size()) {
return null;
}
TransferStatus status = statuses.get(index);
TimeSeries series = new TimeSeries("Kbps");
TransferStatus.SampleHistory history = status.getHistory();
long to = System.currentTimeMillis();
long from = to - status.getHistoryLengthMillis();
if (!history.isEmpty()) {
TransferStatus.Sample previous = history.get(0);
for (int i = 1; i < history.size(); i++) {
TransferStatus.Sample sample = history.get(i);
long elapsedTimeMilis = sample.getTimestamp() - previous.getTimestamp();
long bytesStreamed = Math.max(0L, sample.getBytesTransfered() - previous.getBytesTransfered());
double kbps = (8.0 * bytesStreamed / 1024.0) / (elapsedTimeMilis / 1000.0);
series.addOrUpdate(new Millisecond(new Date(sample.getTimestamp())), kbps);
previous = sample;
}
}
// Compute moving average.
series = MovingAverage.createMovingAverage(series, "Kbps", 20_000, 5000);
// Find min and max values.
double min = 100;
double max = 250;
for (Object obj : series.getItems()) {
TimeSeriesDataItem item = (TimeSeriesDataItem) obj;
double value = item.getValue().doubleValue();
if (item.getPeriod().getFirstMillisecond() > from) {
min = Math.min(min, value);
max = Math.max(max, value);
}
}
// Add 10% to max value.
max *= 1.1D;
// Subtract 10% from min value.
min *= 0.9D;
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, dataset, false, false, false);
StandardChartTheme theme = (StandardChartTheme) StandardChartTheme.createJFreeTheme();
Font font = fontLoader.getFont(12F);
theme.setExtraLargeFont(font);
theme.setLargeFont(font);
theme.setRegularFont(font);
theme.setSmallFont(font);
theme.apply(chart);
Color bgColor = getBackground(request);
chart.setBackgroundPaint(bgColor);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(bgColor);
Color fgColor = getForeground(request);
plot.setOutlinePaint(fgColor);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setRangeGridlinePaint(fgColor);
plot.setRangeGridlineStroke(new BasicStroke(0.2f));
plot.setDomainGridlinePaint(fgColor);
plot.setDomainGridlineStroke(new BasicStroke(0.2f));
XYItemRenderer renderer = plot.getRendererForDataset(dataset);
Color stColor = getStroke(request);
renderer.setSeriesPaint(0, stColor);
renderer.setSeriesStroke(0, new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setRange(new DateRange(from, to));
domainAxis.setTickLabelPaint(fgColor);
domainAxis.setTickMarkPaint(fgColor);
domainAxis.setAxisLinePaint(fgColor);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setRange(new Range(min, max));
rangeAxis.setTickLabelPaint(fgColor);
rangeAxis.setTickMarkPaint(fgColor);
rangeAxis.setAxisLinePaint(fgColor);
synchronized (LOCK) {
ChartUtils.writeChartAsPNG(response.getOutputStream(), chart, IMAGE_WIDTH, IMAGE_HEIGHT);
}
return null;
}
use of org.jfree.data.time.TimeSeriesCollection in project projectforge by micromata.
the class LiquidityChartBuilder method createBarChart.
/**
* @param forecast
* @param settings (next days)
* @return
*/
public JFreeChart createBarChart(final LiquidityForecast forecast, final LiquidityForecastSettings settings) {
if (!(settings.getNextDays() > 0 && settings.getNextDays() <= LiquidityForecastSettings.MAX_FORECAST_DAYS)) {
settings.setNextDays(LiquidityForecastSettings.DEFAULT_FORECAST_DAYS);
}
final LiquidityForecastCashFlow cashFlow = new LiquidityForecastCashFlow(forecast, settings.getNextDays());
final TimeSeries accumulatedSeriesExpected = new TimeSeries(I18n.getString("plugins.liquidityplanning.forecast.expected"));
final TimeSeries creditSeries = new TimeSeries(I18n.getString("plugins.liquidityplanning.common.credit"));
final TimeSeries debitSeries = new TimeSeries(I18n.getString("plugins.liquidityplanning.common.debit"));
double accumulatedExpected = settings.getStartAmount().doubleValue();
PFDay dt = PFDay.fromOrNow(forecast.getBaseDate());
final Date lower = dt.getUtilDate();
for (int i = 0; i < settings.getNextDays(); i++) {
final Day day = new Day(dt.getDayOfMonth(), dt.getMonthValue(), dt.getYear());
if (i > 0) {
accumulatedExpected += cashFlow.getDebitsExpected()[i - 1].doubleValue() + cashFlow.getCreditsExpected()[i - 1].doubleValue();
}
accumulatedSeriesExpected.add(day, accumulatedExpected);
creditSeries.add(day, cashFlow.getCreditsExpected()[i].doubleValue());
debitSeries.add(day, cashFlow.getDebitsExpected()[i].doubleValue());
dt = dt.plusDays(1);
}
dt = dt.minusDays(1);
final XYChartBuilder cb = new XYChartBuilder(ChartFactory.createXYBarChart(null, null, false, null, null, PlotOrientation.VERTICAL, false, false, false));
int counter = 0;
final TimeSeriesCollection xyDataSeries = new TimeSeriesCollection();
xyDataSeries.addSeries(accumulatedSeriesExpected);
final XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true, true);
lineRenderer.setSeriesPaint(0, cb.getRedMarker());
lineRenderer.setSeriesVisibleInLegend(0, true);
cb.setRenderer(counter, lineRenderer).setDataset(counter++, xyDataSeries).setStrongStyle(lineRenderer, false, accumulatedSeriesExpected);
final TimeSeriesCollection cashflowSet = new TimeSeriesCollection();
cashflowSet.addSeries(debitSeries);
cashflowSet.addSeries(creditSeries);
final XYBarRenderer barRenderer = new XYBarRenderer(.2);
barRenderer.setSeriesPaint(0, cb.getGreenFill());
barRenderer.setSeriesPaint(1, cb.getRedFill());
barRenderer.setShadowVisible(false);
cb.setRenderer(counter, barRenderer).setDataset(counter++, cashflowSet);
cb.setDateXAxis(true).setDateXAxisRange(lower, dt.getUtilDate()).setYAxis(true, null);
return cb.getChart();
}
use of org.jfree.data.time.TimeSeriesCollection in project projectforge by micromata.
the class LiquidityChartBuilder method createXYPlot.
/**
* @param forecast
* @param settings (next days)
* @return
*/
public JFreeChart createXYPlot(final LiquidityForecast forecast, final LiquidityForecastSettings settings) {
if (!(settings.getNextDays() > 0 && settings.getNextDays() <= LiquidityForecastSettings.MAX_FORECAST_DAYS)) {
settings.setNextDays(LiquidityForecastSettings.DEFAULT_FORECAST_DAYS);
}
final LiquidityForecastCashFlow cashFlow = new LiquidityForecastCashFlow(forecast, settings.getNextDays());
final TimeSeries accumulatedSeries = new TimeSeries(I18n.getString("plugins.liquidityplanning.forecast.dueDate"));
final TimeSeries accumulatedSeriesExpected = new TimeSeries(ThreadLocalUserContext.getLocalizedString("plugins.liquidityplanning.forecast.expected"));
// The paranoia case assumes only costs and no incomes (no invoice will be paid by the customers).
final TimeSeries paranoiaCaseSeries = new TimeSeries(I18n.getString("plugins.liquidityplanning.forecast.paranoiaCase"));
double accumulatedExpected = settings.getStartAmount().doubleValue();
double accumulated = accumulatedExpected;
double paranoiaCase = accumulated;
PFDay dt = PFDay.fromOrNow(forecast.getBaseDate());
final Date lower = dt.getUtilDate();
for (int i = 0; i < settings.getNextDays(); i++) {
if (log.isDebugEnabled()) {
log.debug("day: " + i + ", credits=" + cashFlow.getCredits()[i] + ", debits=" + cashFlow.getDebits()[i]);
}
final Day day = new Day(dt.getDayOfMonth(), dt.getMonthValue(), dt.getYear());
if (i > 0) {
accumulated += cashFlow.getDebits()[i - 1].doubleValue() + cashFlow.getCredits()[i - 1].doubleValue();
accumulatedExpected += cashFlow.getDebitsExpected()[i - 1].doubleValue() + cashFlow.getCreditsExpected()[i - 1].doubleValue();
paranoiaCase += cashFlow.getCredits()[i - 1].doubleValue();
}
accumulatedSeries.add(day, accumulated);
accumulatedSeriesExpected.add(day, accumulatedExpected);
paranoiaCaseSeries.add(day, paranoiaCase);
dt = dt.plusDays(1);
}
dt = dt.minusDays(1);
final XYChartBuilder cb = new XYChartBuilder(null, null, null, null, true);
int counter = 0;
final TimeSeriesCollection xyDataSeries = new TimeSeriesCollection();
xyDataSeries.addSeries(accumulatedSeries);
xyDataSeries.addSeries(paranoiaCaseSeries);
final XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true, false);
lineRenderer.setSeriesPaint(0, Color.BLACK);
lineRenderer.setSeriesVisibleInLegend(0, true);
lineRenderer.setSeriesPaint(1, cb.getGrayMarker());
lineRenderer.setSeriesStroke(1, cb.getDashedStroke());
lineRenderer.setSeriesVisibleInLegend(1, true);
cb.setRenderer(counter, lineRenderer).setDataset(counter++, xyDataSeries);
final TimeSeriesCollection accumulatedSet = new TimeSeriesCollection();
accumulatedSet.addSeries(accumulatedSeriesExpected);
final XYDifferenceRenderer diffRenderer = new XYDifferenceRenderer(cb.getGreenFill(), cb.getRedFill(), true);
diffRenderer.setSeriesPaint(0, cb.getRedMarker());
cb.setRenderer(counter, diffRenderer).setDataset(counter++, accumulatedSet).setStrongStyle(diffRenderer, false, accumulatedSeriesExpected);
diffRenderer.setSeriesVisibleInLegend(0, true);
cb.setDateXAxis(true).setDateXAxisRange(lower, dt.getUtilDate()).setYAxis(true, null);
return cb.getChart();
}
use of org.jfree.data.time.TimeSeriesCollection in project projectforge by micromata.
the class TimesheetDisciplineChartBuilder method create.
/**
* Ein Diagramm, welches über die letzten n Tage die Tage visualisiert, die zwischen Zeitberichtsdatum und Zeitpunkt der tatsächlichen
* Buchung liegen.
* @param timesheetDao
* @param userId
* @param forLastNDays
* @return
*/
public JFreeChart create(final TimesheetDao timesheetDao, final Integer userId, final short forLastNDays) {
PFDateTime dt = PFDateTime.now();
final TimesheetFilter filter = new TimesheetFilter();
filter.setStopTime(dt.getUtilDate());
dt = dt.minusDays(forLastNDays);
filter.setStartTime(dt.getUtilDate());
filter.setUserId(userId);
filter.setOrderType(OrderDirection.ASC);
final List<TimesheetDO> list = timesheetDao.getList(filter);
final TimeSeries planSeries = new TimeSeries("Soll");
final TimeSeries actualSeries = new TimeSeries("Ist");
final Iterator<TimesheetDO> it = list.iterator();
TimesheetDO current = null;
if (it.hasNext()) {
current = it.next();
}
long numberOfBookedDays = 0;
long totalDifference = 0;
for (int i = 0; i <= forLastNDays; i++) {
long difference = 0;
// Weight for average.
long totalDuration = 0;
PFDateTime dateTime = null;
if (current != null)
// not null
dateTime = PFDateTime.from(current.getStartTime());
while (current != null && (dt.isSameDay(dateTime) || dateTime.isBefore(dt))) {
final long duration = current.getWorkFractionDuration();
difference += (current.getCreated().getTime() - current.getStartTime().getTime()) * duration;
totalDuration += duration;
if (it.hasNext()) {
current = it.next();
// not null
dateTime = PFDateTime.from(current.getStartTime());
} else {
current = null;
break;
}
}
// In days.
final double averageDifference = difference > 0 ? ((double) difference) / totalDuration / 86400000 : 0;
final Day day = new Day(dt.getDayOfMonth(), dt.getMonthValue(), dt.getYear());
if (averageDifference > 0) {
// plan average
planSeries.add(day, PLANNED_AVERAGE_DIFFERENCE_BETWEEN_TIMESHEET_AND_BOOKING);
// (PLANNED_AVERAGE_DIFFERENCE_BETWEEN_TIMESHEET_AND_BOOKING
// days).
actualSeries.add(day, averageDifference);
totalDifference += averageDifference;
numberOfBookedDays++;
}
dt = dt.plusDays(1);
}
averageDifferenceBetweenTimesheetAndBooking = numberOfBookedDays > 0 ? new BigDecimal(totalDifference).divide(new BigDecimal(numberOfBookedDays), 1, RoundingMode.HALF_UP) : BigDecimal.ZERO;
final TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(actualSeries);
dataset.addSeries(planSeries);
final XYChartBuilder cb = new XYChartBuilder(null, null, null, dataset, false);
final XYDifferenceRenderer diffRenderer = new XYDifferenceRenderer(cb.getRedFill(), cb.getGreenFill(), true);
diffRenderer.setSeriesPaint(0, cb.getRedMarker());
diffRenderer.setSeriesPaint(1, cb.getGreenMarker());
cb.setRenderer(0, diffRenderer).setStrongStyle(diffRenderer, false, actualSeries, planSeries);
cb.setDateXAxis(true).setYAxis(true, "days");
return cb.getChart();
}
Aggregations