Skip to content

Commit

Permalink
Merge pull request #148 from akperkins/allowToChangePointColor
Browse files Browse the repository at this point in the history
added the ability to supply points with their own custom colors
  • Loading branch information
lecho committed Jul 2, 2015
2 parents 57be790 + ad1273b commit 11230a0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
18 changes: 17 additions & 1 deletion hellocharts-library/src/lecho/lib/hellocharts/model/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class Line {
private static final int DEFAULT_LINE_STROKE_WIDTH_DP = 3;
private static final int DEFAULT_POINT_RADIUS_DP = 6;
private static final int DEFAULT_AREA_TRANSPARENCY = 64;
public static final int UNINITIALIZED = 0;
private int color = ChartUtils.DEFAULT_COLOR;
private int pointColor = UNINITIALIZED;
private int darkenColor = ChartUtils.DEFAULT_DARKEN_COLOR;
/**
* Transparency of area when line is filled. *
Expand Down Expand Up @@ -47,7 +49,8 @@ public Line(List<PointValue> values) {

public Line(Line line) {
this.color = line.color;
this.darkenColor = line.color;
this.pointColor = line.pointColor;
this.darkenColor = line.darkenColor;
this.areaTransparency = line.areaTransparency;
this.strokeWidth = line.strokeWidth;
this.pointRadius = line.pointRadius;
Expand Down Expand Up @@ -101,6 +104,19 @@ public Line setColor(int color) {
return this;
}

public int getPointColor() {
if(pointColor == UNINITIALIZED){
return color;
} else {
return pointColor;
}
}

public Line setPointColor(int pointColor) {
this.pointColor = pointColor;
return this;
}

public int getDarkenColor() {
return darkenColor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ private void prepareLinePaint(final Line line) {
// may cause problems in the future with
// implementing point styles.
private void drawPoints(Canvas canvas, Line line, int lineIndex, int mode) {
pointPaint.setColor(line.getColor());
pointPaint.setColor(line.getPointColor());
int valueIndex = 0;
for (PointValue pointValue : line.getValues()) {
int pointRadius = ChartUtils.dp2px(density, line.getPointRadius());
Expand Down
4 changes: 4 additions & 0 deletions hellocharts-samples/res/menu/line_chart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
android:id="@+id/action_toggle_area"
android:title="Toggle area"
app:showAsAction="never"/>
<item
android:id="@+id/action_point_color"
android:title="Toggle Point Color"
app:showAsAction="never"/>
<item
android:id="@+id/action_shape_circles"
android:title="Circle shape"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static class PlaceholderFragment extends Fragment {
private boolean hasLabels = false;
private boolean isCubic = false;
private boolean hasLabelForSelected = false;
private boolean pointsHaveDifferentColor;

public PlaceholderFragment() {
}
Expand Down Expand Up @@ -119,6 +120,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
toggleFilled();
return true;
}
if (id == R.id.action_point_color) {
togglePointColor();
return true;
}
if (id == R.id.action_shape_circles) {
setCircles();
return true;
Expand Down Expand Up @@ -196,6 +201,7 @@ private void reset() {
hasLabels = false;
isCubic = false;
hasLabelForSelected = false;
pointsHaveDifferentColor = false;

chart.setValueSelectionEnabled(hasLabelForSelected);
resetViewport();
Expand Down Expand Up @@ -231,6 +237,9 @@ private void generateData() {
line.setHasLabelsOnlyForSelected(hasLabelForSelected);
line.setHasLines(hasLines);
line.setHasPoints(hasPoints);
if (pointsHaveDifferentColor){
line.setPointColor(ChartUtils.COLORS[(i + 1) % ChartUtils.COLORS.length]);
}
lines.add(line);
}

Expand Down Expand Up @@ -340,6 +349,12 @@ private void toggleFilled() {
generateData();
}

private void togglePointColor() {
pointsHaveDifferentColor = !pointsHaveDifferentColor;

generateData();
}

private void setCircles() {
shape = ValueShape.CIRCLE;

Expand Down

0 comments on commit 11230a0

Please sign in to comment.