From 0f97f496fd673c469ea0250c43d3a3ffba3125bf 2012-06-03 13:09:08 From: Marek Rosa Date: 2012-06-03 13:09:08 Subject: [PATCH] Added documentation for BarModelMapper example --- diff --git a/doc/src/examples-barmodelmapper.qdoc b/doc/src/examples-barmodelmapper.qdoc new file mode 100644 index 0000000..6f7c048 --- /dev/null +++ b/doc/src/examples-barmodelmapper.qdoc @@ -0,0 +1,55 @@ +/*! + \example examples/barmodelmapper + \title BarModelMapper example + \subtitle + + This example shows how to use QAbstractItemModel derived model as the data for the bar series. + + Let's start by creating an instance of CustomTableModel class. + CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example. + The constructor of this class populates the internal data store of the model with the data that is good for our chart example. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 1 + + We now have a model with data that we would like to display both on the chart and in a QTableView. + First, we create QTableView and tell it use the model as a data source. To make the data cells fill the table view we also change headers resize mode. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 2 + + Now we need QChart instance to display the same data on the chart. + We also enable animations. It makes it easier to see how modifying the model's data affect the chart. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 3 + + First line of the code below creates new grouped bar series. Variables firstRow and rowCount are used to define a custom model mapping. + Custom mapping allows to take only part of the data from the model. In this case data from 5 rows starting with the row with the index 3. + Following three lines create an instance of QVBarModelMapper class and specifie that data for the bar sets should be taken from the model's columns(Qt::Vertical) with indexes from 1 to 4(inclusive). + To create a connection between the series and the model we set both of those objects to QVBarModelMapper. + + Finally the series is added to the chart. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 4 + + To show in QTableView which data coresponds with which series this example uses table coloring. + When series is added to the chart it is assigned a color beased on the currently selected theme. + Code below extracts that color from the series and uses it to create colored QTableView. + Coloring of the view is not a part of the QChart functionality. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 5 + + We would like to have categories placed on the chart's axis that describe what the data means. + Next snippet shows how to do that. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 6 + + To avoid setting up the QGraphicsScene we use QChartView class that does it for us. QChart object pointer is used as a parameter of the QChartView constructor. + To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 7 + + Finally we place both widgets in a layout and use the layout as the application layout. + + \snippet ../examples/barmodelmapper/tablewidget.cpp 8 + + Application is ready. Try modifying the data in the table view and see how it affects the chart. +*/ diff --git a/doc/src/examples.qdoc b/doc/src/examples.qdoc index 8e99ca7..c66bd46 100644 --- a/doc/src/examples.qdoc +++ b/doc/src/examples.qdoc @@ -20,6 +20,7 @@
  • Legend
  • Line chart
  • Model data
  • +
  • Bar chart from model
  • Percent bar chart
  • Pie chart
  • Pie chart drilldown
  • diff --git a/examples/barmodelmapper/customtablemodel.cpp b/examples/barmodelmapper/customtablemodel.cpp index c137489..3e954e1 100644 --- a/examples/barmodelmapper/customtablemodel.cpp +++ b/examples/barmodelmapper/customtablemodel.cpp @@ -30,7 +30,7 @@ CustomTableModel::CustomTableModel(QObject *parent) : qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); m_columnCount = 6; - m_rowCount = 10; + m_rowCount = 12; // m_data for (int i = 0; i < m_rowCount; i++) @@ -69,7 +69,7 @@ QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, return QString("201%1").arg(section); } else - return QString("%1").arg(section); + return QString("%1").arg(section + 1); } QVariant CustomTableModel::data(const QModelIndex & index, int role) const diff --git a/examples/barmodelmapper/tablewidget.cpp b/examples/barmodelmapper/tablewidget.cpp index 0f44c10..3e49210 100644 --- a/examples/barmodelmapper/tablewidget.cpp +++ b/examples/barmodelmapper/tablewidget.cpp @@ -46,7 +46,7 @@ TableWidget::TableWidget(QWidget *parent) // create table view and add model to it QTableView *tableView = new QTableView; tableView->setModel(model); - tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); + tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch); //! [2] @@ -71,11 +71,6 @@ TableWidget::TableWidget(QWidget *parent) chart->addSeries(series); //! [4] - QStringList categories; - categories << "June" << "July" << "August" << "September" << "October" << "November"; - - chart->axisX()->categories()->insert(categories); - //! [5] // for storing color hex from the series QString seriesColorHex = "#000000"; @@ -88,13 +83,20 @@ TableWidget::TableWidget(QWidget *parent) } //! [5] - //! [8] + //! [6] + QStringList categories; + categories << "April" << "May" << "June" << "July" << "August"; + + chart->axisX()->categories()->insert(categories); + //! [6] + + //! [7] QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); chartView->setMinimumSize(640, 480); - //! [8] + //! [7] - //! [9] + //! [8] // create main layout QGridLayout* mainLayout = new QGridLayout; mainLayout->addWidget(tableView, 1, 0); @@ -102,5 +104,5 @@ TableWidget::TableWidget(QWidget *parent) mainLayout->setColumnStretch(1, 1); mainLayout->setColumnStretch(0, 0); setLayout(mainLayout); - //! [9] + //! [8] } diff --git a/src/areachart/qareaseries.cpp b/src/areachart/qareaseries.cpp index fa89ba9..8d9c66c 100644 --- a/src/areachart/qareaseries.cpp +++ b/src/areachart/qareaseries.cpp @@ -126,6 +126,9 @@ QAbstractSeries::SeriesType QAreaSeries::type() const return QAbstractSeries::SeriesTypeArea; } +/*! + Sets the \a series that is to be used as the area chart upper series. +*/ void QAreaSeries::setUpperSeries(QLineSeries* series) { Q_D(QAreaSeries); @@ -138,6 +141,9 @@ QLineSeries* QAreaSeries::upperSeries() const return d->m_upperSeries; } +/*! + Sets the \a series that is to be used as the area chart lower series. +*/ void QAreaSeries::setLowerSeries(QLineSeries* series) { Q_D(QAreaSeries);