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:
public class ChartingDemoActivity extends Activity implements NChartSeriesDataSource {
NChartView mNChartView;
Random random = new Random();
NChartBrush[] brushes;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
mNChartView = (NChartView) findViewById(R.id.surface);
loadView();
}
private void loadView() {
// Paste your license key here.
mNChartView.getChart().setLicenseKey("");
// Margin
mNChartView.getChart().getPolarSystem().setMargin(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.setDataSource(this);
// Set tag of the series.
series.tag = i;
// Set brush that will fill that series with color.
series.setBrush(brushes[i % brushes.length]);
// Add series to the chart.
mNChartView.getChart().addSeries(series);
}
// Set hole ratio.
NChartPieSeriesSettings settings = new NChartPieSeriesSettings();
settings.setHoleRatio(0.1f);
mNChartView.getChart().addSeriesSettings(settings);
// Update data in the chart.
mNChartView.getChart().updateData();
}
protected void onResume() {
super.onResume();
mNChartView.onResume();
}
protected void onPause() {
super.onPause();
mNChartView.onPause();
}
// NChartSeriesDataSource
public NChartPoint[] points(NChartSeries series) {
// Create one point with some data for the series.
NChartPoint[] result = new NChartPoint[1];
result[0] = new NChartPoint(NChartPointState.PointStateWithCircleValue(0, random.nextInt(30) + 1), series);
return result;
}
public String name(NChartSeries series) {
return String.format("Series #%d", series.tag + 1);
}
public Bitmap image(NChartSeries series) {
return null;
}
public NChartPoint[] extraPoints(NChartSeries series) {
return null;
}
}
Comments
No comments yet.
Please log in to place a comment.