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:
iOS version
public sealed partial class MainPage : Page, INChartSeriesDataSource
{
Random m_rand = new Random();
public MainPage()
{
// m_view is NChartView variable from page XAML
// Paste your license key here.
m_view.Chart.LicenseKey = "";
// Create brushes.
Color[] colors = new Color[] { Colors.Aqua, Colors.Yellow, Colors.OrangeRed, };
for (int i = 0; i < 3; ++i) {
// Create series that will be displayed on the chart.
NChartPieSeries series = new NChartPieSeries();
// Set data source for the series.
series.DataSource = this;
// Set tag of the series.
series.Tag = i;
// Set brush that will fill that series with color.
series.Brush = new NChartSolidColorBrush(colors[i]);
// Add series to the chart.
m_view.Chart.AddSeries(series);
}
// Set hole ratio.
NChartPieSeriesSettings settings = new NChartPieSeriesSettings();
settings.HoleRatio = holeRatio;
m_view.Chart.AddSeriesSettings (settings);
// Update data in the chart.
m_view.Chart.UpdateData();
}
#region INChartSeriesDataSource
IReadOnlyList INChartSeriesDataSource.Points(INChartSeries series)
{
// Create points with some data for the series.
return new List() {
new NChartPoint(NChartPointState.PointStateWithCircleValue(0, m_rand.Next(30) + 1), series)
};
}
string INChartSeriesDataSource.Name(INChartSeries series)
{
// Get name of the series.
return string.Format("Series {0}", series.Tag);
}
// If you don't want to implement method, return null.
NChartRawBitmap INChartSeriesDataSource.Image(INChartSeries series) { return null; }
IReadOnlyList INChartSeriesDataSource.ExtraPoints (INChartSeries series) { return null; }
#endregion
}
Comments
No comments yet.
Please log in to place a comment.