如何在ios-chart中绘制CombinedChart

时间:2016-05-12 06:23:58

标签: ios-charts

我将这个lib用于drawChart。链接:https://github.com/danielgindi/Charts

这是我的代码。当我只绘制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")
}

请帮帮我:)

1 个答案:

答案 0 :(得分:1)

请查看图库中的ChartsDemo,有组合图表供您参考。