| 720 | } |
| 721 | |
| 722 | std::map<std::string, std::vector<legend::LegendItem>> XChart::GetLegendItems() { |
| 723 | |
| 724 | std::map<std::string, std::vector<legend::LegendItem>> legendItems; |
| 725 | |
| 726 | std::for_each(geoms_.begin(), geoms_.end(), [&](auto &geom) -> void { |
| 727 | if(geom->ContainsAttr(attr::AttrType::Color)) { |
| 728 | const unique_ptr<attr::AttrBase> &_attr = geom->GetColor(); |
| 729 | if(!_attr->GetFields().empty()) { |
| 730 | const attr::Color &colorAttr = static_cast<const attr::Color &>(*_attr.get()); |
| 731 | |
| 732 | scale::AbstractScale &scale = GetScale(colorAttr.GetFields()[0]); |
| 733 | |
| 734 | if(scale::IsCategory(scale.GetType())) { |
| 735 | const scale::Category &cat = static_cast<Category &>(scale); |
| 736 | std::vector<scale::Tick> ticks = scale.GetTicks(this); |
| 737 | |
| 738 | std::vector<legend::LegendItem> fieldItems; |
| 739 | |
| 740 | std::for_each(ticks.begin(), ticks.end(), [&](const scale::Tick &tick) -> void { |
| 741 | legend::LegendItem item; |
| 742 | item.name = tick.text; |
| 743 | item.field = cat.GetField(); |
| 744 | item.marker.fill = colorAttr.GetColor(cat.Transform(tick.tickValue)); |
| 745 | fieldItems.push_back(std::move(item)); |
| 746 | }); |
| 747 | legendItems[cat.field] = std::move(fieldItems); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | }); |
| 752 | |
| 753 | return legendItems; |
| 754 | } |
| 755 | |
| 756 | |
| 757 | #pragma mark - |