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

added bar title #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 36 additions & 1 deletion ARCharts/ARBarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,19 @@ public class ARBarChart: SCNNode {

self.addChildNode(barNode)

//Add value title on top of every bar
self.addTitle(barNode: barNode, barBox: barBox)

if series == 0 {
self.addLabel(forIndex: index, atXPosition: xPosition + xShift, withMaxHeight: barWidth)
}
previousXPosition = xPosition

presenter?.addAnimation(to: barNode, in: self)
}

self.addLabel(forSeries: series, atZPosition: zPosition + zShift, withMaxHeight: barLength)
previousZPosition = zPosition

}
}

Expand Down Expand Up @@ -295,6 +298,7 @@ public class ARBarChart: SCNNode {

return previousIndexXPosition + indexSize + indexSize * gapSize
}


/**
* Calculates the Z position (Series Axis) for a specific series.
Expand Down Expand Up @@ -343,6 +347,37 @@ public class ARBarChart: SCNNode {
}
}

/**
* Add a bar title on top of every bar
* - parameter bar: the ARChartBar to find height, seriesNo and indexNo.
* - parameter box: the SCNBox object to find geometry of the Bar.
*/
private func addTitle(barNode bar: ARBarChartBar, barBox box: SCNBox) {
let titleLabelText = String(dataSource!.barChart(self, valueAtIndex: bar.index, forSeries: bar.series))
let titleLabel = SCNText(string: titleLabelText, extrusionDepth: 0.0)
titleLabel.truncationMode = kCATruncationNone
titleLabel.alignmentMode = kCAAlignmentCenter
titleLabel.firstMaterial!.isDoubleSided = true
titleLabel.firstMaterial!.diffuse.contents = delegate!.barChart(self, colorForLabelForTitle: Int("\(bar.series)\(bar.index)")!)

//TODO: Did type casting to give unique id
let backgroundColor = delegate!.barChart(self, backgroundColorForLabelForTitle: Int("\(bar.series)\(bar.index)")!)
let barNameLabelNode = ARChartLabel(text: titleLabel, type: .title, id: Int("\(bar.series)\(bar.index)")!, backgroundColor: backgroundColor)

let unscaledLabelWidth = barNameLabelNode.boundingBox.max.x - barNameLabelNode.boundingBox.min.x
let desiredLabelWidth = box.length
let unscaledLabelHeight = barNameLabelNode.boundingBox.max.y - barNameLabelNode.boundingBox.min.y
let labelScale = min(Float(desiredLabelWidth) / unscaledLabelWidth, Float(bar.finalHeight) / Float(unscaledLabelHeight))
barNameLabelNode.scale = SCNVector3(labelScale, labelScale, labelScale)

let position = SCNVector3(x: bar.position.x,
y: bar.finalHeight,
z: bar.position.z - Float(box.length/2))
barNameLabelNode.position = position
barNameLabelNode.eulerAngles = SCNVector3(0.0, -0.5 * Float.pi, 0.0)
self.addChildNode(barNameLabelNode)
}

/**
* Add an index label to the X axis for a given index and at a given X position.
* - parameter index: The index (X axis) to be labeled.
Expand Down