I was doing a bit of thinking about how I would be adding a horizontal line to my financial data series because my x-axis was in Date format. So I wrote this walkthrough with 2 functions to help any devs that come along after me.
My example is a bar chart with prices on the vertical y-axis and dates on the x-axis. the final product is posted below as a pic. All that is needed is to count the items in your date data series and you can position your bar relative to that.
my dates were in this object array and I just counted the array to get my bearings.
let totalBars = lastPriceList.count // holds number of bars in my price object
let startBar = totalBars - 100 // this will make my line 100 bars long
let sellPrice = lastBar?.shortEntryPrice // here is the price level of the line I want to plot
then I passed that to this function to get a horizontal line
addTradeEntry(SignalLine: sellPrice!, StartBar: startBar, EndBar: totalBars)
here are the 2 functions that do all of the work
private func addTradeEntry(SignalLine: Double, StartBar: Int, EndBar: Int) {
let horizontalLine1 = SCIHorizontalLineAnnotation()
horizontalLine1.coordinateMode = .absolute;
// lower number pushes to left side of x axis
horizontalLine1.x1 = SCIGeneric(StartBar)
// higher number pushes bar right of x axis
horizontalLine1.x2 = SCIGeneric(EndBar)
// the position on y (price) axis
horizontalLine1.y1 = SCIGeneric(SignalLine)
horizontalLine1.horizontalAlignment = .center
horizontalLine1.isEditable = false
horizontalLine1.style.linePen = SCISolidPenStyle.init(color: UIColor.red, withThickness: 2.0)
horizontalLine1.add(self.buildLineTextLabel("Sell \(SignalLine)", alignment: .left, backColor: UIColor.clear, textColor: UIColor.red))
surface.annotations.add(horizontalLine1)
}
this one formats the text
private func buildLineTextLabel(_ text: String, alignment: SCILabelPlacement, backColor: UIColor, textColor: UIColor) -> SCILineAnnotationLabel {
let lineText = SCILineAnnotationLabel()
lineText.text = text
lineText.style.labelPlacement = alignment
lineText.style.backgroundColor = backColor
lineText.style.textStyle.color = textColor
return lineText
}
- Warren Hansen asked 7 years ago
- last edited 7 years ago
- You must login to post comments
Hi Warren,
Thank you very much for sharing this!
Please let us know if you need any assistance.
Best regards,
Julia,
SciChart Team
- Julia Skorobogata answered 7 years ago
- You must login to post comments
Please login first to submit.