7
7
import android .view .WindowManager ;
8
8
9
9
import com .github .mikephil .charting .charts .BarChart ;
10
- import com .github .mikephil .charting .components .Legend ;
11
- import com .github .mikephil .charting .components .Legend .LegendForm ;
12
- import com .github .mikephil .charting .components .Legend .LegendPosition ;
13
10
import com .github .mikephil .charting .components .XAxis ;
14
11
import com .github .mikephil .charting .components .XAxis .XAxisPosition ;
15
12
import com .github .mikephil .charting .components .YAxis ;
16
13
import com .github .mikephil .charting .data .BarData ;
17
14
import com .github .mikephil .charting .data .BarDataSet ;
18
15
import com .github .mikephil .charting .data .BarEntry ;
19
16
import com .github .mikephil .charting .data .Entry ;
20
- import com .github .mikephil .charting .formatter .ValueFormatter ;
21
- import com .github .mikephil .charting .interfaces .datasets .IBarDataSet ;
22
17
import com .github .mikephil .charting .utils .ViewPortHandler ;
23
18
import com .xxmassdeveloper .mpchartexample .notimportant .DemoBase ;
24
19
@@ -42,6 +37,8 @@ protected void onCreate(Bundle savedInstanceState) {
42
37
mChart = (BarChart ) findViewById (R .id .chart1 );
43
38
mChart .setExtraTopOffset (-30f );
44
39
mChart .setExtraBottomOffset (10f );
40
+ mChart .setExtraLeftOffset (70f );
41
+ mChart .setExtraRightOffset (70f );
45
42
46
43
mChart .setDrawBarShadow (false );
47
44
mChart .setDrawValueAboveBar (true );
@@ -60,7 +57,7 @@ protected void onCreate(Bundle savedInstanceState) {
60
57
xAxis .setDrawAxisLine (false );
61
58
xAxis .setSpaceBetweenLabels (2 );
62
59
xAxis .setTextColor (Color .LTGRAY );
63
- xAxis .setTextSize (12f );
60
+ xAxis .setTextSize (13f );
64
61
65
62
YAxis left = mChart .getAxisLeft ();
66
63
left .setDrawLabels (false );
@@ -85,42 +82,35 @@ protected void onCreate(Bundle savedInstanceState) {
85
82
86
83
private void setData (List <Data > dataList ) {
87
84
88
- ArrayList <BarEntry > positiveValues = new ArrayList <BarEntry >();
89
- ArrayList <BarEntry > negativeValues = new ArrayList <BarEntry >();
85
+ ArrayList <BarEntry > values = new ArrayList <BarEntry >();
90
86
String [] dates = new String [dataList .size ()];
87
+ List <Integer > colors = new ArrayList <Integer >();
88
+
89
+ int green = Color .rgb (110 , 190 , 102 );
90
+ int red = Color .rgb (211 , 74 , 88 );
91
91
92
92
for (int i = 0 ; i < dataList .size (); i ++) {
93
93
94
94
Data d = dataList .get (i );
95
95
BarEntry entry = new BarEntry (d .yValue , d .xIndex );
96
+ values .add (entry );
96
97
98
+ dates [i ] = dataList .get (i ).xAxisValue ;
99
+
100
+ // specific colors
97
101
if (d .yValue >= 0 )
98
- positiveValues .add (entry );
102
+ colors .add (red );
99
103
else
100
- negativeValues .add (entry );
101
-
102
- dates [i ] = dataList .get (i ).xAxisValue ;
104
+ colors .add (green );
103
105
}
104
106
105
- int green = Color .rgb (110 , 190 , 102 );
106
- int red = Color .rgb (211 , 74 , 88 );
107
-
108
- BarDataSet positive = new BarDataSet (positiveValues , "Positive" );
109
- positive .setBarSpacePercent (35f );
110
- positive .setColor (red );
111
- positive .setValueTextColor (red );
112
-
113
- BarDataSet negative = new BarDataSet (negativeValues , "Negative" );
114
- negative .setBarSpacePercent (35f );
115
- negative .setColor (green );
116
- negative .setValueTextColor (green );
117
-
118
- ArrayList <IBarDataSet > dataSets = new ArrayList <IBarDataSet >();
119
- dataSets .add (positive );
120
- dataSets .add (negative );
107
+ BarDataSet set = new BarDataSet (values , "Values" );
108
+ set .setBarSpacePercent (40f );
109
+ set .setColors (colors );
110
+ set .setValueTextColor (red );
121
111
122
- BarData data = new BarData (dates , dataSets );
123
- data .setValueTextSize (12f );
112
+ BarData data = new BarData (dates , set );
113
+ data .setValueTextSize (13f );
124
114
data .setValueTypeface (mTf );
125
115
data .setValueFormatter (new ValueFormatter ());
126
116
0 commit comments