AchartEngine:调整Y轴并增加图例大小?

时间:2014-08-19 12:37:58

标签: android achartengine

如何调整AchartEngine图表以执行以下操作:

  1. 移回Y-axis标题,使其不会侵入图表区域(填充?)

  2. 增加legend的尺寸并将其垂直向下移动远离图表?

  3. 当前图表的图片:

    enter image description here

    与图表相关的代码:

    public class LineGraph4 {
    
        //declaring instance of context
        protected Context context;
    
        public Intent getIntent(Context context){
    
            DatabaseHelper db = new DatabaseHelper(context);
    
    
    
            /**
             * Series 1
             */
            //trying to pull Scores data from SQLite database for graphs 
            List<Integer> l= db.getAllGameScores();
    
            //creating y axis array of values by converting list to array.
            int[] y=convertIntegers(l);
    
            //creating x axis array of values dynamically
            int totalGames= db.getTotalGamesPlayed();
            int[] x = new int[totalGames];
    
            for(int i=0; i<x.length; i++){
    
                x[i]=i;
    
    
            }
            //creating and naming the line
            TimeSeries series= new TimeSeries("Score");
    
            //adding both the x and y values to the series
            for(int i=0; i <x.length; i++){
    
                series.add(x[i], y[i]);
            }
    
    
            /**
             * Series 2
             */
            //trying to pull Scores data from SQLite database for graphs 
            List<Integer> l2= db.getAllAverageMeditationLevels();
    
            //creating y axis array of values by converting list to array.
            int[] y2=convertIntegers(l2);
    
            //creating x axis array of values dynamically
            int totalGames2= db.getTotalGamesPlayed();
            int[] x2 = new int[totalGames2];
    
            for(int i=0; i<x2.length; i++){
    
                x2[i]=i;
    
    
            }
            //creating and naming the line
            TimeSeries series2= new TimeSeries("Avg meditation");
    
            //adding both the x and y values to the series
            for(int i=0; i <x2.length; i++){
    
                series2.add(x2[i], y2[i]);
            }
    
    
            /**
             * Series 3
             */
            //trying to pull Scores data from SQLite database for graphs 
            List<Integer> l3= db.getAllAverageAttentionLevels();
    
            //creating y axis array of values by converting list to array.
            int[] y3=convertIntegers(l3);
    
            //creating x axis array of values dynamically
            int totalGames3= db.getTotalGamesPlayed();
            int[] x3 = new int[totalGames3];
    
            for(int i=0; i<x3.length; i++){
    
                x3[i]=i;
    
    
            }
            //creating and naming the line
            TimeSeries series3= new TimeSeries("Avg Attention");
    
            //adding both the x and y values to the series
            for(int i=0; i <x3.length; i++){
    
                series3.add(x3[i], y3[i]);
            }
    
    
    
    
            //declaring the Multiple series dataset that the single data sets are added to
            XYMultipleSeriesDataset dataset= new XYMultipleSeriesDataset();
            //adding multiple series 
            dataset.addSeries(series);
            dataset.addSeries(series2);
            dataset.addSeries(series3);
    
    
            //renderer that changes the look etc of the first line (score)
            XYSeriesRenderer renderer= new XYSeriesRenderer();
            renderer.setColor(Color.YELLOW);//set line colour
            renderer.setPointStyle(PointStyle.SQUARE);
            renderer.setFillPoints(true);
    
            //renderer that changes the look etc of the second line (score)
            XYSeriesRenderer renderer2= new XYSeriesRenderer();
            renderer2.setColor(Color.WHITE);//set line colour
            renderer2.setPointStyle(PointStyle.SQUARE);
            renderer2.setFillPoints(true);
    
            // renderer that changes the look etc of the second line (score)
            XYSeriesRenderer renderer3 = new XYSeriesRenderer();
            renderer3.setColor(Color.RED);// set line colour
            renderer3.setPointStyle(PointStyle.SQUARE);
            renderer3.setFillPoints(true);
    
    
            //Only related to the display of the graphs x and y values
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            float val = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, metrics);
    
    
            //renderer that changes the look etc of the overall graph
            XYMultipleSeriesRenderer multRenderer= new XYMultipleSeriesRenderer();
            //adding single to multiple
            multRenderer.addSeriesRenderer(renderer);
            multRenderer.addSeriesRenderer(renderer2);
            multRenderer.addSeriesRenderer(renderer3);
            multRenderer.setApplyBackgroundColor(true);
            multRenderer.setBackgroundColor(Color.BLACK);
            multRenderer.setLabelsTextSize(val);
            multRenderer.setChartTitle("");
            multRenderer.setChartTitleTextSize(40);
            multRenderer.setXTitle("Games");
            multRenderer.setYTitle("Value");
            multRenderer.setLabelsTextSize(25); //sets size of numbers of axis
            //setting size of text for labels, legend etc
            multRenderer.setChartValuesTextSize(22); 
            multRenderer.setDisplayChartValues(true);
            multRenderer.setAxisTitleTextSize(val);
    
            //creating the intent
            Intent intent= ChartFactory.getLineChartIntent(context, dataset, multRenderer, "Summary Graph");
    
            return intent;
    
        }
    
        /**
         * Mehthod that converts a list
         * of integers to an array of lists
         * @param integers
         * @return
         */
        public static int[] convertIntegers(List<Integer> integers)
        {
            int[] ret = new int[integers.size()];
            for (int i=0; i < ret.length; i++)
            {
                ret[i] = integers.get(i).intValue();
            }
            return ret;
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

对于第一个问题,您可以使用renderer.setMargins(new int[] {top, left, bottom, right})。在您的情况下,您将不得不增加左边距。

对于第二个问题,请使用renderer.setLegendHeight(heightInPixels)