我正在使用 achartengine lib 开发条形图,它有两个单系列。其中一个系列应始终显示相同的颜色,而其他系列根据给定值显示不同的颜色
例如:
<200 绿色,200> x < 300 蓝色 >300 橙色
代码如下:`
public class ChartActivity extends Activity {
private String[] mMonth = new String[] {
"Jan", "Feb" , "Mar", "Apr", "May", "Jun",
"Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
Button btnChart = (Button) findViewById(R.id.btn_chart);
// Defining click event listener for the button btn_chart
OnClickListener clickListener = new OnClickListener() {
@Override
public void onClick(View v) {
// Draw the Income vs Expense Chart
openChart();
}
};
btnChart.setOnClickListener(clickListener);
}
private void openChart(){
int[] x = { 0,1,2,3,4,5,6,7 };
int[] income = { 25,50,75,100,125,150,175};
int[] expense = {50, 75, 100, 125, 150, 175, 200, 225 };
// Creating an XYSeries for Income
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Expense
XYSeries expenseSeries = new XYSeries("Expense");
// Adding data to Income and Expense Series
for(int i=0;i<x.length;i++){
incomeSeries.add(i,income[i]);
expenseSeries.add(i,expense[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
dataset.addSeries(expenseSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.BLUE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.RED);
expenseRenderer.setFillPoints(true);
expenseRenderer.setLineWidth(2);
expenseRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setAxesColor(Color.BLACK);
multiRenderer.setXLabelsAlign(Align.RIGHT);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setXTitle("Year 2012");
multiRenderer.setYTitle("Amount in Dollars");
multiRenderer.setBarSpacing(0.5);
multiRenderer.setMarginsColor(Color.WHITE);
multiRenderer.setApplyBackgroundColor(true);
multiRenderer.setBackgroundColor(Color.WHITE);
multiRenderer.setMargins(new int[] {20, 30, 15, 0});
multiRenderer.setZoomButtonsVisible(true);
for(int i=0; i< x.length;i++){
multiRenderer.addXTextLabel(i, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
// Creating an intent to plot bar chart using dataset and multipleRenderer
Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);
// Start Activity
startActivity(intent);
}`
为此,我开发了下面编写的代码: