android下图表工具graphview使用之添加坐标标签

默认情况下,我们只需要添加数据,会自动添加坐标便签,我们也可以自己添加我们定制的标签。

1.静态添加

通过以下两句话添加。

graphView.setHorizontalLabels(new String[] {"2 days ago", "yesterday", "today", "tomorrow"});
graphView.setVerticalLabels(new String[] {"high", "middle", "low"});

所有程序为:

public class MainActivity extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		GraphViewSeries exampleSeries = new GraphViewSeries(new GraphViewData[] {
			    new GraphViewData(1, 2.0d)
			    , new GraphViewData(2, 1.5d)
			    , new GraphViewData(3, 2.5d)
			    , new GraphViewData(4, 1.0d)
			});
			 
			GraphView graphView = new LineGraphView(
			    this // context
			    , "GraphViewDemo" // heading
			);
			
			graphView.setHorizontalLabels(new String[] {"2 days ago", "yesterday", "today", "tomorrow"});
			graphView.setVerticalLabels(new String[] {"high", "middle", "low"});
			
			graphView.addSeries(exampleSeries); // data
			 
			LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
			layout.addView(graphView);
		
	}
}

2.动态定制

你可以实现 CustomLabelFormatter 接口。然后方法setCustomLabelFormatter设置。

GraphView graphView = new LineGraphView(this, "example");
graphView.setCustomLabelFormatter(new CustomLabelFormatter() {
  @Override
  public String formatLabel(double value, boolean isValueX) {
    if (isValueX) {
      if (value < 5) {
        return "small";
      } else if (value < 15) {
        return "middle";
      } else {
        return "big";
      }
    }
    return null; // let graphview generate Y-axis label for us
  }
});


3. 设置标签宽度 数目 表格颜色等方法

// set styles
graphView.getGraphViewStyle().setGridColor(Color.GREEN);
graphView.getGraphViewStyle().setHorizontalLabelsColor(Color.YELLOW);
graphView.getGraphViewStyle().setVerticalLabelsColor(Color.RED);
graphView.getGraphViewStyle().setTextSize(getResources().getDimension(R.dimen.big));
graphView.getGraphViewStyle().setNumHorizontalLabels(5);
graphView.getGraphViewStyle().setNumVerticalLabels(4);
graphView.getGraphViewStyle().setVerticalLabelsWidth(300);




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值