我正在尝试将 Achart 引擎集成到我的活动类中以显示折线图,但问题是我必须在图表活动上制作菜单。而且我无权访问该活动。我正在使用的代码如下。
public class AchartActivity extends Activity implements OnClickListener
{
Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double[] a1 = new double[] { 18, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 };
double[] a2 = new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,13.9 };
showChart(a1,a2);
}
void showChart(double[] a1,double[] a2 )
{
String[] titles = new String[] { "Crete", "Corfu" };
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < titles.length; i++) {
x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
List<double[]> values = new ArrayList<double[]>();
values.add(a1);
values.add(a2);
int[] colors = new int[] { Color.BLUE, Color.GREEN};
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND,
};
XYMultipleSeriesRenderer renderer;
renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
renderer.setPointSize(5f);
renderer.setMargins(new int[] { 20, 30, 15, 20 });
int length = colors.length;
for (int i = 0; i < length; i++) {
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(colors[i]);
r.setPointStyle(styles[i]);
renderer.addSeriesRenderer(r);
}
int length1 = renderer.getSeriesRendererCount();
for (int i = 0; i < length1; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
}
renderer.setChartTitle("Average temperature");
renderer.setXTitle("Month");
renderer.setYTitle("Temperature(degree celcius)");
renderer.setXAxisMin(0.5);
renderer.setXAxisMax(12.5);
renderer.setYAxisMin(-10);
renderer.setYAxisMax( 40);
renderer.setAxesColor(Color.LTGRAY);
renderer.setLabelsColor(Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
XYMultipleSeriesDataset dataset=null;
dataset = new XYMultipleSeriesDataset();
int length2 = titles.length;
for (int i = 0; i < length2; i++) {
XYSeries series = new XYSeries(titles[i], 0);
double[] xV = x.get(i);
double[] yV = values.get(i);
int seriesLength = xV.length;
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
Intent intent = ChartFactory.getLineChartIntent(this, dataset,
renderer, "Average temperature");
startActivity(intent);
}
}