Skip to content

Commit 6e614c0

Browse files
committed
Indroducing chart minimum height of 50dp if no other height attribute is set (fix issue PhilJay#566, PhilJay#720)
1 parent 04bb22b commit 6e614c0

5 files changed

Lines changed: 144 additions & 14 deletions

File tree

MPChartExample/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<activity android:name="CombinedChartActivity"></activity>
5151
<activity android:name="PerformanceLineChart"></activity>
5252
<activity android:name="BarChartActivitySinus"></activity>
53+
<activity android:name="ScrollViewActivity"></activity>
5354
</application>
5455

5556
</manifest>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content" >
5+
6+
<LinearLayout
7+
android:layout_width="match_parent"
8+
android:layout_height="wrap_content"
9+
android:orientation="vertical" >
10+
11+
<TextView
12+
android:layout_width="match_parent"
13+
android:layout_height="30dp"
14+
android:gravity="center"
15+
android:text="START OF SCROLLVIEW" />
16+
17+
<!-- THIS IS JUST A PLACEHOLDER -->
18+
19+
<View
20+
android:layout_width="match_parent"
21+
android:layout_height="350dp" />
22+
23+
<com.github.mikephil.charting.charts.BarChart
24+
android:id="@+id/chart1"
25+
android:layout_width="match_parent"
26+
android:layout_height="450dp" />
27+
28+
<!-- THIS IS JUST A PLACEHOLDER -->
29+
30+
<View
31+
android:layout_width="match_parent"
32+
android:layout_height="700dp" />
33+
34+
<TextView
35+
android:layout_width="match_parent"
36+
android:layout_height="30dp"
37+
android:gravity="center"
38+
android:text="END OF SCROLLVIEW" />
39+
40+
</LinearLayout>
41+
42+
</ScrollView>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
package com.xxmassdeveloper.mpchartexample;
3+
4+
import android.os.Bundle;
5+
import android.view.WindowManager;
6+
7+
import com.github.mikephil.charting.charts.BarChart;
8+
import com.github.mikephil.charting.components.XAxis;
9+
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
10+
import com.github.mikephil.charting.data.BarData;
11+
import com.github.mikephil.charting.data.BarDataSet;
12+
import com.github.mikephil.charting.data.BarEntry;
13+
import com.github.mikephil.charting.utils.ColorTemplate;
14+
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
15+
16+
import java.util.ArrayList;
17+
18+
public class ScrollViewActivity extends DemoBase {
19+
20+
private BarChart mChart;
21+
22+
@Override
23+
protected void onCreate(Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
26+
WindowManager.LayoutParams.FLAG_FULLSCREEN);
27+
setContentView(R.layout.activity_scrollview);
28+
29+
mChart = (BarChart) findViewById(R.id.chart1);
30+
31+
mChart.setDescription("");
32+
33+
// scaling can now only be done on x- and y-axis separately
34+
mChart.setPinchZoom(false);
35+
36+
mChart.setDrawBarShadow(false);
37+
mChart.setDrawGridBackground(false);
38+
39+
XAxis xAxis = mChart.getXAxis();
40+
xAxis.setPosition(XAxisPosition.BOTTOM);
41+
xAxis.setLabelsToSkip(0);
42+
xAxis.setDrawGridLines(false);
43+
44+
mChart.getAxisLeft().setDrawGridLines(false);
45+
46+
mChart.getLegend().setEnabled(false);
47+
48+
setData(10);
49+
}
50+
51+
private void setData(int count) {
52+
53+
ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
54+
ArrayList<String> xVals = new ArrayList<String>();
55+
56+
for (int i = 0; i < count; i++) {
57+
float val = (float) (Math.random() * count) + 15;
58+
yVals.add(new BarEntry((int) val, i));
59+
xVals.add((int) val + "");
60+
}
61+
62+
BarDataSet set = new BarDataSet(yVals, "Data Set");
63+
set.setColors(ColorTemplate.VORDIPLOM_COLORS);
64+
set.setDrawValues(false);
65+
66+
BarData data = new BarData(xVals, set);
67+
68+
mChart.setData(data);
69+
mChart.invalidate();
70+
mChart.animateY(800);
71+
}
72+
}

MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.xxmassdeveloper.mpchartexample.RadarChartActivitry;
4444
import com.xxmassdeveloper.mpchartexample.RealtimeLineChartActivity;
4545
import com.xxmassdeveloper.mpchartexample.ScatterChartActivity;
46+
import com.xxmassdeveloper.mpchartexample.ScrollViewActivity;
4647
import com.xxmassdeveloper.mpchartexample.StackedBarActivity;
4748
import com.xxmassdeveloper.mpchartexample.fragments.SimpleChartDemo;
4849

@@ -120,6 +121,9 @@ protected void onCreate(Bundle savedInstanceState) {
120121
objects.add(new ContentItem(
121122
"Sinus Bar Chart",
122123
"A Bar Chart plotting the sinus function with 8.000 values."));
124+
objects.add(new ContentItem(
125+
"Chart in ScrollView",
126+
"This demonstrates how to use a chart inside a ScrollView."));
123127

124128
MyAdapter adapter = new MyAdapter(this, objects);
125129

@@ -241,6 +245,10 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
241245
i = new Intent(this, BarChartActivitySinus.class);
242246
startActivity(i);
243247
break;
248+
case 25:
249+
i = new Intent(this, ScrollViewActivity.class);
250+
startActivity(i);
251+
break;
244252
}
245253

246254
overridePendingTransition(R.anim.move_right_in_activity, R.anim.move_left_out_activity);

MPChartLib/src/com/github/mikephil/charting/charts/Chart.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,20 +1510,6 @@ public boolean saveToGallery(String fileName, int quality) {
15101510
? false : true;
15111511
}
15121512

1513-
@Override
1514-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1515-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
1516-
}
1517-
1518-
@Override
1519-
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
1520-
1521-
// prepareContentRect();
1522-
for (int i = 0; i < getChildCount(); i++) {
1523-
getChildAt(i).layout(left, top, right, bottom);
1524-
}
1525-
}
1526-
15271513
/** tasks to be done after the view is setup */
15281514
protected ArrayList<Runnable> mJobs = new ArrayList<Runnable>();
15291515

@@ -1555,6 +1541,27 @@ public ArrayList<Runnable> getJobs() {
15551541
return mJobs;
15561542
}
15571543

1544+
@Override
1545+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
1546+
1547+
for (int i = 0; i < getChildCount(); i++) {
1548+
getChildAt(i).layout(left, top, right, bottom);
1549+
}
1550+
}
1551+
1552+
@Override
1553+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
1554+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
1555+
int size = (int) Utils.convertDpToPixel(50f);
1556+
setMeasuredDimension(
1557+
Math.max(getSuggestedMinimumWidth(),
1558+
resolveSize(size,
1559+
widthMeasureSpec)),
1560+
Math.max(getSuggestedMinimumHeight(),
1561+
resolveSize(size,
1562+
heightMeasureSpec)));
1563+
}
1564+
15581565
@Override
15591566
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
15601567
if (mLogEnabled)

0 commit comments

Comments
 (0)