我正在尝试使用Android中的achart引擎库绘制图表(如下图所示)。
DESIRED_CHART.png
这是我取得的成就
CURRENT_CHART.png
绘制此代码的代码如下
private void drawChart(){
int[] x = { 1,2,3,4,5,6,7,8 };
int[] happyness = { 5,2,4,1,0,4,5,0};
int[] energy = { 5,4,3,1,4,3,5,0};
int[] strenth = { 2,0,1,3,2,4,3,0};
int[] endurance = { 0,2,1,4,3,5,2,0};
// Creating an XYSeries for Income
XYSeries happynessSeries = new XYSeries("Happyness");
// Creating an XYSeries for Income
XYSeries energySeries = new XYSeries("Energy");
// Adding data to Income and Expense Series
XYSeries strenthSeries = new XYSeries("Strenth");
XYSeries enduranceSeries = new XYSeries("Endurance");
for(int i=0;i<x.length;i++){
happynessSeries.add(x[i], happyness[i]);
energySeries.add(x[i],energy[i]);
strenthSeries.add(x[i],strenth[i]);
enduranceSeries.add(x[i],endurance[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(happynessSeries);
// Adding Expense Series to dataset
dataset.addSeries(energySeries);
dataset.addSeries(strenthSeries);
dataset.addSeries(enduranceSeries);
// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer happynessRenderer = new XYSeriesRenderer();
happynessRenderer.setColor(Color.parseColor("#f4ed21"));//chart border color
happynessRenderer.setPointStyle(PointStyle.CIRCLE);
happynessRenderer.setFillPoints(true);
happynessRenderer.setLineWidth(2);
happynessRenderer.setDisplayChartValues(true);
happynessRenderer.setFillBelowLine(true);
happynessRenderer.setFillBelowLineColor(Color.parseColor("#bbf4ed21"));
// Creating XYSeriesRenderer to customize expenseSeries
XYSeriesRenderer energyRenderer = new XYSeriesRenderer();
energyRenderer.setColor(Color.parseColor("#20bff2"));
energyRenderer.setPointStyle(PointStyle.CIRCLE);
energyRenderer.setFillPoints(true);
energyRenderer.setLineWidth(2);
energyRenderer.setDisplayChartValues(true);
energyRenderer.setFillBelowLine(true);
energyRenderer.setFillBelowLineColor(Color.parseColor("#bb20bff2"));
// strenth
XYSeriesRenderer strenthRenderer = new XYSeriesRenderer();
strenthRenderer.setColor(Color.parseColor("#f97b1d"));
strenthRenderer.setPointStyle(PointStyle.CIRCLE);
strenthRenderer.setFillPoints(true);
strenthRenderer.setLineWidth(2);
strenthRenderer.setDisplayChartValues(true);
strenthRenderer.setFillBelowLine(true);
strenthRenderer.setFillBelowLineColor(Color.parseColor("#bbf97b1d"));
// endurance
XYSeriesRenderer enduranceRenderer = new XYSeriesRenderer();
enduranceRenderer.setColor(Color.parseColor("#c68c4d"));
enduranceRenderer.setPointStyle(PointStyle.CIRCLE);
enduranceRenderer.setFillPoints(true);
enduranceRenderer.setLineWidth(2);
enduranceRenderer.setDisplayChartValues(true);
enduranceRenderer.setFillBelowLine(true);
enduranceRenderer.setFillBelowLineColor(Color.parseColor("#bbc68c4d"));
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setZoomButtonsVisible(true);
for(int i=0;i<x.length;i++){
multiRenderer.addXTextLabel(i+1, mMonth[i]);
}
multiRenderer.setLabelsTextSize(50);
multiRenderer.setYLabels(0);
multiRenderer.setMargins(new int[]{0,0,0,0});
multiRenderer.addSeriesRenderer(happynessRenderer);
multiRenderer.addSeriesRenderer(energyRenderer);
multiRenderer.addSeriesRenderer(strenthRenderer);
multiRenderer.addSeriesRenderer(enduranceRenderer);
multiRenderer.setPanEnabled(false, false);
multiRenderer.setZoomEnabled(false, false);
multiRenderer.setZoomEnabled(false, false);
// Creating an intent to plot line chart using dataset and multipleRenderer
mChart =(GraphicalView) ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);
llChart.addView(mChart);
}
第一个和最后一个x轴点数值(Mon bb 3和Mon Feb 9)正在削减。我正在努力将这些值显示为DESIRED_CHART.png
如何删除Xaxis值背后的黑色背景?
答案 0 :(得分:6)
更改背景颜色
multiRenderer.setMarginsColor(Color.GREEN);
并给出边距以显示整个x轴点值文本
multiRenderer.setMargins(new int[]{0,40,0,40});
隐藏标题
multiRenderer.setChartTitle("");
隐藏图例
multiRenderer.setShowLegend(false);
其中multiRenderer
为XYMultipleSeriesRenderer
。