SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, iOS Chart, Android Chart and JavaScript Chart Components


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 = SCISolidPenStyle.init(color:, withThickness: 2.0)
    horizontalLine1.add(self.buildLineTextLabel("Sell \(SignalLine)", alignment: .left, backColor: UIColor.clear, textColor:

this one formats the text

private func buildLineTextLabel(_ text: String, alignment: SCILabelPlacement, backColor: UIColor, textColor: UIColor) -> SCILineAnnotationLabel {
    let lineText = SCILineAnnotationLabel()
    lineText.text = text = alignment = backColor = textColor
    return lineText
  • You must to post comments

Hi Warren,

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

Best regards,
SciChart Team

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.