下面是我的条形图。
我想要实现的是将xLabels定位在网格线之间。 未对齐到X网格线LEFT \ CENTER \ RIGHT。
e.g。标签Mon需要放在第一个条的中间(条形值为20)
这可能使用achart吗?
真的很感激帮助。
这是它的代码:
public class AChartEngingFragment extends Fragment {
private GraphicalView chartView;
private String[] weekDays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat","Sun"};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_achart_engine, container, false);
final Resources resources = getResources();
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.chart);
chartView = createIntent();
linearLayout.addView(chartView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return v;
}
public GraphicalView createIntent() {
String[] titles = new String[]{"Benchmark line"};
//List<double []> values = new ArrayList<double []>(); //as y1
//values.add(new double[] {20.0, 10.0, 30.0, 25.0, 40.0});
double[] y1 = new double[]{20.0, 10.0, 30.0, 25.0, 40.0, 15.0, 19.2};
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < titles.length; i++) {
x.add(new double[]{0, 1, 2, 3, 4, 5, 6});
}
/*****************************************/
List<double[]> lineValues = new ArrayList<double[]>(); //as values
lineValues.add(new double[]{10, 10, 10, 10, 10, 10, 10, 10, 10});
int[] colors = new int[]{Color.parseColor("#ea2e49")}; //pink
XYMultipleSeriesRenderer mRenderer = buildBarRenderer(colors);
mRenderer.setPointSize(0);
int length = mRenderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
XYSeriesRenderer seriesRenderer = (XYSeriesRenderer) mRenderer.getSeriesRendererAt(i);
seriesRenderer.setDisplayChartValues(false);
seriesRenderer.setLineWidth(2);
seriesRenderer.setFillPoints(true);
}
mRenderer.setOrientation(XYMultipleSeriesRenderer.Orientation.HORIZONTAL);
setChartSettings(mRenderer, "Average Customer Waiting Time", "Table Categories", "Minutes", 0, 7.5, 0, 30, Color.BLACK, Color.BLACK);
mRenderer.setXLabels(0);
mRenderer.setYLabels(10);
mRenderer.setShowGrid(true);
mRenderer.addXTextLabel(1, weekDays[0]);
mRenderer.addXTextLabel(2, weekDays[1]);
mRenderer.addXTextLabel(3, weekDays[2]);
mRenderer.addXTextLabel(4, weekDays[3]);
mRenderer.addXTextLabel(5, weekDays[4]);
mRenderer.addXTextLabel(6, weekDays[5]);
mRenderer.addXTextLabel(7, weekDays[6]);
mRenderer.setXLabelsPadding(20f);
mRenderer.setXLabelsAlign(Paint.Align.RIGHT);
mRenderer.setYLabelsAlign(Paint.Align.RIGHT);
XYSeries ySeries = new XYSeries("Average Customer Waiting Time");
ySeries.add(0.5, y1[0]);
ySeries.add(1.5, y1[1]);
ySeries.add(2.5, y1[2]);
ySeries.add(3.5, y1[3]);
ySeries.add(4.5, y1[4]);
ySeries.add(5.5, y1[5]);
ySeries.add(6.5, y1[6]);
mRenderer.setBarSpacing(0.5);
XYSeriesRenderer yRenderer = new XYSeriesRenderer();
yRenderer.setColor(Color.parseColor("#77c4d3")); //light blue
XYMultipleSeriesDataset dataset = buildBarDataset(titles, x, lineValues);
dataset.addSeries(0, ySeries);
mRenderer.addSeriesRenderer(0, yRenderer);
yRenderer.setDisplayChartValues(true);
yRenderer.setChartValuesTextSize(20); //text size on each bar
String[] types = new String[]{BarChart.TYPE, LineChart.TYPE};
final GraphicalView grfv = ChartFactory.getCombinedXYChartView(getActivity(), dataset, mRenderer, types);
return grfv;
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
PointStyle[] styles = new PointStyle[]{PointStyle.CIRCLE};
mRenderer.setAxisTitleTextSize(17);
mRenderer.setChartTitleTextSize(25);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setBarSpacing(-0.5);
mRenderer.setPointSize(1f);
mRenderer.setMarginsColor(Color.parseColor("#EEEDED"));
mRenderer.setXLabelsColor(Color.BLACK);
mRenderer.setYLabelsColor(0, Color.BLACK);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.parseColor("#FBFBFC"));
int length = colors.length;
for (int i = 0; i < length; i++) { //build benchmark line
XYSeriesRenderer r = new XYSeriesRenderer(); //change
r.setColor(colors[i]);
r.setPointStyle(styles[i]);
mRenderer.addSeriesRenderer(r);
}
return mRenderer;
}
protected void setChartSettings(XYMultipleSeriesRenderer mRenderer, String title, String xTitle, String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor) {
mRenderer.setChartTitle(title);
mRenderer.setYLabelsAlign(Paint.Align.RIGHT);
mRenderer.setXTitle(xTitle);
mRenderer.setYTitle(yTitle);
mRenderer.setXAxisMin(xMin);
mRenderer.setXAxisMax(xMax);
mRenderer.setYAxisMin(yMin);
mRenderer.setYAxisMax(yMax);
mRenderer.setMargins(new int[]{10, 65, 10, 15});
mRenderer.setAxesColor(axesColor);
mRenderer.setLabelsColor(labelsColor);
mRenderer.setXLabelsAlign(Paint.Align.RIGHT);
//mRenderer.setZoomEnabled(false);
mRenderer.setPanEnabled(false, false);
mRenderer.setZoomEnabled(false, false);
}
protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> xValues, List<double[]> yValues) {
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
int length = titles.length;
for (int i = 0; i < length; i++) {
XYSeries series = new XYSeries(titles[i], 0);
double[] xV = xValues.get(i);
double[] yV = yValues.get(i);
int seriesLength = xV.length;
for (int k = 0; k < seriesLength; k++) {
series.add(xV[k], yV[k]);
}
dataset.addSeries(series);
}
return dataset;
}
}
答案 0 :(得分:0)
您需要更新
的值 ySeries.add(1, y1[0]);
ySeries.add(2, y1[1]);
ySeries.add(3, y1[2]);
ySeries.add(4, y1[3]);
ySeries.add(5, y1[4]);
ySeries.add(6, y1[5]);
ySeries.add(7, y1[6]);
更改
mRenderer.setXLabelsAlign(Paint.Align.CENTER);
你会得到下一个结果