No Description

mainwindow.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "qcustomplot.h"
  4. #include <cmath>
  5. using namespace std;
  6. /// \fn MainWindow::MainWindow(QWidget *parent)
  7. /// \brief Constructor
  8. MainWindow::MainWindow(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::MainWindow)
  11. {
  12. ui->setupUi(this);
  13. }
  14. /// \fn void MainWindow::histo(string names[], double values[], int size, string xAxisLabel, string yAxisLabel)
  15. /// \~English
  16. /// \brief This function receives information about the ticks and
  17. /// values for a bar chart, then displays it using the customPlot widget
  18. /// from qcustomplot.
  19. /// \param names an array of strings for the names of the x-axis ticks
  20. /// \param values an array of doubles for the values of y-axis
  21. /// \param size the size of the names (and values) arrays
  22. /// \param xAxisLabel a string for the x-axis title
  23. /// \param yAxisLabel a string for the y-axis title
  24. /// \~Spanish
  25. /// \brief Esta funcion recibe informacion sobre las marcas en el eje y los valores
  26. /// para una grafica de barra, entonces la desplega usando el widget de qcustomplot.
  27. /// \param names un arreglo de cadenas de caracteres para los nombres de las marcas del el axis-x
  28. /// \param values un arreglo de dobles (doubles) para los valores del axis-y.
  29. /// \param size el tamano de los arreglos con los nombres (y valores).
  30. /// \param xAxisLabel una cadena de caracteres para el titulo del axis-x.
  31. /// \param yAxisLabel una cadena de caracteres para el titulo del axis-y.
  32. void MainWindow::histo(
  33. std::string names[], double values[], int size, string xAxisLabel, string yAxisLabel)
  34. {
  35. QCustomPlot *customPlot = ui->customPlot;
  36. QString demoName = "Bar Chart Demo";
  37. QCPBars *barChart = new QCPBars(customPlot->xAxis, customPlot->yAxis);
  38. customPlot->addPlottable(barChart);
  39. QPen pen;
  40. pen.setWidthF(1.2);
  41. //barChart->setName("Fossil fuels");
  42. pen.setColor(QColor(255, 131, 0));
  43. barChart->setPen(pen);
  44. barChart->setBrush(QColor(255, 131, 0, 50));
  45. // prepare x axis with labels:
  46. QVector<double> ticks;
  47. QVector<QString> labels;
  48. for (int i = 1; i <= size; i++) {
  49. ticks.push_back(i);
  50. labels.push_back(QString(names[i-1].c_str()));
  51. }
  52. // prepare y axis:
  53. customPlot->xAxis->setAutoTicks(false);
  54. customPlot->xAxis->setAutoTickLabels(false);
  55. customPlot->xAxis->setTickVector(ticks);
  56. customPlot->xAxis->setTickVectorLabels(labels);
  57. customPlot->xAxis->setTickLabelRotation(60);
  58. customPlot->xAxis->setSubTickCount(0);
  59. customPlot->xAxis->setTickLength(0, 4);
  60. customPlot->xAxis->grid()->setVisible(true);
  61. customPlot->xAxis->setRange(0, size+1);
  62. customPlot->xAxis->setLabel(QString(xAxisLabel.c_str()));
  63. // prepare y axis:
  64. customPlot->yAxis->setRange(0, *std::max_element(values,values+size));
  65. customPlot->yAxis->setPadding(5); // a bit more space to the left border
  66. customPlot->yAxis->setLabel(QString(yAxisLabel.c_str()));
  67. customPlot->yAxis->grid()->setSubGridVisible(true);
  68. QPen gridPen;
  69. gridPen.setStyle(Qt::SolidLine);
  70. gridPen.setColor(QColor(0, 0, 0, 25));
  71. customPlot->yAxis->grid()->setPen(gridPen);
  72. gridPen.setStyle(Qt::DotLine);
  73. customPlot->yAxis->grid()->setSubGridPen(gridPen);
  74. // Add data:
  75. QVector<double> barChartData;
  76. for (int i = 0; i < size; i++) {
  77. ticks.push_back(i);
  78. barChartData.push_back(values[i]);
  79. }
  80. barChart->setData(ticks, barChartData);
  81. this->resize(customPlot->width(),customPlot->height());
  82. }
  83. /// \fn MainWindow::~MainWindow()
  84. /// \brief Destructor
  85. ///
  86. MainWindow::~MainWindow()
  87. {
  88. delete ui;
  89. }