How to Display AChartEngine bar chart properly

南楼画角 提交于 2019-12-04 05:07:07

Following your numbering scheme:

  1. You can fix this by setting the X axis min and max of the visible area: mRenderer.setXAxisMin(min); and mRenderer.setXAxisMax(max);
  2. You can reduce it by increasing the width or by using the above methods such as there is more space to the left and right.
  3. There is an opened issue for this.
  4. Try mRenderer.setXLabelsColor(color);
  5. renderer.setChartValuesTextSize(size);
  6. mRenderer.setXLabels(0);

Edit:

  1. Use renderer.setChartValuesTextSize(size);
  2. Same as #1.
  3. mRenderer.setYAxisMax(300);
  4. Known issue: http://code.google.com/p/achartengine/issues/detail?id=312

I discovered that sizes are not correctly density calculated.

If u want:

mRenderer.setLabelsTextSize(15); 
mRenderer.setLegendTextSize(15); 

You need to set:

mRenderer.setLabelsTextSize(15* getResources().getDisplayMetrics().density); 
mRenderer.setLegendTextSize(15* getResources().getDisplayMetrics().density); 

I encounter the same problem. This is because AChartegine is trying to allocate enough space for each renderer so make a shift. This should simply resolved by Type.STACKED

Change

ChartFactory.getBarChartView(context, dataset,mRenderer, Type.DEFAULT);

to

ChartFactory.getBarChartView(context, dataset,mRenderer, Type.STACKED);

Hope this helps!

   GraphicalView mChart = createIntent();
   layout_graph.addView(mChart);



public GraphicalView createIntent() {

        String[] titles = new String[] { " " };

        List<Double> values = new ArrayList<Double>();




        for (int i = 0; i < yaxes_value.size(); i++) {
//          values.add(Double.parseDouble(yaxes_value.get(i)));


        }


        int[] colors = new int[] { Color.parseColor("#4DBA7A") };
        XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
        setChartSettings(renderer, "Simple Bar Chart", X_title, Y_title+type,
                Color.BLACK, Color.BLACK,values);

        for (int i = 0; i < Xaxes_value.size(); i++) {
            renderer.addXTextLabel(i + 1, Xaxes_value.get(i));
        }

        int length = renderer.getSeriesRendererCount();
        for (int i = 0; i < length; i++) {
            SimpleSeriesRenderer seriesRenderer = renderer
                    .getSeriesRendererAt(i);
            // seriesRenderer.setDisplayChartValues(true);
        }

        final GraphicalView grfv = ChartFactory.getBarChartView(
                GraphActivity.this, buildBarDataset(titles, values), renderer,
                Type.DEFAULT);


        return grfv;
    }

    protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setAxisTitleTextSize(25);
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(20);
        renderer.setLegendTextSize(20);
        renderer.setPanEnabled(false, false);
        renderer.setPanEnabled(false);
        renderer.setZoomEnabled(false);
        renderer.setZoomEnabled(false, false);
        renderer.setBarSpacing(1f);
        renderer.setBarWidth(50f);
        renderer.setMarginsColor(Color.WHITE);
        renderer.setXLabelsColor(Color.BLACK);
        renderer.setYLabelsColor(0, Color.BLACK);
        renderer.setApplyBackgroundColor(true);
        renderer.setXLabelsAngle(335);

        int length = colors.length;
        for (int i = 0; i < length; i++) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(colors[i]);
            r.setChartValuesSpacing(10);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }

    protected XYMultipleSeriesDataset buildBarDataset(String[] titles,
            List<Double> values) {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int length = titles.length;
        for (int i = 0; i < length; i++) {
            CategorySeries series = new CategorySeries(titles[i]);
            for (int j = 0; j < values.size(); j++) {
                series.add(values.get(j));
            }
            dataset.addSeries(series.toXYSeries());
        }
        return dataset;
    }

    protected void setChartSettings(XYMultipleSeriesRenderer renderer,
            String title, String xTitle, String yTitle, int axesColor,
            int labelsColor,List<Double> values) {

        renderer.setChartTitle(title);
        renderer.setYLabelsAlign(Align.RIGHT);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(0);
        renderer.setXLabels(0);
        renderer.setXLabelsPadding(10);
        renderer.setYLabelsPadding(2);
        renderer.setYAxisMin(0);
        renderer.setMargins(new int[] { 40, 100, 40, 20 });
//      renderer.setYAxisMin((Collections.min(values)) / 2);
//      renderer.setYAxisMax((Collections.max(values))
//              + (Collections.min(values)));
        renderer.setXAxisMax(layout_addrow.getChildCount() + 1);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);

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