void testDiagramOwnership() { Legend* l = new Legend( m_chart ); QVERIFY( l->diagrams().size() == 0 ); l->addDiagram( m_lines ); QVERIFY( l->diagrams().size() == 1 ); l->addDiagram( m_bars ); QVERIFY( l->diagrams().size() == 2 ); QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines ); l->removeDiagram( m_lines ); QVERIFY( l->diagrams().size() == 1 ); QCOMPARE( dynamic_cast< BarDiagram * >(l->diagram()), m_bars ); l->replaceDiagram( m_lines, m_bars ); QVERIFY( l->diagrams().size() == 1 ); QCOMPARE( dynamic_cast< LineDiagram * >(l->diagram()), m_lines ); }
MainWindow::MainWindow( QWidget* parent ) : QWidget( parent ) { setupUi( this ); QHBoxLayout* chartLayout = new QHBoxLayout( chartFrame ); m_chart = new Chart(); m_chart->setGlobalLeading( 20, 20, 20, 20 ); chartLayout->addWidget( m_chart ); // Initialize the model, and fill it with data const int rowCount = 8; const int columnCount = 3; m_model = new QStandardItemModel(rowCount, columnCount, this); m_model->setHeaderData(0, Qt::Horizontal, tr("Product A")); m_model->setHeaderData(1, Qt::Horizontal, tr("Product B")); m_model->setHeaderData(2, Qt::Horizontal, tr("Product C")); openFile(":/Charts/qtdata.cht"); // Set up the diagram m_lines = new LineDiagram(); // Register the data model at the diagram m_lines->setModel( m_model ); // Add axes to the diagram CartesianAxis *xAxis = new CartesianAxis( m_lines ); CartesianAxis *yAxis = new CartesianAxis ( m_lines ); xAxis->setPosition ( KDChart::CartesianAxis::Bottom ); yAxis->setPosition ( KDChart::CartesianAxis::Left ); m_lines->addAxis( xAxis ); m_lines->addAxis( yAxis ); // Make the lines thicker for( int iColumn = 0; iColumn < columnCount; ++iColumn ){ QPen linePen( m_lines->pen( iColumn ) ); linePen.setWidth( 3 ); m_lines->setPen( iColumn, linePen ); } // Register the diagram at the coordinate plane m_chart->coordinatePlane()->replaceDiagram( m_lines ); // Add a legend Legend* legend = new Legend( m_lines, m_chart ); legend->setPosition( Position::South ); legend->setAlignment( Qt::AlignCenter ); legend->setShowLines( true ); legend->setTitleText(""); legend->setOrientation( Qt::Horizontal ); legend->addDiagram( m_lines ); m_chart->addLegend( legend ); }