如何使用achartengine椭圆化条形图的y轴标签

时间:2013-03-19 06:17:20

标签: android achartengine android-graphview

我是Android Developer特别是Graph的新手。现在我使用 AchartEngine 库创建以下图表,但我遇到以下问题:

Q-1。我想用固定宽度显示左侧标签,用省略号显示剩余内容。

Q-2 当我要将方向从纵向更改为横向时,图形显示如下第二个图像。如何为图表设置最佳布局。?

Q-3 如何在X轴上设置0 1 2 3 ... 10?

我为此图写了以下代码:

createLayoutMethod():

/ **用于创建图形布局** /

@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():,用于放置在图表上的JSON数据:

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();
        }
    }

图形图像(纵向): Protrait Image with bar-chart

图形(风景)图像:  Landscape Image with bar-chart

请给我建议如何解决这个问题...

提前致谢....

2 个答案:

答案 0 :(得分:1)

对于Q1,您必须以像素为单位计算标签宽度,并且只添加适合您所需宽度的标签宽度。 AChartEngine不会为你做那件事。

答案 1 :(得分:1)

/** For creating lable, display 10 char if length is > 10 **/

// getting String from JSON
lable = offer.getJSONObject(forOffer).getString("offerTitle");

//if string length is grater than 10 then display only first 10 char
if(lable.length() > 10)
{
    lable = lable.substring(0, 9);
}

mRenderer.addXTextLabel(forOffer+1,lable);

当用户点击

时,我在Toast中显示了全文