Android,如何使用GraphView库组合图形

时间:2014-09-11 08:26:19

标签: android android-graphview

我想使用GraphView库创建组合的水平和可滚动图形,如下图所示:

enter image description here

我该怎么办?

非常感谢任何样品。

1 个答案:

答案 0 :(得分:0)

我认为你不能在GraphView中将LineGraph与BarGraph混合

但GraphView中具有不同颜色和可滚动性的多个系列如下所示: (您将不得不对视口做一些技巧,因为如果显示了一个列,它会扩展视口以适应图形 - 它的宽度为1个单位。)

GraphView multiple series bar chart

<强> MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GraphView graphView = new BarGraphView(this, "Graph Title");

    GraphViewData[] data1 = new GraphViewData[] {
            new GraphViewData(1, 5.0d),
            new GraphViewData(2, 3.0d),
            new GraphViewData(3, 4.0d),
            new GraphViewData(4, 3.0d),
            new GraphViewData(5, 7.0d),
            new GraphViewData(6, 6.0d),
            new GraphViewData(7, 7.0d),
            new GraphViewData(8, 2.0d),
    };
    GraphViewSeriesStyle style1 = new GraphViewSeriesStyle();
    style1.setValueDependentColor(new ValueDependentColor() {
        @Override
        public int get(GraphViewDataInterface data) {
            if(((int)data.getX())%2 == 0) {
                return Color.rgb(200, 60, 0);
            }
            else {
                return Color.rgb(60, 200, 0);
            }
        }
    });
    GraphViewSeries series1 = new GraphViewSeries("Series 1", style1, data1);

    graphView.setManualYAxisBounds(10,0);
    graphView.setViewPort(1,3.99999);
    graphView.setScrollable(true);
    graphView.addSeries(series1);
    LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
    layout.addView(graphView);
}

<强> activity_main.xml中

<RelativeLayout 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"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/graph1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"></LinearLayout>