棒グラフでユーザーが選択した特定の棒の色を変更する必要があります。ユーザーがバーを選択するたびに、そのバーの色を変更したいのですが、ユーザーがバーの選択を変更すると、最後に選択したバーがデフォルトの色として表示されます。
注:ある投稿で、選択したバーを含むシリーズをもう 1 つ使用する必要があることを確認しました。しかし、それを理解するにはサンプルコードが必要です。
private String[] mMonth = new String[] { "January", "Feburary", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
int[] x = { 0, 1, 2, 3, 4, 5, 6, 7 };
int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 };
public static final String TYPE = "type";
private XYMultipleSeriesDataset mDataset = getDemoDataset();
private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
private GraphicalView mChartView;
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(NewTest.this, "No chart element", Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i < x.length; i++) {
SimpleSeriesRenderer renderer = mRenderer.getSeriesRendererAt(seriesSelection.getSeriesIndex());
renderer.setColor(Color.RED);
}
mChartView.repaint();
// display information of the clicked point
Toast.makeText(
NewTest.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 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;
}
private XYMultipleSeriesRenderer getDemoRenderer() {
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setChartValuesTextAlign(Align.CENTER);
incomeRenderer.setChartValuesSpacing(15);
incomeRenderer.setDisplayChartValues(true);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setYLabelsPadding(15);
multiRenderer.setBarSpacing(0.3f);
multiRenderer.setBarWidth(30);
multiRenderer.setChartTitle("Income Chart");
multiRenderer.setXTitle("Year 2013");
multiRenderer.setYTitle("Amount in Dollars");
//multiRenderer.setZoomLimits(1.0,0.7,1.0,3000.0);
multiRenderer.setZoomButtonsVisible(true);
//multiRenderer.setZoomEnabled(true, true);
//multiRenderer.setPanEnabled(true, true);
multiRenderer.setInScroll(true);
multiRenderer.setXAxisMin(-0.5);
multiRenderer.setXAxisMax(7);
multiRenderer.setYAxisMin(0);
multiRenderer.setYAxisMax(4000);
multiRenderer.setMargins(new int[] {30, 50, 20, 10});
multiRenderer.setClickEnabled(true);
multiRenderer.setShowGridX(true);
for (int i = 0; i < x.length; i++) {
multiRenderer.addXTextLabel(i, mMonth[i]);
}
multiRenderer.addSeriesRenderer(incomeRenderer);
return multiRenderer;
}
}