BarChartView的自定义X轴

时间:2016-03-29 13:53:22

标签: ios iphone swift ios-charts

我正在使用danielgindi / ios-charts图表库中的BarChartView。到目前为止,我能够得到以下BarChartView。我的问题是,有没有办法以这种方式自定义X轴,它只显示最小值(最左边的值37.03),最大值(最隐藏的最右边的值37.31),绿色条的值(37.17)和橙色条的值( 37.18再次隐藏)。简而言之,我想隐藏XAxis的一些价值,这在我的案例中并不重要。

enter image description here

1 个答案:

答案 0 :(得分:0)

这是一个示例:在showOnlyLeftRightEnabled类中添加名为YourChartXAxis的属性,然后在自定义x轴渲染器中添加internal override func drawLabels(context context: CGContext, pos: CGFloat, anchor: CGPoint)

要显示中间的一个,你需要确定它是哪个,并将x索引转换为像素值,就像我通过CGPointApplyAffineTransform(position, valueToPixelMatrix)

最左边和最右边的值一样
internal override func drawLabels(context context: CGContext, pos: CGFloat, anchor: CGPoint)
{
    let paraStyle = NSParagraphStyle.defaultParagraphStyle().mutableCopy() as! NSMutableParagraphStyle
    paraStyle.alignment = .Center

    let labelAttrs = [NSFontAttributeName: _xAxis.labelFont,
        NSForegroundColorAttributeName: _xAxis.labelTextColor,
        NSParagraphStyleAttributeName: paraStyle]
    let labelRotationAngleRadians = _xAxis.labelRotationAngle * ChartUtils.Math.FDEG2RAD

    let valueToPixelMatrix = transformer.valueToPixelMatrix

    var position = CGPoint(x: 0.0, y: 0.0)

    var labelMaxSize = CGSize()

    if (_xAxis.isWordWrapEnabled)
    {
        labelMaxSize.width = _xAxis.wordWrapWidthPercent * valueToPixelMatrix.a
    }
    if let showOnlyLeftRight = (_xAxis as? YourChartXAxis)?.showOnlyLeftRightEnabled
    {
        if (showOnlyLeftRight)
        {
            for (var i = lowestVisibleXIndex, maxX = min(highestVisibleXIndex + 1, _xAxis.values.count); i < maxX; i += maxX - lowestVisibleXIndex - 1)
            {
                let label = _xAxis.values[i]
                if (label == nil)
                {
                    continue
                }

                position.x = CGFloat(i)
                position.y = 0.0
                position = CGPointApplyAffineTransform(position, valueToPixelMatrix)

                let labelns = label! as NSString

                //label location by string width
                // fix rightLabel
                if (highestVisibleXIndex == lowestVisibleXIndex)
                {
                    return super.drawLabels(context: context, pos: pos, anchor: anchor)
                }
                else if (i == highestVisibleXIndex)
                {
                    let width = labelns.boundingRectWithSize(labelMaxSize, options: .UsesLineFragmentOrigin, attributes: labelAttrs, context: nil).size.width
                    rightLabelX -= width / 2.0
                }
                else if (i == lowestVisibleXIndex)
                { // fix leftLabel
                    let width = labelns.boundingRectWithSize(labelMaxSize, options: .UsesLineFragmentOrigin, attributes: labelAttrs, context: nil).size.width
                    leftLabelX += width / 2.0
                }

                // fill leftLabel and rightLabel in current visible screen
                if (lowestVisibleXIndex == highestVisibleXIndex) {
                    ChartUtils.drawMultilineText(context: context, text: label!, point: CGPoint(x: leftLabelX, y: pos), attributes: labelAttrs, constrainedToSize: labelMaxSize, anchor: anchor, angleRadians: labelRotationAngleRadians)
                    ChartUtils.drawMultilineText(context: context, text: label!, point: CGPoint(x: rightLabelX, y: pos), attributes: labelAttrs, constrainedToSize: labelMaxSize, anchor: anchor, angleRadians: labelRotationAngleRadians)
                    break
                } else if (i == lowestVisibleXIndex) {
                    ChartUtils.drawMultilineText(context: context, text: label!, point: CGPoint(x: leftLabelX, y: pos), attributes: labelAttrs, constrainedToSize: labelMaxSize, anchor: anchor, angleRadians: labelRotationAngleRadians)
                } else if (i == highestVisibleXIndex) {
                    ChartUtils.drawMultilineText(context: context, text: label!, point: CGPoint(x: rightLabelX, y: pos), attributes: labelAttrs, constrainedToSize: labelMaxSize, anchor: anchor, angleRadians: labelRotationAngleRadians)
                }
            }
        } else {
            super.drawLabels(context: context, pos: pos, anchor: anchor)
        }
    }
}