Skip to content

Commit

Permalink
Refactored ColorTemplate, library is now no longer dependant on .xml …
Browse files Browse the repository at this point in the history
…resources.
  • Loading branch information
PhilJay committed Sep 28, 2014
1 parent df90695 commit eb41b26
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void addDataSet() {
set.setLineWidth(2.5f);
set.setCircleSize(4.5f);

int color = getResources().getColor(mColors[count % mColors.length]);
int color = mColors[count % mColors.length];

set.setColor(color);
set.setCircleColor(color);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.mikephil.charting.data.filter.Approximator;
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
import com.github.mikephil.charting.utils.XLabels;
Expand Down Expand Up @@ -49,15 +50,15 @@ protected void onCreate(Bundle savedInstanceState) {

mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
mSeekBarY.setOnSeekBarChangeListener(this);

mChart = (ScatterChart) findViewById(R.id.chart1);
mChart.setDescription("");

Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
mChart.setValueTypeface(tf);

mChart.setOnChartValueSelectedListener(this);

mChart.setDrawGridBackground(false);

mChart.setTouchEnabled(true);
Expand All @@ -71,14 +72,14 @@ protected void onCreate(Bundle savedInstanceState) {

mSeekBarX.setProgress(45);
mSeekBarY.setProgress(100);

Legend l = mChart.getLegend();
l.setPosition(LegendPosition.RIGHT_OF_CHART);
l.setTypeface(tf);

YLabels yl = mChart.getYLabels();
yl.setTypeface(tf);

XLabels xl = mChart.getXLabels();
xl.setTypeface(tf);
}
Expand Down Expand Up @@ -129,7 +130,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
case R.id.actionToggleAdjustXLegend: {
XLabels xLabels = mChart.getXLabels();

if (xLabels.isAdjustXLabelsEnabled())
xLabels.setAdjustXLabels(false);
else
Expand All @@ -139,9 +140,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
break;
}
case R.id.actionToggleFilter: {

Approximator a = new Approximator(ApproximatorType.DOUGLAS_PEUCKER, 25);

if (!mChart.isFilteringEnabled()) {
mChart.enableFiltering(a);
} else {
Expand Down Expand Up @@ -174,7 +175,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

tvX.setText("" + (mSeekBarX.getProgress() + 1));
tvY.setText("" + (mSeekBarY.getProgress()));

Expand Down Expand Up @@ -205,14 +206,14 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// create a dataset and give it a type
ScatterDataSet set1 = new ScatterDataSet(yVals1, "DS 1");
set1.setScatterShape(ScatterShape.SQUARE);
set1.setColor(getResources().getColor(R.color.colorful_1));
set1.setColor(ColorTemplate.COLORFUL_COLORS[0]);
ScatterDataSet set2 = new ScatterDataSet(yVals2, "DS 2");
set2.setScatterShape(ScatterShape.CIRCLE);
set2.setColor(getResources().getColor(R.color.colorful_2));
set2.setColor(ColorTemplate.COLORFUL_COLORS[1]);
ScatterDataSet set3 = new ScatterDataSet(yVals3, "DS 3");
set3.setScatterShape(ScatterShape.TRIANGLE);
set3.setColor(getResources().getColor(R.color.colorful_3));
set3.setColor(ColorTemplate.COLORFUL_COLORS[2]);

set1.setScatterShapeSize(8f);
set2.setScatterShapeSize(8f);
set3.setScatterShapeSize(8f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected BarData generateBarData(int dataSets, float range, int count) {
}

BarDataSet ds = new BarDataSet(entries, getLabel(i));
ds.setColors(ColorTemplate.VORDIPLOM_COLORS, getActivity());
ds.setColors(ColorTemplate.VORDIPLOM_COLORS);
sets.add(ds);
}

Expand All @@ -63,7 +63,7 @@ protected ScatterData generateScatterData(int dataSets, float range, int count)
ScatterDataSet ds = new ScatterDataSet(entries, getLabel(i));
ds.setScatterShapeSize(12f);
ds.setScatterShape(shapes[i % shapes.length]);
ds.setColors(ColorTemplate.COLORFUL_COLORS, getActivity());
ds.setColors(ColorTemplate.COLORFUL_COLORS);
ds.setScatterShapeSize(9f);
sets.add(ds);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ protected PieData generatePieData() {
}

PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2014");
ds1.setColors(ColorTemplate.VORDIPLOM_COLORS, getActivity());
ds1.setColors(ColorTemplate.VORDIPLOM_COLORS);
ds1.setSliceSpace(2f);

PieData d = new PieData(xVals, ds1);
Expand Down
55 changes: 28 additions & 27 deletions MPChartLib/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--
<color name="liberty_1">#CFF8F6</color>
<color name="liberty_2">#94D4D4</color>
<color name="liberty_3">#88B4BB</color>
<color name="liberty_4">#76AEAF</color>
<color name="liberty_5">#2A6D82</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>
<color name="fresh_1">#FFFFB3</color>
<color name="fresh_2">#94FF85</color>
<color name="fresh_3">#75FFC2</color>
Expand All @@ -12,42 +25,30 @@
<color name="mono_3">#075075</color>
<color name="mono_4">#57BEF2</color>
<color name="mono_5">#326E8C</color>

<color name="liberty_1">#CFF8F6</color>
<color name="liberty_2">#94D4D4</color>
<color name="liberty_3">#88B4BB</color>
<color name="liberty_4">#76AEAF</color>
<color name="liberty_5">#2A6D82</color>

<color name="colorful_1">#C12552</color>
<color name="colorful_2">#FF6600</color>
<color name="colorful_3">#F5C700</color>
<color name="colorful_4">#6A961F</color>
<color name="colorful_5">#008885</color>

<color name="pastel_1">#405980</color>
<color name="pastel_2">#95A57C</color>
<color name="pastel_3">#D9B8A2</color>
<color name="pastel_4">#BF8686</color>
<color name="pastel_5">#B33050</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>

<color name="pastel_1">#405980</color>
<color name="pastel_2">#95A57C</color>
<color name="pastel_3">#D9B8A2</color>
<color name="pastel_4">#BF8686</color>
<color name="pastel_5">#B33050</color>
<!--
<color name="colorful_1">#C12552</color>
<color name="colorful_2">#FF6600</color>
<color name="colorful_3">#F5C700</color>
<color name="colorful_4">#6A961F</color>
<color name="colorful_5">#008885</color>
<color name="vordiplom_1">#C0FF8C</color>
<color name="vordiplom_2">#FFF78C</color>
<color name="vordiplom_3">#FFD08C</color>
<color name="vordiplom_4">#8CEAFF</color>
<color name="vordiplom_5">#FF8C9D</color>
-->
</resources>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.ArrayList;

/**
* Class that holds predefined color arrays (e.g.
* Class that holds predefined color integer arrays (e.g.
* ColorTemplate.VORDIPLOM_COLORS) and convenience methods for loading colors
* from resources.
*
Expand All @@ -21,38 +21,40 @@ public class ColorTemplate {
* THE COLOR THEMES ARE PREDEFINED (predefined color integer arrays), 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
Color.rgb(207, 248, 246), Color.rgb(148, 212, 212), Color.rgb(136, 180, 187),
Color.rgb(118, 174, 175), Color.rgb(42, 109, 130)
};
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
Color.rgb(217, 80, 138), Color.rgb(254, 149, 7), Color.rgb(254, 247, 120),
Color.rgb(106, 167, 134), Color.rgb(53, 194, 209)
};
public static final int[] PASTEL_COLORS = {
R.color.pastel_1, R.color.pastel_2, R.color.pastel_3, R.color.pastel_4,
R.color.pastel_5
Color.rgb(64, 89, 128), Color.rgb(149, 165, 124), Color.rgb(217, 184, 162),
Color.rgb(191, 134, 134), Color.rgb(179, 48, 80)
};
public static final int[] COLORFUL_COLORS = {
Color.rgb(193, 37, 82), Color.rgb(255, 102, 0), Color.rgb(245, 199, 0),
Color.rgb(106, 150, 31), Color.rgb(179, 100, 53)
};
public static final int[] VORDIPLOM_COLORS = {
Color.rgb(192, 255, 140), Color.rgb(255, 247, 140), Color.rgb(255, 208, 140),
Color.rgb(140, 234, 255), Color.rgb(255, 140, 157)
};

// 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[] GREEN_COLORS = {
// R.color.greens_1, R.color.greens_2, R.color.greens_3, R.color.greens_4,
// R.color.greens_5
// };

/**
* turn an array of resource-colors (contains resource-id integers) into an
* array list of actual color integers
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ Features
- **PieChart (with selection, ...)**

![alt tag](https://raw.github.com/PhilJay/MPAndroidChart/master/screenshots/simpledesign_piechart1.png)
![alt tag](https://raw.github.com/PhilJay/MPAndroidChart/master/screenshots/piechart_holeradius_space.png)

- **ScatterChart** (with squares, triangles, circles, ... and more)

Expand Down

0 comments on commit eb41b26

Please sign in to comment.