2016-11-10 23 views
2

我需要繪製一個「閉合圖表」在我的Android應用,這樣的:MPAndroidChart:創建一個封閉的圖表(圓線圖)

a line chart that forms a rough circle

數據先增大後減小的xIndex。就像這個例子:

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

當我嘗試繪製與此數據MPAndroidChart一個LineСhart,它只是數據的前半部分呈現(前xIndex的下降)。其他數據未顯示。

如何使用MPAndroidChart正確繪製這些數據?

回答

1

sample app on the Google Play Store給出了庫中可用的每種圖表的示例。同樣,source code可供您檢查並查看它是否具有您想要的功能。

此外,the wiki明確指出,不支持無序的折線圖條目。

請注意,這個庫並不正式支持從沒有在上升方式中的條目的x位置排序的條目列表繪製線型圖數據。

這就是說,如果你願意預處理你的數據,你應該能夠實現你想要的。你將不得不採取你的數據,並提取兩個不同的數據集,你會應用相同的樣式。我是能夠實現的東西像你想使用這種技術的效果:

a "closed chart" in blue

下面是代碼:

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

我以同樣的方式決定,但我認爲,有一個正確的解。謝謝你的回答 – gearquicker

+0

@gearquicker別擔心 - 你的解決方案是正確的! –