在Android GraphView中,将X轴上的字符串值映射为Y轴上的字符串值,可以通过以下步骤实现:
implementation 'com.jjoe64:graphview:4.2.2'
<com.jjoe64.graphview.GraphView
android:id="@+id/graph"
android:layout_width="match_parent"
android:layout_height="match_parent" />
或者在Java代码中创建GraphView对象:
GraphView graphView = new GraphView(context);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
其中,DataPoint的构造函数接受两个参数,分别是X轴和Y轴上的值。
String[] xLabels = new String[] {"A", "B", "C", "D", "E"};
graphView.getGridLabelRenderer().setHorizontalLabels(xLabels);
同样地,可以设置Y轴的标签。
graphView.addSeries(series);
完整的示例代码如下:
GraphView graphView = findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
String[] xLabels = new String[] {"A", "B", "C", "D", "E"};
graphView.getGridLabelRenderer().setHorizontalLabels(xLabels);
graphView.addSeries(series);
这样,就可以在Android GraphView中将X轴上的字符串值映射为Y轴上的字符串值。关于GraphView的更多用法和功能,请参考腾讯云的相关产品和文档。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云