MPAndroidChart - 如何在折线图底部绘制条形

时间:2018-01-21 12:32:45

标签: android charts mpandroidchart

我想用股票绘制股票的历史价格图表,类似于:

enter image description here

使用MPAndroidChart,我设法使用CombinedChart获得此结果:

enter image description here

这是我的代码段:

CombinedChart mChart = (CombinedChart) findViewById(R.id.chart);

final ArrayList<BarEntry> barList = new ArrayList<>();
final ArrayList<Entry> lineList = new ArrayList<>();

// add Bar & Line data entries
.....

// bar depends on LEFT y-axis
BarDataSet barSet = new BarDataSet(barList, "");
barSet.setAxisDependency(YAxis.AxisDependency.LEFT);
BarData barData = new BarData(barSet);

// line depends on RIGHT y-axis
LineDataSet lineSet = new LineDataSet(lineList, "");
lineSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
LineData lineData = new LineData(lineSet);

// set data to chart
....

float barYMax = barData.getYMax();
float lineYMin = lineData.getYMin();

// to make bar appears at bottom
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMaximum(barYMax * 10);

// to make line appears at top
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setAxisMinimum(lineYMin * 0.5f);

为了"Line Chart" appears above "Bar Chart"

  1. 条形图取决于左侧y轴。为了使条形图显示在底部,我使用leftAxis.setAxisMaximum(barYMax * 10)使其缩放更大。

  2. 折线图取决于右y轴。为了换班,我做rightAxis.setAxisMinimum(lineYMin * 0.5f)

  3. 我想知道有更好的推荐方法来实现这种效果吗?

    根据我的解决方案,折线图可能仍然部分重叠。理想情况下,我希望no overlapping能够实现line appears completely on top of bar

0 个答案:

没有答案