android - Bar graph X axis labels disappeared after Shunting the viewport to show the full width of bars
问题描述
In my android code, I am creating a bar graph with x axis labeled with dates (String), and y axis being double values. Everything was fine, but when I had to Shunt the viewport to show the full width of the first and last bars, the dates disappeared on the X axis. Only 2 of them are visible, and 3 are not. How can I fix the problem?
Below is my code:
//BAR Graph:
//Only possible to make bar graph for 2 weeks or more
GraphView barGraph = (GraphView) findViewById(R.id.bar_graph);
if (MainActivity.weekLabelsArray.length <= 1)
barGraph.setVisibility(GraphView.GONE);
else {
ArrayList<DataPoint> barData = MainActivity.weeklyData;
BarGraphSeries<DataPoint> barSeries = new BarGraphSeries<>();
for (DataPoint d : barData)
barSeries.appendData(d, true, 30);
barGraph.addSeries(barSeries);
// styling
barSeries.setValueDependentColor(new ValueDependentColor<DataPoint>() {
@Override
public int get(DataPoint data) {
double expense = data.getY();
if (expense > 500)
return Color.rgb(255, 0, 0);
else if (expense > 200)
return Color.rgb(0, 0, 255);
else
return Color.rgb(0, 220, 0);
}
});
barSeries.setSpacing(10);
barSeries.setDrawValuesOnTop(true);
barSeries.setValuesOnTopColor(Color.RED);
//All this below just to make the x axis labeled with the dates
barGraph.getViewport().setScrollable(true);
barGraph.getViewport().setYAxisBoundsManual(true);
barGraph.getViewport().setXAxisBoundsManual(true);
StaticLabelsFormatter staticLabelsFormatterBar = new StaticLabelsFormatter(barGraph);
staticLabelsFormatterBar.setHorizontalLabels(MainActivity.weekLabelsArray);
barGraph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatterBar);
//adjusting labels and other stuff
barGraph.getGridLabelRenderer().setHorizontalLabelsAngle(100);
barGraph.getViewport().setMinY(0);
barGraph.getGridLabelRenderer().setTextSize(40);
barGraph.getGridLabelRenderer().reloadStyles();
barGraph.setTitle("Total Weekly Expenses");
//adjusting bars width
double xInterval=1.0;
barGraph.getViewport().setXAxisBoundsManual(true);
barGraph.getViewport().setMinX(barSeries.getLowestValueX() - (xInterval/2.0));
barGraph.getViewport().setMaxX(barSeries.getHighestValueX() + (xInterval/2.0));
barGraph.getViewport().setYAxisBoundsManual(true);
barGraph.getViewport().setMinY(barSeries.getLowestValueY() );
barGraph.getViewport().setMaxY(barSeries.getHighestValueY()+50);
barGraph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.NONE );
barGraph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.HORIZONTAL);
The dates disappeared after adding the following exact lines of code:
barGraph.getViewport().setXAxisBoundsManual(true);
barGraph.getViewport().setMinX(barSeries.getLowestValueX() - (xInterval/2.0));
barGraph.getViewport().setMaxX(barSeries.getHighestValueX() + (xInterval/2.0));
Here's how my graph looks like with missing date labels on the x Axis:
解决方案
推荐阅读
- ruby-on-rails - Rails CSV 删除具有空标题的列
- php - 不区分大小写的用户名检查?
- java - 异常:java.lang.IllegalStateException:getWriter() 已为此响应调用
- java - 如何在不使用包装对象的情况下反序列化前面带有前导标签的数组?
- c# - 创建新列然后收到“无效列名”错误
- firebase - 未捕获的类型错误:db.collection 不是 firebase 中实时数据库的功能
- sql - 本地发布中缺少 SQL Server 复制,但仍存在于复制监视器和分发数据库中
- javascript - 返回 $_POST 的 safari 作品,但不在 Firefox 和 IE 上
- raspberry-pi - 如何在 Raspberry PI 4 中打开和关闭 USB 端口电源
- sql - 如何使用列作为条件。PostgreSQL