问题
I am using the 4.x version of GraphView in my Android app. I have a bar graph with 7 data points and the first and last bars are being cut off.
Here is my code
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);
}
Here is a screen shot of the bar chart
回答1:
Appears there have been quite a few changes between v3.1.3 and 4.0.0, anyway also hit this one. The BarGraph specific logic in v3 of the library that shunted the viewport up and down half an xInterval, so you got a full width bar for the first and last entry on screen appears to have been dropped. Anyway its trivial to code around e.g.
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());
}
来源:https://stackoverflow.com/questions/28705981/android-graphview-4-x-bar-graph-not-fitting