0

I am using this lib for drawChart. Link: https://github.com/danielgindi/Charts

THis is my code. When i draw only CandleChart, it is OK. But when I try to draw CombinedChart. I receiver this result. 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")
}

Please help me :)

1 Answer 1

1

Please take a look at the ChartsDemo which is part of the library, there is combined chart for your reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.