Search code examples
swiftcore-plot

Core Plot Linear to Logarithmic toggle


Having an issue toggling from xScaleType .linear to .log (or .logModulus). The following code causes the plot to disappear in the X direction, ie can still see the Y axis and its ticks, but the X axis disappears and a single data line is seen at the Y axis. The issue only happens if you pan to the far left X location 0. If not at zero then no issue.

If I start with .log type then all is well. I can pan/zoom to location 0 with no issues, issue occurs if previously was set to .linear.

Code to reproduce:

// Set as linear
let plotSpace = ((hostView.hostedGraph as? CPTXYGraph)?.defaultPlotSpace as? CPTXYPlotSpace)
plotSpace?.xScaleType = .linear
plotSpace?.graph?.plot(withIdentifier: plotId as NSCopying?)?.reloadData()

// Some UI user interaction to trigger next code

// Set as log (or logModulus)
plotSpace?.xScaleType = .log
plotSpace?.graph?.plot(withIdentifier: plotId as NSCopying?)?.reloadData()

// Pan to location 0(if not already there).

Has anyone came across this or have a workaround?


Solution

  • Scale type .log cannot handle a plot range that includes zero (0) because log(0) is undefined. The .logModulus scale type is designed to handle this case. Note that you also probably need to update some of the labeling parameters when shifting between scale types. By default, major tick marks on a log scale are separated by powers of ten. Depending on the plot range, a log scale might not even cover one major tick interval.