1

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;
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • show snapshot how it looks – Kiran May 03 '13 at 09:19
  • how to show image here in the comments??? – user2190720 May 03 '13 at 09:38
  • means edit your question and add images – Kiran May 03 '13 at 09:39
  • Hi Kiran, Its not allowing me to add an image saying you need 10 reputations to post an image, is there some other way i could show you the image? – user2190720 May 03 '13 at 09:50
  • mail to `kiran3545@gmail.com` will post them – Kiran May 03 '13 at 10:41
  • i have upvoted your questios now u can post i think – Kiran May 03 '13 at 10:44
  • Hey, is it visible to you now?? – user2190720 May 03 '13 at 10:52
  • ya , you mean u want to remove the gap between bars rite??? – Kiran May 03 '13 at 10:54
  • spacing between the bars is odd because, my first bar is showing three specific colors acc to values, like incomeseries1 representing the green for values <250 then icomeseries2(yellow) and incomeseries3(red) has null values but multiseries rendered is still render – user2190720 May 03 '13 at 10:57
  • rendering the empty bars and creating undesired spaces, its different than the setbarspacing thing. – user2190720 May 03 '13 at 10:58
  • As showm in the image above, there is a difference in the spacing b/w green and blue bar and yellow and blue bar, red and blue bar.. – user2190720 May 03 '13 at 10:59
  • Are you getting my question??? – user2190720 May 03 '13 at 10:59
  • @Dan: Can you help me out with this – user2190720 May 03 '13 at 11:11
  • I don't understand where the null value is? Is it a null, or a MathHelper.NULL_VALUE? – Dan D. May 03 '13 at 11:20
  • If you sincerely see the image , then you must have noticed that there is odd spacing b/w the green and blue, yellow and blue and red and blue...this is because when my first bar is showing red color (value<250) for first bar,(incomeseries1) other single series (incomeseries2,incomeseries3) are empty but XYmultiseries renders still renders the chart for them. How not to do that??? – user2190720 May 03 '13 at 11:25
  • @ Dan:: is there any API to change positon of bars in multi series bar graph, `s1,s2,s3` are series if we plot graph we get `s1,s2,s3` `s1,s2,s3` `s1,s2,s3` `s1,s2,s3` ....so on is there anyway to change it every time based on value to plot like this `s1,s3,s2` `s3,s2,s1` `s1,s2,s3` `s1,s3,s2` `s3,s1,s2` ... so on if so this problem can be solved – Kiran May 03 '13 at 12:20
  • @user2190720 just look at this link and read is carefully http://stackoverflow.com/questions/13757166/how-to-make-space-between-bars-in-bargraph-using-achartengine-if-each-bar-is-dif – Abhijit Chakra May 06 '13 at 12:19
  • @rickky: I have carefully gone through the link, But my issue is , I am rendering multiple series for one dataset of values(mentioned in the code), but if at one point other series have blank values, renderer still renders the chart, shows uneven spaces as in the chart shown above... – user2190720 May 06 '13 at 17:29
  • You have a setBarSpacing(1.0) call in your code. Just remove that thing. – Dan D. May 07 '13 at 08:54
  • @Kiran Downvoting will help you get answers to all your questions. – Dan D. May 07 '13 at 08:56

1 Answers1

0

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

Dan D.
  • 32,246
  • 5
  • 63
  • 79