Skip to content

Commit 4826f3b

Browse files
committed
Improved combined example, add grouped bars
1 parent 95dd249 commit 4826f3b

3 files changed

Lines changed: 31 additions & 14 deletions

File tree

MPChartExample/src/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,36 @@ private LineData generateLineData() {
132132

133133
private BarData generateBarData() {
134134

135-
BarData d = new BarData();
136-
d.setBarWidth(0.9f);
135+
ArrayList<BarEntry> entries1 = new ArrayList<BarEntry>();
136+
ArrayList<BarEntry> entries2 = new ArrayList<BarEntry>();
137137

138-
ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
138+
for (int index = 0; index < itemcount; index++) {
139+
entries1.add(new BarEntry(0, getRandom(25, 25)));
140+
entries2.add(new BarEntry(0, getRandom(25, 25)));
141+
}
139142

140-
for (int index = 0; index < itemcount; index++)
141-
entries.add(new BarEntry(index + 0.5f, getRandom(25, 25)));
143+
BarDataSet set1 = new BarDataSet(entries1, "Bar 1");
144+
set1.setColor(Color.rgb(60, 220, 78));
145+
set1.setValueTextColor(Color.rgb(60, 220, 78));
146+
set1.setValueTextSize(10f);
147+
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
142148

143-
BarDataSet set = new BarDataSet(entries, "Bar DataSet");
144-
set.setColor(Color.rgb(60, 220, 78));
145-
set.setValueTextColor(Color.rgb(60, 220, 78));
146-
set.setValueTextSize(10f);
147-
d.addDataSet(set);
149+
BarDataSet set2 = new BarDataSet(entries2, "Bar 2");
150+
set2.setColor(Color.rgb(61, 165, 255));
151+
set2.setValueTextColor(Color.rgb(61, 165, 255));
152+
set2.setValueTextSize(10f);
153+
set2.setAxisDependency(YAxis.AxisDependency.LEFT);
148154

149-
set.setAxisDependency(YAxis.AxisDependency.LEFT);
155+
float groupSpace = 0.06f;
156+
float barSpace = 0.02f; // x2 dataset
157+
float barWidth = 0.45f; // x2 dataset
158+
// (0.45 + 0.02) * 2 + 0.06 = 1.00 -> interval per "group"
159+
160+
BarData d = new BarData(set1, set2);
161+
d.setBarWidth(barWidth);
162+
163+
// make this BarData object grouped
164+
d.groupBars(0, groupSpace, barSpace); // start at x = 0
150165

151166
return d;
152167
}
@@ -198,7 +213,7 @@ protected BubbleData generateBubbleData() {
198213

199214
for (int index = 0; index < itemcount; index++) {
200215
float y = getRandom(10, 105);
201-
float size = getRandom(50, 105);
216+
float size = getRandom(100, 105);
202217
entries.add(new BubbleEntry(index + 0.5f, y, size));
203218
}
204219

MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ public void setFitBars(boolean enabled) {
201201

202202
/**
203203
* Groups all BarDataSet objects this data object holds together by modifying the x-position of their entries.
204-
* Leaves space as specified by the parameters.
204+
* Previously set x-positions of entries will be overwritten. Leaves space between bars and groups as specified
205+
* by the parameters.
205206
* Calls notifyDataSetChanged() afterwards.
206207
*
207208
* @param fromX the starting point on the x-axis where the grouping should begin

MPChartLib/src/main/java/com/github/mikephil/charting/data/BarData.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public float getBarWidth() {
4545

4646
/**
4747
* Groups all BarDataSet objects this data object holds together by modifying the x-position of their entries.
48-
* Leaves space as specified by the parameters.
48+
* Previously set x-positions of entries will be overwritten. Leaves space between bars and groups as specified
49+
* by the parameters.
4950
* Do not forget to call notifyDataSetChanged() on your BarChart object after calling this method.
5051
*
5152
* @param fromX the starting point on the x-axis where the grouping should begin

0 commit comments

Comments
 (0)