#3 -- show 3 charts of time series

This commit is contained in:
Anton Romanov 2020-11-02 13:45:03 +04:00
parent 9602e40873
commit 39b09484ec
3 changed files with 76 additions and 41 deletions

View File

@ -22,10 +22,12 @@ public class TimeSeriesUtils {
}
public static TimeSeries fillDates(TimeSeries timeSeries, long milliseconds) {
if (!timeSeries.isEmpty()) {
timeSeries.getLastValue().setDate(LocalDateTime.now());
for (int i = timeSeries.getLength() - 2; i >= 0; i--) {
timeSeries.getValues().get(i).setDate(timeSeries.getValues().get(i + 1).getDate().minus(milliseconds, ChronoUnit.MILLIS));
}
}
return timeSeries;
}

View File

@ -1,23 +1,33 @@
package ru.ulstu.pages;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType;
import org.primefaces.model.chart.DateAxis;
import org.primefaces.model.chart.LegendPlacement;
import org.primefaces.model.chart.LineChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import ru.ulstu.models.TimeSeries;
import ru.ulstu.models.TimeSeriesValue;
import ru.ulstu.models.exceptions.ModelingException;
import ru.ulstu.services.TimeSeriesService;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.time.format.DateTimeFormatter;
@Named
@ViewScoped
public class IndexView implements Serializable {
private LineChartModel lineModel1;
private final static Logger LOG = LoggerFactory.getLogger(IndexView.class);
private LineChartModel model;
private String timeSeriesString = "1,2,3";
public LineChartModel getLineModel1() {
return lineModel1;
}
@Autowired
private TimeSeriesService timeSeriesService;
@PostConstruct
public void init() {
@ -30,44 +40,57 @@ public class IndexView implements Serializable {
LineChartSeries series1 = new LineChartSeries();
series1.setLabel("Временной ряд");
series1.set(1, 2);
series1.set(2, 1);
series1.set(3, 3);
series1.set(4, 6);
series1.set(5, 8);
TimeSeries timeSeries = timeSeriesService.getTimeSeriesFromString(timeSeriesString);
for (TimeSeriesValue value : timeSeries.getValues()) {
series1.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue());
}
LineChartSeries series2 = new LineChartSeries();
series2.setLabel("Сглаженный ряд");
try {
for (TimeSeriesValue value : timeSeriesService.getForecast(timeSeries, 10).getModel().getModelTimeSeries().getValues()) {
series2.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue());
}
} catch (ModelingException ex) {
LOG.warn(ex.getMessage());
}
series2.set(1, 6);
series2.set(2, 3);
series2.set(3, 2);
series2.set(4, 7);
series2.set(5, 9);
LineChartSeries series3 = new LineChartSeries();
series3.setLabel("Прогноз");
series3.set(1, 6);
series3.set(2, 3);
series3.set(3, 2);
series3.set(4, 7);
series3.set(6, 9);
series3.set(7, 9);
try {
for (TimeSeriesValue value : timeSeriesService.getForecast(timeSeries, 10).getForecastTimeSeries().getValues()) {
series3.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue());
}
} catch (ModelingException ex) {
LOG.warn(ex.getMessage());
}
model.addSeries(series1);
model.addSeries(series2);
model.addSeries(series3);
return model;
}
public void createChart() {
lineModel1 = initLinearModel();
lineModel1.setTitle("Сглаживание временного ряда");
lineModel1.setLegendPosition("e");
Axis yAxis = lineModel1.getAxis(AxisType.Y);
yAxis.setMin(0);
yAxis.setMax(10);
model = initLinearModel();
model.setTitle("Сглаживание временного ряда");
model.setLegendPosition("d");
model.setLegendPlacement(LegendPlacement.OUTSIDE);
DateAxis xAxis = new DateAxis("Time");
xAxis.setTickFormat("%#d %b %Y");
model.getAxes().put(AxisType.X, xAxis);
}
public LineChartModel getModel() {
return model;
}
public String getTimeSeriesString() {
createChart();
return timeSeriesString;
}
public void setTimeSeriesString(String timeSeriesString) {
this.timeSeriesString = timeSeriesString;
}
}

View File

@ -2,18 +2,28 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<ui:composition template="/basicTemplate.xhtml">
<ui:define name="content">
<h:form>
<div class="ui-g">
<div class="ui-g-12 ui-md-12 ui-lg-6">
<p:inputTextarea style="height:200px; width:100%;" placeholder="Paste time series here"
autoResize="false"/>
</div>
<div class="ui-g-12 ui-md-12 ui-lg-6">
<p:chart type="line" model="#{indexView.lineModel1}" style="height:200px;"/>
<div class="ui-g-12 ui-md-12 ui-lg-12">
<p:inputTextarea id="timeSeriesString" style="height:200px; width:100%;"
placeholder="Paste time series here"
autoResize="false" value="#{indexView.timeSeriesString}">
<p:ajax event="change" update="chart"/>
<p:ajax event="keypress" update="chart"/>
</p:inputTextarea>
<p:commandButton value="Refresh" update="chart" process="timeSeriesString"/>
</div>
</div>
<div class="ui-g">
<div class="ui-g-12 ui-md-12 ui-lg-12">
<p:chart id="chart" type="line" model="#{indexView.model}" style="height:300px;"/>
</div>
</div>
</h:form>
</ui:define>
</ui:composition>
</html>