我正在尝试使用2个条形图和一个折线图数据集创建组合图表。所有数据集的X轴应相同。在barcharts之间应该是使用BarData setGroupSpace(300f)设置的300%空间。这就是问题所在,我无法让线图也有这个空间,所以线图的点总是在这2个柱之间。但看起来线图有自己的X轴。另外X轴上的标签应该移动到以2条为中心。 有任何提示怎么做?
private void setupChart(){
chart.setDrawOrder(new CombinedChart.DrawOrder[]{CombinedChart.DrawOrder.BAR, CombinedChart.DrawOrder.BUBBLE, CombinedChart.DrawOrder.CANDLE,
CombinedChart.DrawOrder.LINE, CombinedChart.DrawOrder.SCATTER});
chart.getAxisLeft().setDrawGridLines(false);
chart.getXAxis().setDrawGridLines(false);
XAxis x = chart.getXAxis();
x.setTextSize(14f);
x.setPosition(XAxis.XAxisPosition.BOTTOM);
YAxis y = chart.getAxisLeft();
y.setEnabled(false);
chart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);
chart.setVisibleXRangeMaximum(10f);
chart.setVisibleXRangeMinimum(10f);
chart.setDescription("");
ArrayList<String> labels = new ArrayList<String>();
for (int i = 0; i < itemcount; i++) {
String timeString = formatter.format(i * 1000);
labels.add(timeString);
}
CombinedData data = new CombinedData(labels);
data.setData(generateLineData());
data.setData(generateBarData());
chart.setData(data);
GraphPopup popup = new GraphPopup(this, R.layout.view_graph_tooltip);
chart.setMarkerView(popup);
Legend legend = chart.getLegend();
legend.setTextSize(14f);
legend.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
//chart.animateY(2000);
chart.setVisibleXRangeMaximum(10f);
chart.setVisibleXRangeMinimum(10f);
chart.moveViewToX(entries1.size());
buttonAdd = (Button) findViewById(R.id.buttonAdd);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataset0.addEntry(new BarEntry(3, dataset0.getEntryCount()));
dataset1.addEntry(new BarEntry(12, dataset1.getEntryCount()));
// labels.add(labels.size(), "x" + labels.size());
chart.notifyDataSetChanged(); // let the chart know it's data changed
chart.setVisibleXRangeMaximum(10f);
chart.setVisibleXRangeMinimum(10f);
chart.moveViewToX(entries1.size());
//chart.invalidate(); // refresh
}
});
}
private LineData generateLineData() {
ArrayList<Entry> entries = new ArrayList<Entry>();
ArrayList<String> labels = new ArrayList<String>();
for (int index = 0; index < itemcount; index++) {
entries.add(new Entry(rand.nextInt(22), index));
String timeString = formatter.format(index * 3600*24*1000);
labels.add(timeString);
}
LineData d = new LineData(labels);
LineDataSet set = new LineDataSet(entries, "Line DataSet");
set.setColor(Color.RED);
set.setLineWidth(2.5f);
set.setCircleColor(Color.RED);
set.setCircleSize(5f);
set.setFillColor(Color.WHITE);
set.setDrawCubic(true);
set.setDrawValues(true);
set.setValueTextSize(10f);
set.setValueTextColor(Color.RED);
set.setAxisDependency(YAxis.AxisDependency.LEFT);
d.addDataSet(set);
return d;
}
private BarData generateBarData() {
ArrayList<String> labels0 = new ArrayList<String>();
for (int i = 0; i < itemcount; i++) {
BarEntry entry = new BarEntry(rand.nextInt(22) + 8, i);
entries.add(entry);
String timeString = formatter.format(i * 3600*24*1000);
labels0.add(timeString);
}
dataset0.setColor(getResources().getColor(R.color.graph_bar));
dataset0.setHighLightColor(getResources().getColor(R.color.graph_bar_selected));
dataset0.setBarSpacePercent(0f);
ArrayList<String> labels1 = new ArrayList<String>();
for (int j = 0; j < itemcount; j++) {
BarEntry entry = new BarEntry(rand.nextInt(22) + 0, j);
entries1.add(entry);
String timeString = formatter.format(j * 3600*24*1000);
labels1.add(timeString);
}
dataset1.setColor(getResources().getColor(R.color.color1));
dataset1.setHighLightColor(getResources().getColor(R.color.color2));
dataset1.setBarSpacePercent(0f);
BarData d = new BarData(labels0);
d.addDataSet(dataset0);
d.addDataSet(dataset1);
d.setGroupSpace(300f);
return d;
}
答案 0 :(得分:0)
各个酒吧之间的空间:
barDataSet.setBarSpacePercent(...);
DataSet之间的空间:
barData.setGroupSpace(...);
以百分比表示的值。
答案 1 :(得分:-3)
尝试使用mpandroidchart。它已经实现了。