/**
 * Construct a QwtWorkspaceSpectrumData object with a source workspace
 * @param workspace The workspace containing the data
 * @param wsIndex Index of the spectrum to plot
 * @param logScaleY If true, plot a log scale
 * @param plotAsDistribution If true and the data is histogram and not already a
 * distribution then plot the Y values/X bin-width
 */
QwtWorkspaceSpectrumData::QwtWorkspaceSpectrumData(
    const Mantid::API::MatrixWorkspace &workspace, int wsIndex,
    const bool logScaleY, const bool plotAsDistribution)
    : MantidQwtMatrixWorkspaceData(logScaleY), m_wsIndex(wsIndex),
      m_X(workspace.readX(wsIndex)), m_Y(workspace.readY(wsIndex)),
      m_E(workspace.readE(wsIndex)), m_xTitle(), m_yTitle(),
      m_isHistogram(workspace.isHistogramData()),
      m_dataIsNormalized(workspace.isDistribution()), m_binCentres(false),
      m_isDistribution(false) {
  // Actual plotting based on what type of data we have
  setAsDistribution(plotAsDistribution &&
                    !m_dataIsNormalized); // takes into account if this is a
                                          // histogram and sets m_isDistribution

  m_xTitle = MantidQt::API::PlotAxis(workspace, 0).title();
  m_yTitle = MantidQt::API::PlotAxis((m_dataIsNormalized || m_isDistribution),
                                     workspace).title();

  // Calculate the min and max values
  calculateYMinAndMax();
}
/**
 * @param workspace A reference to the workspace object that this data refers to
 */
void QwtWorkspaceBinData::init(const Mantid::API::MatrixWorkspace &workspace)
{
  if(workspace.axes() != 2)
  {
    std::ostringstream os;
    os << "QwtWorkspaceBinData(): Workspace must have two axes, found "
       << workspace.axes();
    throw std::invalid_argument(os.str());
  }

  // Check binIndex is valid
  if(static_cast<size_t>(m_binIndex) >= workspace.blocksize())
  {
    std::ostringstream os;
    os << "QwtWorkspaceBinData(): Index out of range. index="
       << m_binIndex << ", nvalues=" << workspace.blocksize();
    throw std::out_of_range(os.str());
  }

  // Fill vectors of data
  const size_t nhist = workspace.getNumberHistograms();
  auto* vertAxis = workspace.getAxis(1); //supplies X values
  m_X.resize(nhist);
  m_Y.resize(nhist);
  m_E.resize(nhist);
  for(size_t i = 0; i < nhist; ++i)
  {
    m_X[i] = vertAxis->getValue(i);
    m_Y[i] = workspace.readY(i)[m_binIndex];
    m_E[i] = workspace.readE(i)[m_binIndex];
  }

  // meta data
  m_xTitle = MantidQt::API::PlotAxis(workspace, 1).title();
  m_yTitle = MantidQt::API::PlotAxis(workspace).title();
}