TimeSeriesChart.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18 */
19 
20 /*!
21  \file terralib/qt/widgets/charts/TimeSeriesChart.cpp
22 
23  \brief A class to represent a timeSeries' chart.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/ObjectIdSet.h"
28 #include "../../../datatype/Date.h"
29 #include "../../../datatype/DateTime.h"
30 #include "../../../datatype/TimeInstant.h"
31 #include "../../../qt/widgets/se/Utils.h"
32 #include "../../../st/core/timeseries/TimeSeries.h"
33 #include "../../../st/core/timeseries/TimeSeriesIterator.h"
34 #include "Enums.h"
35 #include "StringScaleDraw.h"
36 #include "TimeScaleDraw.h"
37 #include "TimeSeriesChart.h"
38 #include "TimeSeriesStyle.h"
39 #include "Utils.h"
40 
41 //QWT
42 #include <qwt_symbol.h>
43 #include <qwt_plot.h>
44 #include <qwt_date.h>
45 
46 //Boost
47 #include "boost/date_time/gregorian/gregorian.hpp"
48 
49 //STL
50 #include <limits>
51 
53  :
54 
55  m_timeSeries(data)
56 {
57  //Set style
58  setStyle(QwtPlotCurve::Lines);
59  setPaintAttribute(QwtPlotCurve::ClipPolygons);
60 
61  //Set Values
62  setData();
63 
64  //Adjusting the symbol
66  QPen linePen;
68 }
69 
71  TimeSeriesStyle* style,
72  size_t /*size*/)
73  : m_timeSeries(data), m_timeSeriesStyle(style)
74 {
75  //Set style
76  setStyle(QwtPlotCurve::Lines);
77  setPaintAttribute(QwtPlotCurve::ClipPolygons);
78 
79  //Set Values
80  setData();
81 }
82 
84 {
85  QPolygonF samples;
86  std::set<std::string> dates;
87 
89  while(it != m_timeSeries->end())
90  {
91  te::dt::DateTime* dateTime = it.getTime();
92  double date=0;
93 
94  if(dateTime->getDateTimeType() == te::dt::TIME_INSTANT)
95  {
96  std::unique_ptr<te::dt::TimeInstant> time((te::dt::TimeInstant*)dateTime);
97  QDate qdatei(time->getDate().getYear(), time->getDate().getMonth(), time->getDate().getDay());
98  QTime qtimei(time->getTime().getHours(), time->getTime().getMinutes(), time->getTime().getSeconds());
99  QDateTime qdate(qdatei, qtimei);
100  date = QwtDate::toDouble(qdate);
101  }
102  else if(dateTime->getDateTimeType() == te::dt::DATE)
103  {
104  std::unique_ptr<te::dt::Date> d ((te::dt::Date*)dateTime);
105  QDate qdatei(d->getDate().year(), d->getDate().month(), d->getDate().day());
106  QTime qtimei(0, 0); // Hours, minutes and second information not available in this date type
107  QDateTime qdate(qdatei, qtimei);
108  date = QwtDate::toDouble(qdate);
109  }
110 
111  samples += QPointF(date, it.getDouble());
112  ++it;
113  }
114  m_dates = dates;
115  setSamples( samples );
116 }
117 
119 {
120  delete m_timeSeriesStyle;
121 }
122 
124 {
126 }
127 
129 {
130  plot->setAxisScaleDraw(QwtPlot::xBottom, new te::qt::widgets::TimeScaleDraw());
131  plot->axisScaleDraw(QwtPlot::xBottom)->setLabelAlignment(Qt::AlignLeft | Qt::AlignVCenter);
132  plot->axisScaleDraw(QwtPlot::xBottom)->setLabelRotation(-60);
133  QwtPlotCurve::attach(plot);
134 }
135 
137 {
138  return m_timeSeries;
139 }
140 
142 {
143  delete m_timeSeries;
144  m_timeSeries = newTimeSeries;
145 }
146 
148 {
149  return m_timeSeriesStyle->clone();
150 }
151 
153 {
154  delete m_timeSeriesStyle;
155  m_timeSeriesStyle = newStyle;
156 
157  //The pen that will be used
158  QPen linePen;
159 
160  //Configuring the pen and brush based on the current style
162 
163  //Updating the chart's pen.
164  setPen(linePen);
165 }
166 
168 {
169  //Checking if this chart has a plot attached
170  if(plot())
171  plot()->setAxisScaleDraw(QwtPlot::xBottom, new te::qt::widgets::TimeScaleDraw(dateFormat));
172 }
double getDouble() const
It returns the attribute value as a double pointed by the internal cursor.
te::st::TimeSeries * getTimeSeries()
It returns the chart&#39;s timeSeries.
A class to represent a timeSeries&#39; chart.
A class to traverse the observations of a TimeSeries.
virtual DateTimeType getDateTimeType() const =0
It returns the subtype of the date and time type.
A class used to define the style of a TimeSeries&#39;s chart.
std::set< std::string > m_dates
The dates that will be plotted on the X axis.
te::dt::DateTime * getTime() const
It returns the datetime pointed by the internal cursor.
TimeSeriesStyle * getTimeSeriesStyle()
Returns a clone of the pointer to the timeSeries&#39; style.
A class to represent a set of labels to be used on a histogram&#39;s chart.
A class that adjusts the labels that based on temporal data on a chart.
A class to represent time instant.
Definition: TimeInstant.h:55
TEQTWIDGETSEXPORT void Config(QPen &pen, const te::se::Stroke *stroke)
It configs the given pen based on Symbology Enconding Stroke element.
void setTimeSeries(te::st::TimeSeries *newTimeSeries)
It sets the chart&#39;s timeSeries.
A base class for date data types.
Definition: Date.h:53
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
A class that adjusts the labels on a chart.
Definition: TimeScaleDraw.h:49
TimeSeriesStyle * clone()
Returns a pointer to a clone of this TimeSeriesStyle.
virtual int rtti() const
Returns the chart&#39;s type.
TimeSeriesChart(te::st::TimeSeries *data)
Constructor.
TimeSeriesStyle * m_timeSeriesStyle
The symbol that defines the look of a scatter&#39;s point.
te::se::Stroke * getStroke()
Returns a pointer to the line&#39;s stroke.
void setTimeSeriesStyle(TimeSeriesStyle *newStyle)
It sets the chart&#39;s style.
A class to represent time series.
Definition: TimeSeries.h:66
void attach(QwtPlot *plot)
It atttaches a QwtPlot to this Cahrt.
te::st::TimeSeries * m_timeSeries
The scatter that will be shown on this chart.
void setDateFormat(std::string dateFormat)
It sets the date format that will be used on the labels.
TimeSeriesIterator end() const
It returns an iterator that points to the end of the time series.
Definition: TimeSeries.cpp:203
TimeSeriesIterator begin() const
It returns an iterator that points to the first observation of the time series.
Definition: TimeSeries.cpp:197
This file contains a set of utility chart functions.