我正在尝试使用Swift下的core-plot制作条形图,我在xAxis上制作自定义标签时遇到问题,它们始终位于tickLocation零。

也许有人可以帮助我,这是我的代码。

import UIKit

class aaa: UIViewController, CPTPlotDataSource{

    var items :[NSNumber] = [10,35,18, 20, 50, 5]


    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet weak var lblTitle: UILabel!

    @IBOutlet weak var lblChartName: UILabel!
    @IBOutlet weak var vGraph: CPTGraphHostingView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.scrollView.contentSize = CGSizeMake(320, 568);

        var id: Int=general.idListed
        lblTitle.text=general.namListed

        items  = [10, 35, 18, 20, 50, 5]

        var CPDBarWidth:CGFloat = 0.25
        var CPDBarInitialX:CGFloat = 0.25


        var graph = CPTXYGraph(frame: CGRectZero)
        graph.plotAreaFrame.masksToBorder = false
        graph.paddingBottom = 50.0
        graph.paddingLeft  = 50.0
        graph.paddingTop    = 50.0
        graph.paddingRight  = 50.0
        graph.backgroundColor = UIColor.whiteColor().CGColor


        var titleStyle = CPTMutableTextStyle()
        titleStyle.color = CPTColor.blackColor()
        titleStyle.fontName = "Helvetica-Bold"
        titleStyle.fontSize = 16.0

        var title : NSString = "January 19 - 24, 2015"
        graph.title = title
        graph.titleTextStyle = titleStyle
        graph.titlePlotAreaFrameAnchor = CPTRectAnchorTop
        graph.titleDisplacement = CGPointMake(0.0, 40.0)


        var xMin : Float = 0
        var xMax : Float = Float(items.count) + 1
        var yMin : Float = 0
        var yMax : Float = maxItemsValue(items) + 5

        var plotSpace = graph.defaultPlotSpace as CPTXYPlotSpace
        var xRange = plotSpace.yRange.mutableCopy() as CPTMutablePlotRange
        var yRange = plotSpace.yRange.mutableCopy() as CPTMutablePlotRange

        xRange.setLocationFloat(xMin)
        xRange.setLengthFloat(xMax)

        yRange.setLocationFloat(yMin)
        yRange.setLengthFloat(yMax)

        plotSpace.xRange = xRange
        plotSpace.yRange = yRange

        var aaplPlot = CPTBarPlot()
        aaplPlot.barsAreHorizontal = false

        var barLineStyle = CPTMutableLineStyle()


        barLineStyle.lineColor = CPTColor.lightGrayColor()
        barLineStyle.lineWidth = 1

        aaplPlot.dataSource = self
        aaplPlot.delegate = self
        aaplPlot.barWidthScale = 1
        aaplPlot.barOffsetScale = 1
        aaplPlot.lineStyle = barLineStyle


        graph.addPlot(aaplPlot)

        var axisTitleStyle = CPTMutableTextStyle()
        axisTitleStyle.color = CPTColor.redColor()
        axisTitleStyle.fontName = "Helvetica-Bold"
        axisTitleStyle.fontSize = 12.0

        var axisLineStyle = CPTMutableLineStyle()
        axisLineStyle.lineWidth = 4.0
        axisLineStyle.lineColor = CPTColor.redColor()

        var axisSet =  CPTXYAxisSet()
        graph.axisSet = axisSet

        axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyNone
        axisSet.xAxis.title = "Days of Week"
        axisSet.xAxis.titleTextStyle = axisTitleStyle
        axisSet.xAxis.titleOffset = 30.0
        axisSet.xAxis.majorTickLength = 4
        axisSet.xAxis.minorTickLength = 0
        axisSet.xAxis.tickDirection = CPTSignNegative
        axisSet.xAxis.axisLineStyle = axisLineStyle

        var customLabels : NSMutableArray  = NSMutableArray (capacity: items.count)
        var tickLocations : NSMutableArray  = NSMutableArray (capacity: items.count)
        var labels : [String] = ["MON","THU","WEN","THR","FRI","SAT"]

        var next : Int = 0

        var newLabel : CPTAxisLabel
        for item in items {
            var xlabel : String = labels[next]
            next++;

            var tstyle : CPTMutableTextStyle = CPTMutableTextStyle()
            tstyle.color = CPTColor.blueColor()
            tstyle.fontSize = 10

            newLabel = CPTAxisLabel(text: xlabel, textStyle: tstyle);
            newLabel.setTickLocationFloat(Float(next))
            newLabel.offset = 5
            customLabels.addObject(newLabel)
            tickLocations.addObject(Float(next))

        }

        axisSet.xAxis.majorTickLocations = NSSet(array: tickLocations)
        axisSet.xAxis.axisLabels = NSSet(array: customLabels)

        self.vGraph.hostedGraph = graph


    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func maxItemsValue(items :[NSNumber])-> NSNumber{
        var max : NSNumber=0

        for item in items{
            if item.floatValue > max.floatValue {
                max = item
            }
        }

        return max
    }

    func numberOfRecordsForPlot(plot: CPTPlot!) -> UInt {
        return UInt(items.count)
    }

    func numberForPlot(plot: CPTPlot!, field fieldEnum: UInt, recordIndex idx: UInt) -> NSNumber! {
        switch (fieldEnum) {
        case 0:
            if (idx < UInt(items.count)) {
                return idx + 1
            }
            break;

        case 1:
            return items[Int(idx)]

        default:

            return 1

        }

        return 1
    }


}

最佳答案

您需要设置每个新标签的tickLocation

newLabel.tickLocation = next

关于ios - 核心图Swift BarPlot tickLocation始终为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28203479/

10-11 17:09