Coreplot with Swift: There Is No Yaxis.Majorintervallength

CorePlot with swift: there is no yAxis.majorIntervalLength

Swift doesn't support NSDecimal values. We've made some changes to the Core Plot API, replacing NSDecimal with NSDecimalNumber. The changes are on the release-2.0 branch and not in a release package yet. See Core Plot issue #96 for more discussion of the issue.

Showing max y-axis with CorePlot CPTAxisLabelingPolicyAutomatic setting

Call -layoutIfNeeded on the graph to make sure the axis labels are updated and then read the majorTickLocations set from the y-axis. You can find the max location value in the set easily.

Core Plot Bar Graph Y-Axis issues

As I seem to be finding with most things xcode, it is all about where you put your code. If I have CPTPlotRange:PlotRangeWithLocation:Length before I do my set up it is really slow and buggy. If instead I add it to the end of my code after setting the other values for the axises suddenly there is only a very minor delay. I actually got this code originally from a tutorial and modified it to fit what I needed but I guess the tutorial didn't have the same delay because the length of the data was much lower.

So all I did to fix this problem was put the following code after my axis set up instead of before;

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0)
length:CPTDecimalFromFloat(1000)];
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:_liftOffPlot,_sideLeanPlot,_forwardLeanPlot,_crossLegsPlot,nil]];
graph.plotAreaFrame.borderLineStyle = nil;

Core Plot 2.1 fixed Y axis

The axisConstraints property is part of the CPTXYAxis class. Casting the axis set to CPTXYAxisSet is the easiest way to do this in Swift:

let axisSet = graph.axisSet as! CPTXYAxisSet
let y = axisSet.yAxis
y.axisConstraints = CPTConstraints(lowerOffset: 0.0)

CPTMutablePlotRange - Can't set length and location

This seems to be an issue regarding Objective-C's NSDecimal type which is not available in Swift. I found this work around: http://blog.alwold.com/2014/09/03/coreplot-nsdecimal-and-swift/

How do I add values to my graph in core plot swift?

Your initialization logic of the graph should look like below. Use this in viewDidLoad

func initPlot() {
let graph = CPTXYGraph(frame: hostView.bounds)
graph.plotAreaFrame?.masksToBorder = false
hostView.hostedGraph = graph
graph.backgroundColor = UIColor.white.cgColor
graph.paddingBottom = 40.0
graph.paddingLeft = 40.0
graph.paddingTop = 40.0
graph.paddingRight = 40.0

//configure title
let title = "f(x) = x*x + 10"
graph.title = title

//configure axes
let axisSet = graph.axisSet as! CPTXYAxisSet

if let x = axisSet.xAxis {
x.majorIntervalLength = 20
x.minorTicksPerInterval = 1
}

if let y = axisSet.yAxis {
y.majorIntervalLength = 5
y.minorTicksPerInterval = 5
}

let xMin = 0.0
let xMax = 100.0
let yMin = 0.0
let yMax = 100.0
guard let plotSpace = graph.defaultPlotSpace as? CPTXYPlotSpace else { return }
plotSpace.xRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(xMin), lengthDecimal: CPTDecimalFromDouble(xMax - xMin))
plotSpace.yRange = CPTPlotRange(locationDecimal: CPTDecimalFromDouble(yMin), lengthDecimal: CPTDecimalFromDouble(yMax - yMin))

//create the plot
plot = CPTScatterPlot()
plot.dataSource = self
graph.add(plot, to: graph.defaultPlotSpace)
}

Additionally you need to implement CPTScatterPlotDataSource, where you define the numberOfRecords and respective X and Y values

extension ViewController: CPTScatterPlotDataSource {
func numberOfRecords(for plot: CPTPlot) -> UInt {
return 100
}

func number(for plot: CPTPlot, field: UInt, record: UInt) -> Any? {
switch CPTScatterPlotField(rawValue: Int(field))! {
case .X:
return record
case .Y:
return (record * record) + 10
default:
return 0
}
}
}

Sample Image

Cannot invoke an Objective-C method in Swift

expandRangeByFactor exists, but it takes an NSDecimal. You are trying to supply an NSDecimalNumber, which is not the same thing. (Another problem with your code - irrelevant in this situation, but still a problem - is that you cannot say NSDecimalNumber(1.1). There is no such initializer.)

You need to pass an NSDecimal parameter here. That is why the original Objective-C code calls the Core Plot function CPTDecimalFromCGFloat() - to generate the NSDecimal that goes here.

However, you can't do that in Swift because it doesn't know about NSDecimal. Thus, Swift cannot see this method no matter what you do, because it can't see a method that takes a value of a type that it can't deal with.

The Core Plot people are aware of this issue: see https://github.com/core-plot/core-plot/issues/96 The new release-2.0 branch contains a fix. As you can see here, in the new API you can just pass an NSNumber to expandRangeByFactor.

Update: Swift 1.2 (available in Xcode 6.3 and later) can deal with NSDecimal.



Related Topics



Leave a reply



Submit