Skip to content

Commit 035354b

Browse files
authored
Merge pull request #7 from swapnil1104/horizontal_guidelines
Horizontal guidelines
2 parents 69049d8 + 3c68740 commit 035354b

6 files changed

Lines changed: 61 additions & 32 deletions

File tree

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ A highly customizable and performant custom view to render curved line graph.
1010
![Animation with straight & curved](documentation/images/straight_curved_demo.gif)
1111
![Animation with opaque color](documentation/images/animation_demo2.gif)
1212
![Animated and non animated graph](documentation/images/static_dynamic_graph_demo.gif)
13+
![Horizontal guidelines](documentation/images/horizontal_guidelines.png)
14+
1315
## Packed with features
1416
- Add multiple line graphs within one graph plane.
1517
- Extensible styling options.
@@ -51,16 +53,17 @@ Step 3. Add CurveGraphView to your layout file
5153
curveGraphView = findViewById(R.id.cgv);
5254
5355
curveGraphView.configure(
54-
new CurveGraphConfig.Builder(this)
55-
.setAxisColor(R.color.Blue) // Set X and Y axis line color stroke.
56-
.setIntervalDisplayCount(7) // Set number of values to be displayed in X ax
57-
.setGuidelineCount(2) // Set number of background guidelines to be shown.
58-
.setGuidelineColor(R.color.GreenYellow) // Set color of the visible guidelines.
59-
.setNoDataMsg(" No Data ") // Message when no data is provided to the view.
60-
.setxAxisScaleTextColor(R.color.Black) // Set X axis scale text color.
61-
.setyAxisScaleTextColor(R.color.Black) // Set Y axis scale text color
62-
.build()
63-
););
56+
new CurveGraphConfig.Builder(this)
57+
.setAxisColor(R.color.Blue) // Set number of values to be displayed in X ax
58+
.setVerticalGuideline(4) // Set number of background guidelines to be shown.
59+
.setHorizontalGuideline(2)
60+
.setGuidelineColor(R.color.Red) // Set color of the visible guidelines.
61+
.setNoDataMsg(" No Data ") // Message when no data is provided to the view.
62+
.setxAxisScaleTextColor(R.color.Black) // Set X axis scale text color.
63+
.setyAxisScaleTextColor(R.color.Black) // Set Y axis scale text color
64+
.setAnimationDuration(2000) // Set Animation Duration
65+
.build()
66+
);
6467
```
6568
## How to provide data to the view.
6669
### Create PointMap object

app/src/main/java/com/broooapps/curvegraphview/MainActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ protected void onCreate(Bundle savedInstanceState) {
2525

2626
curveGraphView.configure(
2727
new CurveGraphConfig.Builder(this)
28-
.setAxisColor(R.color.Blue) // Set number of values to be displayed in X ax
29-
.setGuidelineCount(4) // Set number of background guidelines to be shown.
28+
.setAxisColor(R.color.Blue) // Set number of values to be displayed in X ax
29+
.setVerticalGuideline(4) // Set number of background guidelines to be shown.
30+
.setHorizontalGuideline(2)
3031
.setGuidelineColor(R.color.Red) // Set color of the visible guidelines.
3132
.setNoDataMsg(" No Data ") // Message when no data is provided to the view.
3233
.setxAxisScaleTextColor(R.color.Black) // Set X axis scale text color.
@@ -49,7 +50,6 @@ protected void onCreate(Bundle savedInstanceState) {
4950
.animateLine(true)
5051
.setPointColor(R.color.Red)
5152
.setPointRadius(5)
52-
.setStraightLine(true)
5353
.build();
5454

5555
PointMap p2 = new PointMap();

app/src/main/res/values/colors.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<color name="colorPrimaryDark">#00574B</color>
55
<color name="colorAccent">#D81B60</color>
66

7-
<color name="gradientStartColor">#C34DD0</color>
8-
<color name="gradientEndColor">#4A4DD0</color>
9-
<color name="gradientStartColor2">#FFC400</color>
10-
<color name="gradientEndColor2">#B2FF59</color>
7+
<color name="gradientStartColor">#BF4A4DD0</color>
8+
<color name="gradientEndColor">#ADC34DD0</color>
9+
<color name="gradientStartColor2">#A9304FFE</color>
10+
<color name="gradientEndColor2">#DA1976D2</color>
1111

1212

1313
</resources>
19.5 KB
Loading

graphview/src/main/java/com/broooapps/graphview/CurveGraphConfig.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class CurveGraphConfig {
2424

2525
int guidelineCount;
2626

27+
int horizontalGuidelineCount;
28+
2729
int intervalCount;
2830

2931
long animationDuration;
@@ -37,6 +39,7 @@ private CurveGraphConfig(Builder builder) {
3739
axisColor = (builder.axisColor == 0) ? ContextCompat.getColor(builder.ctxWeakRef.get(), R.color.axisColor) : builder.axisColor;
3840
intervalCount = builder.intervalCount;
3941
guidelineCount = builder.guidelineCount;
42+
horizontalGuidelineCount = builder.horizontalGuidelineCount;
4043
noDataMsg = (builder.noDataMsg == null) ? "NO DATA" : builder.noDataMsg;
4144
animationDuration = builder.animationDuration;
4245
builder.ctxWeakRef.clear();
@@ -61,6 +64,8 @@ public static class Builder {
6164

6265
int intervalCount = 0;
6366

67+
int horizontalGuidelineCount = 0;
68+
6469
long animationDuration = 2000;
6570

6671
String noDataMsg = null;
@@ -77,11 +82,16 @@ public Builder setNoDataMsg(String message) {
7782
return this;
7883
}
7984

80-
public Builder setGuidelineCount(int count) {
85+
public Builder setVerticalGuideline(int count) {
8186
this.guidelineCount = count;
8287
return this;
8388
}
8489

90+
public Builder setHorizontalGuideline(int count) {
91+
this.horizontalGuidelineCount = count;
92+
return this;
93+
}
94+
8595
public Builder setGuidelineColor(int colorRes) {
8696
this.guidelineColor = ContextCompat.getColor(ctxWeakRef.get(), colorRes);
8797
return this;

graphview/src/main/java/com/broooapps/graphview/CurveGraphView.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ public class CurveGraphView extends View {
3838
DecimalFormat df = new DecimalFormat("#######.##");
3939

4040
// Builder fields
41-
private int guidelineCount;
41+
private int verticalGuidelineCount;
4242
private int intervalCount;
43+
private int horizontalGuidelineCount;
4344

4445
private GraphData[] graphDataArray = {};
4546

4647
int viewHeight, viewWidth;
4748
int graphHeight, graphWidth, graphPadding = 32;
4849

49-
private Path path = new Path();
50+
private Path vPath = new Path();
51+
private Path hPath = new Path();
5052

5153
private Paint xAxisScalePaint;
5254
private Paint yAxisScalePaint;
@@ -132,7 +134,8 @@ public void configure(CurveGraphConfig builder) {
132134
yAxis = new RectF(0, 0, 0, 0);
133135
}
134136
this.noDataMsg = builder.noDataMsg;
135-
this.guidelineCount = builder.guidelineCount;
137+
this.verticalGuidelineCount = builder.guidelineCount;
138+
this.horizontalGuidelineCount = builder.horizontalGuidelineCount;
136139
this.intervalCount = builder.intervalCount;
137140

138141
xAxisScalePaint.setTextSize(28f);
@@ -228,7 +231,10 @@ public void onAnimationRepeat(Animator animation) {
228231
protected void onDraw(Canvas canvas) {
229232
super.onDraw(canvas);
230233
drawAxis(canvas);
231-
drawGuideline(canvas);
234+
if (!(graphDataArray.length == 0 || noDataInGraph())) {
235+
drawVertGuideline(canvas);
236+
drawHorizontalGuidelines(canvas);
237+
}
232238
drawScaleText(canvas);
233239
drawInterval(canvas);
234240

@@ -253,21 +259,31 @@ private void drawInterval(Canvas canvas) {
253259
}
254260
}
255261

256-
private void drawGuideline(Canvas canvas) {
257-
if (drawGuideline()) return;
258-
for (int i = 1; i <= guidelineCount; i++) {
259-
path.reset();
262+
private void drawVertGuideline(Canvas canvas) {
263+
if (verticalGuidelineCount == 0) return;
264+
for (int i = 1; i <= verticalGuidelineCount; i++) {
265+
vPath.reset();
260266

261-
int xPos = (i * (graphWidth - (graphPadding) * 2)) / (guidelineCount + 1);
262-
path.moveTo(xPos, graphPadding);
263-
path.lineTo(xPos, yAxis.top);
267+
int xPos = (i * (graphWidth - (graphPadding) * 2)) / (verticalGuidelineCount + 1);
268+
vPath.moveTo(xPos, graphPadding);
269+
vPath.lineTo(xPos, yAxis.top);
264270

265-
canvas.drawPath(path, guidelinePaint);
271+
canvas.drawPath(vPath, guidelinePaint);
266272
}
267273
}
268274

269-
private boolean drawGuideline() {
270-
return guidelineCount == 0 || graphDataArray.length == 0 || noDataInGraph();
275+
276+
private void drawHorizontalGuidelines(Canvas canvas) {
277+
if (horizontalGuidelineCount == 0) return;
278+
for (int i = 1; i <= horizontalGuidelineCount; i++) {
279+
hPath.reset();
280+
281+
int yPos = (i * graphHeight - graphPadding) / (horizontalGuidelineCount + 1);
282+
hPath.moveTo(graphPadding, yPos);
283+
hPath.lineTo(graphWidth - graphPadding, yPos);
284+
285+
canvas.drawPath(hPath, guidelinePaint);
286+
}
271287
}
272288

273289
private boolean noDataInGraph() {

0 commit comments

Comments
 (0)