我正在尝试使用文件中保存的值绘制折线图。我对AChartEngine网站提供的原始演示代码做了一些改动,以便应用我的。但是,有一个我无法理解的错误。请给我一些帮助。
主要代码有错误:'setChartSettings(...);'显示错误消息。(缺少许多语法错误和返回类型的方法)
public class WeightGraph extends AbstractDemoChart {
...
double[] values = getValue();
int color = Color.BLUE;
PointStyle style = PointStyle.CIRCLE;
XYMultipleSeriesRenderer renderer = buildRenderer(color, style);
setChartSettings(renderer, "Weight", "Time", "Weight(kg)", 0, 10, 30, 70, Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setZoomButtonsVisible(true);
Intent intent = ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values),
renderer, "Average temperature");
return intent;
}
/*AbstractDemoChart Class, which the code above has extended:*/
public abstract class AbstractDemoChart implements IDemoChart {
...
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
int labelsColor) {
renderer.setChartTitle(title);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
答案 0 :(得分:1)
您首先以WeightGraph创建新活动,并使用一些布局设计您的line.xml文件。然后使用以下代码。你会得到结果。
public class WeightGraph extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.line);
LinearLayout line_chart_layout;
line_chart_layout = findViewById(R.id.line_chart);
.................
double[] values = getValue();
int color = Color.BLUE;
PointStyle style = PointStyle.CIRCLE;
XYMultipleSeriesRenderer renderer = buildRenderer(color, style);
setChartSettings(renderer, "Weight", "Time", "Weight(kg)", 0, 10, 30, 70, Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setZoomButtonsVisible(true);
line_chart_layout.addView(ChartFactory.getLineChartIntent(context, ui ldDataset(titles, x, values), renderer, "Average temperature"));
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,int labelsColor)
{
renderer.setChartTitle(title);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
}
答案 1 :(得分:0)
ArrayList<Integer> z = new ArrayList<Integer>();
// int z[]={0,1,2,3,4,5,6,7};
List<Double> values_len = new ArrayList<Double>();
List<Double> values = new ArrayList<Double>();
XYSeries xSeries = new XYSeries("");
for (int i = 0; i < yaxes_value.size(); i++) {
values.add((double) Long.parseLong(yaxes_value.get(i)));
xSeries.add(i+1, values.get(i));
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(xSeries);
XYSeriesRenderer Xrenderer = new XYSeriesRenderer();
Xrenderer.setColor(Color.parseColor("#99CC33"));
Xrenderer.setPointStyle(PointStyle.CIRCLE);
Xrenderer.setShowLegendItem(true);
Xrenderer.setDisplayChartValues(false);
Xrenderer.setChartValuesSpacing(25);
Xrenderer.setChartValuesTextSize(25f);
// Xrenderer.setChartValuesTextAlign(Align.LEFT);
Xrenderer.setPointStrokeWidth(10);
Xrenderer.setDisplayChartValuesDistance(10);
Xrenderer.setLineWidth(2);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setChartTitle("Simple Line Chart");
mRenderer.setXTitle(X_title);
mRenderer.setYTitle(Y_title+type);
mRenderer.setMargins(new int[] { 50, 100, 40, 20 });// TOP,LEFT,BOTTOM,RIGHT
mRenderer.setYLabelsPadding(2);
mRenderer.setXLabelsPadding(15);
mRenderer.setXLabelsAngle(335);
mRenderer.setYAxisMin(0);
// mRenderer
// .setYAxisMax((Collections.max(values)) + (Collections.min(values)));
mRenderer.setXAxisMin(-1);
mRenderer.setLabelsTextSize(20);
mRenderer.setYLabelsAlign(Align.RIGHT);
mRenderer.setXLabelsAlign(Align.CENTER);
mRenderer.setChartValuesTextSize(20);
mRenderer.setDisplayValues(true);
mRenderer.setMarginsColor(Color.WHITE);
mRenderer.setLegendTextSize(20);
mRenderer.setAxesColor(Color.BLACK);
mRenderer.setYLabelsColor(0, Color.BLACK);
mRenderer.setXLabelsColor(Color.BLACK);
mRenderer.setLabelsColor(Color.BLACK);
mRenderer.setZoomButtonsVisible(false);
mRenderer.setChartTitleTextSize(20);
mRenderer.setAxisTitleTextSize(25);
mRenderer.setXLabels(0);
mRenderer.setYLabels(10);
mRenderer.setShowLabels(true);
mRenderer.setPanEnabled(false);
mRenderer.setPanEnabled(false, false);
mRenderer.setZoomEnabled(false, false);
mRenderer.setZoomEnabled(false);
mRenderer.setGridColor(Color.BLACK);
mRenderer.setShowGrid(true);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.WHITE);
for (int i = 0; i < Xaxes_value.size(); i++) {
mRenderer.addXTextLabel(i + 1, Xaxes_value.get(i));
}
for (int i = 0; i < z.size(); i++) {
mRenderer.addXTextLabel(i, Xaxes_value.get(i));
}
mRenderer.addSeriesRenderer(Xrenderer);
GraphicalView mChart = (GraphicalView) ChartFactory.getLineChartView(
getBaseContext(), dataset, mRenderer);
line_chart_layout.addView(mChart);