如何在ios-charts中更改Balloon Marker图像?

时间:2016-01-12 19:59:36

标签: ios iphone swift bar-chart ios-charts

我的图表气球正在从屏幕the answer by @usr的边界切割 那我怎么能改变图像的箭头Like this 是否可以更改标记箭头(箭头到左侧/右侧)作为图像显示?

此气球标记由CGContext绘制,其代码如下所示

CGContextSaveGState(context)
    CGContextSetFillColorWithColor(context, color?.CGColor)
    CGContextBeginPath(context)
    CGContextMoveToPoint(context,
        rect.origin.x,
        rect.origin.y)
    CGContextAddLineToPoint(context,
        rect.origin.x + rect.size.width,
        rect.origin.y)
    CGContextAddLineToPoint(context,
        rect.origin.x + rect.size.width,
        rect.origin.y + rect.size.height - arrowSize.height)
    CGContextAddLineToPoint(context,
        rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
        rect.origin.y + rect.size.height - arrowSize.height)
    CGContextAddLineToPoint(context,
        rect.origin.x + rect.size.width / 2.0,
        rect.origin.y + rect.size.height)
    CGContextAddLineToPoint(context,
        rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
        rect.origin.y + rect.size.height - arrowSize.height)
    CGContextAddLineToPoint(context,
        rect.origin.x,
        rect.origin.y + rect.size.height - arrowSize.height)
    CGContextAddLineToPoint(context,
        rect.origin.x,
        rect.origin.y)
    CGContextFillPath(context)

此代码以balloon-marker.swift文件

编写

1 个答案:

答案 0 :(得分:1)

气球标记只是您如何使用它的一个示例。 ChartMarker类有一些属性,其中包括image属性。您可以直接使用它来显示任何图像。

您需要考虑的是位置和大小。您有责任确定位置和大小,因为默认实现是:

/// Draws the ChartMarker on the given position on the given context
public func draw(context context: CGContext, point: CGPoint)
{
    let offset = self.offsetForDrawingAtPos(point)
    let size = self.size

    let rect = CGRect(x: point.x + offset.x, y: point.y + offset.y, width: size.width, height: size.height)

    UIGraphicsPushContext(context)
    image!.drawInRect(rect)
    UIGraphicsPopContext()
}

您看到它只是在x: point.x + offset.x, y: point.y + offset.y处使用width: size.width, height: size.height

绘制图像

您只需创建ChartMarker子类并通过覆盖draw(context context: CGContext, point: CGPoint)进行自定义

如果您不想使用图像,那么您必须使用CoreGraphics,因为您已经看到了绘制您喜欢的形状。不过,您有责任决定如何绘制形状。

关于在屏幕边缘切割,这是另一个问题,因为你的形状太大而无法在中心显示。无论是改变大小还是改变你的形状方向等等都可以解决它,这取决于你。