Mantid
Loading...
Searching...
No Matches
PlotAxis.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
10#include "MantidAPI/Axis.h"
12#include "MantidKernel/Unit.h"
13
14#include <boost/algorithm/string/join.hpp>
15#include <boost/algorithm/string/regex.hpp>
16#include <boost/lexical_cast.hpp>
17
18namespace MantidQt {
19namespace API {
20
21namespace {
22QString replacePerWithNegativeIndice(const std::string &label, const bool &plotAsDistribution,
23 const Mantid::Kernel::UnitLabel &xLabel = "") {
24 std::vector<std::string> splitVec;
25 QString negativeOnePower = toQStringInternal(L"\u207b\u00b9");
26 QString newLabel;
27
28 boost::split_regex(splitVec, label, boost::regex(" per "));
29 if (splitVec.size() > 1) {
30 newLabel = QString::fromStdString(splitVec[0]);
31 splitVec.erase(splitVec.begin());
32 std::string unitString = boost::algorithm::join(splitVec, " ");
33 newLabel += " (" + QString::fromStdString(unitString);
34 if (plotAsDistribution && xLabel != "") {
35 newLabel += " " + toQStringInternal(xLabel.utf8());
36 }
37 newLabel += ")" + negativeOnePower;
38 } else {
39 newLabel = QString::fromStdString(label);
40 if (plotAsDistribution && xLabel != "") {
41 newLabel += " (" + toQStringInternal(xLabel.utf8()) + ")" + negativeOnePower;
42 }
43 }
44 return newLabel;
45}
46} // namespace
54 if (index < workspace.getNumDims())
56 else
57 throw std::invalid_argument("PlotAxis() - Unknown axis index: '" + boost::lexical_cast<std::string>(index) + "'");
58}
59
66
74PlotAxis::PlotAxis(const bool plottingDistribution, const Mantid::API::MatrixWorkspace &workspace) {
75 titleFromYData(workspace, plottingDistribution);
76}
77
81QString PlotAxis::title() const { return m_title; }
82
88 auto dim = workspace.getDimension(index);
90 if (m_title.isEmpty()) {
91 m_title = (index == 0) ? "X axis" : "Y axis";
92 }
93}
94
99 m_title = QString::fromStdString(dim.getName());
100 if (!m_title.isEmpty()) {
101 auto unitLbl = dim.getUnits();
102 if (!unitLbl.utf8().empty()) {
103 m_title += " (" + toQStringInternal(unitLbl.utf8()) + ")";
104 } else if (!unitLbl.ascii().empty()) {
105 m_title += " (" + QString::fromStdString(unitLbl.ascii()) + ")";
106 }
107 }
108}
109
116void PlotAxis::titleFromYData(const Mantid::API::MatrixWorkspace &workspace, const bool plottingDistribution) {
117 std::string yLabel = workspace.YUnitLabel();
118 if (yLabel.empty()) {
119 yLabel = workspace.YUnit();
120 }
121 if (!workspace.isDistribution() && plottingDistribution) {
122 const auto xLabel = workspace.getAxis(0)->unit()->label();
123 m_title = replacePerWithNegativeIndice(yLabel, true, xLabel);
124 } else {
125 m_title = replacePerWithNegativeIndice(yLabel, false);
126 }
127}
128
129} // namespace API
130} // namespace MantidQt
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
QString m_title
Title.
Definition: PlotAxis.h:49
PlotAxis()=delete
Disable default constructor.
void titleFromDimension(const Mantid::Geometry::IMDDimension &dim)
Creates a title suitable for an axis attached to the given dimension.
Definition: PlotAxis.cpp:98
void titleFromIndex(const Mantid::API::IMDWorkspace &workspace, const size_t index)
Creates a title suitable for an axis attached to the given index.
Definition: PlotAxis.cpp:87
QString title() const
Create a new axis title.
Definition: PlotAxis.cpp:81
void titleFromYData(const Mantid::API::MatrixWorkspace &workspace, const bool plottingDistribution)
Creates a title suitable for the Y data values.
Definition: PlotAxis.cpp:116
Basic MD Workspace Abstract Class.
Definition: IMDWorkspace.h:40
Base MatrixWorkspace Abstract Class.
The class describes one dimension of multidimensional dataset representing an orthogonal dimension an...
Definition: IMDDimension.h:39
virtual std::string getName() const =0
virtual const Kernel::UnitLabel getUnits() const =0
A base-class for the a class that is able to return unit labels in different representations.
Definition: UnitLabel.h:20
QString toQStringInternal(const wchar_t *str)
Internal version of QString::fromStdWString.
Definition: QStringUtils.h:21
The AlgorithmProgressDialogPresenter keeps track of the running algorithms and displays a progress ba...