1

我已经用最新的 jar 文件实现了条形图,但它没有正确显示

这是我的代码:

private void openChart(){
        int[] x = { 10,20,30,40,50,60,70 };
       // int[] y = { 10,20,30,40,50,60,70 };
        int[] income = { 20,25,27,30,28,35,37,38};
        int[] expense = {22, 27, 29, 28, 26, 30, 33, 34 };
        int[] sample = {22, 27, 29, 28, 26, 30, 33, 34 };

        // Creating an  XYSeries for Income
        XYSeries incomeSeries = new XYSeries("Income");
        // Creating an  XYSeries for Expense
        XYSeries expenseSeries = new XYSeries("Expense");
     // Creating an  XYSeries for Expense
        XYSeries sampleseries = new XYSeries("Sample");
        // 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]);
            sampleseries.add(i,sample[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);
        // Adding Expense Series to dataset
        dataset.addSeries(sampleseries);

        // Creating XYSeriesRenderer to customize incomeSeries
        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
        incomeRenderer.setColor(Color.rgb(130, 130, 230));
        incomeRenderer.setFillPoints(true);
        incomeRenderer.setLineWidth(2);
        incomeRenderer.setChartValuesTextAlign(Align.CENTER);
        incomeRenderer.setChartValuesTextSize(18);
        incomeRenderer.setDisplayChartValues(true);

        // Creating XYSeriesRenderer to customize expenseSeries
        XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
        expenseRenderer.setColor(Color.rgb(220, 80, 80));
        expenseRenderer.setFillPoints(true);
        expenseRenderer.setLineWidth(2);
        expenseRenderer.setChartValuesTextAlign(Align.CENTER);
        expenseRenderer.setChartValuesTextSize(18);
        expenseRenderer.setDisplayChartValues(true);

     // Creating XYSeriesRenderer to customize expenseSeries
        XYSeriesRenderer samplseries = new XYSeriesRenderer();
        samplseries.setColor(Color.rgb(120, 40, 40));
        samplseries.setFillPoints(true);
        samplseries.setLineWidth(2);
        samplseries.setChartValuesTextAlign(Align.CENTER);
        samplseries.setChartValuesTextSize(18);
        samplseries.setDisplayChartValues(true);


        // Creating a XYMultipleSeriesRenderer to customize the whole chart
        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setMargins(new int[] {30, 50, 50, 20});
        multiRenderer.setZoomRate(0.2f); // disable pinch to zoom
        multiRenderer.setZoomEnabled(false, false); // disable pinch to zoom
        multiRenderer.setBarSpacing(0.3f); // space between series
        multiRenderer.setXAxisMin(-1); // for x axis to start with some space
        multiRenderer.setXAxisMax(5);
        multiRenderer.setYAxisMin(0);
        multiRenderer.setYAxisMax(50);
        multiRenderer.setAxisTitleTextSize(20); // increase the text size of title of x and y
        multiRenderer.setAxesColor(Color.BLACK); // x and y line color
        multiRenderer.setGridColor(Color.GRAY); // grid line color
        multiRenderer.setShowGridX(true); // to show the grid line
        multiRenderer.setXLabels(0);
        multiRenderer.setPanEnabled(true, false);
        multiRenderer.setPanLimits(new double[] {-1,x.length,0,x.length});
       // multiRenderer.setYLabels(0);
      //  multiRenderer.setChartTitle("Income vs Expense Chart");
        multiRenderer.setLegendTextSize(26);
        multiRenderer.setLabelsColor(Color.BLACK); // changing the x,y title color
        multiRenderer.setLabelsTextSize(20); // changing the text size labels of x and y  

        multiRenderer.setXLabelsColor(Color.BLACK); // changing the color of x label
        multiRenderer.setXTitle("Tests"); // title for x
        multiRenderer.setYLabelsAlign(Align.RIGHT);// labels to display right
        multiRenderer.setYLabelsColor(0, Color.BLACK);// changing the color of y label
        multiRenderer.setYTitle("Scores");// title for y

        for(int i=0; i< x.length;i++){
            multiRenderer.addXTextLabel(i, mMonth[i]);
        }
        /*for(int i=0; i< x.length;i++){
            multiRenderer.addYTextLabel(i, mScores[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);
        multiRenderer.addSeriesRenderer(samplseries);

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

        // Start Activity
        startActivity(intent);

    }

这就是我的条形图的样子;

在此处输入图像描述

我哪里错了?

由于黑色背景,它不滚动,不显示 x 轴,y 轴值,而且我已经给出了平移限制,但没有任何工作

4

1 回答 1

2

好的,我自己解决了我错过了添加 multiRenderer.setMarginsColor(Color.WHITE);

于 2013-02-05T12:27:18.533 回答