Android GraphView只使x轴水平滚动

时间:2016-02-02 14:59:54

标签: android scroll android-graphview

我正在使用graphView库在我的Android应用中绘制线图。由于我的x轴有更多的数据,我想让它可以水平滚动,这样数据就不会在较小的设备上相互重叠。到目前为止,我尝试在下面显示的xml中添加一个Horizo​​ntalScrollView,它能够使整个图形滚动。 我的问题是:如果我想要Y轴保持并且只有X轴可滚动,那该怎么做?有没有GraphView API?

我的xml:

      <HorizontalScrollView android:id="@+id/horizontalScroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:fadingEdgeLength="20dp">
        <LinearLayout
            android:id="@+id/graph_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <CustomFontTextView
                style=""
                android:layout_marginTop=""
                android:layout_marginBottom=""
                android:focusable="true"
                android:text="graph_header" />
            <LinearLayout
                android:id="@+id/Graph"
                android:focusable="true"
                android:layout_width="match_parent"
                android:layout_height="190dip"
                android:contentDescription="@string/talkback_graph"
                android:orientation="vertical" />
        </LinearLayout>
    </HorizontalScrollView>

我的java代码:

 public void setGraphView(Map<Integer, Integer> dt,String[] horizontalLabels ){
    GraphViewData[] graphData = new GraphViewData[dt.size()];
    int graphPos=0;

    for(int pos:dt.keySet())
    {
        graphData[graphPos++] = new GraphViewData(pos, dt.get(pos));
    }

    GraphViewSeries series = new GraphViewSeries(graphData); 
    LineGraphView lineGraphView = new  LineGraphView( getMainActivity(), "" );
    lineGraphView.setScrollable(true);
    lineGraphView.setScalable(false);
    lineGraphView.setPadding(10, 10, 10, 10);
    lineGraphView.setHorizontalScrollBarEnabled(true);
    lineGraphView.setGravity(Gravity.CENTER);
    lineGraphView.setManualYAxisBounds(500, 300);
    lineGraphView.setDrawDataPoints(true);
    lineGraphView.setDataPointsRadius(14f);
    lineGraphView.getGraphViewStyle().setGridColor(Color.RED);
    lineGraphView.setHorizontalLabels(horizontalLabels);
    lineGraphView.setVerticalLabels(new String[]{"max 500", "min 300"});
    lineGraphView.getGraphViewStyle().setTextSize(getResources().getDimension(R.dimen.cell_text_size));
    lineGraphView.getGraphViewStyle().setVerticalLabelsWidth(70);
    lineGraphView.getGraphViewStyle().setVerticalLabelsAlign(Align.LEFT);
    lineGraphView.addSeries(series);
    graphView.addView(lineGraphView);
}

谢谢!

1 个答案:

答案 0 :(得分:0)

lineGraphView.setScrollableY(真); lineGraphView.setScalableY(假);

对于Y轴,它应该使用不同的api。