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();


}
Esempio n. 2
0
/**
 * @brief TimerWindow::slotUpdatePlotHorizontal 更新横向绘图槽
 * @param data 数据
 * @param labels 文本
 */
void TimerWindow::slotUpdatePlotHorizontal(QVector<double> &data, QVector<QString> &labels)
{
    qDebug()<<"slotUpdatePlotHorizontal";
    delete(customPlot);
    customPlot = new QCustomPlot();
    customPlot->setFixedSize(500, 250);
    vLayoutMain->addWidget(customPlot);

    // create empty bar chart objects:
    QCPBars *regen = new QCPBars(customPlot->yAxis, customPlot->xAxis);//横向
    customPlot->addPlottable(regen);

    // set names and colors:
    QPen pen;
    regen->setName("Regenerative");
    pen.setColor(QColor("#D26079"));
    regen->setPen(pen);
    regen->setBrush(QColor("#D26079"));

    // prepare x axis with country labels:
    QVector<double> ticks;
    double maxValue = 0;
    //查找最大值
    for(int i = 0; i < labels.size(); i++){
        ticks << i + 1;
        maxValue = maxValue > data.at(i) ? maxValue : data.at(i);
    }
    customPlot->yAxis->setAutoTicks(false);
    customPlot->yAxis->setAutoTickLabels(false);
    customPlot->yAxis->setTickVector(ticks);
    customPlot->yAxis->setTickVectorLabels(labels);
    customPlot->yAxis->setTickLabelRotation(0);
    customPlot->yAxis->setSubTickCount(0);
    customPlot->yAxis->setTickLength(0, 4);
    customPlot->yAxis->grid()->setVisible(true);
    customPlot->yAxis->setRange(0, ticks.size() + 1);
    customPlot->yAxis->setLabel("姓名");

    // prepare y axis:
//    customPlot->xAxis->
    customPlot->xAxis->setRange(0, maxValue + maxValue / 10);
    customPlot->xAxis->setPadding(5); // a bit more space to the left border
    customPlot->xAxis->setLabel("本周时长 (小时)");
    customPlot->xAxis->grid()->setSubGridVisible(true);
    QPen gridPen;
    gridPen.setStyle(Qt::SolidLine);
    gridPen.setColor(QColor(0, 0, 0, 25));
    customPlot->xAxis->grid()->setPen(gridPen);
    gridPen.setStyle(Qt::DotLine);
    customPlot->xAxis->grid()->setSubGridPen(gridPen);

    /*
    // setup legend:
    customPlot->legend->setVisible(false);
    customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
    customPlot->legend->setBrush(QColor(255, 255, 255, 200));
    QPen legendPen;
    legendPen.setColor(QColor(130, 130, 130, 200));
    customPlot->legend->setBorderPen(legendPen);
    QFont legendFont = font();
    legendFont.setPointSize(10);
    customPlot->legend->setFont(legendFont);
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
*/

    regen->setData(ticks, data);
    customPlot->replot();
    customPlot->setHidden(false);
    qDebug()<<"slotUpdatePlotHorizontal end";
}
Esempio n. 3
0
/**
 * @brief TimerWindow::slotUpdateVerticalPlot 更新纵向绘图槽
 * @param data 数据
 * @param labels 文本
 */
void TimerWindow::slotUpdateVerticalPlot(QVector<double> &data, QVector<QString> &labels)
{
    qDebug()<<"slotUpdateVerticalPlot";
    delete(customPlot);
    customPlot = new QCustomPlot();
    customPlot->setFixedSize(500, 250);
    vLayoutMain->addWidget(customPlot);

    //只取labels的month/day
    QDate tmpDate;
    for(int index = 0; index < labels.size(); ++index){
//        qDebug()<<"before"<<labels.at(index);
        tmpDate = QDate::fromString(labels.at(index),"yyyy-MM-dd");
        labels.replace(index, QString::number(tmpDate.month())+"/"+QString::number(tmpDate.day()));
//        qDebug()<<"after"<<labels.at(index);
    }

    // create empty bar chart objects:
    QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis);
    customPlot->addPlottable(regen);

    // set names and colors:
    QPen pen;
    regen->setName("Regenerative");
    pen.setColor(QColor("#D26079"));
    regen->setPen(pen);
    regen->setBrush(QColor("#D26079"));

    QVector<double> ticks;
    double maxValue = 0;
    //查找最大值
    for(int i = 0; i < labels.size(); i++){
        ticks << i + 1;
        maxValue = maxValue > data.at(i) ? maxValue : data.at(i);
    }
    customPlot->xAxis->setAutoTicks(false);
    customPlot->xAxis->setAutoTickLabels(false);
    customPlot->xAxis->setTickVector(ticks);
    customPlot->xAxis->setTickVectorLabels(labels);
    customPlot->xAxis->setTickLabelRotation(0);
    customPlot->xAxis->setSubTickCount(0);
    customPlot->xAxis->setTickLength(0, 4);
    customPlot->xAxis->grid()->setVisible(true);
    customPlot->xAxis->setRange(0, ticks.size() + 1);
    customPlot->xAxis->setLabel("日期");

    // prepare y axis:
    customPlot->yAxis->setRange(0, maxValue + maxValue / 10);
    customPlot->yAxis->setPadding(5); // a bit more space to the left border
    customPlot->yAxis->setLabel("时长 (小时)");
    customPlot->yAxis->grid()->setSubGridVisible(true);
    QPen gridPen;
    gridPen.setStyle(Qt::SolidLine);
    gridPen.setColor(QColor(0, 0, 0, 25));
    customPlot->yAxis->grid()->setPen(gridPen);
    gridPen.setStyle(Qt::DotLine);
    customPlot->yAxis->grid()->setSubGridPen(gridPen);

/*
    // setup legend:
    customPlot->legend->setVisible(false);
    customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
    customPlot->legend->setBrush(QColor(255, 255, 255, 200));
    QPen legendPen;
    legendPen.setColor(QColor(130, 130, 130, 200));
    customPlot->legend->setBorderPen(legendPen);
    QFont legendFont = font();
    legendFont.setPointSize(10);
    customPlot->legend->setFont(legendFont);
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
*/

    regen->setData(ticks, data);
    customPlot->replot();

//    for(int i=0; i < labels.size(); i++){
//        qDebug()<<data.at(i)<<labels.at(i);
//    }
    qDebug()<<"slotUpdateVerticalPlot end";
}
Esempio n. 4
0
void chartcreate::repaintplot()
{
    QCustomPlot * customPlot = ui->chart_preview;
    // create empty bar chart objects:

    QCPBars *regen = new QCPBars(customPlot->xAxis, customPlot->yAxis);
    QCPBars *nuclear = new QCPBars(customPlot->xAxis, customPlot->yAxis);
    QCPBars *fossil = new QCPBars(customPlot->xAxis, customPlot->yAxis);
    customPlot->addPlottable(regen);
    customPlot->addPlottable(nuclear);
    customPlot->addPlottable(fossil);
    // set names and colors:
    QPen pen;
    pen.setWidthF(1.2);
    fossil->setName("Danone");
    pen.setColor(QColor(255, 131, 0));
    fossil->setPen(pen);
    fossil->setBrush(QColor(255, 131, 0, 50));
    nuclear->setName("AuBonLait");
    pen.setColor(QColor(1, 92, 191));
    nuclear->setPen(pen);
    nuclear->setBrush(QColor(1, 92, 191, 50));
    regen->setName("Yoplait");
    pen.setColor(QColor(150, 222, 0));
    regen->setPen(pen);
    regen->setBrush(QColor(150, 222, 0, 70));
    // stack bars ontop of each other:
    nuclear->moveAbove(fossil);
    regen->moveAbove(nuclear);

    // prepare x axis with country labels:
    QVector<double> ticks;
    QVector<QString> labels;
    ticks << 1 << 2 ;
    labels << "Homme" << "Femme" ;
    customPlot->xAxis->setAutoTicks(false);
    customPlot->xAxis->setAutoTickLabels(false);
    customPlot->xAxis->setTickVector(ticks);
    customPlot->xAxis->setTickVectorLabels(labels);
    customPlot->xAxis->setTickLabelRotation(60);
    customPlot->xAxis->setSubTickCount(0);
    customPlot->xAxis->setTickLength(0, 4);
    customPlot->xAxis->grid()->setVisible(true);
    customPlot->xAxis->setRange(0, 8);

    // prepare y axis:
    customPlot->yAxis->setRange(0, 12.1);
    customPlot->yAxis->setPadding(5); // a bit more space to the left border
    customPlot->yAxis->setLabel("Marque de Yaourt Connu");
    customPlot->yAxis->grid()->setSubGridVisible(true);
    QPen gridPen;
    gridPen.setStyle(Qt::SolidLine);
    gridPen.setColor(QColor(0, 0, 0, 25));
    customPlot->yAxis->grid()->setPen(gridPen);
    gridPen.setStyle(Qt::DotLine);
    customPlot->yAxis->grid()->setSubGridPen(gridPen);

    // Add data:
    QVector<double> fossilData, nuclearData, regenData;
    fossilData  << 0.86*10.5 << 0.83*5.5;
    nuclearData << 0.08*10.5 << 0.12*5.5;
    regenData   << 0.06*10.5 << 0.05*5.5;
    fossil->setData(ticks, fossilData);
    nuclear->setData(ticks, nuclearData);
    regen->setData(ticks, regenData);

    // setup legend:
    customPlot->legend->setVisible(true);
    customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
    customPlot->legend->setBrush(QColor(255, 255, 255, 200));
    QPen legendPen;
    legendPen.setColor(QColor(130, 130, 130, 200));
    customPlot->legend->setBorderPen(legendPen);
    QFont legendFont = font();
    legendFont.setPointSize(10);
    customPlot->legend->setFont(legendFont);
    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}
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);
}
Esempio n. 6
0
void MainWindow::makePlot()
{
    /*
    QRegExp rx("(\\ |\\n)");
    QStringList query = szoveg.split(rx);
    for (int i=0; i<query.size(); i++)
        QMessageBox::information(0, "info", query[i]);

    int szohossz[3];
    for (int i=0; i<3; i++)
        szohossz[i] = query[i].size();
    for (int i=0; i<3; i++)
        QMessageBox::information(0, "info", QString::number(szohossz[i]));*/

    // create empty bar chart objects:
    QCPBars *regen = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    QCPBars *nuclear = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    QCPBars *fossil = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    ui->customPlot->addPlottable(regen);
    ui->customPlot->addPlottable(nuclear);
    ui->customPlot->addPlottable(fossil);
    // set names and colors:
    QPen pen;
    pen.setWidthF(1.2);
    fossil->setName("Fossil fuels");
    pen.setColor(QColor(255, 131, 0));
    fossil->setPen(pen);
    fossil->setBrush(QColor(255, 131, 0, 50));
    nuclear->setName("Nuclear");
    pen.setColor(QColor(1, 92, 191));
    nuclear->setPen(pen);
    nuclear->setBrush(QColor(1, 92, 191, 50));
    regen->setName("Regenerative");
    pen.setColor(QColor(150, 222, 0));
    regen->setPen(pen);
    regen->setBrush(QColor(150, 222, 0, 70));
    // stack bars ontop of each other:
    nuclear->moveAbove(fossil);
    regen->moveAbove(nuclear);

    // prepare x axis with country labels:
    QVector<double> ticks;
    QVector<QString> labels;
    ticks << 1 << 2 << 3 << 4 << 5 << 6 << 7;
    labels << "Japan" << "Germany" << "France" << "UK" << "Italy" << "Canada";
    ui->customPlot->xAxis->setAutoTicks(false);
    ui->customPlot->xAxis->setAutoTickLabels(false);
    ui->customPlot->xAxis->setTickVector(ticks);
    ui->customPlot->xAxis->setTickVectorLabels(labels);
    ui->customPlot->xAxis->setTickLabelRotation(60);
    ui->customPlot->xAxis->setSubTickCount(0);
    ui->customPlot->xAxis->setTickLength(0, 4);
    ui->customPlot->xAxis->grid()->setVisible(true);
    ui->customPlot->xAxis->setRange(0, 8);

    // prepare y axis:
    ui->customPlot->yAxis->setRange(0, 12.1);
    ui->customPlot->yAxis->setPadding(5); // a bit more space to the left border
    ui->customPlot->yAxis->setLabel("Power Consumption in\nKilowatts per Capita (2007)");
    ui->customPlot->yAxis->grid()->setSubGridVisible(false);
    QPen gridPen;
    gridPen.setStyle(Qt::SolidLine);
    gridPen.setColor(QColor(0, 0, 0, 25));
    ui->customPlot->yAxis->grid()->setPen(gridPen);
    gridPen.setStyle(Qt::DotLine);
    ui->customPlot->yAxis->grid()->setSubGridPen(gridPen);

    // Add data:
    QVector<double> fossilData, nuclearData, regenData;
    fossilData  << 0.86*10.5 << 0.83*5.5 << 0.84*5.5 << 0.52*5.8 << 0.89*5.2 << 0.90*4.2 << 0.67*11.2;
    nuclearData << 0.08*10.5 << 0.12*5.5 << 0.12*5.5 << 0.40*5.8 << 0.09*5.2 << 0.00*4.2 << 0.07*11.2;
    regenData   << 0.06*10.5 << 0.05*5.5 << 0.04*5.5 << 0.06*5.8 << 0.02*5.2 << 0.07*4.2 << 0.25*11.2;
    fossil->setData(ticks, fossilData);
    nuclear->setData(ticks, nuclearData);
    regen->setData(ticks, regenData);

    // setup legend:
    ui->customPlot->legend->setVisible(true);
    ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignHCenter);
    ui->customPlot->legend->setBrush(QColor(255, 255, 255, 200));
    QPen legendPen;
    legendPen.setColor(QColor(130, 130, 130, 200));
    ui->customPlot->legend->setBorderPen(legendPen);
    QFont legendFont = font();
    legendFont.setPointSize(10);
    ui->customPlot->legend->setFont(legendFont);
    ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}
/*
 * makePlot()
 *
 * This function deals with creating a fully modifable bar graph based on the totals
 * of the surveys. It fully sets up the graph with plottting, the legend, and anything related
 * with the graphing.
*/
void MainWindow::makePlot()
{
     ui -> customPlot->clearPlottables();

    // create empty bar chart objects:
    QCPBars *theoretical = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    QCPBars *actual = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    QCPBars *expected = new QCPBars(ui->customPlot->xAxis, ui->customPlot->yAxis);
    ui->customPlot->addPlottable(actual);
    ui->customPlot->addPlottable(expected);
    ui->customPlot->addPlottable(theoretical);

    // set names and colors:
    QPen pen;
    pen.setWidthF(1.2);

    theoretical ->setName("Personal Goal");
    pen.setColor(QColor(255, 131, 0));
    theoretical ->setPen(pen);
    theoretical ->setBrush(QColor(255, 131, 0, 50));

    actual->setName("Actual");
    pen.setColor(QColor(1, 92, 191));
    actual->setPen(pen);
    actual->setBrush(QColor(1, 92, 191, 50));

    expected->setName("Trainer Goal");
    pen.setColor(QColor(150, 222, 0));
    expected->setPen(pen);
    expected->setBrush(QColor(150, 222, 0, 50));

    // prepare x axis with labels:
    QVector<double> actual_ticks , expected_ticks, theoretical_ticks;
    QVector<QString> labels;
    actual_ticks<< 1 << 4 << 7 << 10 << 13;
    expected_ticks << 2 << 5 << 8 << 11 << 14;
    theoretical_ticks << 3 << 6 << 9 << 12 << 15;

    labels << "Prospect" << "PIE" << "FLI" << "PI" << "NCC";
    ui->customPlot->xAxis->setAutoTicks(false);
    ui->customPlot->xAxis->setAutoTickLabels(false);
    ui->customPlot->xAxis->setTickVector(actual_ticks);
    ui->customPlot->xAxis->setTickVectorLabels(labels);
    ui->customPlot->xAxis->setTickLabelRotation(50);
    ui->customPlot->xAxis->setSubTickCount(0);
    ui->customPlot->xAxis->setTickLength(0,5);
    ui->customPlot->xAxis->grid()->setVisible(true);
    ui->customPlot->xAxis->setRange(0, 16);

    // prepare y axis:
    if(actual_total[0] == 0)
        ui->customPlot->yAxis->setRange(0, 9);
    else
        ui->customPlot->yAxis->setRange(0, actual_total[0]+2);

    ui->customPlot->yAxis->setPadding(5);
    ui->customPlot->yAxis->setLabel("Total Surveys Completed");
    ui->customPlot->yAxis->grid()->setSubGridVisible(true);
    QPen gridPen;
    gridPen.setStyle(Qt::SolidLine);
    gridPen.setColor(QColor(0, 0, 0, 25));
    ui->customPlot->yAxis->grid()->setPen(gridPen);
    gridPen.setStyle(Qt::DotLine);
    ui->customPlot->yAxis->grid()->setSubGridPen(gridPen);

    // Add data points:
    QVector<double> theoreticalData, actualData, expectedData;

    theoreticalData << theoretical_total[0] << theoretical_total[1] << theoretical_total[2]<< theoretical_total[3] << theoretical_total[4];
    theoretical->setData(theoretical_ticks, theoreticalData);

    actualData << actual_total[0] << actual_total[1] << actual_total[2] << actual_total[3] << actual_total[4];
    actual->setData(actual_ticks, actualData);

    expectedData << expected_total[0] << expected_total[1] << expected_total[2] << expected_total[3] << expected_total[4];
    expected->setData(expected_ticks, expectedData);

    // setup legend:
    ui->customPlot->legend->setVisible(true);
    ui->customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignRight);
    ui->customPlot->legend->setBrush(QColor(255, 255, 255, 200));
    QPen legendPen;
    legendPen.setColor(QColor(130, 130, 130, 200));
    ui->customPlot->legend->setBorderPen(legendPen);
    QFont legendFont = font();
    legendFont.setPointSize(15);
    ui->customPlot->legend->setFont(legendFont);

}
Esempio n. 8
0
int BarPlot::plotGrant()
{
	string dataType = Node->getFirst();
	// Get the types
	// Note the type is not always going to be the within the children of the node (depending on what node is passed)
	// It will be possible to pass in a different node (say a publication type node) when implemented
	vector<node*> types;
	
	vector<node*>* temptypes = Node->getChildren();
	for (int i = 0; i < temptypes->size(); i++){
		if (temptypes->at(i)->getSecond() > 0)
			types.push_back(temptypes->at(i));
	}

	// Grab Data and prepare x axis with (professor Name) labels:
	QVector<QString> labels;
	//display the types only
	for (int i = 0; i < Node->getChildren()->size(); i++)
		labels.push_back(QString::fromStdString(Node->getChildren()->at(i)->getFirst()));


	// stacked bar chart can get cluttered, ensure no more than 30 different types
	// determine which types to push into an "Others" group
	vector<int> othersNdx;
	if (types.size() > COUNT_MAX){
		vector<double> typeSumCounts;
		for (int i = 0; i < types.size(); i++){
			typeSumCounts.push_back(types.at(i)->getFourth());
		}
		while (types.size() - othersNdx.size() > COUNT_MAX){
			othersNdx.push_back(min_element(typeSumCounts.begin(), typeSumCounts.end()) - typeSumCounts.begin());
			typeSumCounts.at(min_element(typeSumCounts.begin(), typeSumCounts.end()) - typeSumCounts.begin()) = std::numeric_limits<double>::infinity();
		}
	}

	QVector<double> ticks;
	for (int i = 1; i <= labels.size(); i++)
		ticks.push_back(i);

	QVector<double> count(labels.size());
	double *data = count.data();
	// create a new plottable area for each type, group everything within the "Others" group together
	for (int i = 0; i < types.size(); i++){
		data[i] = types.at(i)->getFourth();
	}

	QCPBars *bar = new QCPBars(this->xAxis, this->yAxis);
	bar->setName(QString::fromStdString(dataType));
	bar->setData(ticks, count);
	this->addPlottable(bar);

	// set the colors
	QPen pen;
	pen.setWidthF(1.2);
	int C_HUE = 0;
	QColor color_brush, color_pen;
	color_brush.setHsv(C_HUE, BAR_SAT, BAR_VAL);
	color_brush.setAlpha(BAR_ALPHA);
	color_pen.setHsv(C_HUE, BAR_SAT + 30, BAR_VAL + 10);
	color_pen.setAlpha(255);
	pen.setColor(color_pen);
	bar->setPen(pen);
	bar->setBrush(color_brush);

	// prepare x axis:
	this->xAxis->setAutoTicks(false);
	this->xAxis->setAutoTickLabels(false);
	this->xAxis->setTickVector(ticks);
	this->xAxis->setTickVectorLabels(labels);
	this->xAxis->setTickLabelRotation(60);
	this->xAxis->setSubTickCount(0);
	this->xAxis->setTickLength(0, 3);
	this->xAxis->grid()->setVisible(true);

	// prepare y axis:
	this->yAxis->setTickStep(5);
	this->yAxis->setPadding(5); // a bit more space to the left border
	this->yAxis->setLabel("Amount ($)");
	this->yAxis->grid()->setSubGridVisible(true);
	QPen gridPen;
	gridPen.setStyle(Qt::SolidLine);
	gridPen.setColor(QColor(0, 0, 0, 25));
	this->yAxis->grid()->setPen(gridPen);
	gridPen.setStyle(Qt::DotLine);
	this->yAxis->grid()->setSubGridPen(gridPen);
	this->yAxis->scaleRange(1.3, this->yAxis->range().center());

	this->rescaleAxes(true);
	this->xAxis->setRange(0.5, 10.5);

	// setup legend:
	QCPLayoutGrid *subLayout = new QCPLayoutGrid;
	QCPLayoutElement *dummyElement = new QCPLayoutElement;

	this->plotLayout()->addElement(0, 1, subLayout); // add sub-layout in the cell to the right of the main axis rect
	subLayout->addElement(0, 0, this->legend); // add legend
	subLayout->addElement(1, 0, dummyElement); // add dummy element below legend
	subLayout->setRowStretchFactor(0, 0.01); // make legend cell (in row 0) take up as little vertical space as possible
	this->plotLayout()->setColumnStretchFactor(1, 0.01); // make the legend cell and dummy element column as small as possible

	this->legend->setVisible(true);
	this->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
	this->legend->setBrush(QColor(255, 255, 255, 200));
	QPen legendPen;
	legendPen.setColor(QColor(130, 130, 130, 200));
	this->legend->setBorderPen(legendPen);
	QFont legendFont = font();
	legendFont.setPointSize(8);
	this->legend->setFont(legendFont);
	this->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items

	this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iSelectLegend);
		return 0;
}
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();
}