Coreplot legend with title, country label and under that line in swift

无人久伴 提交于 2019-12-23 13:38:07

问题


How to display the coreplot legend like this?

country flag,country name under that line(resembled in the graph)

func configureLegend() { guard let graph = hostView.hostedGraph else { return }

    let legend = CPTLegend(graph: graph)
    graph.legend = legend
    graph.legendAnchor = .topLeft
    graph.legendDisplacement = CGPoint(x: 50.0, y: -2.0)
    legend.fill = CPTFill(color: CPTColor.clear())
    legend.swatchSize = CGSize(width: 10.0, height: 10.0)
    legend.numberOfRows = 1
    legend.entryPaddingBottom = 12
    let titleStyle = CPTMutableTextStyle()
    titleStyle.color = CPTColor.black()
    titleStyle.fontSize = 6.0
    titleStyle.fontName = Font.NissanPro_Bold
    legend.textStyle = titleStyle
    legend.delegate = self
}
public func legend(_ legend: CPTLegend, shouldDrawSwatchAt idx: UInt, for plot: CPTPlot, in rect: CGRect, in context: CGContext) -> Bool{
    return false
}

回答1:


If you can draw the flags using emoji, do that. Insert a newline (\n) character in the title text to position the text under the flag.

If you need to use custom graphics for the flags, use a legend delegate and implement the -legend:shouldDrawSwatchAtIndex:forPlot:inRect:inContext: delegate method. Draw the flag into the given graphics context inside the swatch rectangle and return NO to tell the legend that you took care of the required drawing.

To put the text under the graphic, enlarge the swatch size and draw both into the swatch rectangle.



来源:https://stackoverflow.com/questions/45615082/coreplot-legend-with-title-country-label-and-under-that-line-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!