Achartengine XYMultipleSeriesRenderer renderes cha

2019-02-15 23:26发布

Image screen shot  is belowI have developed a bar chart using a chart engine, and I have used multiple series showing different colored bars on the basis of customisation done on the data.

But when there is null value for one bar , XYmultiseriesrenderer still renders the chart and creates undesired spaces between the bars.

private void openChart(){
            //int[] x = { 0,1,2,3,4,5,6,7,8,9};
            int[] income = { 230,220,230,250,270,300,280,350,370,400};
            int[] expense = {220, 270, 290, 280, 260, 300, 330, 340}; 
            int[] colors = {Color.GREEN,Color.YELLOW , Color.RED , Color.BLUE};

            XYSeries[] incomeSeries = addIncomeSeriestoDataSet(income);
            XYSeries expenseSeries = constructExpenseSeries(expense);
            // Creating a dataset to hold each series
            XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
            // Adding Income Series to the dataset
            for(int i= 0 ; i< incomeSeries.length ; i++) {
                dataset.addSeries(incomeSeries[i]);
            }
            //Adding Expense Series to the dataset
            dataset.addSeries(expenseSeries);

            // Creating a XYMultipleSeriesRenderer to customize the whole chart
            XYMultipleSeriesRenderer multiRenderer = buildRenderer(colors); 

            // Creating an intent to plot bar chart using dataset and multipleRenderer
            Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, 

    Type.DEFAULT);
      // Start Activity
            startActivity(intent);

        }private XYSeries[] addIncomeSeriestoDataSet(int[] income){
            XYSeries[] incomeSeries = {new XYSeries("Income<=250") , new XYSeries("Income<320") , new 

    XYSeries("Income>320")};
            // Adding data to Income series
            int i = 0;
            int sizeX = mMonth.length;
            int incomeItemsCount = income.length;

            while(i<sizeX && i<incomeItemsCount){
                if(income[i] <= 250) {
                    incomeSeries[0].add(i, income[i]);
                                } else if(income[i] <= 320) {

                    incomeSeries[1].add(i, income[i]);

                } else {

                incomeSeries[2].add(i, income[i]);
                }
               i++;
            }
            return incomeSeries;
        }

        private XYSeries constructExpenseSeries(int[] expense) {
            XYSeries expenseSeries = new XYSeries("Expense");
               int i=0;
               int sizeX = mMonth.length;
               int expenseItemsCount = expense.length;
               while(i<sizeX && i< expenseItemsCount) {
                    expenseSeries.add(i,expense[i]);
                    i++;
                }
              return expenseSeries; 
        }
        private XYMultipleSeriesRenderer buildRenderer(int[] colors) {
            XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
            renderer.setAxisTitleTextSize(16);
            renderer.setChartTitleTextSize(20);
            renderer.setLabelsTextSize(15);
            renderer.setLegendTextSize(15);
            renderer.setXLabels(0);
            renderer.setAxesColor(Color.BLUE);

            renderer.setXLabelsColor(Color.BLUE);
            renderer.setYLabelsColor(0,Color.BLUE);

            renderer.setXLabelsAlign(Align.CENTER);
            renderer.setChartTitle("Income vs Expense Chart");
            renderer.setXTitle("Year 2012");
            renderer.setYLabelsAlign(Align.RIGHT);
            renderer.setLabelsColor(Color.BLUE);
            renderer.setYTitle("Amount in Dollars");
            renderer.setBarSpacing(1.0);
            renderer.setMarginsColor(Color.WHITE);
            renderer.setApplyBackgroundColor(true);
            renderer.setBackgroundColor(Color.WHITE);
            renderer.setMargins(new int[] { 20, 30, 15, 0 });
            renderer.setZoomButtonsVisible(true);
            for(int i=0; i< mMonth.length ;i++){
                renderer.addXTextLabel(i, mMonth[i]);
            }
            int length = colors.length;
            for (int i = 0; i < length; i++) {
                XYSeriesRenderer r = new XYSeriesRenderer();
                r.setColor(colors[i]);
                r.setLineWidth(4);
                r.setFillPoints(true);
                r.setDisplayChartValues(true);
                renderer.addSeriesRenderer(r);
            }
            return renderer;

1条回答
我只想做你的唯一
2楼-- · 2019-02-16 00:06

You have a setBarSpacing(1.0) call in your code. Just remove that.

查看更多
登录 后发表回答