Improved the barchart highlighting. Added more predefined color templates.

This commit is contained in:
Philipp Jahoda 2014-05-03 11:18:10 +02:00
parent 09bd5e1f7b
commit 3a47b397f8
8 changed files with 122 additions and 22 deletions

View file

@ -3,6 +3,8 @@
<item android:id="@+id/actionToggleRound" android:title="Toggle Round"></item>
<item android:id="@+id/actionToggleValues" android:title="Toggle Values"></item>
<item android:id="@+id/actionToggleFilled" android:title="Toggle Filled"></item>
<item android:id="@+id/actionToggleHighlight" android:title="Toggle Highlight"></item>
<item android:id="@+id/actionToggleHighlightArrow" android:title="Toggle Highlight Arrow"></item>
<item android:id="@+id/actionToggle3D" android:title="Toggle 3D"></item>
<item android:id="@+id/actionToggleStartzero" android:title="Toggle StartZero"></item>
<item android:id="@+id/actionToggleAdjustXLegend" android:title="Toggle AdjustXLegend"></item>

View file

@ -4,6 +4,7 @@
<item android:id="@+id/actionToggleValues" android:title="Toggle Values"></item>
<item android:id="@+id/actionToggleFilled" android:title="Toggle Filled"></item>
<item android:id="@+id/actionToggleCircles" android:title="Toggle Circles"></item>
<item android:id="@+id/actionToggleHighlight" android:title="Toggle Highlight"></item>
<item android:id="@+id/actionToggleStartzero" android:title="Toggle StartZero"></item>
<item android:id="@+id/actionToggleAdjustXLegend" android:title="Toggle AdjustXLegend"></item>
<item android:id="@+id/actionSave" android:title="Save to Gallery"></item>

View file

@ -87,6 +87,22 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.isHighlightEnabled())
mChart.setHighlightEnabled(false);
else
mChart.setHighlightEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleHighlightArrow: {
if (mChart.isDrawHighlightArrowEnabled())
mChart.setDrawHighlightArrow(false);
else
mChart.setDrawHighlightArrow(true);
mChart.invalidate();
break;
}
case R.id.actionToggleStartzero: {
if (mChart.isStartAtZeroEnabled())
mChart.setStartAtZero(false);

View file

@ -82,6 +82,14 @@ public class LineChartActivity extends Activity implements OnSeekBarChangeListen
mChart.invalidate();
break;
}
case R.id.actionToggleHighlight: {
if (mChart.isHighlightEnabled())
mChart.setHighlightEnabled(false);
else
mChart.setHighlightEnabled(true);
mChart.invalidate();
break;
}
case R.id.actionToggleFilled: {
if (mChart.isDrawFilledEnabled())
mChart.setDrawFilled(false);

View file

@ -25,4 +25,16 @@
<color name="colorful_4">#6A961F</color>
<color name="colorful_5">#008885</color>
<color name="greens_1">#55FC70</color>
<color name="greens_2">#62E24C</color>
<color name="greens_3">#ACF960</color>
<color name="greens_4">#CBE24C</color>
<color name="greens_5">#FCF355</color>
<color name="joyful_1">#D9508A</color>
<color name="joyful_2">#FE9507</color>
<color name="joyful_3">#FEF778</color>
<color name="joyful_4">#6AA786</color>
<color name="joyful_5">#35C2D1</color>
</resources>

View file

@ -25,6 +25,9 @@ public class BarChart extends BarLineChartBase {
/** flag the enables or disables 3d bars */
private boolean m3DEnabled = true;
/** flag that enables or disables the highlighting arrow */
private boolean mDrawHighlightArrow = true;
public BarChart(Context context) {
super(context);
@ -37,15 +40,16 @@ public class BarChart extends BarLineChartBase {
public BarChart(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void init() {
super.init();
mHighlightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mHighlightPaint.setStyle(Paint.Style.STROKE);
mHighlightPaint.setStrokeWidth(2f);
mHighlightPaint.setColor(Color.rgb(255, 187, 115));
mHighlightPaint.setStyle(Paint.Style.FILL);
mHighlightPaint.setColor(Color.rgb(0, 0, 0));
// set alpha after color
mHighlightPaint.setAlpha(120);
}
/** array that holds all the colors for the top 3D effect */
@ -112,28 +116,57 @@ public class BarChart extends BarLineChartBase {
// increase deltax by 1 because the bars have a width of 1
mDeltaX++;
}
@Override
protected void drawHighlights() {
// if there are values to highlight and highlighnting is enabled, do it
if(mHighlightEnabled && valuesToHighlight()) {
for(int i = 0; i < mIndicesToHightlight.length; i++) {
if (mHighlightEnabled && valuesToHighlight()) {
// distance between highlight arrow and bar
float offsetY = mDeltaY * 0.04f;
for (int i = 0; i < mIndicesToHightlight.length; i++) {
int index = mIndicesToHightlight[i];
// check outofbounds
if (index < mYVals.size()) {
float[] pts = new float[] {
index + 0.5f, mYChartMax, index + 0.5f, mYChartMin,
0, mYVals.get(index), mDeltaX, mYVals.get(index)
};
transformPointArray(pts);
// draw the highlight lines
mDrawCanvas.drawLines(pts, mHighlightPaint);
mHighlightPaint.setAlpha(120);
float y = mYVals.get(index);
float left = index + mBarSpace / 2f;
float right = index + 1f - mBarSpace / 2f;
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;
RectF highlight = new RectF(left, top, right, bottom);
transformRect(highlight);
mDrawCanvas.drawRect(highlight, mHighlightPaint);
if(mDrawHighlightArrow) {
mHighlightPaint.setAlpha(200);
Path arrow = new Path();
arrow.moveTo(index + 0.5f, y + offsetY * 0.3f);
arrow.lineTo(index + 0.2f, y + offsetY);
arrow.lineTo(index + 0.8f, y + offsetY);
transformPath(arrow);
mDrawCanvas.drawPath(arrow, mHighlightPaint);
}
// float[] pts = new float[] {
// index + 0.5f, mYChartMax, index + 0.5f, mYChartMin,
// 0, mYVals.get(index), mDeltaX, mYVals.get(index)
// };
//
// transformPointArray(pts);
// // draw the highlight lines
// mDrawCanvas.drawLines(pts, mHighlightPaint);
}
}
}
@ -353,11 +386,27 @@ public class BarChart extends BarLineChartBase {
return mSideColors;
}
/**
* set this to true to draw the highlightning arrow
* @param enabled
*/
public void setDrawHighlightArrow(boolean enabled) {
mDrawHighlightArrow = enabled;
}
/**
* returns true if drawing the highlighting arrow is enabled, false if not
* @return
*/
public boolean isDrawHighlightArrowEnabled() {
return mDrawHighlightArrow;
}
@Override
public void setPaint(Paint p, int which) {
super.setPaint(p, which);
switch(which) {
switch (which) {
case PAINT_HIGHLIGHT_BAR:
mHighlightPaint = p;
break;

View file

@ -156,8 +156,8 @@ public abstract class BarLineChartBase extends Chart {
drawHorizontalGrid();
drawVerticalGrid();
drawHighlights();
drawData();
drawHighlights();
drawAdditional();
drawValues();

View file

@ -6,10 +6,16 @@ import java.util.ArrayList;
public class ColorTemplate {
/**
* THE COLOR THEMES BELOW ARE PREDEFINED, FEEL FREE TO CREATE YOUR OWN WITH AS MANY DIFFERENT COLORS AS YOU WANT
*/
public static final int[] FRESH_COLORS = { R.color.fresh_1, R.color.fresh_2, R.color.fresh_3, R.color.fresh_4, R.color.fresh_5 };
public static final int[] MONO_COLORS = { R.color.mono_1, R.color.mono_2, R.color.mono_3, R.color.mono_4, R.color.mono_5 };
public static final int[] LIBERTY_COLORS = { R.color.liberty_1, R.color.liberty_2, R.color.liberty_3, R.color.liberty_4, R.color.liberty_5 };
public static final int[] COLORFUL_COLORS = { R.color.colorful_1, R.color.colorful_2, R.color.colorful_3, R.color.colorful_4, R.color.colorful_5 };
public static final int[] GREEN_COLORS = { R.color.greens_1, R.color.greens_2, R.color.greens_3, R.color.greens_4, R.color.greens_5 };
public static final int[] JOYFUL_COLORS = { R.color.joyful_1, R.color.joyful_2, R.color.joyful_3, R.color.joyful_4, R.color.joyful_5 };
private ArrayList<Integer> mColors;
@ -21,6 +27,12 @@ public class ColorTemplate {
return mColors;
}
/**
* turn an array of resource-colors into an arraylist of actual color values
* @param c
* @param colors
* @return
*/
public static ArrayList<Integer> getColors(Context c, int[] colors) {
ArrayList<Integer> result = new ArrayList<Integer>();