2016-11-10 7 views
2

クローズチャート(円折れ線グラフ)の作成、私はこのように、私のAndroidアプリに「閉鎖チャート」を描画する必要があります。MPAndroidChart:

a line chart that forms a rough circle

データ最初の増加のXINDEXが、その後減少しています。この例のように:私はMPAndroidChartでこのデータをLineСhartを描画しようとすると

[3,3],[4,4],[5,5],[6,4],[7,3],[6,2],[5,1],[4,2],[3,3] 

、それだけで(前XINDEXの秋に)データの最初の半分をレンダリングします。その他のデータは表示されません。

どのようにしてこのデータをMPAndroidChartで正しく描画できますか?

答えて

1

sample app on the Google Play Storeは、ライブラリで使用できるあらゆる種類のチャートの例を示しています。同様に、source codeを調べて、必要な機能があるかどうかを調べることができます。

さらに、the wikiは、特に、順序付けられていない折れ線グラフのエントリはサポートされていないと言います。

このライブラリは正式に昇順的にエントリのx位置によってソートされていないエントリーリストから折れ線グラフの描画データをサポートしていないことに注意してください。

言われているように、データを前処理する場合は、必要なものを達成できるはずです。データを取り込んで、同じスタイリングを適用する2つの異なるデータセットを抽出する必要があります。私はあなたがこの技術を使用したいエフェクトのような何かを達成することができました:

import android.graphics.Color; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.view.Menu; 
import android.view.WindowManager; 
import android.widget.SeekBar; 
import android.widget.TextView; 

import com.github.mikephil.charting.charts.LineChart; 
import com.github.mikephil.charting.components.Legend; 
import com.github.mikephil.charting.components.Legend.LegendForm; 
import com.github.mikephil.charting.components.XAxis; 
import com.github.mikephil.charting.components.YAxis; 
import com.github.mikephil.charting.data.Entry; 
import com.github.mikephil.charting.data.LineData; 
import com.github.mikephil.charting.data.LineDataSet; 
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; 
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; 

import java.util.ArrayList; 
import java.util.Random; 

public class LineChartActivity4 extends DemoBase { 

    private LineChart mChart; 
    private Random rand; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     rand = new Random(); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_linechart); 

     mChart = (LineChart) findViewById(R.id.chart1); 
     mChart.setDrawGridBackground(false); 
     mChart.getDescription().setEnabled(false); 
     mChart.setTouchEnabled(true); 
     mChart.setScaleXEnabled(true); 
     mChart.setScaleYEnabled(true); 
     mChart.setPinchZoom(true); 
     mChart.getLegend().setEnabled(false); 
     YAxis leftAxis = mChart.getAxisLeft(); 
     leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines 
     leftAxis.enableGridDashedLine(10f, 10f, 0f); 
     leftAxis.setDrawZeroLine(false); 
     leftAxis.setDrawLimitLinesBehindData(true); 
     mChart.getAxisRight().setEnabled(true); 
     mChart.setDragOffsetX(20); 
     mChart.setData(generateClosedData(90, 180, 15)); 
     mChart.animateX(2500); 
    } 

    private LineData generateClosedData(float offset, float range, float delta) { 
     ArrayList<Entry> topEntries = new ArrayList<>(); 
     ArrayList<Entry> bottomEntries = new ArrayList<>(); 

     for (int x = 0; x <= 180; x++) { 
      float val1 = offset + generateValue(x, range, delta); 
      float val2 = offset - generateValue(x, range, delta); 
      topEntries.add(new Entry(x, val1)); 
      bottomEntries.add(new Entry(x, val2)); 
     } 

     LineDataSet set1 = generateLineDataSet(topEntries); 
     LineDataSet set2 = generateLineDataSet(bottomEntries); 

     ArrayList<ILineDataSet> dataSets = new ArrayList<>(); 
     dataSets.add(set1); 
     dataSets.add(set2); 
     LineData data = new LineData(dataSets); 
     return data; 
    } 

    private float generateValue(int x, float range, float delta) { 
     float sine = (float) Math.sin(Math.toRadians(x)); 
     float scaledSine = sine * range; 
     if (x == 0 || x == 180) { 
      return scaledSine; 
     } 
     else { 
      return scaledSine + rand.nextFloat() * delta; 
     } 
    } 

    @NonNull 
    private LineDataSet generateLineDataSet(ArrayList<Entry> topEntries) { 
     LineDataSet set; 
     set = new LineDataSet(topEntries, ""); 
     set.setColor(Color.BLUE); 
     set.setDrawCircles(false); 
     set.setLineWidth(4f); 
     set.setValueTextSize(9f); 
     set.setFormSize(15.f); 
     return set; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.line, menu); 
     return true; 
    } 
} 
+1

私は同じようにして決定されたが、私は正しいがあることを考えた:ここ

a "closed chart" in blue

コードです溶液。回答ありがとうございました – gearquicker

+0

@gearquicker心配しないでください - あなたの解決策は正しいです! –

関連する問題