Skip to content

Commit

Permalink
Harmonise code to create a title for "Y" values in a workspace.
Browse files Browse the repository at this point in the history
Refs #9252
  • Loading branch information
martyngigg committed Apr 8, 2014
1 parent 120a4bd commit e9c93db
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 47 deletions.
6 changes: 3 additions & 3 deletions Code/Mantid/MantidPlot/src/Mantid/MantidMatrix.cpp
Expand Up @@ -628,9 +628,9 @@ Graph3D * MantidMatrix::plotGraph3D(int style)
plot->addFunction(fun, xStart(), xEnd(), yStart(), yEnd(), zMin, zMax, numCols(), numRows() );

using MantidQt::API::PlotAxis;
plot->setXAxisLabel(PlotAxis(m_workspace, 0).title());
plot->setYAxisLabel(PlotAxis(m_workspace, 1).title());
plot->setZAxisLabel(tr(m_workspace->YUnitLabel().c_str()));
plot->setXAxisLabel(PlotAxis(*m_workspace, 0).title());
plot->setYAxisLabel(PlotAxis(*m_workspace, 1).title());
plot->setZAxisLabel(PlotAxis(*m_workspace).title());

a->initPlot3D(plot);
//plot->confirmClose(false);
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Expand Up @@ -2973,10 +2973,10 @@ void MantidUI::setUpBinGraph(MultiLayer* ml, const QString& Name, Mantid::API::M
QString xtitle;
if (workspace->axes() > 1) // Protection against calling this on 1D/single value workspaces
{
xtitle = MantidQt::API::PlotAxis(workspace, 1).title();
xtitle = MantidQt::API::PlotAxis(*workspace, 1).title();
}
g->setXAxisTitle(xtitle);
g->setYAxisTitle(tr(workspace->YUnitLabel().c_str()));
g->setYAxisTitle(MantidQt::API::PlotAxis(*workspace).title());
}

/**
Expand Down
10 changes: 6 additions & 4 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/PlotAxis.h
Expand Up @@ -38,8 +38,10 @@ namespace MantidQt
{
public:
/// Constructor with workspace & axis index
PlotAxis(const Mantid::API::MatrixWorkspace_const_sptr & workspace,
PlotAxis(const Mantid::API::MatrixWorkspace & workspace,
const size_t index);
/// Constructor with just a workspace
PlotAxis(const Mantid::API::MatrixWorkspace & workspace);

/// Create a new axis title
QString title() const;
Expand All @@ -48,11 +50,11 @@ namespace MantidQt
DISABLE_DEFAULT_CONSTRUCT(PlotAxis);

/// Creates a title suitable for an axis attached to the given index
void titleFromIndex(const Mantid::API::MatrixWorkspace_const_sptr & workspace,
void titleFromIndex(const Mantid::API::MatrixWorkspace & workspace,
const size_t index);

/// Creates a title suitable for the Y axis
void initYAxisTitle(const Mantid::API::MatrixWorkspace_const_sptr & workspace);
/// Creates a title suitable for the Y data values
void titleFromYData(const Mantid::API::MatrixWorkspace & workspace);

/// Title
QString m_title;
Expand Down
14 changes: 2 additions & 12 deletions Code/Mantid/MantidQt/API/src/MantidQwtMatrixWorkspaceData.cpp
Expand Up @@ -148,25 +148,15 @@ double MantidQwtMatrixWorkspaceData::getYMax() const
*/
QString MantidQwtMatrixWorkspaceData::getXAxisLabel() const
{
return MantidQt::API::PlotAxis(m_workspace, 0).title();
return MantidQt::API::PlotAxis(*m_workspace, 0).title();
}

/**
* @return A string containin the text to use as an Y axis label
*/
QString MantidQwtMatrixWorkspaceData::getYAxisLabel() const
{
QString yTitle = QString::fromStdString(m_workspace->YUnitLabel());
Mantid::API::Axis* ax = m_workspace->getAxis(0);
if (m_isDistribution && ax->unit())
{
const std::string unitID = ax->unit()->unitID();
if (unitID != "" || unitID != "Empty")
{
yTitle = yTitle % " / " % ax->unit()->label().ascii().c_str();
}
}
return yTitle;
return MantidQt::API::PlotAxis(*m_workspace).title();
}

void MantidQwtMatrixWorkspaceData::setLogScale(bool on)
Expand Down
40 changes: 26 additions & 14 deletions Code/Mantid/MantidQt/API/src/PlotAxis.cpp
@@ -1,16 +1,17 @@
#include "MantidQtAPI/PlotAxis.h"
#include <QStringBuilder>

namespace MantidQt
{
namespace API
{

/**
* @param workspace A pointer to a MatrixWorkspace object
* The title will be filled with the caption & units from the given Axis object
* of the workspace
* @param workspace The workspace containing the axis information
* @param index Index of the axis in the workspace to inspect
*/
PlotAxis::PlotAxis(const Mantid::API::MatrixWorkspace_const_sptr &workspace,
PlotAxis::PlotAxis(const Mantid::API::MatrixWorkspace &workspace,
const size_t index)
: m_title()
{
Expand All @@ -20,6 +21,16 @@ namespace MantidQt
boost::lexical_cast<std::string>(index) + "'");
}

/**
* The title will be filled with the caption & label for the Y data values
* within the workspace, ~ the Z axis
* @param workspace The workspace containing the Y title information
*/
PlotAxis::PlotAxis(const Mantid::API::MatrixWorkspace &workspace)
{
titleFromYData(workspace);
}

/**
* @return A new title for this axis
*/
Expand All @@ -29,14 +40,14 @@ namespace MantidQt
}

/**
* @param workspace A pointer to a MatrixWorkspace object
* @param workspace The workspace containing the axis information
* @param index Index of the axis in the workspace to inspect
*/
void PlotAxis::titleFromIndex(const Mantid::API::MatrixWorkspace_const_sptr &workspace,
void PlotAxis::titleFromIndex(const Mantid::API::MatrixWorkspace &workspace,
const size_t index)
{
// Deal with axis names
Mantid::API::Axis* ax = workspace->getAxis(index);
Mantid::API::Axis* ax = workspace.getAxis(index);
m_title = "";
if ( ax->isSpectra() ) m_title = "Spectrum Number";
else if (ax->unit() && ax->unit()->unitID() != "Empty" )
Expand All @@ -59,18 +70,19 @@ namespace MantidQt
}

/**
* @param workspace A pointer to a MatrixWorkspace object
* Constructs a title using the unicode methods of the UnitLabel
* @param workspace The workspace containing the Y title information
*/
void PlotAxis::initYAxisTitle(const Mantid::API::MatrixWorkspace_const_sptr &workspace)
void PlotAxis::titleFromYData(const Mantid::API::MatrixWorkspace &workspace)
{
m_title = QString::fromStdString(workspace->YUnitLabel());
Mantid::API::Axis* ax = workspace->getAxis(0);
if (workspace->isDistribution() && ax->unit())
m_title = QString::fromStdString(workspace.YUnit());
if(workspace.isDistribution() && workspace.axes() > 0 && workspace.getAxis(0)->unit())
{
const std::string unitID = ax->unit()->unitID();
if (unitID != "" || unitID != "Empty")
const auto xunit = workspace.getAxis(0)->unit();
const auto lbl = xunit->label();
if(!lbl.utf8().empty())
{
m_title = m_title % " / " % ax->unit()->label().ascii().c_str();
m_title += " (" + QString::fromStdWString(lbl.utf8()) + QString::fromWCharArray(L"\u207b\u00b9)");
}
}
}
Expand Down
43 changes: 31 additions & 12 deletions Code/Mantid/MantidQt/API/test/PlotAxisTest.h
Expand Up @@ -22,8 +22,8 @@ class PlotAxisTest : public CxxTest::TestSuite
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->replaceAxis(1, new Mantid::API::NumericAxis(1));

TS_ASSERT_EQUALS("X axis", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Y axis", PlotAxis(ws, 1).title());
TS_ASSERT_EQUALS("X axis", PlotAxis(*ws, 0).title());
TS_ASSERT_EQUALS("Y axis", PlotAxis(*ws, 1).title());
}

void test_Empty_Unit_And_Empty_Axis_Title_On_Indexed_Axis_Prints_Default()
Expand All @@ -34,8 +34,8 @@ class PlotAxisTest : public CxxTest::TestSuite
ws->replaceAxis(1, new Mantid::API::NumericAxis(1));
ws->getAxis(1)->setUnit("Empty");

TS_ASSERT_EQUALS("X axis", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Y axis", PlotAxis(ws, 1).title());
TS_ASSERT_EQUALS("X axis", PlotAxis(*ws, 0).title());
TS_ASSERT_EQUALS("Y axis", PlotAxis(*ws, 1).title());
}

void test_Empty_Unit_And_Non_Empty_Title_On_Indexed_Axis_Prints_Title()
Expand All @@ -51,8 +51,8 @@ class PlotAxisTest : public CxxTest::TestSuite
ax1->setUnit("Empty");
ax1->title() = "Custom title 2";

TS_ASSERT_EQUALS("Custom title 1", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Custom title 2", PlotAxis(ws, 1).title());
TS_ASSERT_EQUALS("Custom title 1", PlotAxis(*ws, 0).title());
TS_ASSERT_EQUALS("Custom title 2", PlotAxis(*ws, 1).title());
}

void test_Axis_With_Unit_Has_Label_In_Parentheses()
Expand All @@ -64,8 +64,8 @@ class PlotAxisTest : public CxxTest::TestSuite
ws->getAxis(1)->setUnit("TOF");

QString expected = QString::fromUtf8("Time-of-flight (\u03bcs)");
TS_ASSERT_EQUALS(expected, PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS(expected, PlotAxis(ws, 1).title());
TS_ASSERT_EQUALS(expected, PlotAxis(*ws, 0).title());
TS_ASSERT_EQUALS(expected, PlotAxis(*ws, 1).title());
}

void test_SpectraAxis_Gives_Standard_Text()
Expand All @@ -74,8 +74,27 @@ class PlotAxisTest : public CxxTest::TestSuite
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->replaceAxis(0, new Mantid::API::SpectraAxis(ws.get()));

TS_ASSERT_EQUALS("Spectrum Number", PlotAxis(ws, 0).title());
TS_ASSERT_EQUALS("Spectrum Number", PlotAxis(ws, 1).title());
TS_ASSERT_EQUALS("Spectrum Number", PlotAxis(*ws, 0).title());
TS_ASSERT_EQUALS("Spectrum Number", PlotAxis(*ws, 1).title());
}

void test_Passing_Just_NonDistribution_Workspace_Creates_UnitLess_Title_For_Y_Data()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->setYUnit("Counts");

TS_ASSERT_EQUALS("Counts", PlotAxis(*ws).title());
}

void test_Passing_Just_Distribution_Workspace_Creates_Title_For_Y_Data_With_Unit()
{
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);
ws->setYUnit("Counts");

QString expected = QString::fromUtf8("Counts (\u03bcs\u207b\u00b9)");
TS_ASSERT_EQUALS("Counts", PlotAxis(*ws).title());
}

//---------------------- Failure cases -------------------------------
Expand All @@ -85,8 +104,8 @@ class PlotAxisTest : public CxxTest::TestSuite
using MantidQt::API::PlotAxis;
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1,1);

TS_ASSERT_THROWS(PlotAxis(ws, 2), std::invalid_argument);
TS_ASSERT_THROWS(PlotAxis(ws, -1), std::invalid_argument);
TS_ASSERT_THROWS(PlotAxis(*ws, 2), std::invalid_argument);
TS_ASSERT_THROWS(PlotAxis(*ws, -1), std::invalid_argument);
}
};

Expand Down

0 comments on commit e9c93db

Please sign in to comment.