void PlottingDialog::drawRGBHistogram(QImage *img){
    QCPBars *rBar = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    QCPBars *gBar = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    QCPBars *bBar = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    ui->customPlot->addPlottable(rBar);
    ui->customPlot->addPlottable(gBar);
    ui->customPlot->addPlottable(bBar);
    int bins = 16;
    QVector<double> keyRData(bins);
    QVector<double> valueRData(bins, 0);
    QVector<double> keyGData(bins);
    QVector<double> valueGData(bins, 0);
    QVector<double> keyBData(bins);
    QVector<double> valueBData(bins, 0);
    QColor currentPixel;
    int currentRed, currentGreen, currentBlue;
    // now we can modify properties of myBars:
    for(int x=0;x<img->width();x++){
        for(int y=0;y<img->height();y++){
            currentPixel = QColor(img->pixel(x,y));
            currentRed = currentPixel.redF() * bins;
            valueRData[(currentRed == bins) ? bins - 1 : currentRed]++;
            currentGreen = currentPixel.greenF() * bins;
            valueGData[(currentGreen == bins) ? bins - 1 : currentGreen]++;
            currentBlue = currentPixel.blueF() * bins;
            valueBData[(currentBlue == bins) ? bins - 1 : currentBlue]++;
        }
    }
    rBar->setName("R Value");
    rBar->setBrush(QBrush(Qt::red));
    gBar->setName("G Value");
    gBar->setBrush(QBrush(Qt::green));
    bBar->setName("B Value");
    bBar->setBrush(QBrush(Qt::blue));
    int totalPix = img->width() * img->height();
    for(int i = 0;i<bins;i++){
        keyRData[i] = (i/(double) bins);
        keyGData[i] = (i/(double) bins);
        keyBData[i] = (i/(double) bins);
        valueRData[i] = valueRData[i] / (double)totalPix;
        valueGData[i] = valueGData[i] / (double)totalPix;
        valueBData[i] = valueBData[i] / (double)totalPix;
    }
    rBar->setData(keyRData, valueRData);
    gBar->setData(keyGData, valueGData);
    bBar->setData(keyBData, valueBData);
    ui->customPlot->legend->setVisible(true);
    ui->customPlot->xAxis->setLabel("Color %");
    ui->customPlot->xAxis->setRange(0,1);
    rBar->setWidth(1/(4*(double)bins));
    gBar->setWidth(1/(4*(double)bins));
    bBar->setWidth(1/(4*(double)bins));
    ui->customPlot->yAxis->setLabel("Image %");
    ui->customPlot->yAxis->setRange(0,1);
    ui->customPlot->replot();
    qDebug() << "Replotting";
}
Beispiel #2
0
/**
 * @brief widgetBarChart::drawYear draw a given year on a bar chart
 * @param sel the index of the selected year
 */
void widgetBarChart::drawYear(const int &sel) {
    QCPBars *bar = new QCPBars(ui->widgetBarChart_2->xAxis,ui->widgetBarChart_2->yAxis);
    bar->setName(_meas->yearRange().at(sel));
    bar->setWidth(BAR_WIDTH);
    QPen pen;
    bar->setBrush(COLOUR_LIST[sel%5]);
    pen.setWidthF(PEN_WIDTH);

    QVector<double> valueData, keyData;
    ui->widgetBarChart_2->addPlottable(bar);
    double i = 0.25 + 0.2*ui->widgetBarChart_2->xAxis->plottables().size();
    QList<QListWidgetItem*> tempList = ui->listBarChartMunicipality->selectedItems();
    for(QList<QListWidgetItem*>::Iterator it = tempList.begin(); it != tempList.end(); ++it) {
        QListWidgetItem *tempItem = *it;
        keyData << i;
        i+=1.5;
        try {
            valueData.append(_meas->findMuni(tempItem->text()).getYear(_meas->yearRange().at(sel)).value());
        } catch (std::string s) {
            valueData.append(0.0);
        }
    }
    bar->setData(keyData,valueData);
    bar->keyAxis()->setTickVector(keyData);
    ui->widgetBarChart_2->legend->setVisible(true);
    ui->widgetBarChart_2->replot();
}
void Window::plotHistogram(QVector<double> key, QVector<double> nonEq, QVector<double> eqValue, QVector<double> lutValue)
{
    QCustomPlot *histogramPlot = ui->histogramPlot;

    histogramPlot->clearGraphs();

    // Non Equalized
    QCPBars *nonEqHistBars = new QCPBars(histogramPlot->xAxis, histogramPlot->yAxis);
    histogramPlot->addPlottable(nonEqHistBars);
    nonEqHistBars->setWidth(1);
    nonEqHistBars->setData(key, nonEq);
    nonEqHistBars->setPen(Qt::NoPen);
    nonEqHistBars->setBrush(QColor(10, 140, 70, 160));

    //    // Equalized
    //    QCPBars *eqHistBars = new QCPBars(histogramPlot->xAxis, histogramPlot->yAxis);
    //    histogramPlot->addPlottable(eqHistBars);
    //    eqHistBars->setWidth(1);
    //    eqHistBars->setData(key, eqValue);
    //    eqHistBars->setPen(Qt::NoPen);
    //    eqHistBars->setBrush(QColor(10, 100, 50, 70));
    ////    eqHistBars->moveAbove(eqHistBars);

    //    // LUT
    //    QCPGraph *lut = histogramPlot->addGraph();
    //    lut->setData(key, lutValue);
    //    lut->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssNone, QPen(Qt::black, 1.5), QBrush(Qt::white), 9));
    //    lut->setLineStyle(QCPGraph::lsStepCenter);
    //    lut->setPen(QPen(QColor(120, 120, 120), 2));

    histogramPlot->replot();
    histogramPlot->rescaleAxes();


}
void PlottingDialog::drawLumHistogram(QImage *img){
    QCPBars *lBar = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    ui->customPlot->addPlottable(lBar);
    int bins = 16; // edit this to define the number of bins in which the histogram is separated
    QVector<double> keyLData(bins);
    QVector<double> valueLData(bins, 0);
    qreal currentLightness;
    for(int x=0;x<img->width();x++) {
        for(int y=0;y<img->height();y++){
            currentLightness = qFloor(QColor(img->pixel(x,y)).lightnessF() * bins);
            //qDebug() << "CurrentLightness" << currentLightness;
            //only special case: 100% luminance -> last expression returns BINS == ilegal access!
            currentLightness = (currentLightness == bins) ? bins -1 : currentLightness;
            valueLData[currentLightness]++;
        }
    }
    int totalPix = img->width() * img->height();
    for (int i=0;i<bins;i++){
        keyLData[i] = (i/(double)bins);
        valueLData[i] = valueLData[i] / (double) totalPix;
    }
    qDebug() << keyLData;
    qDebug() << valueLData;
    lBar->setData(keyLData, valueLData);
    ui->customPlot->xAxis->setLabel("Luminance %");
    ui->customPlot->xAxis->setRange(0,1);
    lBar->setWidth(1/(2*(double)bins));
    ui->customPlot->yAxis->setLabel("Image %");
    ui->customPlot->yAxis->setRange(0,1);
    ui->customPlot->replot();
}
Beispiel #5
0
void RImageDock::drawHist()
{
    cv::Mat tempHist;
    matHist.convertTo(tempHist, CV_64F);

    int nBins = matHist.rows;
    double minHist;
    double maxHist;
    cv::minMaxLoc(matHist, &minHist, &maxHist);

    QVector<double> x(nBins); // y must be the histogram values
    for (int i=0; i< nBins; ++i)
    {
      x[i] = dataMin + histWidth*i; // x goes from 0 to nBins-1
    }

    // Pointer to matHist data.
    const double* matHistPtr = tempHist.ptr<double>(0);
    std::vector<double> matHistStdVect(matHistPtr, matHistPtr + matHist.rows);
    QVector<double> y = QVector<double>::fromStdVector(matHistStdVect);

    // create graph and assign data to it:
    customPlot = new QCustomPlot();
    customPlot->xAxis->setLabel("Intensity bins");
    customPlot->yAxis->setLabel("Pixels");

    QCPBars *myBars = new QCPBars(customPlot->xAxis, customPlot->yAxis);
    customPlot->addPlottable(myBars);
    myBars->setData(x, y);
    myBars->setWidth(1);

//    customPlot->addGraph();
//    customPlot->graph(0)->setData(x, y);

    customPlot->yAxis->setScaleType(QCPAxis::stLogarithmic);
    customPlot->rescaleAxes();
    // set axes ranges, so we see all data:
    //customPlot->xAxis->setRange(0, nBins+100);
    double maxXRange = std::min(dataMax, (double) 4*percentileHigh);
    double minXRange = std::min(0.0, (double) percentileLow);
    customPlot->xAxis->setRange(minXRange, maxXRange);
    customPlot->yAxis->setRange(1, maxHist);

//    customPlot->plottable(0)->setPen(QPen(QColor(0, 0, 255, 50))); // line color blue for first graph
//    customPlot->plottable(0)->setBrush(QBrush(QColor(0, 0, 255, 50)));
    customPlot->plottable(0)->setPen(QPen(QColor(125, 125, 125, 50))); // line color blue for first graph
    customPlot->plottable(0)->setBrush(QBrush(QColor(125, 125, 125, 50)));
    customPlot->setInteraction(QCP::iRangeDrag, true);
    customPlot->setInteraction(QCP::iRangeZoom, true);
    customPlot->axisRect(0)->setRangeDrag(Qt::Horizontal);
    customPlot->axisRect(0)->setRangeZoom(Qt::Horizontal);

    qDebug("customPlot ready for emit signal.");
    emit plotSignal(customPlot);

}
/*!
 * Function: slot_get_graph
 * Description: When "Get Graph" is clicked, this function is responsible for generating the graph based on the data passed,
 * a full description of each part is given below
 * @param bool check when it is check
 */
void MainWindow::slot_get_graph(bool)
{


    Stub *stub = new Stub(); //! stub contains data for testing purpose.

    //! This is the variable for bar width; the value will be calculated and scaled accordingly, depending on how
    //! many checkboxes were selected
    double barWidth = 0;
    double i = 0;  //! loop index; counts how many checkboxes where checked
    int barsAdded = 0;


    //! Creating the legend box
    ui -> customPlot -> legend -> clearItems();
    ui -> customPlot -> axisRect()->insetLayout()->setInsetAlignment(0,Qt::AlignTop|Qt::AlignRight);
    ui -> customPlot -> legend -> setVisible(true);
    ui->customPlot->clearPlottables(); //! clear the graph

    //! Set graph interval
    ui->customPlot->xAxis->setAutoTickStep(0);
    ui->customPlot->xAxis->setTicks(1);
    ui->customPlot->xAxis->setTickStep(1.0);
    ui->customPlot->xAxis->setSubTickCount(0);

    chartcolour *colourSelect = new chartcolour();

    ui->customPlot->legend->clearItems();       //!Clears the existing data from the legend
    ui->customPlot->axisRect()->setAutoMargins(QCP::msLeft | QCP::msTop | QCP::msBottom);
    ui->customPlot->axisRect()->setMargins(QMargins(0,0,150,0));
    ui->customPlot->axisRect()->insetLayout()->setInsetPlacement(0, QCPLayoutInset::ipFree);
    ui->customPlot->axisRect()->insetLayout()->setInsetRect(0, QRectF(1.1,0,0.1,0.1));



    //! Scaling the bar width based on the number of cities checked
        if (ui->barrie->isChecked()) {
            ++i;
        }
        if (ui->calgary->isChecked()) {
            ++i;
        }
        if (ui->hamilton->isChecked()) {
            ++i;
        }
        if (ui->london->isChecked()) {
            ++i;
        }
        if (ui->ottawa->isChecked()) {
            ++i;
        }
        if (ui->sudbury->isChecked()) {
            ++i;
        }
        if (ui->thunderBay->isChecked()) {
            ++i;
        }
        if (ui->toronto->isChecked()) {
            ++i;
        }

        if (ui->windsor->isChecked()) {
            ++i;
        }
        if (ui->winnipeg->isChecked()) {
            ++i;
        }


    //! Calculating the new bar width
    double totalWidth = 0.8;
    barWidth = totalWidth / i;
    double setBack = totalWidth/2;
    double offset = 0;


    //! Plot data and create a bar graph for Barrie if this city is selected
    if(ui->barrie->isChecked()) {   //! if barrie is checked
            QCPBars *myBars = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars->setWidth(barWidth);
            myBars->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars->setName("Barrie");

            ui->customPlot->addPlottable(myBars);
            //! Barrie data
            QVector<double> barrieDatatemp = stub->getData("Barrie");
            QVector<double> barrieData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                barrieData << barrieDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                barrieData << barrieDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                barrieData << barrieDatatemp.at(2);
            }
            //!valueYears << 2009-setBack+offset << 2010-setBack+offset<< 2011-setBack+offset;
            ++barsAdded;


            myBars->setData(valueYears, barrieData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();
        }


        //! Plot data and create a bar graph for Calgary if this city is selected
        if (ui->calgary->isChecked()) {
            //! This is for Calgary
            QCPBars *myBars2 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars2->setWidth(barWidth);
            myBars2->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));


            ui->customPlot->addPlottable(myBars2);
            //! now we can modify properties of myBars
            myBars2->setName("Calgary");

            //! Calgary Data
            QVector<double> calgaryDatatemp = stub->getData("Calgary");
            QVector<double> calgaryData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                calgaryData << calgaryDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                calgaryData << calgaryDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                calgaryData << calgaryDatatemp.at(2);
            }

           //! valueYears2 << 2009-setBack+offset << 2010-setBack+offset<< 2011-setBack+offset;
            //!2008.60+barWidth*barsAdded << 2009.60+barWidth*barsAdded << 2010.60+barWidth*barsAdded;
            ++barsAdded;

            myBars2->setData(valueYears, calgaryData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();
        }

        //!Plot data and create a bar graph for London if this city is selected
        if (ui->london->isChecked()) {
            QCPBars *myBars3 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars3->setWidth(barWidth);
            myBars3->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars3->setName("London");

            ui->customPlot->addPlottable(myBars3);
            //! London data
            QVector<double> londonDatatemp = stub->getData("London");
            QVector<double> londonData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                londonData << londonDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                londonData << londonDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                londonData << londonDatatemp.at(2);
            }

           //! valueYears << 2009-setBack+offset << 2010-setBack+offset<< 2011-setBack+offset;
            ++barsAdded;


            myBars3->setData(valueYears, londonData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();


        }


        //!Plot data and create a bar graph for Hamilton if this city is selected
        if (ui->hamilton->isChecked()) {
            QCPBars *myBars4 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars4->setWidth(barWidth);
            myBars4->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars4->setName("Hamilton");

            ui->customPlot->addPlottable(myBars4);
            //! Hamilton data
            QVector<double> hamiltonDatatemp = stub->getData("Hamilton");
            QVector<double> hamiltonData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                hamiltonData << hamiltonDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                hamiltonData << hamiltonDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                hamiltonData << hamiltonDatatemp.at(2);
            }

            ++barsAdded;


            myBars4->setData(valueYears, hamiltonData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();

        }

        //! Plot data and create a bar graph for Ottawa if this city is selected
        if (ui->ottawa->isChecked()) {
            QCPBars *myBars5 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars5->setWidth(barWidth);
            myBars5->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars5->setName("Ottawa");

            ui->customPlot->addPlottable(myBars5);
            //! Ottawa data
            QVector<double> ottawaDatatemp = stub->getData("Ottawa");
            QVector<double> ottawaData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                ottawaData << ottawaDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                ottawaData << ottawaDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                ottawaData << ottawaDatatemp.at(2);
            }

            ++barsAdded;


            myBars5->setData(valueYears, ottawaData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();

        }

        //! Plot data and create a bar graph for Sudbury (Greater) if this city is selected
        if (ui->sudbury->isChecked()) {
            QCPBars *myBars6 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars6->setWidth(barWidth);
            myBars6->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars6->setName("Sudbury");

            ui->customPlot->addPlottable(myBars6);
            //! Sudbury data
            QVector<double> sudburyDatatemp = stub->getData("Sudbury (Greater)");
            QVector<double> sudburyData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                sudburyData << sudburyDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                sudburyData << sudburyDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                sudburyData << sudburyDatatemp.at(2);
            }


            ++barsAdded;


            myBars6->setData(valueYears, sudburyData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();

        }


        //! Plot data and create a bar graph for Thunder Bay if this city is selected
        if (ui->thunderBay->isChecked()) {
            QCPBars *myBars7 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars7->setWidth(barWidth);
            myBars7->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars7->setName("Thunder Bay");

            ui->customPlot->addPlottable(myBars7);
            //! Thunder Bay data
            QVector<double> thunderDatatemp = stub->getData("Thunder Bay");
            QVector<double> thunderData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                thunderData << thunderDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                thunderData << thunderDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                thunderData << thunderDatatemp.at(2);
            }

            ++barsAdded;


            myBars7->setData(valueYears, thunderData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();
        }

        //! Plot data and create a bar graph for Toronto if this city is selected
        if (ui->toronto->isChecked()) {
            QCPBars *myBars8 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars8->setWidth(barWidth);
            myBars8->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars8->setName("Toronto");

            ui->customPlot->addPlottable(myBars8);
            //! Sudbury data
            QVector<double> torontoDatatest = stub->getData("Toronto");
            QVector<double> torontoData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                torontoData << torontoDatatest.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                torontoData << torontoDatatest.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                torontoData << torontoDatatest.at(2);
            }
            ++barsAdded;


            myBars8->setData(valueYears, torontoData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();
        }


        //! Plot data and create a bar graph for Winnipeg if this city is selected
        if (ui->winnipeg->isChecked()) {
            QCPBars *myBars9 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars9->setWidth(barWidth);
            myBars9->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars9->setName("Winnipeg");

            ui->customPlot->addPlottable(myBars9);
            //! Sudbury data
            QVector<double> winnipegDatatemp = stub->getData("Winnipeg");
            QVector<double> winnipegData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                winnipegData << winnipegDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                winnipegData << winnipegDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                winnipegData << winnipegDatatemp.at(2);
            }

            ++barsAdded;


            myBars9->setData(valueYears, winnipegData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();
        }

        //! Plot data and create a bar graph for Windsor if this city is selected
        if (ui->windsor->isChecked()) {
            QCPBars *myBars10 = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
            myBars10->setWidth(barWidth);
            myBars10->setBrush(QBrush(QColor(colourSelect->getColour(barsAdded))));
            myBars10->setName("Windor");

            ui->customPlot->addPlottable(myBars10);
            //! Sudbury data
            QVector<double> windsorDatatemp = stub->getData("Windsor");
            QVector<double> windsorData;
            QVector<double> valueYears;

            offset = (barWidth*barsAdded)+(barWidth/2);

            /*!
                Checks which years are checked.  Plots the years selected, and scales the graph accordingly.
              */
            if(ui->twoThousandNine->isChecked())
            {
                valueYears << 2009-setBack+offset;
                windsorData << windsorDatatemp.at(0);
            }
            if(ui->twoThousandTen->isChecked())
            {
                valueYears << 2010-setBack+offset;
                windsorData << windsorDatatemp.at(1);
            }
            if(ui->twoThousandEleven->isChecked())
            {
                valueYears << 2011-setBack+offset;
                windsorData << windsorDatatemp.at(2);
            }

            ++barsAdded;


            myBars10->setData(valueYears, windsorData);

            ui->customPlot->rescaleAxes();
            ui->customPlot->replot();
        }
}
DrawFinancialChart::DrawFinancialChart(QWidget *parent) :
    QCustomPlot(parent)
{
    resize(600,400);

    legend->setVisible(true);

    // generate two sets of random walk data (one for candlestick and one for ohlc chart):
    int n = 500;
    QVector<double> time(n), value1(n), value2(n);
    QDateTime start = QDateTime(QDate(2014, 6, 11));
    start.setTimeSpec(Qt::UTC);
    double startTime = start.toTime_t();
    double binSize = 3600*24; // bin data in 1 day intervals
    time[0] = startTime;
    value1[0] = 60;
    value2[0] = 20;
    qsrand(9);
    for (int i=1; i<n; ++i)
    {
      time[i] = startTime + 3600*i;
      value1[i] = value1[i-1] + (qrand()/(double)RAND_MAX-0.5)*10;
      value2[i] = value2[i-1] + (qrand()/(double)RAND_MAX-0.5)*3;
    }

    // create candlestick chart:
    QCPFinancial *candlesticks = new QCPFinancial(xAxis, yAxis);
    addPlottable(candlesticks);
    QCPFinancialDataMap data1 = QCPFinancial::timeSeriesToOhlc(time, value1, binSize, startTime);
    candlesticks->setName("Candlestick");
    candlesticks->setChartStyle(QCPFinancial::csCandlestick);
    candlesticks->setData(&data1, true);
    candlesticks->setWidth(binSize*0.9);
    candlesticks->setTwoColored(true);
    candlesticks->setBrushPositive(QColor(245, 245, 245));
    candlesticks->setBrushNegative(QColor(0, 0, 0));
    candlesticks->setPenPositive(QPen(QColor(0, 0, 0)));
    candlesticks->setPenNegative(QPen(QColor(0, 0, 0)));

    // create ohlc chart:
    QCPFinancial *ohlc = new QCPFinancial(xAxis, yAxis);
    addPlottable(ohlc);
    QCPFinancialDataMap data2 = QCPFinancial::timeSeriesToOhlc(time, value2, binSize/3.0, startTime); // divide binSize by 3 just to make the ohlc bars a bit denser
    ohlc->setName("OHLC");
    ohlc->setChartStyle(QCPFinancial::csOhlc);
    ohlc->setData(&data2, true);
    ohlc->setWidth(binSize*0.2);
    ohlc->setTwoColored(true);

    // create bottom axis rect for volume bar chart:
    QCPAxisRect *volumeAxisRect = new QCPAxisRect(this);
    plotLayout()->addElement(1, 0, volumeAxisRect);
    volumeAxisRect->setMaximumSize(QSize(QWIDGETSIZE_MAX, 100));
    volumeAxisRect->axis(QCPAxis::atBottom)->setLayer("axes");
    volumeAxisRect->axis(QCPAxis::atBottom)->grid()->setLayer("grid");
    // bring bottom and main axis rect closer together:
    plotLayout()->setRowSpacing(0);
    volumeAxisRect->setAutoMargins(QCP::msLeft|QCP::msRight|QCP::msBottom);
    volumeAxisRect->setMargins(QMargins(0, 0, 0, 0));
    // create two bar plottables, for positive (green) and negative (red) volume bars:
    QCPBars *volumePos = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft));
    QCPBars *volumeNeg = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft));
    for (int i=0; i<n/5; ++i)
    {
      int v = qrand()%20000+qrand()%20000+qrand()%20000-10000*3;
      (v < 0 ? volumeNeg : volumePos)->addData(startTime+3600*5.0*i, qAbs(v)); // add data to either volumeNeg or volumePos, depending on sign of v
    }
    setAutoAddPlottableToLegend(false);
    addPlottable(volumePos);
    addPlottable(volumeNeg);
    volumePos->setWidth(3600*4);
    volumePos->setPen(Qt::NoPen);
    volumePos->setBrush(QColor(100, 180, 110));
    volumeNeg->setWidth(3600*4);
    volumeNeg->setPen(Qt::NoPen);
    volumeNeg->setBrush(QColor(180, 90, 90));

    // interconnect x axis ranges of main and bottom axis rects:
    connect(xAxis, SIGNAL(rangeChanged(QCPRange)), volumeAxisRect->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));
    connect(volumeAxisRect->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), xAxis, SLOT(setRange(QCPRange)));
    // configure axes of both main and bottom axis rect:
    volumeAxisRect->axis(QCPAxis::atBottom)->setAutoTickStep(false);
    volumeAxisRect->axis(QCPAxis::atBottom)->setTickStep(3600*24*4); // 4 day tickstep
    volumeAxisRect->axis(QCPAxis::atBottom)->setTickLabelType(QCPAxis::ltDateTime);
    volumeAxisRect->axis(QCPAxis::atBottom)->setDateTimeSpec(Qt::UTC);
    volumeAxisRect->axis(QCPAxis::atBottom)->setDateTimeFormat("dd. MMM");
    volumeAxisRect->axis(QCPAxis::atBottom)->setTickLabelRotation(15);
    volumeAxisRect->axis(QCPAxis::atLeft)->setAutoTickCount(3);
    xAxis->setBasePen(Qt::NoPen);
    xAxis->setTickLabels(false);
    xAxis->setTicks(false); // only want vertical grid in main axis rect, so hide xAxis backbone, ticks, and labels
    xAxis->setAutoTickStep(false);
    xAxis->setTickStep(3600*24*4); // 4 day tickstep
    rescaleAxes();
    xAxis->scaleRange(1.025, xAxis->range().center());
    yAxis->scaleRange(1.1, yAxis->range().center());

    // make axis rects' left side line up:
    QCPMarginGroup *group = new QCPMarginGroup(this);
    axisRect()->setMarginGroup(QCP::msLeft|QCP::msRight, group);
    volumeAxisRect->setMarginGroup(QCP::msLeft|QCP::msRight, group);
}
void CPerformance::graphColors(QCustomPlot *customPlot)
{
    QVector<double> datax = QVector<double>() << 1 << 2 << 3 << 4  << 5;

    customPlot->clearPlottables();

//    QCPPlotTitle *title = new QCPPlotTitle(customPlot, "Colors Performance");

//    ///To print only one time the title
//    if(customPlot->plotLayout()->rowCount() < 2)
//    {
//        customPlot->plotLayout()->insertRow(0);
//    }

//    title->setTextColor(QColor(255, 255, 255));
//    customPlot->plotLayout()->addElement(0, 0, title);

    QCPBarsGroup *group1 = new QCPBarsGroup(customPlot);
    QCPBars *bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);

    customPlot->addPlottable(bars);
    bars->setData(datax, blueRights);
    bars->setBrush(QColor(25, 115, 215, 50));
    bars->setPen(QColor(25, 115, 215));
    bars->setWidth(0.15);

    group1->insert(1, bars);
//        bars->setBarsGroup(group1);
    bars->setName("Blue");
//        customPlot->plottable(0)->setName("squareRights");

    bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);

    customPlot->addPlottable(bars);
    bars->setData(datax,redRights);
    bars->setBrush(QColor(240, 0, 0, 50));
    bars->setPen(QColor(240, 0, 0));
    bars->setWidth(0.15);

    group1->insert(2,bars);
//        bars->setBarsGroup(group1);
    bars->setName("Red");

    bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);

    customPlot->addPlottable(bars);
    bars->setData(datax, greenRights);
    bars->setBrush(QColor(30, 210, 60, 50));
    bars->setPen(QColor(30, 210, 60));
    bars->setWidth(0.15);

    group1->insert(3,bars);
//        bars->setBarsGroup(group1);
    bars->setName("Green");

    bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);

    customPlot->addPlottable(bars);
    bars->setData(datax, yellowRights);
    bars->setBrush(QColor(240, 230, 0, 50));
    bars->setPen(QColor(240, 230, 0));
    bars->setWidth(0.15);

    group1->insert(4,bars);
//        bars->setBarsGroup(group1);
    bars->setName("Yellow");

    bars = new QCPBars(customPlot->xAxis, customPlot->yAxis);

    customPlot->addPlottable(bars);
    bars->setData(datax, blackRights);
    bars->setBrush(QColor(0, 0, 0, 50));
    bars->setPen(QColor(0, 0, 0));
    bars->setWidth(0.15);

    group1->insert(5,bars);
//        bars->setBarsGroup(group1);
    bars->setName("Black");

    customPlot->legend->setVisible(true);
    customPlot->replot();
}