1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/candlestick_p.h>
5#include <private/candlestickanimation_p.h>
6#include <private/candlestickbodywicksanimation_p.h>
7
8
9QT_BEGIN_NAMESPACE
10
11CandlestickBodyWicksAnimation::CandlestickBodyWicksAnimation(Candlestick *candlestick,
12 CandlestickAnimation *animation,
13 int duration, QEasingCurve &curve)
14 : ChartAnimation(candlestick),
15 m_candlestick(candlestick),
16 m_candlestickAnimation(animation),
17 m_changeAnimation(false)
18{
19 setDuration(duration);
20 setEasingCurve(curve);
21}
22
23CandlestickBodyWicksAnimation::~CandlestickBodyWicksAnimation()
24{
25 if (m_candlestickAnimation)
26 m_candlestickAnimation->removeCandlestickAnimation(candlestick: m_candlestick);
27}
28
29void CandlestickBodyWicksAnimation::setup(const CandlestickData &startData,
30 const CandlestickData &endData)
31{
32 setKeyValueAt(step: 0.0, value: QVariant::fromValue(value: startData));
33 setKeyValueAt(step: 1.0, value: QVariant::fromValue(value: endData));
34}
35
36void CandlestickBodyWicksAnimation::setStartData(const CandlestickData &startData)
37{
38 if (state() != QAbstractAnimation::Stopped)
39 stop();
40
41 setStartValue(QVariant::fromValue(value: startData));
42}
43
44void CandlestickBodyWicksAnimation::setEndData(const CandlestickData &endData)
45{
46 if (state() != QAbstractAnimation::Stopped)
47 stop();
48
49 setEndValue(QVariant::fromValue(value: endData));
50}
51
52void CandlestickBodyWicksAnimation::updateCurrentValue(const QVariant &value)
53{
54 const CandlestickData data = qvariant_cast<CandlestickData>(v: value);
55 m_candlestick->setLayout(data);
56}
57
58QVariant CandlestickBodyWicksAnimation::interpolated(const QVariant &from, const QVariant &to,
59 qreal progress) const
60{
61 CandlestickData startData = qvariant_cast<CandlestickData>(v: from);
62 CandlestickData endData = qvariant_cast<CandlestickData>(v: to);
63 CandlestickData result = endData;
64
65 if (m_changeAnimation) {
66 result.m_open = startData.m_open + progress * (endData.m_open - startData.m_open);
67 result.m_high = startData.m_high + progress * (endData.m_high - startData.m_high);
68 result.m_low = startData.m_low + progress * (endData.m_low - startData.m_low);
69 result.m_close = startData.m_close + progress * (endData.m_close - startData.m_close);
70 } else {
71 const qreal median = (endData.m_open + endData.m_close) / 2;
72 result.m_low = median + progress * (endData.m_low - median);
73 result.m_close = median + progress * (endData.m_close - median);
74 result.m_open = median + progress * (endData.m_open - median);
75 result.m_high = median + progress * (endData.m_high - median);
76 }
77
78 return QVariant::fromValue(value: result);
79}
80
81QT_END_NAMESPACE
82
83#include "moc_candlestickbodywicksanimation_p.cpp"
84

source code of qtcharts/src/charts/animations/candlestickbodywicksanimation.cpp