android - 使用 GraphView 库的 BarChartGraph 中的空白区域

标签 android android-layout view graph charts

我目前正在尝试使用 GraphView 库(http://www.jjoe64.com/p/graphview-library.html)绘制图表。我目前使用的是版本 3。

我对 BarChartGraph 中的 Bar 有疑问。我推送了一个 33 元素的数组,但整个图中的 char 较小,图中仍然存在未使用的空间。

我附上了一张图片。

这是 XML 代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
    android:layout_height="fill_parent">

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/txtX" 
        android:text="X: 0" 
        android:textSize="15sp"></TextView>

    <TextView
        android:id="@+id/txtZ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="39dp"
        android:text="Z: 0"
        android:textSize="15sp" />
    [....]

    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="39dp"
        android:ems="10"
        android:inputType="textMultiLine"
        android:focusable="false">
    </EditText>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:id="@+id/graph1" />

</LinearLayout>

这是主类中关于图的代码:

  GraphViewData[] data;
    GraphView graphView;

     // Inizializzazioni utili per il grafico  
        data = new GraphViewData[(N/2)+1];  // N=64

        graphView = new BarGraphView(this, "example") {  //Prova a mettere primad di questo setHorizontalabe
            @Override
            protected String formatLabel(double value, boolean isValueX) {
                if (isValueX) {
                    return Double.toString(value);
                } else return super.formatLabel(value, isValueX); // let the y-value be normal-formatted
               }
            };

      LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);   
      layout.addView(graphView);

        for (int i=0; i<(N/2)+1; i++)
           data[i] = new GraphViewData(i, new_sig[i]);

      graphView.setBackgroundColor(Color.BLACK);

      graphView.addSeries(new GraphViewSeries(data));

      graphView.invalidate();
      graphView.redrawAll();

https://docs.google.com/open?id=0B5sm5aNTZkIiN1hrV1Q2MU9iYmM

我设置:

graphView.setViewPort(0, 33);

但是什么都没有改变!

我也尝试使用较小的 ViewPort,但结果是相同的,就像这篇新文章中的图像一样。 在本例中,视口(viewport)为 20:

graphView.setViewPort(0, 20);  

https://docs.google.com/open?id=0B5sm5aNTZkIiMFlSSFd1T1ViQ00

但是图中怎么显示,保留了空白!

我还尝试更改 XML 文件中的维度标签。 我将“wrap_content”交换为“match_parent”,但没有结果。 然后我更改为 match_parent 以及父 View LinearLayout,然后也是 ScrollView。

我也尝试创建一系列 8 个数据(data.length=8),但结果是相同的,在图表右侧保留相同的空白空间。

谢谢大家! :)

最佳答案

也许您已经解决了这个问题,但它可以帮助其他人。 我遇到过同样的问题。我在这里找到了解决方案。 BarGraphView · Issue #14 · jjoe64/GraphView · GitHub

您必须像这样修改库的源代码:

// BarGraphView.java
// float colwidth = (graphwidth - (2 * border)) / values.length;
float colwidth = graphwidth / values.length;

// GraphView.java
// horizontal labels + lines
int hors = horlabels.length;
for (int i = 0; i <= horlabels.length; i++) {
    paint.setColor(Color.DKGRAY);
    float x = ((graphwidth / hors) * i) + horstart;
    canvas.drawLine(x, height - border, x, border, paint);
    paint.setTextAlign(Align.CENTER);
    if (i < horlabels.length) {
        paint.setColor(Color.WHITE);
        canvas.drawText(horlabels[i], x + (graphwidth / hors) / 2,
                height - 4, paint);
    }
}

这对我来说非常有效。我希望它有帮助。

关于android - 使用 GraphView 库的 BarChartGraph 中的空白区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14059037/

相关文章:

android - 如何在 Android 中集成 Twitter 关注按钮?

java - R.java 错误

android - 大 ImageView 加载为小 - 不采取完整的 parent

CSS 3 Div 在同一行 - ! Fix With - 其他两个是百分比明智的

mysql - 从 mysql 数据库转储中排除 View

android - Android 键盘关闭时闪烁?

android - 如何在 android 中更改默认的 toast 消息颜色和背景颜色?

android - DialogFragment 被状态栏重叠

java - Android Canvas 锁定抛出 IllegalArgumentException

iphone NavigationController 清除 View 堆栈