sharry
sharry

Reputation: 370

How to align legends vertically in AchartEngine for PieChart in android

I am using AchartEngine in my application. Is it possible to replace the legends.

Its horizontal

oA oB oC oD

but i want to make it vertical

oA
oB
oC 
oD

Is it possible to change the position of the legends? Any help would be highly appreciated.

Upvotes: 0

Views: 990

Answers (3)

Phlip
Phlip

Reputation: 5343

Then I took the spaces out and made a lower level hack. Inside AbstractChart.java, function drawLegend(), adjust this block:

            if (true) { // i > 0 && getExceed(currentWidth, renderer, right, width)) {
              currentX = left;
              currentY += renderer.getLegendTextSize();
              size += renderer.getLegendTextSize();
              currentWidth = currentX + extraSize;
            }

Someone could easily make a real feature out of that if they need various charts with various legend orientations.

Upvotes: 0

Phlip
Phlip

Reputation: 5343

I fixed this by a hack. (For the first time EVER in my /distinguished/ career!)

Witness:

xySeries.setTitle(legend + "                                                                                                                                   ");

The chart measures the spaces, thinks it has no horizontal room for them, and stacks the legends vertically.

Upvotes: 0

Murali Ganesan
Murali Ganesan

Reputation: 2955

I'm not using AchartEngine but creating something like that dynamically.enter image description here

Here the sample java code.

public class MainActivity extends Activity {

private LinearLayout linear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Random randomGenerator = new Random();
    for (int i = 0; i < 100; i++) {
        linear = (LinearLayout) findViewById(R.id.linear);
        LinearLayout lay = new LinearLayout(this);
        LinearLayout lay1 = new LinearLayout(this);
        TextView text = new TextView(this);
        text.setText("ONES");
        text.setTextColor(Color.parseColor("#000000"));
        text.setLayoutParams(new LinearLayout.LayoutParams(60, 40));

        TextView line = new TextView(this);
        line.setBackgroundColor(Color.parseColor("#00FF00"));
        line.setLayoutParams(new LinearLayout.LayoutParams(5, 90));

        TextView btn = new TextView(this);
        int randomInt = randomGenerator.nextInt(100);
        if (randomInt > 0 && randomInt <= 10) {
            btn.setBackgroundColor(Color.parseColor("#FF1919"));
        } else if (randomInt > 10 && randomInt <= 20) {
            btn.setBackgroundColor(Color.parseColor("#C41300"));
        } else if (randomInt > 20 && randomInt <= 30) {
            btn.setBackgroundColor(Color.parseColor("#AB1100"));
        } else if (randomInt > 30 && randomInt <= 40) {
            btn.setBackgroundColor(Color.parseColor("#7A0C00"));
        } else if (randomInt > 40 && randomInt <= 50) {
            btn.setBackgroundColor(Color.parseColor("#3E0600"));
        } else if (randomInt < 50 && randomInt <= 60) {
            btn.setBackgroundColor(Color.parseColor("#000000"));
        } else if (randomInt > 60 && randomInt <= 70) {
            btn.setBackgroundColor(Color.parseColor("#003E0F"));

        } else if (randomInt > 70 && randomInt <= 80) {
            btn.setBackgroundColor(Color.parseColor("#007A1D"));

        } else if (randomInt > 80 && randomInt <= 90) {
            btn.setBackgroundColor(Color.parseColor("#00C42F"));

        } else {
            btn.setBackgroundColor(Color.parseColor("#11FF4A"));

        }
        Log.d("TAG", "randnumber" + randomInt);

        btn.setLayoutParams(new LinearLayout.LayoutParams(randomInt, 40));

        btn.setId(i);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Log.d("TAG", "get id" + arg0.getId());
            }
        });
        TextView text1 = new TextView(this);
        text1.setText("1");
        text1.setTextColor(Color.parseColor("#000000"));
        text1.setLayoutParams(new LinearLayout.LayoutParams(60, 40));
        lay1.setOrientation(LinearLayout.HORIZONTAL);
        lay1.setGravity(Gravity.CENTER_VERTICAL);
        // lay1.setPadding(0, 10, 0, 10);
        lay1.addView(text);
        lay1.addView(line);
        lay1.addView(btn);
        lay1.addView(text1);
        lay.setOrientation(LinearLayout.VERTICAL);
        lay.addView(lay1);
        linear.addView(lay);
    }
}

}

Here is the xml code.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" >

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

Upvotes: -2

Related Questions