2017-01-10 29 views
0

私はMPAndroidChartを使用しています。私の次のコード水平棒グラフでラベルと値を設定する方法は?

activity_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> 

MainActivity.java

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

     HorizontalBarChart chart = (HorizontalBarChart) findViewById(R.id.chart); 

     BarData data = new BarData(getDataSet(Color.rgb(115,115,115),4,6,"Low","lowSet"), 
       getDataSet(Color.rgb(71,209,71),3,44,"Normal","normalSet"), 
       getDataSet(Color.rgb(255,204,0),2,44,"High","highSet"), 
       getDataSet(Color.rgb(255,51,0),1,6,"Very High","veryHighSet")); 
     chart.setData(data); 
     chart.setDragEnabled(true); // on by default 
     //chart.setVisibleXRange(0,3); // sets the viewport to show 3 bars 
     chart.animateXY(2000, 2000); 
     chart.invalidate(); 
    } 

private BarDataSet getDataSet(int color, float position, float value, String entryLabel, String datasetLabel) { 
     ArrayList<BarEntry> entries = new ArrayList(); 
     entries.add(new BarEntry(position, value,entryLabel)); 

     BarDataSet dataset = new BarDataSet(entries,datasetLabel); 
     dataset.setColor(color); 
     return dataset; 
    } 

これは私

enter image description here

を以下のビューを与えを確認してください

しかし、私が欲しいのは enter image description here

以下のようなものは今、私のチャートでは、 1にはどうすれパーセントマークとバーの端にあるバーの値を表示することができますか?私の現在のチャートには値が表示されません! 2.バーの左側にlabelsを設定するにはどうすればよいですか?私はBarEntryコンストラクタを使って試しましたが、うまくいきませんでした。 3.グリッド線をすべて削除するにはどうすればよいですか?

更新

私はそれが以下のエラーを生成し、PPartasianにより溶液を試してみました。

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 

UPDATE

問題が整理しました。 BarView UIは、所望の出力の画像を見てから

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

、あなたは完全にライブラリをの前述し、単に色付きバーのための標準的な 'View'を使用したほうが良いかもしれ'%'の値に基づいて '' View''の長さを動的に設定します。 – PPartisan

+0

@PPartisan返信ありがとうございます。どうすればいいですか?任意のコードの例してください? –

+0

私はあなたがこれを達成する方法のサンプルをまとめました。 – PPartisan

答えて

1

以下のように変更する必要があり、サードパーティのライブラリを使用せずに、この種の簡単な棒グラフを作成することが可能でなければなりません。 「バー」は、利用可能な総幅のパーセンテージを取って計算された図に基づいて伸長または短縮することができる簡単なViewで構成することができます。幅を設定するには、パーセント値を使用します)、「バー」

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); 
    } 

} 

2として動作するように調整可能な長さを持つカスタムViewを作成します)

:ここでは、これを達成できる方法の一例です
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 + "%"; 
    } 

} 

3)XMLに設定します。バーのweightは1に設定され、利用可能なスペースをすべて埋めるので、「100%」の読みが構成されます。ここで

<?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> 

が出力のイメージです:

画像を見て

Bar Output Image

+0

ありがとうございます。小さな質問.... 'com.github.ppartisan.graphish.view.BarView'これはgithubから来ていますか?その場合は、最初に依存関係をインストールする必要がありますか? –

+0

@ PeakGenこれは、私がまとめたカスタムの 'View'へのパスです。 'layout'ファイルを適切なパスに更新したいのであれば、' BarView'を自分のプロジェクトにコピーすることができます。 – PPartisan

+0

それは素晴らしいです。私がチェックします。 –

関連する問題