Search code examples
androidachartengineandroid-graphview

how to ellipsize label of y-axis with bar chart using achartengine


I am very new to android Developer Especially for Graph. Now I am creating a following graph using AchartEngine library but i have following problem:

Q-1. I want to Display label of left side with fixed width and remaining content with ellipsis.

Q-2 When i am going to change orientation from portrait to landscape, graph is display like following second image. How to set Best Layout for graph.?

Q-3 How to set 0 1 2 3...10 at X-axis?

I've written following code for this graph :

createLayoutMethod():

/** For Creating Graph Layout **/

@SuppressWarnings("deprecation")
private void createGraphLayout() {
    // TODO Auto-generated method stub
    /** Initialixation for Graph */
    /** objext of dataset which is put on Graph*/
    dataset = new XYMultipleSeriesDataset();
    series = new CategorySeries("Number of Customer");


    renderer = new XYSeriesRenderer();
    //renderer.setColor(Color.rgb(0, 168, 240));

    mRenderer = new XYMultipleSeriesRenderer();

    /** Setting for Renderer **/
    mRenderer.setLabelsColor(Color.BLACK);
    mRenderer.setBackgroundColor(Color.WHITE);
    mRenderer.setOrientation(Orientation.VERTICAL);
    mRenderer.setClickEnabled(true);
    mRenderer.setLegendTextSize(15);
    mRenderer.setLabelsTextSize(13);
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setMargins(new int[] {10,10,200,10});

    mRenderer.setMarginsColor(Color.WHITE);
    mRenderer.setInScroll(true);

    mRenderer.setPanEnabled(true, false);
    mRenderer.setScale(1);
    mRenderer.setXLabelsAlign(Align.RIGHT);         // alignment of label x
    mRenderer.setXAxisMax(offer.length());
    mRenderer.setYLabelsAlign(Align.CENTER);        // alignment of label y
    mRenderer.setXLabelsColor(Color.BLACK);         // color of label x
    mRenderer.setYLabelsColor(0, Color.BLACK);      // color of label y

    mRenderer.setLegendHeight(60);
    mRenderer.setDisplayChartValues(true);

    mRenderer.setYAxisMax(offer.length());

    mRenderer.addSeriesRenderer(renderer);

}

putDataOnGraph(): for JSON DATA put on graph :

private void putDataOnGraph() {
        // TODO Auto-generated method stub
        try {

            for(int forOffer = 0; forOffer < offer.length(); forOffer++)
            {
                no_cust = 0;
                for(int forCust = 0; forCust < customer.length(); forCust++)
                {
                    if(offer.getJSONObject(forOffer).getInt("id") == customer.getJSONObject(forCust).getInt("offerId") && customer.getJSONObject(forCust).getInt("status") == 1)
                        no_cust = no_cust+1;
                }

                /** Check for x-axis maximum than customer **/
                maxX = no_cust > maxX ? no_cust : maxX;

                mRenderer.setXLabels(0);
                mRenderer.addXTextLabel(forOffer+1,offer.getJSONObject(forOffer).getString("offerTitle"));


                //Log.d("msg", ""+offer.getJSONObject(forOffer).getString("offerTitle") +" : "+no_cust);
                //Log.d("msg", ""+maxX);
                series.add(no_cust);
            }
            mRenderer.setYAxisMax(maxX);
            dataset.addSeries(series.toXYSeries());


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Image of Graph (Portrait): Protrait Image with bar-chart

Image of Graph (Landscape): Landscape Image with bar-chart

Your help would be appreciated.

Thanks in advance....


Solution

  • For Q1, you will have to compute the labels width in pixels and add only as much as it fits in your desired width. AChartEngine doesn't do that for you.