我正在使用achartengine实现水平条形图,它会在图表区域中正确显示但是当我点击任何条形图时它不会给我所选条形图的值。系列视图将为null。请在下面找到以下代码建议我做错了。
private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int[] x = { 0, 1, 2, 3, 4, 5, 6, 7 };
int[] income = { 100, 200, 370, 450, 500, 600, 700, 800 };
public static final String TYPE = "type";
private XYMultipleSeriesDataset mDataset = getDemoDataset();
private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
private GraphicalView mChartView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xy_chart);
if (mChartView == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
mChartView = ChartFactory.getBarChartView(this, mDataset,
mRenderer, Type.DEFAULT);
mRenderer.setSelectableBuffer(100);
layout.addView(mChartView, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
mChartView.repaint();
}
mChartView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// handle the click event on the chart
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(NewTestHorizontal.this, "No chart element", Toast.LENGTH_SHORT).show();
} else {
// display information of the clicked point
Toast.makeText(
NewTestHorizontal.this,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y="
+ seriesSelection.getValue(), Toast.LENGTH_SHORT).show();
}
}
});
}
private XYMultipleSeriesRenderer getDemoRenderer() {
// TODO Auto-generated method stub
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setChartValuesSpacing(15);
incomeRenderer.setDisplayChartValues(true);
incomeRenderer.setChartValuesTextAlign(Align.CENTER);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setOrientation(Orientation.VERTICAL);
multiRenderer.setXLabels(1);
multiRenderer.setYLabels(0);
multiRenderer.setBarSpacing(0.9f);
multiRenderer.setBarWidth(30);
multiRenderer.setChartTitle("Income Chart");
multiRenderer.setXTitle("Year 2013");
multiRenderer.setYTitle("Amount in Dollars");
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setInScroll(true);
multiRenderer.setShowGridY(true);
//multiRenderer.setXLabelsPadding(10);
multiRenderer.setYLabelsPadding(10);
multiRenderer.setInScroll(true);
multiRenderer.setXAxisMin(-0.5);
multiRenderer.setXAxisMax(7);
multiRenderer.setYAxisMin(10);
multiRenderer.setChartTitleTextSize(15);
multiRenderer.setLabelsTextSize(10);
multiRenderer.setLegendTextSize(15);
multiRenderer.setAxisTitleTextSize(15);
multiRenderer.setMargins(new int[] {10, 20, 20, 10});
multiRenderer.setYAxisMax(3000);
multiRenderer.setClickEnabled(true);
multiRenderer.setShowGridX(true);
multiRenderer.setShowGridY(true);
for (int i = 0; i < x.length; i++) {
multiRenderer.addXTextLabel(i, mMonth[i]);
}
multiRenderer.addSeriesRenderer(incomeRenderer);
return multiRenderer;
}
private XYMultipleSeriesDataset getDemoDataset() {
XYSeries incomeSeries = new XYSeries("Income");
for (int i = 0; i < x.length; i++) {
incomeSeries.add(i, income[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(incomeSeries);
return dataset;
}
}