1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/**
* SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
*
* This file is part of the KD Chart library.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "mainwindow.h"
#include <KChartLegend>
#include <KChartCartesianAxis>
#include <QColorDialog>
using namespace KChart;
MainWindow::MainWindow( QWidget *parent )
: QWidget( parent )
, m_chart( new Chart() )
, m_diagram( m_chart )
{
setupUi( this );
m_HLCModel.loadFromCSV( ":/HLC" );
m_OHLCModel.loadFromCSV( ":/OHLC" );
m_diagram.setType( StockDiagram::HighLowClose );
m_diagram.setModel( &m_HLCModel );
m_chart->coordinatePlane()->replaceDiagram( &m_diagram );
KChart::Legend* legend = new KChart::Legend( &m_diagram, m_chart );
m_chart->addLegend( legend );
QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame );
chartLayout->addWidget( m_chart );
// Abscissa
CartesianAxis *leftAxis = new CartesianAxis( &m_diagram );
// Ordinate
CartesianAxis *bottomAxis = new CartesianAxis( &m_diagram );
leftAxis->setPosition( CartesianAxis::Left );
TextAttributes attributes = bottomAxis->textAttributes();
attributes.setRotation( 90 );
attributes.setFontSize( Measure( 7.0, KChartEnums::MeasureCalculationModeAbsolute ) );
bottomAxis->setTextAttributes( attributes );
bottomAxis->setPosition( CartesianAxis::Bottom );
m_diagram.addAxis( leftAxis );
m_diagram.addAxis( bottomAxis );
m_diagram.addAxis( bottomAxis );
applyColor( QColor( "chartreuse" ) );
const bool connected = connect( colorChooser, SIGNAL(clicked()), SLOT(chooseColor()) );
Q_ASSERT( connected );
Q_UNUSED( connected );
// Initialize all values for the stock chart to sane defaults
initValues();
}
void MainWindow::chooseColor()
{
applyColor( QColorDialog::getColor( m_diagram.brush().color(), this ) );
}
void MainWindow::applyColor(const QColor &color)
{
if ( color.isValid() ) {
m_diagram.setPen( 0, QPen( color.darker( 130 ) ) );
m_diagram.setBrush( 0, QBrush( color ) );
QColor inverse( 255 - color.red(), 255 - color.green(), 255 - color.blue() );
m_diagram.setPen( 1, QPen( inverse.darker( 130 ) ) );
m_diagram.setBrush( 1, QBrush( inverse ) );
QPalette pal = colorChooser->palette();
pal.setBrush( QPalette::Button, QBrush( color ) );
colorChooser->setPalette( pal );
}
}
void MainWindow::initValues()
{
m_threeDBarAttributes = m_diagram.threeDBarAttributes();
m_threeDBarAttributes.setDepth( 10.0 );
m_threeDBarAttributes.setUseShadowColors( false );
threeDProperties->setChecked( m_threeDBarAttributes.isEnabled() );
perspectiveAngle->setValue( m_threeDBarAttributes.angle() );
perspectiveDepth->setValue( (int)m_threeDBarAttributes.depth() );
useShadowColors->setChecked( m_threeDBarAttributes.useShadowColors() );
m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
}
void MainWindow::on_threeDProperties_toggled( bool checked )
{
m_threeDBarAttributes.setEnabled( checked );
m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
m_chart->update();
}
void MainWindow::on_perspectiveAngle_valueChanged( int value )
{
m_threeDBarAttributes.setAngle( value );
m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
m_chart->update();
}
void MainWindow::on_perspectiveDepth_valueChanged( int value )
{
m_threeDBarAttributes.setDepth( value );
m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
m_chart->update();
}
void MainWindow::on_useShadowColors_toggled( bool checked )
{
m_threeDBarAttributes.setUseShadowColors( checked );
m_diagram.setThreeDBarAttributes( m_threeDBarAttributes );
m_chart->update();
}
void MainWindow::on_stockTypeCB_currentIndexChanged( const QString &text )
{
// FIXME: Workaround for disappearing diagram when setting new model
m_chart->coordinatePlane()->takeDiagram( &m_diagram );
if ( text == "High-Low-Close" ) {
m_diagram.setType( StockDiagram::HighLowClose );
m_diagram.setModel( &m_HLCModel );
} else if ( text == "Open-High-Low-Close" ) {
m_diagram.setType( StockDiagram::OpenHighLowClose );
m_diagram.setModel( &m_OHLCModel );
} else if ( text == "Candlestick" ) {
m_diagram.setType( StockDiagram::Candlestick );
m_diagram.setModel( &m_OHLCModel );
}
m_chart->coordinatePlane()->replaceDiagram( &m_diagram );
}
|