Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Display AChartEngine bar chart properly

I am using achartengine 1.0.1 (latest) and want to demonstrate a bar chart.

This is what I want:

My Desired Graph

enter image description here

What I achieved so far

1:1 view: enter image description here

Zoom-out View:

enter image description here

Problems:

  1. the left and right portion of graph are lost in 1:1 view.
  2. The distance between Bar is so much high. I need to reduce it.
  3. The Bar is not appropriately above the text label.
  4. The text label color is white. I tried setLabelColor() but not working.
  5. The text-size above bar is small.
  6. XAxis Text "AC", "WA" etc are overlapping with 1, 2, 3. I want to remove 1, 2, 3 and only show "AC", "WA" etc.

Do you know how to fix these problems? I need a graph approximately like my desired graph. Thanks in advance.

Edit:

This is my trying code:

    public GraphicalView getView(Context context) 
    {   
        // Bar 1

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        XYSeries ac = new XYSeries("AC");
        XYSeries pe = new XYSeries("PE");
        XYSeries wa = new XYSeries("WA");
        XYSeries tl = new XYSeries("TL");
        XYSeries ml = new XYSeries("ML");
        XYSeries ce = new XYSeries("CE");
        XYSeries re = new XYSeries("RE");
        XYSeries ot = new XYSeries("OT");
        ac.add(1, 291);
        pe.add(2, 11);
        wa.add(3, 204);
        tl.add(4, 28);
        ml.add(5, 0);
        ce.add(6, 21);
        re.add(7, 51);
        ot.add(8, 4);
        dataset.addSeries(ac);
        dataset.addSeries(pe);
        dataset.addSeries(wa);
        dataset.addSeries(tl);
        dataset.addSeries(ml);
        dataset.addSeries(ce);
        dataset.addSeries(re);
        dataset.addSeries(ot);


        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        mRenderer.setChartTitle("Submission Statistics");
        mRenderer.setXTitle("Verdict Code");
        mRenderer.setYTitle("No. of Submissions");
        mRenderer.setAxesColor(Color.BLACK);
        mRenderer.setLabelsColor(Color.BLACK);
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.WHITE);
        mRenderer.setMarginsColor(Color.WHITE);
        mRenderer.setZoomEnabled(true);
        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setBarSpacing(-0.5);
//      mRenderer.setMargins(new int[] {20, 30, 15, 0});
        mRenderer.setAxisTitleTextSize(16);
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setLabelsTextSize(15);
        mRenderer.setLegendTextSize(15);
        mRenderer.addXTextLabel(1, "AC");
        mRenderer.addXTextLabel(2, "PE");
        mRenderer.addXTextLabel(3, "WA");
        mRenderer.addXTextLabel(4, "TL");
        mRenderer.addXTextLabel(5, "ML");
        mRenderer.addXTextLabel(6, "CE");
        mRenderer.addXTextLabel(7, "RE");
        mRenderer.addXTextLabel(8, "OT");
        mRenderer.setBarWidth(50);
//      mRenderer.setXAxisMax(9);
        mRenderer.setXAxisMin(0);
        mRenderer.setYAxisMin(0);

        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setColor(Color.parseColor("#00AA00"));
        renderer.setDisplayChartValues(true);



        XYSeriesRenderer renderer2 = new XYSeriesRenderer();
        renderer2.setColor(Color.parseColor("#666600"));
        renderer2.setDisplayChartValues(true);

        XYSeriesRenderer renderer3 = new XYSeriesRenderer();
        renderer3.setColor(Color.parseColor("#FF0000"));
        renderer3.setDisplayChartValues(true);

        XYSeriesRenderer renderer4 = new XYSeriesRenderer();
        renderer4.setColor(Color.parseColor("#0000FF"));
        renderer4.setDisplayChartValues(true);

        XYSeriesRenderer renderer5 = new XYSeriesRenderer();
        renderer5.setColor(Color.parseColor("#6767D0"));
        renderer5.setDisplayChartValues(true);

        XYSeriesRenderer renderer6 = new XYSeriesRenderer();
        renderer6.setColor(Color.parseColor("#AAAA00"));
        renderer6.setDisplayChartValues(true);

        XYSeriesRenderer renderer7 = new XYSeriesRenderer();
        renderer7.setColor(Color.parseColor("#00AAAA"));
        renderer7.setDisplayChartValues(true);

        XYSeriesRenderer renderer8 = new XYSeriesRenderer();
        renderer8.setColor(Color.parseColor("#000000"));
        renderer8.setDisplayChartValues(true);


        mRenderer.addSeriesRenderer(renderer);
        mRenderer.addSeriesRenderer(renderer2);
        mRenderer.addSeriesRenderer(renderer3);
        mRenderer.addSeriesRenderer(renderer4);
        mRenderer.addSeriesRenderer(renderer5);
        mRenderer.addSeriesRenderer(renderer6);
        mRenderer.addSeriesRenderer(renderer7);
        mRenderer.addSeriesRenderer(renderer8);

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

Edit (After applying Dan's suggestion)

enter image description here

My new code:

.........
mRenderer.setChartTitle("Submission Statistics");
        mRenderer.setXTitle("Verdict Code");
        mRenderer.setYTitle("No. of Submissions");
        mRenderer.setAxesColor(Color.BLACK);
        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.WHITE);
        mRenderer.setMarginsColor(Color.WHITE);
        mRenderer.setZoomEnabled(true);
        mRenderer.setBarSpacing(-0.5);
        mRenderer.setMargins(new int[] {20, 30, 15, 0});
        mRenderer.setShowLegend(false);
        mRenderer.setAxisTitleTextSize(16);
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setLabelsTextSize(15);
        mRenderer.setLegendTextSize(15);
        mRenderer.addXTextLabel(1, "AC");
        mRenderer.addXTextLabel(2, "PE");
        mRenderer.addXTextLabel(3, "WA");
        mRenderer.addXTextLabel(4, "TL");
        mRenderer.addXTextLabel(5, "ML");
        mRenderer.addXTextLabel(6, "CE");
        mRenderer.addXTextLabel(7, "RE");
        mRenderer.addXTextLabel(8, "OT");
        mRenderer.setBarWidth(50);
        mRenderer.setXAxisMin(-2);
        mRenderer.setXAxisMax(11);
        mRenderer.setYAxisMin(0);
        mRenderer.setYLabelsAlign(Align.RIGHT);
        mRenderer.setXLabelsColor(Color.BLACK);
        mRenderer.setYLabelsColor(0, Color.BLACK);
        mRenderer.setXLabels(0);
        mRenderer.setChartValuesTextSize(16);
.........

Now the problems are:

  1. mRenderer.setChartValuesTextSize(size); is not working as it is deprecated
  2. Bar text remains small till now
  3. The highest bar is split off at the top
  4. X Axis Label is not in proper alignment with bar
like image 543
Kaidul Avatar asked Jul 16 '13 15:07

Kaidul


2 Answers

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
like image 69
Dan D. Avatar answered Oct 23 '22 02:10

Dan D.


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); 
like image 2
alorma Avatar answered Oct 23 '22 04:10

alorma