• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 package com.github.mikephil.charting.data;
3 
4 import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
5 
6 import java.util.List;
7 
8 /**
9  * Data object that represents all data for the BarChart.
10  *
11  * @author Philipp Jahoda
12  */
13 public class BarData extends BarLineScatterCandleBubbleData<IBarDataSet> {
14 
15     /**
16      * the width of the bars on the x-axis, in values (not pixels)
17      */
18     private float mBarWidth = 0.85f;
19 
BarData()20     public BarData() {
21         super();
22     }
23 
BarData(IBarDataSet... dataSets)24     public BarData(IBarDataSet... dataSets) {
25         super(dataSets);
26     }
27 
BarData(List<IBarDataSet> dataSets)28     public BarData(List<IBarDataSet> dataSets) {
29         super(dataSets);
30     }
31 
32     /**
33      * Sets the width each bar should have on the x-axis (in values, not pixels).
34      * Default 0.85f
35      *
36      * @param mBarWidth
37      */
setBarWidth(float mBarWidth)38     public void setBarWidth(float mBarWidth) {
39         this.mBarWidth = mBarWidth;
40     }
41 
getBarWidth()42     public float getBarWidth() {
43         return mBarWidth;
44     }
45 
46     /**
47      * Groups all BarDataSet objects this data object holds together by modifying the x-value of their entries.
48      * Previously set x-values of entries will be overwritten. Leaves space between bars and groups as specified
49      * by the parameters.
50      * Do not forget to call notifyDataSetChanged() on your BarChart object after calling this method.
51      *
52      * @param fromX      the starting point on the x-axis where the grouping should begin
53      * @param groupSpace the space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f
54      * @param barSpace   the space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
55      */
groupBars(float fromX, float groupSpace, float barSpace)56     public void groupBars(float fromX, float groupSpace, float barSpace) {
57 
58         int setCount = mDataSets.size();
59         if (setCount <= 1) {
60             throw new RuntimeException("BarData needs to hold at least 2 BarDataSets to allow grouping.");
61         }
62 
63         IBarDataSet max = getMaxEntryCountSet();
64         int maxEntryCount = max.getEntryCount();
65 
66         float groupSpaceWidthHalf = groupSpace / 2f;
67         float barSpaceHalf = barSpace / 2f;
68         float barWidthHalf = mBarWidth / 2f;
69 
70         float interval = getGroupWidth(groupSpace, barSpace);
71 
72         for (int i = 0; i < maxEntryCount; i++) {
73 
74             float start = fromX;
75             fromX += groupSpaceWidthHalf;
76 
77             for (IBarDataSet set : mDataSets) {
78 
79                 fromX += barSpaceHalf;
80                 fromX += barWidthHalf;
81 
82                 if (i < set.getEntryCount()) {
83 
84                     BarEntry entry = set.getEntryForIndex(i);
85 
86                     if (entry != null) {
87                         entry.setX(fromX);
88                     }
89                 }
90 
91                 fromX += barWidthHalf;
92                 fromX += barSpaceHalf;
93             }
94 
95             fromX += groupSpaceWidthHalf;
96             float end = fromX;
97             float innerInterval = end - start;
98             float diff = interval - innerInterval;
99 
100             // correct rounding errors
101             if (diff > 0 || diff < 0) {
102                 fromX += diff;
103             }
104         }
105 
106         notifyDataChanged();
107     }
108 
109     /**
110      * In case of grouped bars, this method returns the space an individual group of bar needs on the x-axis.
111      *
112      * @param groupSpace
113      * @param barSpace
114      * @return
115      */
getGroupWidth(float groupSpace, float barSpace)116     public float getGroupWidth(float groupSpace, float barSpace) {
117         return mDataSets.size() * (mBarWidth + barSpace) + groupSpace;
118     }
119 }
120