[英]Android GraphView 4.x - Bar Graph Not Fitting
我在Android应用程序中使用GraphView的4.x版本。 我有一个带有7个数据点的条形图,第一个和最后一个条形被切断。
这是我的代码
private static final String[] WEEK_DAYS = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
private void showChartData() {
ChartData[] data = new ChartData[7];
for (int x = 0; x < 7; x++) {
data[x] = new ChartData();
}
data[0].setEnrollmentCount(2);
data[1].setEnrollmentCount(4);
data[2].setEnrollmentCount(6);
data[3].setEnrollmentCount(8);
data[4].setEnrollmentCount(10);
data[5].setEnrollmentCount(12);
data[6].setEnrollmentCount(14);
chartHolder.removeAllViews();
DataPoint[] graphViewData = new DataPoint[7];
for (int i = 0; i < data.length; i++) {
graphViewData[i] = new DataPoint(i,
data[i].getEnrollmentCount());
}
BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(graphViewData);
series.setColor(getResources().getColor(R.color.custom_red));
GraphView graphView = new GraphView(this);
graphView.addSeries(series); // data
graphView.setBackgroundColor(Color.WHITE);
graphView.setTitle("Sales This Week");
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graphView);
staticLabelsFormatter.setHorizontalLabels(WEEK_DAYS);
graphView.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
graphView.getGridLabelRenderer().setGridStyle(GridStyle.NONE);
chartHolder.addView(graphView);
}
这是条形图的屏幕截图
似乎在v3.1.3和4.0.0之间有很多更改,无论如何也达到了这一要求。 库v3中特定于BarGraph的逻辑在xInterval上下移动了半个视口,因此屏幕上的第一个和最后一个条目的全宽条似乎已被删除。 无论如何,例如在代码周围琐碎
double xInterval=1.0;
graphView.getViewport().setXAxisBoundsManual(true);
if (dataSeries instanceof BarGraphSeries ) {
// Shunt the viewport, per v3.1.3 to show the full width of the first and last bars.
graphView.getViewport().setMinX(dataSeries.getLowestValueX() - (xInterval/2.0));
graphView.getViewport().setMaxX(dataSeries.getHighestValueX() + (xInterval/2.0));
} else {
graphView.getViewport().setMinX(dataSeries.getLowestValueX() );
graphView.getViewport().setMaxX(dataSeries.getHighestValueX());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.