Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在水平条形图中设置标签和值?_Java_Android_Bar Chart_Mpandroidchart - Fatal编程技术网

Java 如何在水平条形图中设置标签和值?

Java 如何在水平条形图中设置标签和值?,java,android,bar-chart,mpandroidchart,Java,Android,Bar Chart,Mpandroidchart,我正在使用。请检查我下面的代码 活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" androi

我正在使用。请检查我下面的代码

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.graphs.graphs2.MainActivity">



    <com.github.mikephil.charting.charts.HorizontalBarChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
更新

问题解决了。
BarView
UI必须更改如下

<com.example.graphs.viewchart.BarView
        android:id="@+id/low_bar"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="8dp" />

通过查看所需输出的图像,应该可以在不使用第三方库的情况下创建此类简单条形图。“条”可以由简单的
视图组成,这些视图可以根据以总可用宽度的百分比计算的数字来加长或缩短。以下是您如何实现这一目标的示例:

1) 创建一个长度可调的自定义
视图
,用作“条形图”

3) 在
XML
中设置它。条的
重量
s设置为1,因此它们填满所有可用空间,这将构成“100%”读数


以下是输出的图像:


通过查看所需输出的图像,应该可以在不使用第三方库的情况下创建此类简单条形图。“条”可以由简单的
视图组成,这些视图可以根据以总可用宽度的百分比计算的数字来加长或缩短。以下是您如何实现这一目标的示例:

1) 创建一个长度可调的自定义
视图
,用作“条形图”

3) 在
XML
中设置它。条的
重量
s设置为1,因此它们填满所有可用空间,这将构成“100%”读数


以下是输出的图像:


查看图像时,您最好完全放弃库,只需为带有彩色背景的条形图使用标准的
视图,并根据其百分比值动态设置
视图的长度。@p谢谢您的回复。请问我怎么做?有代码示例吗?好的。我收集了一个如何实现这一点的示例。看这张图片,您最好完全放弃库,只需对带有彩色背景的条形图使用标准的
视图
,并根据其百分比值动态设置
视图的长度。@PPartisan感谢您的回复。请问我怎么做?有代码示例吗?好的。我已经收集了一个你如何做到这一点的例子。谢谢。一个小问题
com.github.ppartisan.graphish.view.BarView
这是来自github的吗?在这种情况下,我需要先安装依赖项?@PeakGen不,这只是我设置的自定义
视图的路径。如果愿意,您可以将
BarView
复制到您自己的项目中,并将
layout
文件更新到适当的路径。这太棒了。我会检查的。您好,您的代码生成了一个错误。我用完整的stacktrace更新了我的问题,请检查您的
com.github.ppartisan.graphish….
package/path不可用。我在网上也找不到。谢谢。一个小问题
com.github.ppartisan.graphish.view.BarView
这是来自github的吗?在这种情况下,我需要先安装依赖项?@PeakGen不,这只是我设置的自定义
视图的路径。如果愿意,您可以将
BarView
复制到您自己的项目中,并将
layout
文件更新到适当的路径。这太棒了。我会检查的。您好,您的代码生成了一个错误。我用完整的stacktrace更新了我的问题,请检查您的
com.github.ppartisan.graphish….
package/path不可用。我在网上也找不到。
01-11 10:51:10.402 26974-26974/com.example.graphs.viewchart E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.graphs.viewchart, PID: 26974
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.graphs.viewchart/com.example.graphs.viewchart.MainActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class com.github.ppartisan.graphish.view.BarView
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721)
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:168)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5753)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
                                                                               Caused by: android.view.InflateException: Binary XML file line #36: Error inflating class com.github.ppartisan.graphish.view.BarView
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:757)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                  at com.example.graphs.viewchart.MainActivity.onCreate(MainActivity.java:21)
                                                                                  at android.app.Activity.performCreate(Activity.java:6112)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:168) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5753) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
                                                                               Caused by: java.lang.ClassNotFoundException: Didn't find class "com.github.ppartisan.graphish.view.BarView" on path: DexPathList[[zip file "/data/app/com.example.graphs.viewchart-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
                                                                                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                                  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                                  at android.view.LayoutInflater.createView(LayoutInflater.java:571)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284) 
                                                                                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                                  at com.example.graphs.viewchart.MainActivity.onCreate(MainActivity.java:21) 
                                                                                  at android.app.Activity.performCreate(Activity.java:6112) 
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1117) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2721) 
                                                                                  at android.app.ActivityThread.access$900(ActivityThread.java:168) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:135) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5753) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
                                                                                Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.github.ppartisan.graphish.view.BarView" on path: DexPathList[[dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-support-annotations-24.2.1_ee83e15951f6e098dcf8d5c7289233f29c08ba29-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.graphs.viewchart/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.exam
<com.example.graphs.viewchart.BarView
        android:id="@+id/low_bar"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="8dp" />
public final class BarView extends View {

    private final Paint mPaint = new Paint();
    private int percent = 0;

    public BarView(Context context) {
        super(context);
        init();
    }

    public BarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
    }

    public void set(int color, int percent) {

        if(percent < 0 || percent > 100) {
            throw new IllegalArgumentException("Percent value must range from 0 to 100");
        }

        mPaint.setColor(color);
        this.percent = percent;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final float width = MeasureSpec.getSize(widthMeasureSpec);
        final float adjWidth = (width*((float)percent/100));
        setMeasuredDimension((int)adjWidth, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
    }

}
public class MainActivity extends AppCompatActivity {

    private TextView mLowLabel, mMidLabel, mHighLabel;
    private BarView mLowBar, mMidBar, mHighBar;

    //Some sample percentage values
    private final int low = 17;
    private final int mid = 90;
    private final int high = 34;

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

        mLowBar = (BarView) findViewById(R.id.low_bar);
        mMidBar = (BarView) findViewById(R.id.mid_bar);
        mHighBar = (BarView) findViewById(R.id.high_bar);

        mLowLabel = (TextView) findViewById(R.id.low_text);
        mMidLabel = (TextView) findViewById(R.id.mid_text);
        mHighLabel = (TextView) findViewById(R.id.high_text);

        mLowBar.set(Color.BLUE, low);
        mMidBar.set(Color.RED, mid);
        mHighBar.set(Color.GREEN, high);

        mLowLabel.setText(getPercentage(low));
        mMidLabel.setText(getPercentage(mid));
        mHighLabel.setText(getPercentage(high));

    }

    private String getPercentage(int per) {
        return per + "%";
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.github.ppartisan.graphish.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Low"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>

            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/low_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/low_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mid"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>

            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/mid_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/mid_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="High"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:minEms="3"
                android:textStyle="bold"/>

            <com.github.ppartisan.graphish.view.BarView
                android:id="@+id/high_bar"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="8dp" />

            <TextView
                android:id="@+id/high_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>