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 class ViewController : UIViewController, INChartSeriesDataSource
{
NChartView m_view;
Random m_rand;
public NChartSolidColorBrush[] brushes { get; set; }
public ViewController () : base ()
{
m_rand = new Random ();
// Create brushes.
brushes = new NChartSolidColorBrush[3];
brushes [0] = new NChartSolidColorBrush (UIColor.FromRGB (97, 206, 231));
brushes [1] = new NChartSolidColorBrush (UIColor.FromRGB (203, 220, 56));
brushes [2] = new NChartSolidColorBrush (UIColor.FromRGB (229, 74, 131));
}
public override void LoadView ()
{
// Create a chart view that will display the chart.
m_view = new NChartView ();
// Paste your license key here.
m_view.Chart.LicenseKey = "";
m_view.Chart.PolarSystem.Margin = new NChartMargin (10.0f, 10.0f, 10.0f, 20.0f);
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 = brushes[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 ();
// Set chart view to the controller.
this.View = m_view;
}
#region INChartSeriesDataSource
public NChartPoint [] Points (NChartSeries series)
{
// Create points with some data for the series.
NChartPoint[] result = new NChartPoint[1];
result [0] = new NChartPoint (NChartPointState.PointStateWithCircleValue (0, (m_rand.Next () % 30) + 1), series);
return result;
}
public string Name (NChartSeries series)
{
// Get name of the series.
return string.Format ("My series {0}", series.Tag + 1);
}
// If you don't want to implement method, return null.
public UIImage Image (NChartSeries series) { return null; }
public NChartPoint [] ExtraPoints (NChartSeries series) { return null; }
#endregion
}
Android version
public class MainActivity : Activity, INChartSeriesDataSource
{
NChartView mNChartView;
Random random = new Random ();
NChartBrush[] brushes;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
mNChartView = FindViewById (Resource.Id.surface);
LoadView ();
}
private void LoadView ()
{
// Paste your license key here.
mNChartView.Chart.LicenseKey = "";
// Margin
mNChartView.Chart.PolarSystem.Margin = new NChartMargin (10.0f, 10.0f, 10.0f, 40.0f);
// Create brushes.
brushes = new NChartBrush[3];
brushes [0] = new NChartSolidColorBrush (Color.Argb (255, (int)(255 * 0.38), (int)(255 * 0.8), (int)(255 * 0.92)));
brushes [1] = new NChartSolidColorBrush (Color.Argb (255, (int)(255 * 0.8), (int)(255 * 0.86), (int)(255 * 0.22)));
brushes [2] = new NChartSolidColorBrush (Color.Argb (255, (int)(255 * 0.9), (int)(255 * 0.29), (int)(255 * 0.51)));
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 = brushes [i % brushes.Length];
// Add series to the chart.
mNChartView.Chart.AddSeries (series);
}
// Set hole ratio.
NChartPieSeriesSettings settings = new NChartPieSeriesSettings ();
settings.HoleRatio = holeRatio;
mNChartView.Chart.AddSeriesSettings (settings);
// Update data in the chart.
mNChartView.Chart.UpdateData ();
}
protected override void OnResume ()
{
base.OnResume ();
mNChartView.OnResume ();
}
protected override void OnPause ()
{
base.OnPause ();
mNChartView.OnPause ();
}
#region INChartSeriesDataSource
public NChartPoint[] Points (NChartSeries series)
{
// Create points with some data for the series.
NChartPoint[] result = new NChartPoint[1];
result [0] = new NChartPoint (NChartPointState.PointStateWithCircleValue (0, random.Next (30) + 1), series);
return result;
}
public string Name (NChartSeries series)
{
return string.Format ("Series {0}", series.Tag + 1);
}
// If you don't want to implement method, return null.
public Bitmap Image (NChartSeries series) { return null; }
public NChartPoint [] ExtraPoints (NChartSeries series) { return null; }
#endregion
}
Comments
No comments yet.
Please log in to place a comment.