gpt4 book ai didi

java - GraphView 垂直标签从 0 开始以整数递增

转载 作者:太空宇宙 更新时间:2023-11-03 11:43:52 25 4
gpt4 key购买 nike

目前,下面的代码显示附加的条形图,其刻度包括小数并从 2 开始。

我的问题是:有没有办法让 y 轴标签从 0 开始,并增加整数直到数据的最大值?比如这个,0,1,2,3,4,5?

barData = this.getIntent().getExtras().getString("GraphData");

GraphViewSeries barGraphSeries = new GraphViewSeries(
new GraphViewData[] {
new GraphViewData(0, Integer.parseInt(barData
.substring(0, barData.indexOf(",")))),
new GraphViewData(1, Integer.parseInt(barData
.substring(barData.indexOf(",") + 1,
barData.length()))) });

GraphView statGraphView = new BarGraphView(this,
"Current Stat Graph");

statGraphView.getGraphViewStyle().setGridColor(Color.BLACK);
statGraphView.getGraphViewStyle().setHorizontalLabelsColor(
Color.BLACK);
statGraphView.getGraphViewStyle().setVerticalLabelsColor(
Color.BLACK);
String[] horLabels = { "Correct", "Incorrect" };
statGraphView.setHorizontalLabels(horLabels);
statGraphView.getGraphViewStyle().setNumHorizontalLabels(2);
statGraphView.getGraphViewStyle().setNumVerticalLabels(10);



statGraphView.addSeries(barGraphSeries);

LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(statGraphView);

Current bar graph

最佳答案

首先要知道的是,如果让GraphView管理Y-scale,它会显示10个区间,即11个值。因此,如果您的值介于 0 到 10 或 0 到 20 之间,则显示的值将为整数。

您可以使用 GraphView.setManualYAxisBounds(double max, double min) 手动设置垂直边界在您的情况下,您可能想要使用 setManualYAxisBounds(5, 0),但不会显示整数。所以你必须使用 getGraphViewStyle().setNumVerticalLabels(6)

这是我用来动态调整比例的一段代码,值从 0 到 200,最大比例值尽可能接近我的数据的最大值(我希望我能理解,大声笑)

  int maxValue = ...    // here, you find your max value
// search the interval between 2 vertical labels
int interval;
if (maxValue <= 55) {
interval = 5; // increment of 5 between each label
} else if (maxValue <= 110) {
interval = 10; // increment of 10 between each label
} else {
interval = 20; // increment of 20 between each label
}
// search the top value of your graph, it must be a multiplier of your interval
int maxLabel = maxValue;
while (maxLabel % interval != 0) {
maxLabel++;
}
// set manual bounds
setManualYAxisBounds(maxLabel, 0);
// indicate number of vertical labels
getGraphViewStyle().setNumVerticalLabels(maxLabel / interval + 1);
// now, it's ok, you should have a graph with integer labels

关于java - GraphView 垂直标签从 0 开始以整数递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21469943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com