2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MPAndroidChartで棒グラフと折れ線グラフを同時に描く

Last updated at Posted at 2017-01-21

とりあえずコードをのせておきます(あとで清書する)

List<String> labels = new ArrayList<>();
List<BarEntry> bar = new ArrayList<>();
List<Entry> line = new ArrayList<>();
Random random = new Random();
for (int i=0; i<10; i++) {
	// x軸用のラベルはList<String>
    labels.add(String.valueOf(i));
    
    // 各データはList<BarEntry>, List<Entry>などに格納
    // Entry(float value, int index)
    // labelsの長さ以上のインデックスを指定すると実行時エラーが起きる
    bar.add(new BarEntry((float)random.nextInt(10), i));
    line.add(new Entry((float)random.nextInt(5)+3f, i));
}

// エントリーのリスト(List<Entry>) + 名前(String) => DataSet
// ラベルのリスト(List<String>) + DataSet => Data

// 棒グラフ
BarDataSet barDataSet = new BarDataSet(bar, "bar");
BarData barData = new BarData(labels, barDataSet);

// 折れ線グラフ
LineDataSet lineDataSet = new LineDataSet(line, "line");
lineDataSet.setColor(Color.BLACK);
lineDataSet.setLineWidth(1.5f);
lineDataSet.setDrawCircles(false);

LineData lineData = new LineData(labels, lineDataSet);
lineData.setDrawValues(false);

// 組み合わせる
CombinedData data = new CombinedData(labels);
data.setData(lineData);
data.setData(barData);
chart.setData(data);

// チャート
CombinedChart chart = (CombinedChart)findViewById(R.id.combined_chart);
chart.setBackgroundColor(Color.WHITE);
chart.setDrawGridBackground(false);
chart.setDrawBarShadow(false);

chart.setTouchEnabled(true);
chart.setPinchZoom(false);
chart.setDoubleTapToZoomEnabled(false);

chart.setHighlightEnabled(false);
chart.setDescription("");

// x軸設定
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setSpaceBetweenLabels(0);

// 右側y軸設定
chart.getAxisRight().setEnabled(false);

// アニメーション
chart.invalidate();
chart.animateY(2000, Easing.EasingOption.EaseInBack);
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?