我正在使用一个优秀的库 AChart 引擎来生成条形图。所有功能都运行良好,但我有几件事无法解决:
这是我的屏幕截图:
现在有两件事需要修复:
1) 向右滚动时隐藏系列,目前系列 March 即使在 0,0 之后也是可见的。
2) 网格线在图表的顶部,但它应该在图表的后面。
3) 如何在右上角而不是底部显示图例。
这是我的以下代码:
private void openChart(){
int[] x = { 10,20,30,40,50,60,70,80 };
// int[] y = { 10,20,30,40,50,60,70 };
int[] income = { 20,25,27,30,28,35,37,38};
int[] expense = {22, 27, 29, 28, 26, 30, 33, 34 };
int[] sample = {22, 27, 29, 28, 26, 30, 33, 34};
// Creating an XYSeries for Income
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Expense
XYSeries expenseSeries = new XYSeries("Expense");
// Creating an XYSeries for Expense
XYSeries sampleseries = new XYSeries("Sample");
// Adding data to Income and Expense Series
for(int i=0;i<x.length;i++){
incomeSeries.add(i,income[i]);
expenseSeries.add(i,expense[i]);
sampleseries.add(i,sample[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
dataset.addSeries(expenseSeries);
// Adding Expense Series to dataset
dataset.addSeries(sampleseries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setChartValuesTextAlign(Align.CENTER);
incomeRenderer.setChartValuesTextSize(18);
incomeRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
expenseRenderer.setColor(Color.rgb(220, 80, 80));
expenseRenderer.setFillPoints(true);
expenseRenderer.setLineWidth(2);
expenseRenderer.setChartValuesTextAlign(Align.CENTER);
expenseRenderer.setChartValuesTextSize(18);
expenseRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer samplseries = new XYSeriesRenderer();
samplseries.setColor(Color.rgb(120, 40, 40));
samplseries.setFillPoints(true);
samplseries.setLineWidth(2);
samplseries.setChartValuesTextAlign(Align.CENTER);
samplseries.setChartValuesTextSize(18);
samplseries.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setMargins(new int[]{30,50,60,0});
multiRenderer.setXRoundedLabels(true);
multiRenderer.setLegendTextSize(24);
multiRenderer.setZoomRate(0.2f);
multiRenderer.setZoomEnabled(false, false);
multiRenderer.setBarSpacing(0.3f);
multiRenderer.setXAxisMin(-1);
multiRenderer.setXAxisMax(5);
multiRenderer.setYAxisMin(0);
multiRenderer.setYAxisMax(50);
multiRenderer.setAxisTitleTextSize(20);
multiRenderer.setAxesColor(Color.BLACK);
multiRenderer.setGridColor(Color.GRAY);
multiRenderer.setShowGridX(true);
multiRenderer.setXLabels(0);
multiRenderer.setPanLimits(new double[] { -1, x.length, 0, x.length});
multiRenderer.setLabelsColor(Color.BLACK);
multiRenderer.setLabelsTextSize(20);
multiRenderer.setXLabelsColor(Color.BLACK);
multiRenderer.setXTitle("Tests");
multiRenderer.setYLabelsAlign(Align.RIGHT);
multiRenderer.setYLabelsColor(0, Color.BLACK);
multiRenderer.setYTitle("Scores");
for(int i=0; i< x.length;i++){
multiRenderer.addXTextLabel(i, mMonth[i]);
}
// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
multiRenderer.addSeriesRenderer(expenseRenderer);
multiRenderer.addSeriesRenderer(samplseries);
// Creating an intent to plot bar chart using dataset and multipleRenderer
Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);
// Start Activity
startActivity(intent);
}