我正在制作一个水平条形图。我在左轴上总共有 24 个标签。我在绘制条形图时遇到了几个问题:
1) 当条形图开始时,我显示的条目数量有限。用户可以滚动查看其他条目。正如您在图 1 中看到的,一条条出现在 Y 轴的“下方”。我无法弄清楚如何将所有栏保持在轴的顶部。
2)第二个问题是值(或条形)向下移动了一个。“00 AM”标签前面没有条,但在上午 11 点的最后一个条目下方有一个额外的条,如第二张图像所示。怎么换杠?
3)不是每个标签都有焦油,而是一种我可以在这些标签之间显示条的方式?
以下是我得到的图表:
这是代码片段的代码:
public class TimeCravingBarChart extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.timecraving_graph);
final GraphicalView gv =createIntent();
RelativeLayout rl=(RelativeLayout)findViewById(R.id.timegraph);
LayoutParams l1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rl.addView(gv,l1);
}
public GraphicalView createIntent() {
String[] titles = new String[] { "Urge Level"};
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 6, 5, 3, 2, 6, 1, 6, 5, 3, 2, 6, 1, 6, 5, 3, 2, 6, 1, 6, 5, 3, 2, 6, 1});
int[] colors = new int[] { Color.parseColor("#77c4d3")};
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setOrientation(Orientation.VERTICAL);
setChartSettings(renderer, "Urge vs TimeOfTheDay Pattern", "Time of the day", "Urge", 1,
12, 0, 7, Color.WHITE, Color.WHITE);
renderer.setXLabels(0);
renderer.setYLabels(10);
renderer.setXRoundedLabels(false);
//renderer.setYRoundedLabels(false);
renderer.addXTextLabel(0, "00 AM ");
renderer.addXTextLabel(1, "01 AM ");
renderer.addXTextLabel(2, "02 AM ");
renderer.addXTextLabel(3, "03 AM ");
renderer.addXTextLabel(4, "04 AM ");
renderer.addXTextLabel(5, "05 AM ");
renderer.addXTextLabel(6, "06 AM ");
renderer.addXTextLabel(7, "07 AM ");
renderer.addXTextLabel(8, "08 AM ");
renderer.addXTextLabel(9, "09 AM ");
renderer.addXTextLabel(10, "10 AM ");
renderer.addXTextLabel(11, "11 AM ");
renderer.addXTextLabel(12, "12 Noon ");
renderer.addXTextLabel(13, "1 PM ");
renderer.addXTextLabel(14, "2 PM ");
renderer.addXTextLabel(15, "3 PM ");
renderer.addXTextLabel(16, "4 PM ");
renderer.addXTextLabel(17, "5 PM ");
renderer.addXTextLabel(18, "6 PM ");
renderer.addXTextLabel(19, "7 PM ");
renderer.addXTextLabel(20, "8 PM ");
renderer.addXTextLabel(21, "9 PM ");
renderer.addXTextLabel(22, "10 PM ");
renderer.addXTextLabel(23, "11 PM ");
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(true);
}
final GraphicalView grfv = ChartFactory.getBarChartView(TimeCravingBarChart.this, buildBarDataset(titles, values), renderer,Type.DEFAULT);
return grfv;
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(20);
renderer.setLabelsTextSize(20);
renderer.setLegendTextSize(20);
renderer.setPanEnabled(true, false);
renderer.setBarSpacing(0);
renderer.setOrientation(Orientation.VERTICAL);
renderer.setZoomEnabled(false, false);
renderer.setMarginsColor(Color.parseColor("#EEEDED"));
renderer.setXLabelsColor(Color.WHITE);
renderer.setYLabelsColor(0,Color.WHITE);
renderer.setApplyBackgroundColor(false);
int length = colors.length;
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
renderer.addSeriesRenderer(r);
}
return renderer;
}
protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++) {
CategorySeries series = new CategorySeries(titles[i]);
double[] v = values.get(i);
int seriesLength = v.length;
for (int k = 0; k < seriesLength; k++) {
series.add(v[k]);
}
dataset.addSeries(series.toXYSeries());
}
return dataset;
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
int labelsColor) {
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setMargins(new int[] { 0, 60, 50, 0 }); // top, left, bottom or right
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
}