To create pie chart with for example 3 sectors you need to:
- Create 3 objects of NChartPieSeries. Each one will represent a sector.
- Create data source that returns array with one point for each series. Actually you can return more points for each sector and this will be displayed as concentric circles.
To create doughnut you just need to set hole ratio to whatever you want.
Here is the code snippet to create simple doughnut chart with 3 sectors:
class ViewController: UIViewController, NChartSeriesDataSource {
// Create a chart view that will display the chart.
var m_view: NChartView = NChartView()
// Create brushes.
let m_brushes: [NChartSolidColorBrush] = [
NChartSolidColorBrush(color: UIColor(red: 0.38, green: 0.8, blue: 0.91, alpha: 1.0)),
NChartSolidColorBrush(color: UIColor(red: 0.8, green: 0.86, blue: 0.22, alpha: 1.0)),
NChartSolidColorBrush(color: UIColor(red: 0.9, green: 0.29, blue: 0.51, alpha: 1.0))
]
override func loadView() {
// Paste your license key here.
m_view.chart.licenseKey = ""
// Margin to ensure some free space for the iOS status bar.
m_view.chart.polarSystem.margin = NChartMarginMake(10.0, 10.0, 10.0, 20.0)
for i in 0..<3 {
// Create series that will be displayed on the chart.
let series = NChartPieSeries()
// Set data source for the series.
series.dataSource = self
// Set tag of the series.
series.tag = i
// Set brush that will fill that series with color.
series.brush = m_brushes[i]
// Add series to the chart.
m_view.chart.addSeries(series)
}
// Set hole ratio.
let settings = NChartPieSeriesSettings()
settings.holeRatio = 0.1
m_view.chart.add(settings)
// Update data in the chart.
m_view.chart.updateData()
// Set chart view to the controller.
self.view = m_view
}
// MARK: NChartSeriesDataSource
func seriesDataSourcePoints(for series: NChartSeries!) -> [Any]! {
// Create points with some data for the series.
return [NChartPoint(state: NChartPointState(circle: 0, value: Double((arc4random() % 30) + 1)),
for: series)]
}
func seriesDataSourceName(for series: NChartSeries!) -> String! {
// Get name of the series.
return "My series \(series.tag)"
}
}
Comments
No comments yet.
Please log in to place a comment.