#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,9 +22,11 @@ public class TimeSeriesUtils {
} }
public static TimeSeries fillDates(TimeSeries timeSeries, long milliseconds) { public static TimeSeries fillDates(TimeSeries timeSeries, long milliseconds) {
timeSeries.getLastValue().setDate(LocalDateTime.now()); if (!timeSeries.isEmpty()) {
for (int i = timeSeries.getLength() - 2; i >= 0; i--) { timeSeries.getLastValue().setDate(LocalDateTime.now());
timeSeries.getValues().get(i).setDate(timeSeries.getValues().get(i + 1).getDate().minus(milliseconds, ChronoUnit.MILLIS)); 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; return timeSeries;
} }

View File

@ -1,23 +1,33 @@
package ru.ulstu.pages; package ru.ulstu.pages;
import org.primefaces.model.chart.Axis;
import org.primefaces.model.chart.AxisType; 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.LineChartModel;
import org.primefaces.model.chart.LineChartSeries; 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.annotation.PostConstruct;
import javax.faces.view.ViewScoped; import javax.faces.view.ViewScoped;
import javax.inject.Named; import javax.inject.Named;
import java.io.Serializable; import java.io.Serializable;
import java.time.format.DateTimeFormatter;
@Named @Named
@ViewScoped @ViewScoped
public class IndexView implements Serializable { 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() { @Autowired
return lineModel1; private TimeSeriesService timeSeriesService;
}
@PostConstruct @PostConstruct
public void init() { public void init() {
@ -30,44 +40,57 @@ public class IndexView implements Serializable {
LineChartSeries series1 = new LineChartSeries(); LineChartSeries series1 = new LineChartSeries();
series1.setLabel("Временной ряд"); series1.setLabel("Временной ряд");
series1.set(1, 2); TimeSeries timeSeries = timeSeriesService.getTimeSeriesFromString(timeSeriesString);
series1.set(2, 1); for (TimeSeriesValue value : timeSeries.getValues()) {
series1.set(3, 3); series1.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue());
series1.set(4, 6); }
series1.set(5, 8);
LineChartSeries series2 = new LineChartSeries(); LineChartSeries series2 = new LineChartSeries();
series2.setLabel("Сглаженный ряд"); 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(); LineChartSeries series3 = new LineChartSeries();
series3.setLabel("Прогноз"); series3.setLabel("Прогноз");
try {
series3.set(1, 6); for (TimeSeriesValue value : timeSeriesService.getForecast(timeSeries, 10).getForecastTimeSeries().getValues()) {
series3.set(2, 3); series3.set(DateTimeFormatter.ISO_LOCAL_DATE.format(value.getDate()), value.getValue());
series3.set(3, 2); }
series3.set(4, 7); } catch (ModelingException ex) {
series3.set(6, 9); LOG.warn(ex.getMessage());
series3.set(7, 9); }
model.addSeries(series1); model.addSeries(series1);
model.addSeries(series2); model.addSeries(series2);
model.addSeries(series3); model.addSeries(series3);
return model; return model;
} }
public void createChart() { public void createChart() {
lineModel1 = initLinearModel(); model = initLinearModel();
lineModel1.setTitle("Сглаживание временного ряда"); model.setTitle("Сглаживание временного ряда");
lineModel1.setLegendPosition("e"); model.setLegendPosition("d");
Axis yAxis = lineModel1.getAxis(AxisType.Y); model.setLegendPlacement(LegendPlacement.OUTSIDE);
yAxis.setMin(0); DateAxis xAxis = new DateAxis("Time");
yAxis.setMax(10); 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" <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 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:composition template="/basicTemplate.xhtml">
<ui:define name="content"> <ui:define name="content">
<div class="ui-g"> <h:form>
<div class="ui-g-12 ui-md-12 ui-lg-6"> <div class="ui-g">
<p:inputTextarea style="height:200px; width:100%;" placeholder="Paste time series here" <div class="ui-g-12 ui-md-12 ui-lg-12">
autoResize="false"/> <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>
<div class="ui-g-12 ui-md-12 ui-lg-6"> <div class="ui-g">
<p:chart type="line" model="#{indexView.lineModel1}" style="height:200px;"/> <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> </div>
</div> </h:form>
</ui:define> </ui:define>
</ui:composition> </ui:composition>
</html> </html>