2016-12-06 64 views
0

我創建了一個使用MP android圖表的圖形應用程序,我用它來繪製兩個不同的行數據集。但我的問題是xaxis的值標籤出現在圖表的頂部。我需要一種方法使其達到最低點。下面是我的代碼我試圖讓X軸的值顯示在圖形的底部而不是頂部,使用MP android圖表

final LineChart linechart = (LineChart)findViewById(R.id.idlinechart); 

     //created list enties to hold the values 
     List<Entry> valuesCompany1 = new ArrayList<>(); List<Entry> valuesCompany2 = new ArrayList<>(); 
//created entry values to be entered into the lsit entry b 
     Entry c1e1 = new Entry(0f, 100000f); valuesCompany1.add(c1e1); 

     Entry c1e2 = new Entry (1f, 140000f);valuesCompany1.add(c1e2); 

     Entry c1e3 = new Entry(2f,90000f);valuesCompany1.add(c1e3); 

     Entry c1e4 = new Entry (3f, 150000f); valuesCompany1.add(c1e4); 

     Entry c2e1 = new Entry(0f, 50000f); valuesCompany2.add(c2e1); 

     Entry c2e2 = new Entry (1f, 50000f);valuesCompany2.add(c2e2); 

     Entry c2e3 = new Entry(2f,150000f);valuesCompany2.add(c2e3); 

     Entry c2e4 = new Entry (3f, 110000f); valuesCompany2.add(c2e4); 

//so now we create line data to hold the list 
     LineDataSet linedatasetComp1 = new LineDataSet(valuesCompany1, "company 1"); 
     linedatasetComp1.setAxisDependency(YAxis.AxisDependency.LEFT); 
      linedatasetComp1.setColor(Color.BLUE); 
     LineDataSet linedatasetComp2 = new LineDataSet(valuesCompany2, "company 2"); 
     linedatasetComp2.setAxisDependency(YAxis.AxisDependency.LEFT); 
      linedatasetComp2.setColor(Color.RED); 

     List<ILineDataSet> iLinedata = new ArrayList<ILineDataSet>(); 
     iLinedata.add(linedatasetComp1); //adding the line data sets into the line data list 
     iLinedata.add(linedatasetComp2); 
//setting the Ilinedata list into line data 
     LineData linedata = new LineData(iLinedata); 

      //setting the line data into line chart 
     linechart.setData(linedata); 
     linechart.invalidate(); //refreshing the line chart 

      //Saving the picture to galery 
Button savebutton = (Button)findViewById(R.id.button); 
      savebutton.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
          linechart.saveToGallery("new Line chart", 150); 
          Toast.makeText(getApplicationContext(), "graph saved ", Toast.LENGTH_LONG).show(); 
        } 
      }); 

      final String[] Quaters = {"Q1", "Q2", "Q3", "Q4"}; 

      IAxisValueFormatter valueFormater = new IAxisValueFormatter() { 
        @Override 
        public String getFormattedValue(float value, AxisBase axis) { 
          return Quaters[(int) value]; 

        } 

        @Override 
        public int getDecimalDigits() { 
          return 0; 
        } 
      }; 
      XAxis xAxis = linechart.getXAxis(); 
      xAxis.setGranularity(1f); 
      xAxis.setValueFormatter(valueFormater); 
     Description desc = new Description(); 
     desc.setText("A graph comparing revenuews of two companies "); 
     desc.setEnabled(true); 
     desc.setPosition(0,0); 
     desc.setTextColor(Color.BLACK); 
     linechart.setDescription(desc); 
+0

這樣的功能,所述具有唯一'BarChar'和'CombinedChart'。 –

+0

檢查答案,我已經解決了它 – ade

回答

4

我不知道,如果線型圖有一個選項,但如果它,你只需要獲得x軸調整這樣的位置:

linechart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM); 

希望它可以幫助!

+0

是的非常感謝。我創建了Xaxis對象併爲它設置了位置,謝謝 – ade

0

使用MPAndroid圖表版本2.2.4或以上,並使用碼

mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM); 

通過Ponchotg

相關問題