swift - "ios-charts" Do not draw empty or zero values. & Interpolate values on combined chart -
my question 2 fold logic may similar.
i have simple candlestick chart 2 different indicators. 1 @ bottom , other overlayed on top of candlestick data. (see screenshot)
first: in lower chart not want draw values before calculated. in case 14 periods simple moving average. (see screenshot yellow box. not want draw these values)
second: on overlay want "open", lowest value, highest value, , "close" of last bar. want draw line between these 4 points , ignore data in-between. (it should yellow line in screenshot).
how can reformat set chart function ignore values before 14 periods on lower line chart?
how can ignore other points , interpolate between 4 values on line chart combined view?
currently have set values want "ignored" 0 (1950 in screenshot make readable , not squashed) around out of bounds errors.
tried simplify code as possible make more readable here:
// // viewcontroller.swift // import foundation import cocoa import charts var yvaluesindicatorlower: [double] = [] var yvaluesindicatoroverlay: [double] = [] class viewcontroller: nsviewcontroller { //chart view outlets @iboutlet weak var combinedchartview: combinedchartview! @iboutlet weak var linechartview: linechartview! // zoom buttons @ibaction func zoomall(sender: anyobject) { combinedchartview.fitscreen() linechartview.fitscreen() } @ibaction func zoomin(sender: anyobject) { combinedchartview.zoomin() linechartview.zoomin() } @ibaction func zoomout(sender: anyobject) { combinedchartview.zoomout() linechartview.zoomout() } override func viewdidload() { super.viewdidload() // additional setup after loading view. view.wantslayer = true view.layer?.backgroundcolor = nscolor(red: 0.2, green: 0.2, blue: 0.2, alpha: 0.95).cgcolor combinedchartview.nodatatext = "loading data..." linechartview.nodatatext = "loading data..." // additional setup after loading view. let xvalues = ["05-16", "05-17", "05-18", "05-19", "05-20", "05-23", "05-24", "05-25", "05-26", "05-27", "05-31", "06-01", "06-02", "06-03", "06-06", "06-07", "06-08", "06-09", "06-10", "06-13", "06-14", "06-15", "06-16", "06-17", "06-20", "06-21", "06-22", "06-23", "06-24", "06-27", "06-28", "06-29", "06-30", "07-01", "07-05", "07-06", "07-07", "07-08", "07-11", "07-12", "07-13", "07-14", "07-15", "07-18", "07-19", "07-20", "07-21", "07-22", "07-25", "07-26", "07-27", "07-28", "07-29", "08-01", "08-02", "08-03"] ////prices ["open"=[0], "high"=[1], "low"=[2], "last"[3]] let yvalues: [[double]] = [[2032.75, 2060.0, 2027.0, 2053.5], [2052.5, 2061.0, 2028.75, 2036.5], [2035.75, 2049.5, 2022.75, 2034.75], [2035.75, 2036.25, 2014.0, 2030.5], [2030.5, 2047.5, 2029.5, 2042.0], [2043.75, 2047.5, 2035.25, 2036.75], [2037.5, 2069.0, 2032.75, 2067.0], [2066.5, 2084.25, 2065.75, 2080.75], [2080.5, 2083.75, 2074.5, 2083.0], [2082.0, 2090.25, 2080.0, 2090.25], [2091.25, 2095.25, 2078.0, 2086.25], [2085.0, 2090.75, 2075.0, 2088.75], [2088.75, 2096.0, 2078.25, 2095.25], [2094.5, 2097.25, 2074.0, 2088.25], [2088.5, 2103.25, 2085.25, 2099.75], [2099.25, 2108.75, 2098.0, 2101.75], [2101.25, 2110.75, 2098.0, 2108.5], [2108.5, 2109.5, 2097.25, 2104.75], [2104.5, 2104.75, 2079.5, 2087.0], [2081.0, 2089.25, 2066.5, 2067.25], [2067.25, 2072.75, 2054.75, 2064.75], [2064.5, 2079.5, 2059.75, 2062.5], [2062.5, 2071.5, 2040.75, 2069.5], [2068.75, 2074.75, 2053.25, 2059.75], [2071.0, 2092.5, 2070.25, 2082.75], [2079.0, 2086.0, 2074.0, 2079.25], [2079.25, 2091.25, 2075.25, 2077.0], [2083.0, 2113.25, 2083.0, 2113.25], [2115.75, 2119.5, 1999.0, 2018.25], [2013.75, 2022.5, 1981.5, 1982.0], [1982.5, 2029.75, 1982.25, 2028.25], [2028.25, 2074.75, 2022.75, 2074.25], [2072.25, 2091.25, 2056.5, 2086.5], [2086.5, 2100.75, 2081.5, 2096.25], [2099.75, 2104.75, 2072.5, 2084.5], [2084.0, 2094.5, 2065.75, 2093.0], [2092.0, 2102.0, 2081.75, 2092.25], [2092.0, 2125.5, 2087.5, 2120.75], [2120.25, 2136.75, 2120.0, 2130.5], [2130.75, 2149.25, 2128.5, 2146.0], [2145.75, 2152.25, 2139.5, 2145.75], [2145.25, 2168.0, 2142.75, 2157.75], [2156.5, 2164.75, 2143.25, 2146.0], [2154.5, 2163.25, 2152.75, 2160.25], [2160.0, 2160.75, 2151.25, 2159.0], [2159.0, 2169.75, 2155.25, 2168.75], [2168.25, 2170.25, 2153.5, 2159.25], [2159.0, 2169.25, 2156.0, 2167.25], [2167.75, 2172.5, 2155.75, 2163.25], [2162.75, 2168.0, 2153.75, 2166.0], [2166.0, 2169.25, 2152.0, 2163.25], [2162.75, 2168.5, 2153.5, 2166.0], [2165.5, 2171.75, 2157.5, 2171.0], [2174.0, 2177.75, 2159.75, 2165.5], [2166.0, 2171.0, 2141.5, 2153.0], [2152.25, 2158.25, 2145.25, 2156.75]] //pass values indicators , returned indicator values yvaluesindicatoroverlay = indicatoroverlay(yvalues) yvaluesindicatorlower = indicatorlower(yvalues) //pass values chart setting function setchart(xvalues, valuescandlechart: yvalues, indicatorlowervalues: yvaluesindicatorlower, indicatoroverlayvalues: yvaluesindicatoroverlay) } func indicatoroverlay(yvalues: [[double]]) -> [double]{ var highest = double() var zigzag: [double] = [] in 0..<yvalues.count { highest = max(highest, yvalues[i][1]) } var lowest = highest in 0..<yvalues.count { lowest = min(lowest, yvalues[i][2]) } in 0..<yvalues.count { if == 0 { zigzag.append(yvalues[i][0]) } else if yvalues[i][1] == highest { zigzag.append(highest) } else if yvalues[i][2] == lowest { zigzag.append(lowest) } else if == yvalues.count-1 { zigzag.append(yvalues[i][3]) } else { zigzag.append(0)//these values should ignored , interpolated between other values. } } print(highest) print(lowest) return zigzag } func indicatorlower(yvalues: [[double]]) -> [double]{ let peroid = 14.0 var sum = 0.0 var sma: [double] = [] //simple moving average in 0..<int(peroid) { sum += yvalues[i][3] if < int(peroid)-1 { sma.append(0)//these values ignored } } sma.append(sum/peroid) in int(peroid)..<yvalues.count { sum = (sum - yvalues[i - int(peroid)][3]) + yvalues[i][3] sma.append(sum/peroid) } return sma } //function set chart values func setchart(xvalues: [string], valuescandlechart: [[double]], indicatorlowervalues: [double], indicatoroverlayvalues: [double]) { var yvalscandlechart : [candlechartdataentry] = [] in 0..<valuescandlechart.count { let high = valuescandlechart[i][1] let low = valuescandlechart[i][2] let open = valuescandlechart[i][0] let close = valuescandlechart[i][3] yvalscandlechart.append(candlechartdataentry(xindex: i, shadowh: high, shadowl: low, open: open, close: close)) } //how ignore values "0" or below set index? var indicatorlower_yvalues: [chartdataentry] = [] in 0..<xvalues.count { let indicatorlower_yvalue = chartdataentry(value: indicatorlowervalues[i], xindex: i) indicatorlower_yvalues.append(indicatorlower_yvalue) } //how use vlaues calculated , not "0" draw zigzag across candlestick chart? var indicatoroverlay_yvalues: [chartdataentry] = [] in 0..<xvalues.count { let indicatoroverlay_yvalue = chartdataentry(value: indicatoroverlayvalues[i], xindex: i) indicatoroverlay_yvalues.append(indicatoroverlay_yvalue) } let candlechartdataset = candlechartdataset(yvals: yvalscandlechart, label: "price") let lineoverlaychartdataset = linechartdataset(yvals: indicatoroverlay_yvalues, label: "indicatoroverlay") let data: combinedchartdata = combinedchartdata(xvals: xvalues) data.candledata = candlechartdata(xvals: xvalues, datasets: [candlechartdataset]) data.linedata = linechartdata(xvals: xvalues, datasets: [lineoverlaychartdataset]) combinedchartview.draworder = [3, 2] combinedchartview.data = data candlechartdataset.decreasingcolor = nscolor.redcolor() candlechartdataset.increasingcolor = nscolor.greencolor() candlechartdataset.neutralcolor = nscolor.bluecolor() candlechartdataset.shadowcolorsameascandle = true candlechartdataset.shadowwidth = 1 candlechartdataset.decreasingfilled = true candlechartdataset.increasingfilled = false candlechartdataset.drawvaluesenabled = false combinedchartview.doubletaptozoomenabled = false lineoverlaychartdataset.drawcirclesenabled = false lineoverlaychartdataset.drawvaluesenabled = false let linechartdataset = linechartdataset(yvals: indicatorlower_yvalues, label: "indicatorlower") let linechartdata = linechartdata(xvals: xvalues, dataset: linechartdataset) linechartview.data = linechartdata linechartview.doubletaptozoomenabled = false linechartdataset.drawcirclesenabled = false linechartdataset.drawvaluesenabled = false } override var representedobject: anyobject? { didset { // update view, if loaded. } } }
wow easy... =]
shows how library.
per suggestion on issue here: linechartview avoid painting 0 values #830
all had change these:
//how ignore values "0" or below set index? var indicatorlower_yvalues: [chartdataentry] = [] in 0..<xvalues.count { let indicatorlower_yvalue = chartdataentry(value: indicatorlowervalues[i], xindex: i) indicatorlower_yvalues.append(indicatorlower_yvalue) } //how use vlaues calculated , not "0" draw zigzag across candlestick chart? var indicatoroverlay_yvalues: [chartdataentry] = [] in 0..<xvalues.count { let indicatoroverlay_yvalue = chartdataentry(value: indicatoroverlayvalues[i], xindex: i) indicatoroverlay_yvalues.append(indicatoroverlay_yvalue) }
to these:
//ignore values "0" var indicatorlower_yvalues: [chartdataentry] = [] in 0..<xvalues.count { if indicatorlowervalues[i] != 0 {//add: if clause skip "0" let indicatorlower_yvalue = chartdataentry(value: indicatorlowervalues[i], xindex: i) indicatorlower_yvalues.append(indicatorlower_yvalue) } } //use vlaues not "0" draw zigzag var indicatoroverlay_yvalues: [chartdataentry] = [] in 0..<xvalues.count { if indicatoroverlayvalues[i] != 0 {//add: if clause skip "0" let indicatoroverlay_yvalue = chartdataentry(value: indicatoroverlayvalues[i], xindex: i) indicatoroverlay_yvalues.append(indicatoroverlay_yvalue) } }
Comments
Post a Comment