我使用AChartEngine制作了一个硬编码输入饼图。当我测试它时,我得到了这个输出(图片的URL):http://postimg.org/image/xd1tbjssj/
产生这个的代码:
import android.content.Context;
import android.graphics.Color;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
public class PieChart {
// colors to be used for the pie slices
private static int[] colors = new int[]{
Color.GREEN, Color.BLUE, Color.MAGENTA, Color.BLUE
};
private Context context;
private CategorySeries dataset;
private DefaultRenderer renderer;
public PieChart(Context context) {
this.context = context;
}
public GraphicalView getChart() {
createDataSet();
createRenderer();
return ChartFactory.getPieChartView(context, dataset, renderer);
}
/**
* Create the data set used by the pie chart by adding
* a string representation and it's double value to a CategorySeries object
*/
private void createDataSet() {
String[] movies = new String[]{
"Terminator", "Eredet", "A Wall Street farkasa"
};
double[] values = new double[]{
5, 2, 3
};
dataset = new CategorySeries("");
for (int i = 0; i < values.length; i++) {
dataset.add(movies[i], values[i]);
}
}
/**
* Creates the renderer for the pie chart with the basic color scheme
*/
private void createRenderer() {
renderer = new DefaultRenderer();
for (int i=0; i<dataset.getItemCount(); i++) {
SimpleSeriesRenderer simpleSeriesRenderer = new SimpleSeriesRenderer();
simpleSeriesRenderer.setColor(colors[i]);
renderer.addSeriesRenderer(simpleSeriesRenderer);
}
renderer.setStartAngle(180);
renderer.setShowLabels(true);
renderer.setShowLegend(false);
}
}
上面使用这个类的函数:
private void displayChart() {
if (mChartView == null) {
chartContainer.removeAllViews();
PieChart chart = new PieChart(CinemaActivity.this);
mChartView = chart.getChart();
chartContainer.addView(mChartView);
} else {
mChartView.repaint();
}
}
有人遇到过这个问题吗? 提前谢谢!
我使用displayChart()方法的扩展代码(不相关的部分已删除):
private LinearLayout chartContainer;
private GraphicalView mChartView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check login status in database
if (userFunctions.getinstance().isUserLoggedIn(getApplicationContext())) {
// user already logged in show database
prepareActionBar();
setLayout();
initViews();
getActions();
} else {
// user is not logged in show login screen
}
}
private void setLayout() {
setContentView(R.layout.activity_cinema_dashboard);
}
private void initViews() {
incomeTextView = (TextView) findViewById(R.id.income);
content = (LinearLayout) findViewById(R.id.cinema_dashboard);
chartContainer = (LinearLayout) findViewById(R.id.chart);
progress = (ProgressBar) findViewById(R.id.cinema_dashboard_progress);
}
private void getActions() {
displayChart();
new GetIncome(sourceActivity.toString()).execute();
}
XML布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cinema_dashboard"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_medium"
android:layout_marginRight="@dimen/margin_medium"
android:layout_marginBottom="@dimen/margin_medium"
android:layout_marginLeft="@dimen/margin_medium">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView/>
<TextView/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/chart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="@dimen/margin_medium"
android:layout_marginRight="@dimen/margin_medium"
android:layout_marginBottom="@dimen/margin_medium"
android:layout_marginLeft="@dimen/margin_medium">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button/>
<Button/>
<Button/>
</LinearLayout>
</LinearLayout>
<ProgressBar />
</RelativeLayout>
答案 0 :(得分:0)
这看起来像是将图表放在某个容器组合中时发生的显示问题。为了解决这个问题,你可以这样做:
renderer.setInScroll(true);