1

我正在使用 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);

        }`

为此,我开发了下面编写的代码:

4

1 回答 1

0

Achartengine 库尚未实现或提供单个系列(或同一系列的元素)的不同颜色...:

但是有使用多个系列或堆叠条形图的方法

使用 achartengine线程检查条形图。使用多个系列的不同颜色条

更新: 对于堆栈条形图

假设您想要使用不同颜色的输入数据

   int[] expense = {50, 75, 100, 125, 150, 175, 200, 225 };

将不同的系列声明为数组列表

  static ArrayList<Integer> series1 = new ArrayList<Integer>();
  static ArrayList<Integer> series2 = new ArrayList<Integer>();
  static ArrayList<Integer> series3 = new ArrayList<Integer>();

现在检查输入中的数据并将其添加到适当的系列中

for(int i=0;i<expense.lenght;i++)
 { 
  if(expense[i]> 50 & expense[i]<100)
   { series1.add(expense[i]);
     series2.add(0);
    series3.add(0);}

if(expense[i]> 100 & expense[i]<150)
 {.......}
}
于 2013-05-01T15:31:56.863 回答