#827 Added support for text labels in category legend

This commit is contained in:
Magne Sjaastad 2016-09-06 11:15:21 +02:00
parent 5420960b22
commit f01e762ef1
3 changed files with 70 additions and 20 deletions

View File

@ -233,7 +233,7 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer, OverlayColorLegendL
float lastVisibleTextY = 0.0;
CVF_ASSERT(m_categoryMapper.notNull());
size_t numLabels = m_categoryMapper->categories().size();
size_t numLabels = m_categoryMapper->categoryCount();
float categoryHeight = static_cast<float>(layout->legendRect.height() / numLabels);
@ -261,11 +261,10 @@ void CategoryLegend::setupTextDrawer(TextDrawer* textDrawer, OverlayColorLegendL
}
}
double tickValue = m_categoryMapper->categories()[it];
String valueString = String::number(tickValue);
String displayText = m_categoryMapper->textForCategoryIndex(it);
Vec2f pos(textX, textY);
textDrawer->addText(valueString, pos);
textDrawer->addText(displayText, pos);
lastVisibleTextY = textY;
m_visibleCategoryLabels.push_back(true);

View File

@ -22,9 +22,22 @@ CategoryMapper::CategoryMapper()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryMapper::setCategories(const IntArray& categories)
void CategoryMapper::setCategories(const IntArray& categoryValues)
{
m_categories = categories;
m_categoryValues = categoryValues;
ref<Color3ubArray> colorArr = ScalarMapper::colorTableArray(ColorTable::NORMAL);
setColors(*(colorArr.p()));
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CategoryMapper::setCategories(const cvf::IntArray& categoryValues, const std::vector<cvf::String>& categoryNames)
{
m_categoryValues = categoryValues;
m_categoryNames = categoryNames;
ref<Color3ubArray> colorArr = ScalarMapper::colorTableArray(ColorTable::NORMAL);
@ -36,9 +49,9 @@ void CategoryMapper::setCategories(const IntArray& categories)
//--------------------------------------------------------------------------------------------------
void CategoryMapper::setColors(const Color3ubArray& colorArray)
{
m_colors.resize(m_categories.size());
m_colors.resize(m_categoryValues.size());
for (size_t i = 0; i < m_categories.size(); i++)
for (size_t i = 0; i < m_categoryValues.size(); i++)
{
size_t colIdx = i % colorArray.size();
m_colors[i] = colorArray[colIdx];
@ -48,9 +61,41 @@ void CategoryMapper::setColors(const Color3ubArray& colorArray)
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::IntArray CategoryMapper::categories() const
void CategoryMapper::setInterpolateColors(const cvf::Color3ubArray& colorArray)
{
return m_categories;
if (m_categoryValues.size() > 0)
{
m_colors = *interpolateColorArray(colorArray, static_cast<cvf::uint>(m_categoryValues.size()));
}
else
{
m_colors.clear();
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
size_t CategoryMapper::categoryCount() const
{
return m_categoryValues.size();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const cvf::String CategoryMapper::textForCategoryIndex(size_t index) const
{
CVF_ASSERT(index < m_categoryValues.size());
if (index < m_categoryNames.size())
{
return m_categoryNames[index];
}
else
{
double tickValue = m_categoryValues[index];
return String::number(tickValue);
}
}
//--------------------------------------------------------------------------------------------------
@ -101,9 +146,9 @@ double CategoryMapper::normalizedValue(double categoryValue) const
if (catIndex != -1)
{
double halfLevelHeight = 1.0 / (m_categories.size() * 2);
double halfLevelHeight = 1.0 / (m_categoryValues.size() * 2);
double normVal = static_cast<double>(catIndex) / static_cast<double>(m_categories.size());
double normVal = static_cast<double>(catIndex) / static_cast<double>(m_categoryValues.size());
return normVal + halfLevelHeight;
}
@ -120,13 +165,13 @@ double CategoryMapper::domainValue(double normalizedValue) const
{
double clampedValue = cvf::Math::clamp(normalizedValue, 0.0, 1.0);
if (m_categories.size() == 0)
if (m_categoryValues.size() == 0)
{
return 0.0;
}
size_t catIndex = static_cast<size_t>(clampedValue * m_categories.size());
return m_categories[catIndex];
size_t catIndex = static_cast<size_t>(clampedValue * m_categoryValues.size());
return m_categoryValues[catIndex];
}
//--------------------------------------------------------------------------------------------------
@ -137,9 +182,9 @@ int CategoryMapper::categoryIndexForCategory(double domainValue) const
int catIndex = -1;
size_t i = 0;
while (i < m_categories.size() && catIndex == -1)
while (i < m_categoryValues.size() && catIndex == -1)
{
if (m_categories[i] == domainValue)
if (m_categoryValues[i] == domainValue)
{
catIndex = static_cast<int>(i);
}

View File

@ -4,6 +4,7 @@
#include "cvfBase.h"
#include "cvfObject.h"
#include "cvfScalarMapper.h"
#include "cvfString.h"
namespace caf {
@ -17,10 +18,14 @@ class CategoryMapper : public cvf::ScalarMapper
public:
CategoryMapper();
void setCategories(const cvf::IntArray& categories);
void setCategories(const cvf::IntArray& categoryValues);
void setCategories(const cvf::IntArray& categoryValues, const std::vector<cvf::String>& categoryNames);
void setColors(const cvf::Color3ubArray& colorArray);
void setInterpolateColors(const cvf::Color3ubArray& colorArray);
cvf::IntArray categories() const;
size_t categoryCount() const;
const cvf::String textForCategoryIndex(size_t index) const;
virtual cvf::Vec2f mapToTextureCoord(double scalarValue) const;
virtual bool updateTexture(cvf::TextureImage* image) const;
@ -38,7 +43,8 @@ private:
cvf::Color3ubArray m_colors;
cvf::uint m_textureSize; // The size of texture that updateTexture() is will produce.
cvf::IntArray m_categories;
cvf::IntArray m_categoryValues;
std::vector<cvf::String> m_categoryNames;
};
}