2016-12-06 42 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

相关问题