其他分享
首页 > 其他分享> > android – Achartengine XYMultipleSeriesRenderer渲染空值的图表并显示不需要的间距b / .w条

android – Achartengine XYMultipleSeriesRenderer渲染空值的图表并显示不需要的间距b / .w条

作者:互联网

我使用图表引擎开发了一个条形图,并且我根据对数据进行的自定义使用了多个显示不同颜色条的系列.

但是当一个条形的值为空时,XYmultiseriesrenderer仍然会渲染图表并在条形图之间创建不需要的空格.

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;

解决方法:

您的代码中有一个setBarSpacing(1.0)调用.只需删除它.

标签:achartengine,android
来源: https://codeday.me/bug/20191007/1866763.html