Skip to content

Commit

Permalink
Backport stem-and-leaf plot type from today's QtiPlot. Re #2654.
Browse files Browse the repository at this point in the history
Only accessible from Python at the moment because otherwise the
output is stored in a 'Note', and if we want to do that we should
first disentangle them from the scripting environment and just
make them a QTextEdit.
(QtiPlot's scripting takes place in Notes, but ours doesn't).
  • Loading branch information
RussellTaylor committed Dec 16, 2011
1 parent 96f1123 commit b49f40e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
92 changes: 92 additions & 0 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@

#include <zlib.h>

#include <gsl/gsl_sort.h>

//Mantid
#include "ScriptingWindow.h"

Expand Down Expand Up @@ -1898,6 +1900,96 @@ void ApplicationWindow::plotVectXYAM()
QMessageBox::warning(this, tr("MantidPlot - Error"), tr("Please select four columns for this operation!"));//Mantid
}

QString ApplicationWindow::stemPlot(Table *t, const QString& colName, int power, int startRow, int endRow)
{
if (!t)
return QString();

int col = t->colIndex(colName);
if (col < 0){
QMessageBox::critical(this, tr("QtiPlot - Error"),
tr("Data set: %1 doesn't exist!").arg(colName));
return QString();
}

startRow--;
endRow--;
if (startRow < 0 || startRow >= t->numRows())
startRow = 0;
if (endRow < 0 || endRow >= t->numRows())
endRow = t->numRows() - 1;

QString result = tr("Stem and leaf plot of dataset") + ": " + colName + " ";
result += tr("from row") + ": " + QString::number(startRow + 1) + " ";
result += tr("to row") + ": " + QString::number(endRow + 1) + "\n";

int rows = 0;
for (int j = startRow; j <= endRow; j++){
if (!t->text(j, col).isEmpty())
rows++;
}

if (rows >= 1){
double *data = (double *)malloc(rows * sizeof (double));
if (!data){
result += tr("Not enough memory for this dataset!") + "\n";
return result;
}

result += "\n" + tr("Stem") + " | " + tr("Leaf");
result += "\n---------------------\n";

int row = 0;
for (int j = startRow; j <= endRow; j++){
if (!t->text(j, col).isEmpty()){
data[row] = t->cell(j, col);
row++;
}
}
gsl_sort (data, 1, rows);

if (power > 1e3){
power = static_cast<int>(std::ceil(log10(data[rows - 1] - data[0]) - log10(rows - 1)));
bool ok;
int input = QInputDialog::getInteger(this, tr("Please confirm the stem unit!"),
tr("Data set") + ": " + colName + ", " + tr("stem unit") + " = 10<sup>n</sup>, n = ",
power, -1000, 1000, 1, &ok);
if (ok)
power = input;
}

double stem_unit = pow(10.0, power);
double leaf_unit = stem_unit/10.0;

int prev_stem = int(data[0]/stem_unit);
result += " " + QString::number(prev_stem) + " | ";

for (int j = 0; j <rows; j++){
double val = data[j];
int stem = int(val/stem_unit);
int leaf = int(qRound((val - stem*stem_unit)/leaf_unit));
for (int k = prev_stem + 1; k < stem + 1; k++)
result += "\n " + QString::number(k) + " | ";
result += QString::number(leaf);
prev_stem = stem;
}

result += "\n---------------------\n";
result += tr("Stem unit") + ": " + locale().toString(stem_unit) + "\n";
result += tr("Leaf unit") + ": " + locale().toString(leaf_unit) + "\n";

QString legend = tr("Key") + ": " + QString::number(prev_stem) + " | ";
int leaf = int(qRound((data[rows - 1] - prev_stem*stem_unit)/leaf_unit));
legend += QString::number(leaf);
legend += " " + tr("means") + ": " + locale().toString(prev_stem*stem_unit + leaf*leaf_unit) + "\n";

result += legend + "---------------------\n";
free(data);
} else
result += "\t" + tr("Input error: empty data set!") + "\n";
return result;
}

void ApplicationWindow::renameListViewItem(const QString& oldName,const QString& newName)
{
Q3ListViewItem *it=lv->findItem (oldName,0, Q3ListView::ExactMatch | Qt::CaseSensitive );
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ public slots:
void plotVectXYAM();
void plotBoxDiagram();

/// Create a stem plot from a table and return a string representation of it
QString stemPlot(Table *t = 0, const QString& colName = QString(), int power = 0, int startRow = 0, int endRow = -1);

//! Check whether a table is valid for a 3D plot and display an appropriate error if not
bool validFor3DPlot(Table *table);
//! Check whether a table is valid for a 2D plot and display an appropriate error if not
Expand Down
2 changes: 2 additions & 0 deletions Code/Mantid/MantidPlot/src/qti.sip
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,8 @@ public:
Matrix* tableToMatrix(Table* t);
Table* matrixToTable(Matrix* m, MatrixToTableConversion = Direct);

QString stemPlot(Table *t, const QString& colName, int power = 1001, int startRow = 0, int endRow = -1);

//---- Mantid
MantidUI* mantidUI;
void addUserMenu(const QString &);
Expand Down

0 comments on commit b49f40e

Please sign in to comment.