AChart Engine Bar Chart - hide the series when scrolling to right

一曲冷凌霜 提交于 2019-12-23 18:04:28

问题


I am using an excellent library AChart engine for generating bar chart. All features are working fine, but I have couple of things which I am not able to resolve:

this is my screen shot:

Now there are two things that needs to be fixed:

1) Hide the series when it is scrolled to right, currently the series March is visible even after 0,0 also.

2) Gridlines are on top of the graph but it should be behind the graph.

3) How to display the legends on top right corner instead of bottom.

here is my below code:

private void openChart(){
        int[] x = { 10,20,30,40,50,60,70,80 };
       // 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,60,0});
        multiRenderer.setXRoundedLabels(true);
        multiRenderer.setLegendTextSize(24);
        multiRenderer.setZoomRate(0.2f); 
        multiRenderer.setZoomEnabled(false, false); 
        multiRenderer.setBarSpacing(0.3f); 
        multiRenderer.setXAxisMin(-1); 
        multiRenderer.setXAxisMax(5);
        multiRenderer.setYAxisMin(0);
        multiRenderer.setYAxisMax(50);
        multiRenderer.setAxisTitleTextSize(20); 
        multiRenderer.setAxesColor(Color.BLACK); 
        multiRenderer.setGridColor(Color.GRAY); 
        multiRenderer.setShowGridX(true); 
        multiRenderer.setXLabels(0);

        multiRenderer.setPanLimits(new double[] { -1, x.length, 0, x.length});

        multiRenderer.setLabelsColor(Color.BLACK); 
        multiRenderer.setLabelsTextSize(20);  

        multiRenderer.setXLabelsColor(Color.BLACK); 
        multiRenderer.setXTitle("Tests"); 
        multiRenderer.setYLabelsAlign(Align.RIGHT);
        multiRenderer.setYLabelsColor(0, Color.BLACK);
        multiRenderer.setYTitle("Scores");

        for(int i=0; i< x.length;i++){
            multiRenderer.addXTextLabel(i, mMonth[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);

    }

回答1:


  1. renderer.setMarginsColor() and use the right version of ACE.
  2. Hide them.
  3. Hide it or let it the way it is.

Update: I have also added a new API call that will allow you to avoid the values that are partially displayed on the chart to be displayed.

renderer.setDisplayBoundingPoints(false);

You can download a version including this feature here.



来源:https://stackoverflow.com/questions/14700819/achart-engine-bar-chart-hide-the-series-when-scrolling-to-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!