Work on new example with multicolor barchart

This commit is contained in:
Philipp Jahoda 2016-02-06 18:21:46 +01:00
parent f19b8e862a
commit a6e6fed0fa
3 changed files with 169 additions and 0 deletions

View file

@ -61,6 +61,7 @@
<activity android:name="ScrollViewActivity"></activity>
<activity android:name="StackedBarActivityNegative"></activity>
<activity android:name=".realm.RealmWikiExample"></activity>
<activity android:name=".BarChartPositiveNegative"></activity>
</application>
</manifest>

View file

@ -0,0 +1,160 @@
package com.xxmassdeveloper.mpchartexample;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.WindowManager;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class BarChartPositiveNegative extends DemoBase {
protected BarChart mChart;
private Typeface mTf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_barchart_noseekbar);
mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
mChart = (BarChart) findViewById(R.id.chart1);
mChart.setExtraTopOffset(-30f);
mChart.setExtraBottomOffset(10f);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setDescription("");
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxisPosition.BOTTOM);
xAxis.setTypeface(mTf);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setSpaceBetweenLabels(2);
xAxis.setTextColor(Color.LTGRAY);
xAxis.setTextSize(12f);
YAxis left = mChart.getAxisLeft();
left.setDrawLabels(false);
left.setStartAtZero(false);
left.setSpaceTop(25f);
left.setSpaceBottom(25f);
left.setDrawAxisLine(false);
left.setDrawGridLines(false);
mChart.getAxisRight().setEnabled(false);
mChart.getLegend().setEnabled(false);
// THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
List<Data> data = new ArrayList<>();
data.add(new Data(0, -224.1f, "12-29"));
data.add(new Data(1, 238.5f, "12-30"));
data.add(new Data(2, 1280.1f, "12-31"));
data.add(new Data(3, -442.3f, "01-01"));
data.add(new Data(4, -2280.1f, "01-02"));
setData(data);
}
private void setData(List<Data> dataList) {
ArrayList<BarEntry> positiveValues = new ArrayList<BarEntry>();
ArrayList<BarEntry> negativeValues = new ArrayList<BarEntry>();
String[] dates = new String[dataList.size()];
for (int i = 0; i < dataList.size(); i++) {
Data d = dataList.get(i);
BarEntry entry = new BarEntry(d.yValue, d.xIndex);
if (d.yValue >= 0)
positiveValues.add(entry);
else
negativeValues.add(entry);
dates[i] = dataList.get(i).xAxisValue;
}
int green = Color.rgb(110, 190, 102);
int red = Color.rgb(211, 74, 88);
BarDataSet positive = new BarDataSet(positiveValues, "Positive");
positive.setBarSpacePercent(35f);
positive.setColor(red);
positive.setValueTextColor(red);
BarDataSet negative = new BarDataSet(negativeValues, "Negative");
negative.setBarSpacePercent(35f);
negative.setColor(green);
negative.setValueTextColor(green);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(positive);
dataSets.add(negative);
BarData data = new BarData(dates, dataSets);
data.setValueTextSize(12f);
data.setValueTypeface(mTf);
data.setValueFormatter(new ValueFormatter());
mChart.setData(data);
mChart.invalidate();
}
/**
* Demo class representing data.
*/
private class Data {
public String xAxisValue;
public float yValue;
public int xIndex;
public Data(int xIndex, float yValue, String xAxisValue) {
this.xAxisValue = xAxisValue;
this.yValue = yValue;
this.xIndex = xIndex;
}
}
private class ValueFormatter implements com.github.mikephil.charting.formatter.ValueFormatter {
private DecimalFormat mFormat;
public ValueFormatter() {
mFormat = new DecimalFormat("######.0");
}
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
return mFormat.format(value);
}
}
}

View file

@ -18,6 +18,7 @@ import com.xxmassdeveloper.mpchartexample.AnotherBarActivity;
import com.xxmassdeveloper.mpchartexample.BarChartActivity;
import com.xxmassdeveloper.mpchartexample.BarChartActivityMultiDataset;
import com.xxmassdeveloper.mpchartexample.BarChartActivitySinus;
import com.xxmassdeveloper.mpchartexample.BarChartPositiveNegative;
import com.xxmassdeveloper.mpchartexample.BubbleChartActivity;
import com.xxmassdeveloper.mpchartexample.CandleStickChartActivity;
import com.xxmassdeveloper.mpchartexample.CombinedChartActivity;
@ -123,6 +124,9 @@ public class MainActivity extends Activity implements OnItemClickListener {
objects.add(new ContentItem(
"Chart in ScrollView",
"This demonstrates how to use a chart inside a ScrollView."));
objects.add(new ContentItem(
"BarChart positive / negative",
"This demonstrates how to create a BarChart with positive and negative values in different colors."));
ContentItem realm = new ContentItem(
"Realm.io Database",
@ -249,6 +253,10 @@ public class MainActivity extends Activity implements OnItemClickListener {
startActivity(i);
break;
case 26:
i = new Intent(this, BarChartPositiveNegative.class);
startActivity(i);
break;
case 27:
i = new Intent(this, RealmMainActivity.class);
startActivity(i);
break;