ScatterChart.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 ScatterChart.cpp
22 
23  \brief A class to represent a scatter chart.
24 */
25 
26 // TerraLib
27 #include "../../../dataaccess/dataset/ObjectIdSet.h"
28 #include "Enums.h"
29 #include "ScatterChart.h"
30 #include "Scatter.h"
31 #include "ScatterStyle.h"
32 #include "Utils.h"
33 
34 //QWT
35 #include <qwt_symbol.h>
36 #include <qwt_plot.h>
37 
38 //STL
39 #include <limits>
40 
42  :
43 
44  m_scatter(data)
45 {
46  //Set style
47  setStyle(QwtPlotCurve::NoCurve);
48  setPaintAttribute(QwtPlotCurve::FilterPoints );
49 
50  m_selection = new QwtPlotCurve();
51  m_selection->setStyle(QwtPlotCurve::NoCurve);
52  m_selection->setPaintAttribute(QwtPlotCurve::FilterPoints);
53  m_selection->attach(plot());
54 
55  //Set Values
56  setData();
57 
58  //Adjusting the symbol
60  if(dataSize() > 500)
61  {
62  QwtSymbol* newSymbol = m_scatterStyle->getSymbol();
63  newSymbol->setSize(QSize(2 , 2));
64  setSymbol(newSymbol);
65  }
66  else
67  {
68  setSymbol(new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::gray ), QPen( Qt::black, 1 ), QSize( 8, 8 )));
69  }
70 
71  //The default selection color is green
72  m_selColor = ("#00FF00");
73  m_selection->setSymbol(new QwtSymbol( symbol()->style(), m_selColor, m_selColor, symbol()->size()));
74 }
75 
77  size_t /*size*/)
78  : m_scatter(data), m_scatterStyle(style)
79 {
80 
81  m_selection = new QwtPlotCurve();
82  m_selection->setStyle(QwtPlotCurve::NoCurve);
83  m_selection->setPaintAttribute(QwtPlotCurve::FilterPoints);
84  m_selection->attach(plot());
85 
86  //Set style
87  setStyle(QwtPlotCurve::NoCurve);
88  setPaintAttribute(QwtPlotCurve::FilterPoints);
89 
90  //Set Values
91  setData();
92 }
93 
95 {
96  QPolygonF samples;
97 
98  if(m_scatter->sizeX() < m_scatter->sizeY())
99  {
100  for (size_t i = 0; i < m_scatter->sizeX(); i++)
101  {
102  samples += QPointF(m_scatter->getX(static_cast<unsigned int>(i)), m_scatter->getY(static_cast<unsigned int>(i)));
103  }
104  }
105  else
106  {
107  for (size_t i = 0; i < m_scatter->sizeY(); i++)
108  {
109  samples += QPointF(m_scatter->getX(static_cast<unsigned int>(i)), m_scatter->getY(static_cast<unsigned int>(i)));
110  }
111  }
112 
113  setSamples( samples );
114 }
115 
117 {
118  delete m_scatter;
119  delete m_scatterStyle;
120  delete m_selection;
121 }
122 
124 {
126 }
127 
129 {
130  return m_scatter;
131 }
132 
134 {
135  delete m_scatter;
136  m_scatter = newScatter;
137 }
138 
140 {
141  return m_scatterStyle->clone();
142 }
143 
145 {
146  delete m_scatterStyle;
147  m_scatterStyle = newSymbol;
148 
149  //Updating the Chart's symbol based on the current style
150  setSymbol(m_scatterStyle->getSymbol());
151 
152  //Updating the selection symbol based on the current style;
153  QwtSymbol* selSymbol = new QwtSymbol( symbol()->style(), symbol()->brush().color().darker (180 ), symbol()->pen().color().darker( 180), symbol()->size());
154  QPixmap selPixmap = symbol()->pixmap();
155  selPixmap.fill(m_selColor);
156  selSymbol->setPixmap(selPixmap);
157  m_selection->setSymbol(selSymbol);
158 }
159 
161 {
162  //Removing the previous selection, if there was any.
163  m_selection->detach();
164 
165  std::set<te::da::ObjectId*, te::common::LessCmp<te::da::ObjectId*> >::const_iterator itObjSet;
166  QVector<QPointF> highlightedPoints;
167 
168  for(itObjSet = oids->begin(); itObjSet != oids->end(); ++itObjSet)
169  {
170  std::pair<double, double> point = m_scatter->find((*itObjSet));
171  if(this->boundingRect().contains(point.first, point.second))
172  highlightedPoints.push_back(QPointF(point.first, point.second));
173  }
174 
175  m_selection->setSamples(highlightedPoints);
176 
177  m_selection->attach(plot());
178  plot()->replot();
179 }
180 
182 {
183  const QwtScaleMap xMap = plot()->canvasMap( xAxis() );
184  const QwtScaleMap yMap = plot()->canvasMap( yAxis() );
185 
186  const double cx = xMap.transform( point.rx());
187  const double cy = yMap.transform( point.ry() );
188 
189  QPoint mappedPoint = QPoint(cx, cy);
190 
191  int index = closestPoint( mappedPoint);
192 
193  double x = sample(index).x();
194  double y = sample(index).y();
195  return m_scatter->find(x, y);
196 }
197 
199 {
200  QwtSeriesData<QPointF>* values = data();
201  std::vector<QPointF> selected;
202 
203  if(rect.width() == 0 && rect.height() == 0)
204  return highlight(QPoint(rect.x(), rect.y()));
205 
206  //Acquiring all the selected points:
207  for(size_t i = 0; i < values->size(); ++i)
208  {
209  if( rect.contains(values->sample(i)))
210  selected.push_back(values->sample(i));
211  }
212  return m_scatter->find(selected);
213 }
214 
216 {
217  m_selColor = selColor;
218 
219  //The highlighted color will always have an alpha aplied to it
220  m_selColor.setAlpha(100);
221 }
ScatterStyle * m_scatterStyle
The symbol that defines the look of a scatter&#39;s point.
Definition: ScatterChart.h:147
void setScatterStyle(ScatterStyle *newStyle)
It sets the chart&#39;s style.
double getX(unsigned int idx)
It returns the idx-th value of the scatter X axis, as a double.
Definition: Scatter.cpp:97
te::da::ObjectIdSet * find(double &xValue, double &yValue)
It returns a pointer to an ObjectIdSet that contains all of the scatter&#39;s selected points...
Definition: Scatter.cpp:189
QwtSymbol * getSymbol()
Returns a pointer to a QwtSymbol representing the current scatter&#39;s point style.
A class to represent a scatter.
Definition: Scatter.h:51
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
void setSelectionColor(QColor selColor)
color used to hgihlight selected objects on this chart.
virtual int rtti() const
Returns the chart&#39;s type.
QwtPlotCurve * m_selection
The PlotItems to be highlighted when a selection occurs;.
Definition: ScatterChart.h:149
std::size_t sizeX()
It returns the size of the scatter.
Definition: Scatter.cpp:82
ScatterStyle * getScatterStyle()
Returns a clone of the pointer to the scatter&#39;s style.
A class to represent a scatter&#39;s chart.
A class to represent a scatter.
ScatterStyle * clone()
Returns a pointer to a clone of this ScatterStyle.
void highlight(const te::da::ObjectIdSet *oids)
Highlights the objects identified by oids.
QColor m_selColor
The color used to highlight selected obecjts.
Definition: ScatterChart.h:148
void setScatter(te::qt::widgets::Scatter *newScatter)
It sets the chart&#39;s scatter.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator end() const
Returns an iterator for the object ids in container.
ScatterChart(Scatter *data)
Constructor.
std::size_t sizeY()
It returns the size of the scatter.
Definition: Scatter.cpp:87
double getY(unsigned int idx)
It returns the idx-th value of the scatter Y axis, as a double.
Definition: Scatter.cpp:112
te::qt::widgets::Scatter * getScatter()
It returns the chart&#39;s scatter.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
Scatter * m_scatter
The scatter that will be shown on this chart.
Definition: ScatterChart.h:146
This file contains a set of utility chart functions.