GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Eliminate allocs - Rect and RectF buffers (#1892) · FlyingAndroid/MPAndroidChart@d372fd3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • MPChartExample/src/com/xxmassdeveloper/mpchartexample
    • BarChartActivity.java
    • HorizontalBarChartActivity.java
  • MPChartLib/src/main/java/com/github/mikephil/charting
    • charts
      • BarChart.java
      • HorizontalBarChart.java
    • renderer
      • PieChartRenderer.java
    • utils
      • Utils.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java‎

Copy file name to clipboardExpand all lines: MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import android.annotation.SuppressLint;
55
import android.graphics.PointF;
6+
import android.graphics.Rect;
67
import android.graphics.RectF;
78
import android.os.Bundle;
89
import android.util.Log;
@@ -257,14 +258,16 @@ private void setData(int count, float range) {
257258
}
258259
}
259260

261+
protected RectF mOnValueSelectedRectF = new RectF();
260262
@SuppressLint("NewApi")
261263
@Override
262264
public void onValueSelected(Entry e, Highlight h) {
263265

264266
if (e == null)
265267
return;
266268

267-
RectF bounds = mChart.getBarBounds((BarEntry) e);
269+
RectF bounds = mOnValueSelectedRectF;
270+
mChart.getBarBounds((BarEntry) e, bounds);
268271
MPPointF position = mChart.getPosition(e, AxisDependency.LEFT);
269272

270273
Log.i("bounds", bounds.toString());
Collapse file

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java‎

Copy file name to clipboardExpand all lines: MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import android.annotation.SuppressLint;
55
import android.graphics.PointF;
6+
import android.graphics.Rect;
67
import android.graphics.RectF;
78
import android.os.Bundle;
89
import android.util.Log;
@@ -246,14 +247,17 @@ private void setData(int count, float range) {
246247
}
247248
}
248249

250+
protected RectF mOnValueSelectedRectF = new RectF();
249251
@SuppressLint("NewApi")
250252
@Override
251253
public void onValueSelected(Entry e, Highlight h) {
252254

253255
if (e == null)
254256
return;
255257

256-
RectF bounds = mChart.getBarBounds((BarEntry) e);
258+
RectF bounds = mOnValueSelectedRectF;
259+
mChart.getBarBounds((BarEntry) e, bounds);
260+
257261
MPPointF position = mChart.getPosition(e, mChart.getData().getDataSetByIndex(h.getDataSetIndex())
258262
.getAxisDependency());
259263

Collapse file

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

Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java
+25-6Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,37 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
9898

9999
/**
100100
* Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be
101-
* found in the charts data.
101+
* found in the charts data. Performance-intensive code should use void getBarBounds(BarEntry, RectF) instead.
102102
*
103103
* @param e
104104
* @return
105105
*/
106106
public RectF getBarBounds(BarEntry e) {
107107

108+
109+
RectF bounds = new RectF();
110+
getBarBounds(e, bounds);
111+
112+
return bounds;
113+
}
114+
115+
/**
116+
* The passed outputRect will be assigned the values of the bounding box of the specified Entry in the specified DataSet.
117+
* The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data.
118+
*
119+
* @param e
120+
* @return
121+
*/
122+
public void getBarBounds(BarEntry e, RectF outputRect){
123+
124+
RectF bounds = outputRect;
125+
108126
IBarDataSet set = mData.getDataSetForEntry(e);
109127

110-
if (set == null)
111-
return null;
128+
if (set == null) {
129+
bounds.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
130+
return;
131+
}
112132

113133
float y = e.getY();
114134
float x = e.getX();
@@ -120,11 +140,10 @@ public RectF getBarBounds(BarEntry e) {
120140
float top = y >= 0 ? y : 0;
121141
float bottom = y <= 0 ? y : 0;
122142

123-
RectF bounds = new RectF(left, top, right, bottom);
143+
bounds.set(left, top, right, bottom);
124144

125-
getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);
145+
getTransformer(set.getAxisDependency()).rectValueToPixel(outputRect);
126146

127-
return bounds;
128147
}
129148

130149
/**
Collapse file

‎MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java‎

Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java
+7-5Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,15 @@ protected float[] getMarkerPosition(Highlight high) {
141141
}
142142

143143
@Override
144-
public RectF getBarBounds(BarEntry e) {
144+
public void getBarBounds(BarEntry e, RectF outputRect) {
145145

146+
RectF bounds = outputRect;
146147
IBarDataSet set = mData.getDataSetForEntry(e);
147148

148-
if (set == null)
149-
return null;
149+
if (set == null){
150+
outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
151+
return;
152+
}
150153

151154
float y = e.getY();
152155
float x = e.getX();
@@ -158,11 +161,10 @@ public RectF getBarBounds(BarEntry e) {
158161
float left = y >= 0 ? y : 0;
159162
float right = y <= 0 ? y : 0;
160163

161-
RectF bounds = new RectF(left, top, right, bottom);
164+
bounds.set(left, top, right, bottom);
162165

163166
getTransformer(set.getAxisDependency()).rectValueToPixel(bounds);
164167

165-
return bounds;
166168
}
167169

168170
protected float[] mGetPositionBuffer = new float[2];
Collapse file

‎MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java‎

Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,7 @@ protected void drawCenterText(Canvas c) {
725725
}
726726
}
727727

728+
protected RectF mDrawHighlightedRectF = new RectF();
728729
@Override
729730
public void drawHighlighted(Canvas c, Highlight[] indices) {
730731

@@ -743,7 +744,8 @@ public void drawHighlighted(Canvas c, Highlight[] indices) {
743744
? radius * (mChart.getHoleRadius() / 100.f)
744745
: 0.f;
745746

746-
final RectF highlightedCircleBox = new RectF();
747+
final RectF highlightedCircleBox = mDrawHighlightedRectF;
748+
highlightedCircleBox.set(0,0,0,0);
747749

748750
for (int i = 0; i < indices.length; i++) {
749751

Collapse file

‎MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java‎

Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public static int calcTextWidth(Paint paint, String demoText) {
147147
return (int) paint.measureText(demoText);
148148
}
149149

150+
private static Rect mCalcTextHeightRect = new Rect();
150151
/**
151152
* calculates the approximate height of a text, depending on a demo text
152153
* avoid repeated calls (e.g. inside drawing methods)
@@ -157,7 +158,8 @@ public static int calcTextWidth(Paint paint, String demoText) {
157158
*/
158159
public static int calcTextHeight(Paint paint, String demoText) {
159160

160-
Rect r = new Rect();
161+
Rect r = mCalcTextHeightRect;
162+
r.set(0,0,0,0);
161163
paint.getTextBounds(demoText, 0, demoText.length(), r);
162164
return r.height();
163165
}
@@ -188,6 +190,7 @@ public static FSize calcTextSize(Paint paint, String demoText) {
188190
return result;
189191
}
190192

193+
private static Rect mCalcTextSizeRect = new Rect();
191194
/**
192195
* calculates the approximate size of a text, depending on a demo text
193196
* avoid repeated calls (e.g. inside drawing methods)
@@ -198,7 +201,8 @@ public static FSize calcTextSize(Paint paint, String demoText) {
198201
*/
199202
public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) {
200203

201-
Rect r = new Rect();
204+
Rect r = mCalcTextSizeRect;
205+
r.set(0,0,0,0);
202206
paint.getTextBounds(demoText, 0, demoText.length(), r);
203207
outputFSize.width = r.width();
204208
outputFSize.height = r.height();

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.