Jess Murray
Jess Murray

Reputation: 1339

Using Charts. Swift 2.2

I am very new to Charts so please bear with me! I have installed the 'Charts' CocoaPod (ByDaniel Cohen Gindi and Philipp Jahoda) and so far i have the following:

enter image description here

And have the following code:

class AnalysisViewController: UIViewController, ChartViewDelegate {

@IBOutlet weak var label4: UILabel!
@IBOutlet weak var label3: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var barChartView: BarChartView!

var testName : [String]!

func setChart(dataPoints: [String], values: [Double]) {
    barChartView.descriptionText = ""
    barChartView.noDataText = "You need to provide data for the chart."

    var dataEntries: [BarChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: nil)
    let chartData = BarChartData(xVals: testName, dataSet: chartDataSet)
    barChartView.data = chartData
    chartData.setDrawValues(false)


    chartDataSet.colors = [UIColor(red: 250/255, green: 16/255, blue: 34/255, alpha: 1), UIColor(red: 235/255, green: 166/255, blue: 134/255, alpha: 1),UIColor(red: 135/255, green: 66/255, blue: 255/255, alpha: 1), UIColor(red: 35/255, green: 11/255, blue: 15/255, alpha: 1)]
    barChartView.xAxis.labelPosition = .Bottom
}


func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: ChartHighlight) {
    print("\(entry.value) in \(months[entry.xIndex])")

    //need to set chartData.setDrawValues(true)
}

func setLabels() {
    label1.text = testName[0]
    label2.text = testName[1]
    label3.text = testName[2]
    label4.text = testName[3]

    label1.textColor = UIColor(red: 250/255, green: 16/255, blue: 34/255, alpha: 1)
    label2.textColor = UIColor(red: 235/255, green: 166/255, blue: 134/255, alpha: 1)
    label3.textColor = UIColor(red: 135/255, green: 66/255, blue: 255/255, alpha: 1)
    label4.textColor = UIColor(red: 35/255, green: 11/255, blue: 15/255, alpha: 1)
}

override func viewDidLoad() {
 barChartView.delegate = self

 testName = ["Food", "Entertainment", "Misc", "Clothes"]

let unitsSold = [20.0, 4.0, 6.0, 3.0]


    setChart(testName, values: unitsSold)
    setLabels()
}

I was wondering about the following:

  1. How can i remove the x-axis labels?
  2. How can i remove the "Key" (coloured squares) as i am aiming to produce a table underneath the chart but using labels for this purpose.
  3. When you select a value on the chart display that values numerical value. I tried to set the ChartDataEntry's setDraw value however this did not work " ChartDataEntry.setDrawValues(true)".
  4. Can you set limits per value rather than the whole chart? Similar to what you see on Box Plots.
  5. How do i remove the y-axis on the right hand side?

Apologies if this is not the correct place to ask these questions. I am new and have been struggling to find the answers else where.

Thanks,

Jess

Upvotes: 0

Views: 517

Answers (1)

Wingzero
Wingzero

Reputation: 9754

If you are new, please walk through ChartsDemo to get yourself familiar with it.

How can i remove the x-axis labels? Use xAxis.drawLabelsEnabled = false

How can i remove the "Key" (coloured squares)? Use chartView.legend.enabled = false

When you select a value on the chart display that values numerical value? Use ChartMarker

Can you set limits per value rather than the whole chart? Similar to what you see on Box Plots. Don't know what are you talking about

How do i remove the y-axis on the right hand side? Use chartView.rightAxis.enabled = false

Upvotes: 1

Related Questions