如果要显示任何其他元素(如 textview、imageview 等),则不能使用 getLineChartIntent。为此,您必须删除 getLineChartIntent 并且必须使用 getLineChartView。
private GraphicalView mChartView;
LinearLayout layout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout) findViewById(R.id.chart_layout);
}
@Override
protected void onResume() {
super.onResume();
if (mChartView == null) {
mChartView = execute(this);
layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}else {
mChartView.repaint();
}
}
public GraphicalView execute() {
titles = new String[] { "Crete", "Corfu" };
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < tit.length; i++) {
x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
values = new ArrayList<double[]>();
values.add(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 });
values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });
colors = new int[] { Color.BLUE, Color.GREEN};
styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND};
XYMultipleSeriesRenderer renderer = buildRenderer(col, styles);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
}
renderer.setXTitle("Month");
renderer.setYTitle("Temperature");
renderer.setXAxisMin(0.5);
renderer.setXAxisMax(12.5);
renderer.setYAxisMin(-10);
renderer.setYAxisMax(40);
renderer.setAxesColor(Color.BLACK);
renderer.setLabelsColor(Color.BLUE);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setGridColor(Color.RED);
renderer.setMarginsColor(Color.argb(0x00, 0x01, 0x01, 0x01));
renderer.setXLabelsColor(Color.WHITE);
renderer.setYLabelsColor(0, Color.WHITE);
renderer.setApplyBackgroundColor(true);
renderer.setBackgroundColor(Color.RED);
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
renderer.setInScroll(true);
XYMultipleSeriesDataset dataset = buildDataset(tit, x, values);
XYSeries series = dataset.getSeriesAt(0);
series.addAnnotation("Vacation", 6, 30);
//double maxX = series.getMaxX();
//Log.v("maxX", ""+maxX);
// double minX = maxX - 12; // deltaX is your required x-range
// Log.v("minX", ""+minX);
// double minY = series.getMinY();
// Log.v("minY", ""+minY);
// double maxY = series.getMaxY();
// Log.v("maxY", ""+maxY);
renderer.setRange(new double[] { 0, 15, 0, 32 });
//Intent intent = ChartFactory.getLineChartIntent(context, dataset, renderer, "Average temperature");
return ChartFactory.getLineChartView(this, dataset, renderer);
}
protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
List<double[]> yValues) {
for(int k = 0; k < titles.length; k++)
Log.v("titles--", ""+titles[k]);
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
addXYSeries(dataset, titles, xValues, yValues, 0);
return dataset;
}
public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
List<double[]> yValues, int scale) {
int length = titles.length;
for (int i = 0; i < length; i++) {
XYSeries series = new XYSeries(titles[i], scale);
double[] xV = xValues.get(i);
double[] yV = yValues.get(i);
int seriesLength = xV.length;
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
}