【问题标题】:How to align legends vertically in AchartEngine for PieChart in android如何在AchartEngine中为Android中的PieChart垂直对齐图例
【发布时间】:2013-09-04 14:02:46
【问题描述】:

我在我的应用程序中使用 AchartEngine。是否可以替换图例。

它的水平

oA oB oC oD

但我想让它垂直

OA oB oC 外径

是否可以更改图例的位置? 任何帮助将不胜感激。

【问题讨论】:

    标签: android achartengine pie-chart


    【解决方案1】:

    我通过 hack 解决了这个问题。 (这是我 /distinguished/ 职业生涯中的第一次!)

    证人:

    xySeries.setTitle(legend + "                                                                                                                                   ");
    

    图表测量空间,认为它没有水平空间,并将图例垂直堆叠。

    【讨论】:

      【解决方案2】:

      然后我把空格去掉,做了一个较低级别的 hack。在 AbstractChart.java 中,函数drawLegend(),调整这个块:

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

      如果有人需要具有各种图例方向的各种图表,他们可以轻松地从中制作出真正的功能。

      【讨论】:

        【解决方案3】:

        我没有使用 AchartEngine,而是动态创建类似的东西。

        这里是示例 java 代码。

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

        这里是xml代码。

         <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>
        

        【讨论】:

        • 这只是一个想法,如果你想要就使用它。否则离开它
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2018-12-13
        • 2012-08-26
        • 2013-04-17
        • 1970-01-01
        • 2015-10-26
        • 2013-04-25
        相关资源
        最近更新 更多