diff --git a/Fwk/AppFwk/cafVizExtensions/cafCategoryLegend.cpp b/Fwk/AppFwk/cafVizExtensions/cafCategoryLegend.cpp index abc707ecc9..f816dce0f7 100644 --- a/Fwk/AppFwk/cafVizExtensions/cafCategoryLegend.cpp +++ b/Fwk/AppFwk/cafVizExtensions/cafCategoryLegend.cpp @@ -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(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); diff --git a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp index 41100d1a90..11d14e0bd8 100644 --- a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp +++ b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.cpp @@ -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 colorArr = ScalarMapper::colorTableArray(ColorTable::NORMAL); + + setColors(*(colorArr.p())); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void CategoryMapper::setCategories(const cvf::IntArray& categoryValues, const std::vector& categoryNames) +{ + m_categoryValues = categoryValues; + m_categoryNames = categoryNames; ref 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(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(catIndex) / static_cast(m_categories.size()); + double normVal = static_cast(catIndex) / static_cast(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(clampedValue * m_categories.size()); - return m_categories[catIndex]; + size_t catIndex = static_cast(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(i); } diff --git a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h index d955c56235..15ed2d9245 100644 --- a/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h +++ b/Fwk/AppFwk/cafVizExtensions/cafCategoryMapper.h @@ -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& 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 m_categoryNames; }; }