Cross-Device Compatibility Issue of AChartEngine B

2019-07-24 19:14发布

问题:

I am able to develop a Bar Graph with the help of Dan in a 7" tablet emulator and to make it fitted in this screen I used setXAxisMax() and setXAxisMin(). Here is the graph I obtained on that emulator:

But when I installed it my phone with different screen, it looked like this:

Portrait

Landscape

This is my code:

........................
        XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
        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(22);
        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(80);
        mRenderer.setXAxisMin(-6);
        mRenderer.setXAxisMax(15);
        mRenderer.setYAxisMax(311);
        mRenderer.setYAxisMin(0);
        mRenderer.setYLabelsAlign(Align.RIGHT);
        mRenderer.setXLabelsColor(Color.BLACK);
        mRenderer.setLabelsColor(Color.BLACK);
        mRenderer.setYLabelsColor(0, Color.BLACK);
        mRenderer.setXLabels(0);
            mRenderer.setPanEnabled(true, false);
........................

I add now mRenderer.setPanEnabled(true, false); so that I can move along only x axis to see the complete chart. But user should see the full chart in screen regardless of different devices.

How to make this bar graph cross-device compatible so that it won't cut off on left and right side?

回答1:

The setXAxisMin() and setXAxisMax() calls set the scale for the graph, not the size of the graph.

Try reducing the value passed to setBarWidth(). I expect that the total width of the graph is given by num_bars * (X+Y) where X and Y are passed to (setBarWidth(X) + setBarSpacing(Y)).

I have never built a bar chart with ACE, but I think that should be calculated based on the actual window width and the number of bars in your chart. Hope that helps.