AndroidPieChart is a tool that you can generate a fancy pie chart easily on your android app.
AndroidPieChart needs to depend on the following environment:
1. Java 1.6 or above
2. Eclipse
3. Android SDK
Please clone this lib to your workspace first.
git clone https://github.com/Ken-Yang/AndroidPieChart.git
File->Import->Existing Projects into Workspace
Right Click on your project -> Properties -> Android -> Add -> <<Select AndroidPieChart>> -> OK
<net.kenyang.piechart.PieChart
android:id="@+id/pieChart"
android:layout_width="match_parent"
android:layout_height="match_parent">
</net.kenyang.piechart.PieChart>
final PieChart pie = (PieChart) findViewById(R.id.pieChart);
Please ensure your total data percentage is equal to 100%, otherwise the lib will throw Exception and show nothing.
ArrayList<Float> alPercentage = new ArrayList<Float>();
alPercentage.add(10.0f);
alPercentage.add(20.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.0f);
alPercentage.add(10.85f);
alPercentage.add(9.15f);
try {
// setting data
pie.setAdapter(alPercentage);
// setting a listener
pie.setOnSelectedListener(new OnSelectedLisenter() {
@Override
public void onSelected(int iSelectedIndex) {
Toast.makeText(ChartView.this, "Select index:" + iSelectedIndex, Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
if (e.getMessage().equals(PieChart.ERROR_NOT_EQUAL_TO_100)){
Log.e("kenyang","percentage is not equal to 100");
}
}
Copyright 2013 Ken Yang
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.