I have a donut chart using Shinobi. But depending on the data the labels overlap!!
Also one of the labels isn't being showed completely (Max Payment).
I have searched all over the stack overflow and couldn't find any solution for this. Also nothing about this issue in Shinobi website or in their documentations.
this is my Code:
import UIKit
class MyViewController: UIViewController, SChartDatasource {
var donutChartValues:[Double] = []
var donutChartLabels:[String] = []
@IBOutlet weak var chartView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
redrawChart()
}
func redrawChart() {
setDonutChartValues()
let chart = ShinobiChart(frame: chartView.bounds)
chart.autoresizingMask = [.flexibleHeight, .flexibleWidth]
chart.backgroundColor = UIColor.clear
chart.datasource = self
chartView.addSubview(chart)
}
func setDonutChartValues () {
donutChartValues.removeAll()
donutChartLabels.removeAll()
donutChartValues.append(2500.00)
donutChartLabels.append("Max Payment")
donutChartValues.append(300.0)
donutChartLabels.append("Property Tax")
donutChartValues.append(100.0)
donutChartLabels.append("Condo Fees")
donutChartValues.append(150.0)
donutChartLabels.append("Heating Costs")
donutChartValues.append(300.0)
donutChartLabels.append("Debts")
donutChartValues.append(4000.0)
donutChartLabels.append("Other Expenses")
}
/********************************************************************************/
// MARK: - SChartDatasource methods
/********************************************************************************/
func numberOfSeries(in chart: ShinobiChart) -> Int {
return 1
}
func sChart(_ chart: ShinobiChart, seriesAt index: Int) -> SChartSeries {
let donutSeries = SChartDonutSeries()
donutSeries.style().spokeStyle.showSpokes = true;
donutSeries.selectedStyle().spokeStyle.showSpokes = true;
donutSeries.style().labelFontColor = UIColor.black
donutSeries.selectedStyle().labelFontColor = UIColor.black
return donutSeries
}
func sChart(_ chart: ShinobiChart, numberOfDataPointsForSeriesAt seriesIndex: Int) -> Int {
return donutChartValues.count
}
func sChart(_ chart: ShinobiChart, dataPointAt dataIndex: Int, forSeriesAt seriesIndex: Int) -> SChartData {
let dp = SChartDataPoint()
dp.xValue = 0
dp.yValue = (donutChartValues.count > dataIndex) ? donutChartValues[dataIndex] : 0
return dp
}
func sChart(_ chart: ShinobiChart, labelForSliceAt sliceIndex: Int, in series: SChartRadialSeries) -> UILabel? {
let sliceLabel = UILabel()
sliceLabel.text = (donutChartLabels.count > sliceIndex) ? donutChartLabels[sliceIndex] : ""
return sliceLabel
}
}
Any help would be appreciated.
I believe what you want is the
sChart:alterLabel:forDatapoint:atSliceIndex:inRadialSeries:NS_SWIFT_NAME:
method of theSChartDelegate
protocol.According to the documentation for this method, it "gives you each label for each pie/donut series before it is added to the chart. Use this to set colors, borders, or reposition the label." (emphasis added).
This blog article gives an example of how to implement it.