Pre loader

Adding Horizontal line to a Price Series on Financial Graphs iOS

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

1
0

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
}
Version
>2
Images
  • You must to post comments
0
0

Hi Warren,

Thank you very much for sharing this!
Please let us know if you need any assistance.

Best regards,
Julia,
SciChart Team

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies