KD Chart API Documentation  3.1
KDChartStockDiagram.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** This file is part of the KD Chart library.
4 **
5 ** SPDX-FileCopyrightText: 2001 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 ****************************************************************************/
10 
11 #include "KDChartStockDiagram.h"
12 #include "KDChartStockDiagram_p.h"
13 
14 #include "KDChartPaintContext.h"
15 #include "KDChartPainterSaver_p.h"
16 
17 using namespace KDChart;
18 
19 #define d d_func()
20 
22  : AbstractCartesianDiagram(new Private(), parent, plane)
23 {
24  init();
25 }
26 
28 {
29 }
30 
34 void StockDiagram::init()
35 {
36  d->diagram = this;
37  d->compressor.setModel(attributesModel());
38 
39  // Set properties to defaults
40  d->type = HighLowClose;
41  d->upTrendCandlestickBrush = QBrush(Qt::white);
42  d->downTrendCandlestickBrush = QBrush(Qt::black);
43  d->upTrendCandlestickPen = QPen(Qt::black);
44  d->downTrendCandlestickPen = QPen(Qt::black);
45 
46  d->lowHighLinePen = QPen(Qt::black);
48  // setDatasetDimension( 3 );
49 
50  setPen(QPen(Qt::black));
51 }
52 
58 {
59  d->type = type;
60  Q_EMIT propertiesChanged();
61 }
62 
67 {
68  return d->type;
69 }
70 
72 {
74  QVariant::fromValue(attr),
76  Q_EMIT propertiesChanged();
77 }
78 
80 {
82 }
83 
85 {
86  d->setDatasetAttrs(column, QVariant::fromValue(attr), StockBarAttributesRole);
87  Q_EMIT propertiesChanged();
88 }
89 
91 {
92  const QVariant attr(d->datasetAttrs(column, StockBarAttributesRole));
93  if (attr.isValid())
94  return attr.value<StockBarAttributes>();
95  return stockBarAttributes();
96 }
97 
104 {
106  QVariant::fromValue(attr),
108  Q_EMIT propertiesChanged();
109 }
110 
117 {
119 }
120 
131 {
132  d->setDatasetAttrs(column, QVariant::fromValue(attr), StockBarAttributesRole);
133  Q_EMIT propertiesChanged();
134 }
135 
146 {
147  const QVariant attr(d->datasetAttrs(column, ThreeDBarAttributesRole));
148  if (attr.isValid())
149  return attr.value<ThreeDBarAttributes>();
150  return threeDBarAttributes();
151 }
152 
153 void StockDiagram::setLowHighLinePen(const QPen &pen)
154 {
155  d->lowHighLinePen = pen;
156 }
157 
159 {
160  return d->lowHighLinePen;
161 }
162 
163 void StockDiagram::setLowHighLinePen(int column, const QPen &pen)
164 {
165  d->lowHighLinePens[column] = pen;
166 }
167 
168 QPen StockDiagram::lowHighLinePen(int column) const
169 {
170  if (d->lowHighLinePens.contains(column))
171  return d->lowHighLinePens[column];
172  return d->lowHighLinePen;
173 }
174 
176 {
177  d->upTrendCandlestickBrush = brush;
178 }
179 
181 {
182  return d->upTrendCandlestickBrush;
183 }
184 
186 {
187  d->downTrendCandlestickBrush = brush;
188 }
189 
191 {
192  return d->downTrendCandlestickBrush;
193 }
194 
195 void StockDiagram::setUpTrendCandlestickBrush(int column, const QBrush &brush)
196 {
197  d->upTrendCandlestickBrushes[column] = brush;
198 }
199 
201 {
202  if (d->upTrendCandlestickBrushes.contains(column))
203  return d->upTrendCandlestickBrushes[column];
204  return d->upTrendCandlestickBrush;
205 }
206 
207 void StockDiagram::setDownTrendCandlestickBrush(int column, const QBrush &brush)
208 {
209  d->downTrendCandlestickBrushes[column] = brush;
210 }
211 
213 {
214  if (d->downTrendCandlestickBrushes.contains(column))
215  return d->downTrendCandlestickBrushes[column];
216  return d->downTrendCandlestickBrush;
217 }
218 
220 {
221  d->upTrendCandlestickPen = pen;
222 }
223 
225 {
226  return d->upTrendCandlestickPen;
227 }
228 
230 {
231  d->downTrendCandlestickPen = pen;
232 }
233 
235 {
236  return d->downTrendCandlestickPen;
237 }
238 
239 void StockDiagram::setUpTrendCandlestickPen(int column, const QPen &pen)
240 {
241  d->upTrendCandlestickPens[column] = pen;
242 }
243 
245 {
246  if (d->upTrendCandlestickPens.contains(column))
247  return d->upTrendCandlestickPens[column];
248  return d->upTrendCandlestickPen;
249 }
250 
251 void StockDiagram::setDownTrendCandlestickPen(int column, const QPen &pen)
252 {
253  d->downTrendCandlestickPens[column] = pen;
254 }
255 
257 {
258  if (d->downTrendCandlestickPens.contains(column))
259  return d->downTrendCandlestickPens[column];
260  return d->downTrendCandlestickPen;
261 }
262 
263 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && defined(Q_COMPILER_MANGLES_RETURN_TYPE)
264 const
265 #endif
266  int
268 {
269  return 1;
270 }
271 
272 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && defined(Q_COMPILER_MANGLES_RETURN_TYPE)
273 const
274 #endif
275  int
277 {
278  return 1;
279 }
280 
282 {
283  // Clear old reverse mapping data and create new
284  // reverse mapping scene
285  d->reverseMapper.clear();
286 
287  PainterSaver painterSaver(context->painter());
288  const int rowCount = attributesModel()->rowCount(attributesModelRootIndex());
289  const int divisor = (d->type == OpenHighLowClose || d->type == Candlestick) ? 4 : 3;
290  const int colCount = attributesModel()->columnCount(attributesModelRootIndex()) / divisor;
291  for (int col = 0; col < colCount; ++col) {
292  for (int row = 0; row < rowCount; row++) {
293  CartesianDiagramDataCompressor::DataPoint low;
294  CartesianDiagramDataCompressor::DataPoint high;
295  CartesianDiagramDataCompressor::DataPoint open;
296  CartesianDiagramDataCompressor::DataPoint close;
297  CartesianDiagramDataCompressor::DataPoint volume;
298 
299  if (d->type == HighLowClose) {
300  const CartesianDiagramDataCompressor::CachePosition highPos(row, col * divisor);
301  const CartesianDiagramDataCompressor::CachePosition lowPos(row, col * divisor + 1);
302  const CartesianDiagramDataCompressor::CachePosition closePos(row, col * divisor + 2);
303  low = d->compressor.data(lowPos);
304  high = d->compressor.data(highPos);
305  close = d->compressor.data(closePos);
306  } else if (d->type == OpenHighLowClose || d->type == Candlestick) {
307  const CartesianDiagramDataCompressor::CachePosition openPos(row, col * divisor);
308  const CartesianDiagramDataCompressor::CachePosition highPos(row, col * divisor + 1);
309  const CartesianDiagramDataCompressor::CachePosition lowPos(row, col * divisor + 2);
310  const CartesianDiagramDataCompressor::CachePosition closePos(row, col * divisor + 3);
311  open = d->compressor.data(openPos);
312  low = d->compressor.data(lowPos);
313  high = d->compressor.data(highPos);
314  close = d->compressor.data(closePos);
315  }
316 
317  switch (d->type) {
318  case HighLowClose:
319  open.hidden = true;
320  Q_FALLTHROUGH();
321  case OpenHighLowClose:
322  if (close.index.isValid() && low.index.isValid() && high.index.isValid())
323  d->drawOHLCBar(col, open, high, low, close, context);
324  break;
325  case Candlestick:
326  d->drawCandlestick(col, open, high, low, close, context);
327  break;
328  }
329  }
330  }
331 }
332 
333 void StockDiagram::resize(const QSizeF &size)
334 {
335  d->compressor.setResolution(static_cast<int>(size.width() * coordinatePlane()->zoomFactorX()),
336  static_cast<int>(size.height() * coordinatePlane()->zoomFactorY()));
338  QAbstractItemView::resize(size.toSize());
339 }
340 
341 qreal StockDiagram::threeDItemDepth(int column) const
342 {
343  Q_UNUSED(column);
344  // FIXME: Implement threeD functionality
345  return 1.0;
346 }
347 
348 qreal StockDiagram::threeDItemDepth(const QModelIndex &index) const
349 {
350  Q_UNUSED(index);
351  // FIXME: Implement threeD functionality
352  return 1.0;
353 }
354 
355 const QPair<QPointF, QPointF> StockDiagram::calculateDataBoundaries() const
356 {
357  const int rowCount = attributesModel()->rowCount(attributesModelRootIndex());
358  const int colCount = attributesModel()->columnCount(attributesModelRootIndex());
359  qreal xMin = 0.0;
360  qreal xMax = rowCount;
361  qreal yMin = 0.0;
362  qreal yMax = 0.0;
363  for (int row = 0; row < rowCount; row++) {
364  for (int col = 0; col < colCount; col++) {
365  const CartesianDiagramDataCompressor::CachePosition pos(row, col);
366  const CartesianDiagramDataCompressor::DataPoint point = d->compressor.data(pos);
367  yMax = qMax(yMax, point.value);
368  yMin = qMin(yMin, point.value); // FIXME: Can stock charts really have negative values?
369  }
370  }
371  return QPair<QPointF, QPointF>(QPointF(xMin, yMin), QPointF(xMax, yMax));
372 }
#define d
Base class for diagrams based on a cartesian coordianate system.
virtual AttributesModel * attributesModel() const
void setPen(const QModelIndex &index, const QPen &pen)
QModelIndex attributesModelRootIndex() const
returns a QModelIndex pointing into the AttributesModel that corresponds to the root index of the dia...
AbstractCoordinatePlane * coordinatePlane() const
void setDatasetDimensionInternal(int dimension)
int rowCount(const QModelIndex &) const override
QVariant modelData(int role) const
int columnCount(const QModelIndex &) const override
bool setModelData(const QVariant value, int role)
Stores information about painting diagrams.
QPainter * painter() const
Attributes to customize the appearance of a column in a stock chart.
const QPair< QPointF, QPointF > calculateDataBoundaries() const override
void setDownTrendCandlestickBrush(const QBrush &brush)
void setThreeDBarAttributes(const ThreeDBarAttributes &attr)
ThreeDBarAttributes threeDBarAttributes() const
void resize(const QSizeF &size) override
void setUpTrendCandlestickBrush(const QBrush &brush)
StockBarAttributes stockBarAttributes() const
void setStockBarAttributes(const StockBarAttributes &attr)
int numberOfOrdinateSegments() const override
void paint(PaintContext *paintContext) override
QBrush upTrendCandlestickBrush() const
qreal threeDItemDepth(int column) const override
void setDownTrendCandlestickPen(const QPen &pen)
void setUpTrendCandlestickPen(const QPen &pen)
int numberOfAbscissaSegments() const override
void setLowHighLinePen(const QPen &pen)
QBrush downTrendCandlestickBrush() const
StockDiagram(QWidget *parent=nullptr, CartesianCoordinatePlane *plane=nullptr)
@ ThreeDBarAttributesRole
@ StockBarAttributesRole

© 2001 Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
https://www.kdab.com/development-resources/qt-tools/kd-chart/
Generated by doxygen 1.9.1