de.heartbeat.charts.YearOverview.java Source code

Java tutorial

Introduction

Here is the source code for de.heartbeat.charts.YearOverview.java

Source

/*
 * Copyright 2015 momo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.heartbeat.charts;

import com.googlecode.wickedcharts.highcharts.options.Axis;
import com.googlecode.wickedcharts.highcharts.options.AxisType;
import com.googlecode.wickedcharts.highcharts.options.ChartOptions;
import com.googlecode.wickedcharts.highcharts.options.CssStyle;
import com.googlecode.wickedcharts.highcharts.options.ExportingOptions;
import com.googlecode.wickedcharts.highcharts.options.Function;
import com.googlecode.wickedcharts.highcharts.options.Labels;
import com.googlecode.wickedcharts.highcharts.options.Legend;
import com.googlecode.wickedcharts.highcharts.options.Options;
import com.googlecode.wickedcharts.highcharts.options.PlotBand;
import com.googlecode.wickedcharts.highcharts.options.PlotLine;
import com.googlecode.wickedcharts.highcharts.options.SeriesType;
import com.googlecode.wickedcharts.highcharts.options.Title;
import com.googlecode.wickedcharts.highcharts.options.Tooltip;
import com.googlecode.wickedcharts.highcharts.options.color.HexColor;
import com.googlecode.wickedcharts.highcharts.options.color.RgbaColor;
import com.googlecode.wickedcharts.highcharts.options.livedata.LiveDataSeries;
import com.googlecode.wickedcharts.highcharts.options.livedata.LiveDataUpdateEvent;
import com.googlecode.wickedcharts.highcharts.options.series.Point;
import com.googlecode.wickedcharts.wicket7.highcharts.Chart;
import de.heartbeat.backend.GraphPage;
import de.heartbeat.backend.HomePage;
import de.heartbeat.datastorage.HibernateUtil;
import de.heartbeat.datastorage.entities.HeartBeat;
import de.heartbeat.datastorage.entities.Person;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author momo
 */
public class YearOverview extends GraphPage {

    private LiveDataSeries series;
    private final SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    private List<HeartBeat> heartBeatList;
    private List<HeartBeat> heartBeatListNew;

    public YearOverview(PageParameters parameters) {
        super(parameters);
        StringValue deviceId = parameters.get("deviceId");
        Person person;
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        person = (Person) session.createCriteria(Person.class)
                .add(Restrictions.like("deviceID", deviceId.toString())).uniqueResult();
        heartBeatList = person.getHeartbeats();
        session.getTransaction().commit();
        session.close();

        Collections.sort(heartBeatList);
        Options options = new Options();

        options.setTooltip(
                new Tooltip().setFormatter(new Function().setFunction("return '<b>'+ this.series.name +'</b><br/>'+"
                        + "Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+"
                        + "Highcharts.numberFormat(this.y, 2);")));
        List<PlotBand> plotBands = new ArrayList<>();
        plotBands.add(createPlotBand("HIGH", 120, 10000, true));
        plotBands.add(createPlotBand("NORMAL", 45, 120, false));
        plotBands.add(createPlotBand("LOW", 0, 45, true));
        options.setTitle(new Title("Puls"));
        options.setChartOptions((new ChartOptions().setType(SeriesType.SPLINE).setMarginRight(10)));
        options.setxAxis(new Axis().setType(AxisType.DATETIME));

        Axis yAxis = new Axis();
        yAxis.setTitle(new Title("Puls")).setPlotLines(Collections
                .singletonList(new PlotLine().setValue(1f).setWidth(1).setColor(new HexColor("#808080"))));
        yAxis.setPlotBands(plotBands);

        options.setyAxis(yAxis);

        options.setLegend(new Legend(Boolean.FALSE));
        options.setExporting(new ExportingOptions().setEnabled(Boolean.FALSE));

        series = new LiveDataSeries(options, 6000) {

            @Override
            public Point update(LiveDataUpdateEvent ldue) {
                Session session = sessionFactory.openSession();
                session.beginTransaction();
                heartBeatListNew = session.createCriteria(HeartBeat.class)
                        .add(Restrictions.gt("time", heartBeatList.get(heartBeatList.size() - 1).getTime())).list();
                session.getTransaction().commit();
                session.close();

                if (heartBeatListNew.size() > 0) {
                    HeartBeat hb = heartBeatListNew.get(0);
                    Date time = new Date(hb.getTime().getTime());
                    heartBeatList.addAll(heartBeatListNew);
                    return new Point(time.getTime(), Double.parseDouble(hb.getPulse()));
                } else {
                    return null;
                }

            }
        };

        series.setData(getPoints(this.heartBeatList)).setName("Puls");
        options.addSeries(series);

        add(new Chart("chart", options));
    }

    private List<Point> getPoints(List<HeartBeat> hbList) {
        List<Point> points = new ArrayList<>();
        int y = 0;
        for (int i = hbList.size() - 1; i >= 0; i--) {
            Timestamp ts = hbList.get(i).getTime();
            Date time = new Date(ts.getTime());
            // some bug in charts or sth.
            time = DateUtils.addHours(time, 1);
            long timeL = time.getTime();
            points.add(new Point().setX(timeL).setY(Double.parseDouble(hbList.get(i).getPulse())));

        }

        return points;
    }

    private PlotBand createPlotBand(final String label, final float from, final float to,
            final boolean highlighted) {
        Labels plotBandLabel = new Labels(label);
        plotBandLabel.setStyle(new CssStyle());

        PlotBand plotBand = new PlotBand();
        plotBand.setFrom(from);
        plotBand.setTo(to);
        plotBand.setLabel(plotBandLabel);
        if (highlighted) {
            plotBand.setColor(new RgbaColor(205, 51, 51, 0.1f));
        } else {
            plotBand.setColor(new RgbaColor(135, 206, 250, 0.1f));
        }

        return plotBand;
    }
}