我使用aChartEngine(1.2.0 jar)生成一个包含7个条形的条形图。
我有2个问题。 条形图未与其“addXTextLabel”对齐/居中。 当我增加条形的宽度时,并非所有条形都显示在图表上。
唯一保持居中/对齐其“XTextLabel”的栏是第4栏。
这是我的代码
public class BarGraph{
private int background = 0xffffffff;
private int bar1color = 0xff714596;
private int bar2color = 0xff3386a5;
private int bar3color = 0xff96e4e4;
private int bar4color = 0xff75df51;
private int bar5color = 0xfffff033;
private int bar6color = 0xffffbf33;
private int bar7color = 0xffff3333;
private int titleColor = 0xff8d8d8d;
public Intent getIntent(Context context) {
int s1 = 89;
int s2 = 74;
int s3 = 68;
int s4 = 59;
int s5 = 52;
int s6 = 40;
int s7 = 22;
XYSeries series1 = new XYSeries("Bar 1");
XYSeries series2 = new XYSeries("Bar 2");
XYSeries series3 = new XYSeries("Bar 3");
XYSeries series4 = new XYSeries("Bar 4");
XYSeries series5 = new XYSeries("Bar 5");
XYSeries series6 = new XYSeries("Bar 6");
XYSeries series7 = new XYSeries("Bar 7");
series1.add(1, s1);
series2.add(2, s2);
series3.add(3, s3);
series4.add(4, s4);
series5.add(5, s5);
series6.add(6, s6);
series7.add(7, s7);
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series1);
dataset.addSeries(series2);
dataset.addSeries(series3);
dataset.addSeries(series4);
dataset.addSeries(series5);
dataset.addSeries(series6);
dataset.addSeries(series7);
// Customize bar 1
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(bar1color);
renderer.setDisplayChartValues(true);
renderer.setChartValuesTextSize(18);
//renderer.setLineWidth((float) 50.0d);
// Customize bar 2
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(bar2color);
renderer2.setDisplayChartValues(true);
renderer2.setChartValuesTextSize(18);
//renderer2.setLineWidth((float) 50.0d);
// Customize bar 3
XYSeriesRenderer renderer3 = new XYSeriesRenderer();
renderer3.setColor(bar3color);
renderer3.setDisplayChartValues(true);
renderer3.setChartValuesTextSize(18);
//renderer3.setLineWidth((float) 50.0d);
// Customize bar 4
XYSeriesRenderer renderer4 = new XYSeriesRenderer();
renderer4.setColor(bar4color);
renderer4.setDisplayChartValues(true);
renderer4.setChartValuesTextSize(18);
//renderer4.setLineWidth((float) 50.0d);
// Customize bar 5
XYSeriesRenderer renderer5 = new XYSeriesRenderer();
renderer5.setColor(bar5color);
renderer5.setDisplayChartValues(true);
renderer5.setChartValuesTextSize(18);
//renderer5.setLineWidth((float) 50.0d);
// Customize bar 6
XYSeriesRenderer renderer6 = new XYSeriesRenderer();
renderer6.setColor(bar6color);
renderer6.setDisplayChartValues(true);
renderer6.setChartValuesTextSize(18);
//renderer6.setLineWidth((float) 50.0d);
// Customize bar 7
XYSeriesRenderer renderer7 = new XYSeriesRenderer();
renderer7.setColor(bar7color);
renderer7.setDisplayChartValues(true);
renderer7.setChartValuesTextSize(18);
//renderer7.setLineWidth((float) 50.0d);
// This is how the "Graph" itself will look like
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setChartTitle("Performance");
mRenderer.setYTitle("Rating");
mRenderer.setChartTitleTextSize(25);
mRenderer.setAxisTitleTextSize(25);
mRenderer.setAxesColor(titleColor);
mRenderer.setLabelsColor(titleColor);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(background);
mRenderer.setMarginsColor(background);
mRenderer.setZoomEnabled(false,false);
mRenderer.setPanEnabled(false,false);
mRenderer.setMargins(new int[] { 20, 90, 15, 20 });
mRenderer.setShowGridY(true);
mRenderer.setLabelsTextSize(15);
mRenderer.setXLabels(0);
mRenderer.setXAxisMin(0);
mRenderer.setYAxisMin(0);
mRenderer.setXAxisMax(8);
mRenderer.setYAxisMax(100);
mRenderer.addXTextLabel(1, "Bar 1");
mRenderer.addXTextLabel(2, "Bar 2");
mRenderer.addXTextLabel(3, "Bar 3");
mRenderer.addXTextLabel(4, "Bar 4");
mRenderer.addXTextLabel(5, "Bar 5");
mRenderer.addXTextLabel(6, "Bar 6");
mRenderer.addXTextLabel(7, "Bar 7");
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(renderer2);
mRenderer.addSeriesRenderer(renderer3);
mRenderer.addSeriesRenderer(renderer4);
mRenderer.addSeriesRenderer(renderer5);
mRenderer.addSeriesRenderer(renderer6);
mRenderer.addSeriesRenderer(renderer7);
//mRenderer.setBarWidth(80);
mRenderer.setBarSpacing(0);
Intent intent = ChartFactory.getBarChartIntent(context, dataset,mRenderer, Type.DEFAULT);
return intent;
}
}
所以,我想要实现的是让所有条形图显示在图表上,BarWidth为80,并将XTextLabel与每个相应的条形对齐。
提前致谢
答案 0 :(得分:0)
所以我之前尝试过这个,但我似乎做了很多改变并尝试了不同的方法,我没有意识到这已经奏效了。因此,使用上面发布的代码,我只需更改
mRenderer.setBarWidth(100);
mRenderer.setBarSpacing(-50);
Intent intent = ChartFactory.getBarChartIntent(context, dataset,mRenderer, Type.DEFAULT);
到
Intent intent = ChartFactory.getBarChartIntent(context, dataset,mRenderer, Type.STACKED);
我得到了我想要的结果