Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Utilities.swift #39

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ARCharts/ARBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ public class ARBarChart: SCNNode {
private func addLabel(forSeries series: Int, atZPosition zPosition: Float, withMaxHeight maxHeight: Float) {
if let seriesLabelText = dataSource!.barChart(self, labelForSeries: series) {
let seriesLabel = SCNText(string: seriesLabelText, extrusionDepth: 0.0)
seriesLabel.truncationMode = kCATruncationNone
seriesLabel.alignmentMode = kCAAlignmentCenter
seriesLabel.truncationMode = CATextLayerTruncationMode.none.rawValue
seriesLabel.alignmentMode = CATextLayerAlignmentMode.center.rawValue
seriesLabel.font = UIFont.systemFont(ofSize: 10.0)
seriesLabel.firstMaterial!.isDoubleSided = true
seriesLabel.firstMaterial!.diffuse.contents = delegate!.barChart(self, colorForLabelForSeries: series)
Expand Down Expand Up @@ -351,8 +351,8 @@ public class ARBarChart: SCNNode {
private func addLabel(forIndex index: Int, atXPosition xPosition: Float, withMaxHeight maxHeight: Float) {
if let indexLabelText = dataSource!.barChart(self, labelForValuesAtIndex: index) {
let indexLabel = SCNText(string: indexLabelText, extrusionDepth: 0.0)
indexLabel.truncationMode = kCATruncationNone
indexLabel.alignmentMode = kCAAlignmentCenter
indexLabel.truncationMode = CATextLayerTruncationMode.none.rawValue
indexLabel.alignmentMode = CATextLayerAlignmentMode.center.rawValue
indexLabel.font = UIFont.systemFont(ofSize: 10.0)
indexLabel.firstMaterial!.isDoubleSided = true
indexLabel.firstMaterial!.diffuse.contents = delegate!.barChart(self, colorForLabelForValuesAtIndex: index)
Expand Down
4 changes: 2 additions & 2 deletions ARCharts/ARBarChartProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import UIKit
* The `ARBarChartDataSource` protocol is adopted by an object that mediates the application's data model for an `ARBarChart` object.
* The data source proves the bar chart object with the information it needs to construct and modify a bar chart.
*/
public protocol ARBarChartDataSource: class {
public protocol ARBarChartDataSource: AnyObject {

/**
* Asks the data source to return the number of series (rows on the Y axis) in the bar chart.
Expand Down Expand Up @@ -76,7 +76,7 @@ extension ARBarChartDataSource {

}

public protocol ARBarChartDelegate: class {
public protocol ARBarChartDelegate: AnyObject {

/**
* Asks the delegate to return the color for a bar at a given index (X axis) for a specific series (rows on the Y axis) in the bar chart.
Expand Down
136 changes: 20 additions & 116 deletions ARCharts/ARDataSeries.swift
Original file line number Diff line number Diff line change
@@ -1,127 +1,31 @@
//
// ARDataSeries.swift
// ARBarCharts
// CABasicAnimation+Utilities.swift
// ARCharts
//
// Created by Bobo on 7/16/17.
// Created by Christopher Chute on 7/25/17.
// Copyright © 2017 Boris Emorine. All rights reserved.
//

import Foundation
import SceneKit
import UIKit

/**
* The `ARDataSeries` object is used as a convenience to easily create bar charts with `ARBarcharts`.
* If more customization is desired, you should create your own object conforming to `ARBarChartDataSource` and `ARBarChartDelegate`.
*/
public class ARDataSeries: ARBarChartDataSource, ARBarChartDelegate {

private let values: [[Double]]

/// Labels to use for the series (Z-axis).
public var seriesLabels: [String]? = nil

/// Labels to use for the values at each index (X-axis).
public var indexLabels: [String]? = nil

/// Colors to use for the bars, cycled through based on bar position.
public var barColors: [UIColor]? = nil

/// Materials to use for the bars, cycled through based on bar position.
/// If non-nil, `barMaterials` overrides `barColors` to style the bars.
public var barMaterials: [SCNMaterial]? = nil

/// Chamfer radius to use for the bars.
public var chamferRadius: Float = 0.0

/// Gap between series, expressed as a ratio of gap to bar width (Z-axis).
public var seriesGap: Float = 0.5

/// Gap between indices, expressed as a ratio of gap to bar length (X-axis).
public var indexGap: Float = 0.5

/// Space to allow for the series labels, expressed as a ratio of label space to graph width (Z-axis).
public var spaceForSeriesLabels: Float = 0.2

/// Space to allow for the index labels, expressed as a ratio of label space to graph length (X-axis).
public var spaceForIndexLabels: Float = 0.2

/// Opacity of each bar in the graph.
public var barOpacity: Float = 1.0


// MARK - ARBarChartDataSource

public required init(withValues values: [[Double]]) {
self.values = values
}

public func numberOfSeries(in barChart: ARBarChart) -> Int {
return values.count
}

public func barChart(_ barChart: ARBarChart, numberOfValuesInSeries series: Int) -> Int {
return values[series].count
}

public func barChart(_ barChart: ARBarChart, valueAtIndex index: Int, forSeries series: Int) -> Double {
return values[series][index]
}

public func barChart(_ barChart: ARBarChart, labelForSeries series: Int) -> String? {
let label = seriesLabels?[series]

return label
}

public func barChart(_ barChart: ARBarChart, labelForValuesAtIndex index: Int) -> String? {
return indexLabels?[index]
}

// MARK - ARBarChartDelegate

public func barChart(_ barChart: ARBarChart, colorForBarAtIndex index: Int, forSeries series: Int) -> UIColor {
if let barColors = barColors {
return barColors[(series * values[series].count + index) % barColors.count]
}

return UIColor.white
}

public func barChart(_ barChart: ARBarChart, materialForBarAtIndex index: Int, forSeries series: Int) -> SCNMaterial {
if let barMaterials = barMaterials {
return barMaterials[(series * (values.first?.count ?? 0) + index) % barMaterials.count]
}

extension CABasicAnimation {

static func animation(forKey keyPath: String,
from fromValue: Float,
to toValue: Float,
duration: Double,
delay: Double?) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: keyPath)
animation.beginTime = delay != nil ? CACurrentMediaTime() + delay! : 0.0
animation.duration = duration
animation.fromValue = fromValue
animation.toValue = toValue
animation.isRemovedOnCompletion = false
animation.fillMode = CAMediaTimingFillMode.forwards
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)

// If bar materials are not set, default to using colors
let colorMaterial = SCNMaterial()
colorMaterial.diffuse.contents = self.barChart(barChart, colorForBarAtIndex: index, forSeries: series)
colorMaterial.specular.contents = UIColor.white
return colorMaterial
}

public func barChart(_ barChart: ARBarChart, gapSizeAfterSeries series: Int) -> Float {
return seriesGap
}

public func barChart(_ barChart: ARBarChart, gapSizeAfterIndex index: Int) -> Float {
return indexGap
}

public func barChart(_ barChart: ARBarChart, opacityForBarAtIndex index: Int, forSeries series: Int) -> Float {
return barOpacity
}

public func barChart(_ barChart: ARBarChart, chamferRadiusForBarAtIndex index: Int, forSeries series: Int) -> Float {
return chamferRadius
}

public func spaceForSeriesLabels(in barChart: ARBarChart) -> Float {
return spaceForSeriesLabels
}

public func spaceForIndexLabels(in barChart: ARBarChart) -> Float {
return spaceForIndexLabels
return animation
}

}
Loading