2016-05-12 63 views
0

我正在使用這個lib作爲drawChart。鏈接:https://github.com/danielgindi/Charts如何在ios圖表中繪製CombinedChart

這是我的代碼。當我只畫CandleChart時,它是可以的。 但是,當我嘗試繪製CombinedChart。我接收這個結果。 This is my result

@IBOutlet weak var chart: CombinedChartView! 
var date1 = [String]() 
var open = [Double]() 
var high = [Double]() 
var low = [Double]() 
var close = [Double]() 
var volume = [Double]() 
var adj_close = [Double]() 
let gradientLayer = CAGradientLayer() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    loadVcs() 

    chart.delegate = self 

    chart.descriptionText = "" 
    chart.noDataTextDescription = "" 
    chart.drawGridBackgroundEnabled = false 



    chart.maxVisibleValueCount = 0 
    chart.pinchZoomEnabled = false 



    let xAxis = chart.xAxis 
    xAxis.labelPosition = ChartXAxis.XAxisLabelPosition.Bottom 
    xAxis.spaceBetweenLabels = 15 
    xAxis.drawLabelsEnabled = true 



    let leftAxis = chart.leftAxis 
    leftAxis.labelCount = 7 
    leftAxis.drawLabelsEnabled = false 
    leftAxis.drawAxisLineEnabled = false 

    let rightAxis = chart.rightAxis 
    rightAxis.drawGridLinesEnabled = true 
    rightAxis.enabled = true 

    setData() 

} 
override func viewWillAppear(animated: Bool) { 
    gradientLayer.frame = self.view.bounds 

    let color1 = UIColor.yellowColor().CGColor as CGColorRef 
    let color2 = UIColor(red: 1.0, green: 0, blue: 0, alpha: 1.0).CGColor as CGColorRef 
    let color3 = UIColor.clearColor().CGColor as CGColorRef 
    let color4 = UIColor(white: 0.0, alpha: 0.7).CGColor as CGColorRef 
    gradientLayer.colors = [color1, color2, color3, color4] 


    gradientLayer.locations = [0.0, 0.0, 1.0, 1.0] 



    // self.chart.layer.addSublayer(gradientLayer) 

} 

func loadVcs() 
{ 
    var csv :CSV? 
    do { 
     let path = NSBundle.mainBundle().pathForResource("FB", ofType: "csv") 
     csv = try CSV(name: path!) 
     if let data = csv { 
      for dict in data.rows 
      { 
       date1.insert(dict["Date"]!, atIndex: 0) 
       open.insert(Double(dict["Open"]!)!, atIndex: 0) 
       high.insert(Double(dict["High"]!)!, atIndex: 0) 
       low.insert(Double(dict["Low"]!)!, atIndex: 0) 
       close.insert(Double(dict["Close"]!)!, atIndex: 0) 
       volume.insert(Double(dict["Volume"]!)!, atIndex: 0) 
       adj_close.insert(Double(dict["Adj Close"]!)!, atIndex: 0) 
      } 
     } 

    } catch let error { 
     NSLog("\(error)") 

    } 

} 


func setData() 
{ 

    let data : CombinedChartData = CombinedChartData(xVals: date1) 

    data.candleData = generateCandleChartData(date1) 
    data.lineData = generateLineChartData(date1) 

    chart.data = data 


} 

func generateLineChartData(xVals : [String]) -> LineChartData 
{ 
    var d = LineChartData(xVals: xVals) 
    var lineVals = [ChartDataEntry]() 
    for i in 0 ..< date1.count 
    { 
     lineVals.append(ChartDataEntry(value: high[i], xIndex: i)) 
    } 

    let lineChartDataSet : LineChartDataSet = LineChartDataSet(yVals: lineVals, label: "Line Data Set") 
    lineChartDataSet.setColor(UIColor.greenColor()) 
    lineChartDataSet.lineWidth = 2.5 
    lineChartDataSet.drawCubicEnabled = true 
    lineChartDataSet.drawValuesEnabled = true 

    d.addDataSet(lineChartDataSet) 
    return d 
} 

func generateCandleChartData(xVals : [String]) -> CandleChartData 
{ 
    var d = CandleChartData(xVals: xVals) 
    var yVals = [ChartDataEntry]() 


    for i in 0 ..< date1.count 
    { 
     yVals.append(CandleChartDataEntry(xIndex: i, shadowH: high[i], shadowL: low[i], open: open[i] , close: close[i])) 
    } 

    let set1 = CandleChartDataSet(yVals: yVals, label: "Data") 

    set1.axisDependency = ChartYAxis.AxisDependency.Left 
    set1.setColor(UIColor(white: (80/255), alpha: 1.0)) 
    set1.shadowColor = UIColor.darkGrayColor() 
    set1.shadowWidth = 1 

    set1.decreasingColor = UIColor.redColor() 
    set1.decreasingFilled = true 

    set1.increasingColor = UIColor(red: 122/255, green: 242/255, blue: 84/255, alpha: 1) 
    set1.increasingFilled = true 

    set1.neutralColor = UIColor.blueColor() 

    d.addDataSet(set1) 
    return d 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

    // Dispose of any resources that can be recreated. 
} 
func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) { 
    NSLog("chartValueSelected") 
} 

func chartValueNothingSelected(chartView: ChartViewBase) { 
    NSLog("chartValueNoSelected") 
} 

請幫我:)

回答

0

請大家看看ChartsDemo這是庫的一部分,有供你參考組合圖。

+0

我已經編碼如demo –