条形图android

时间:2012-09-15 23:20:24

标签: android bar-chart

我需要在android中绘制简单的条形图(类似于bar chart)。我发现了许多库,但它们非常复杂并不是很有用。也许有人已经使用条形图并且知道一个好条形图?

4 个答案:

答案 0 :(得分:1)

我有一个非常简单的解决方案(你不需要特别的东西):

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class TestesActivity extends Activity {

    private List<Double> s;

    private LinearLayout linearChart;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        linearChart = (LinearLayout)findViewById(R.id.llGraph);
        s = new ArrayList<Double>();

        for (int i = 0; i < 12; i++) {
            Random r = new Random();
            s.add(r.nextDouble());
        }

        Thread t = new Thread() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {

                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            drawChart(s.size());
                        }
                    });
                }
            }           
        };
        t.start();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        drawChart(s.size());
    }

    public void drawChart(int count) {
        linearChart.removeAllViews();

        int h = linearChart.getMeasuredHeight();
        int w = linearChart.getMeasuredWidth();

        for (int k = 1; k <= count; k++) {
            Random r = new Random();
            int alt = r.nextInt(h);

            View view = new View(this);

            if (alt < h/4) {
                view.setBackgroundColor(Color.RED);
            } else if (alt < h /2) {
                view.setBackgroundColor(Color.YELLOW);
            } else {
                view.setBackgroundColor(Color.GREEN);
            }

            view.setLayoutParams(new LinearLayout.LayoutParams((w / count) -3, alt));
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
            params.setMargins(3, h - alt, 0, 0); // substitute parameters for left,top, right, bottom
            view.setLayoutParams(params);
            linearChart.addView(view);
        }
    }   

}

Layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/radialback">

    <LinearLayout
        android:id="@+id/llGraph" >
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:1)

对于使用Bar Chart的Android中的MpAndroidChart Library,我有一个非常简单易用的解决方案。

build.gradle文件中添加库:

compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'

MainActivity.java:

 BarChart barChart = (BarChart) findViewById(R.id.barchart);

    ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry(38f, 0));
    entries.add(new BarEntry(52f, 1));
    entries.add(new BarEntry(65f, 2));
    entries.add(new BarEntry(30f, 3));
    entries.add(new BarEntry(85f, 4));
    entries.add(new BarEntry(19f, 5));
    entries.add(new BarEntry(75f, 6));

    BarDataSet bardataset = new BarDataSet(entries, " ");

    ArrayList<String> labels = new ArrayList<String>();
    labels.add("Mon");
    labels.add("Tue");
    labels.add("Wed");
    labels.add("Thus");
    labels.add("Fri");
    labels.add("Sat");
    labels.add("Sun");

    BarData data = new BarData(labels, bardataset);
    barChart.setData(data); // set the data and list of lables into chart

    barChart.setDescription("Description");  // set the description

    bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

    barChart.animateY(5000);

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jatin.bargraph.MainActivity">
    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/barchart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

这很简单:

class Bar extend View{
  int max; // Size of bar.
  int current; //Used
  Paint paint;

  [...] constructors.

  public void onDraw(Canvas c){
    paint.setColor(0xFF00FF00);
    c.drawRect(0, 0, getWidth(), getHeight()); //Draw whole View to green.
    paint.setColor(0xFFFF0000);
    c.drawRect(0, 0, getWidth()*current/max, getHeight()); //Draw used View to red.
  }

}

进一步的美化取决于你。

答案 3 :(得分:0)

我写了一个小部件来绘制波形或条形图聊天。使用起来非常简单明了:https://github.com/maxyou/SimpleWaveform