Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FilipeLipan/d0cf677546cfb7aeb4d5e9a6d587bf78 to your computer and use it in GitHub Desktop.
Save FilipeLipan/d0cf677546cfb7aeb4d5e9a6d587bf78 to your computer and use it in GitHub Desktop.
package com.xxmassdeveloper.mpchartexample;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.WindowManager;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.xxmassdeveloper.mpchartexample.custom.CustomLineChart;
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
import com.xxmassdeveloper.mpchartexample.custom.MyMarkerView;
import com.xxmassdeveloper.mpchartexample.custom.MyMarkerViewForFun;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
import java.util.ArrayList;
public class FilledLineActivity extends DemoBase {
private LineChart mChart;
private int mFillColor = Color.argb(150, 51, 181, 229);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_linechart_noseekbar);
mChart = (LineChart) findViewById(R.id.chart1);
mChart.setBackgroundColor(Color.WHITE);
mChart.setGridBackgroundColor(mFillColor);
mChart.setDrawGridBackground(true);
mChart.setDrawBorders(true);
// no description text
mChart.getDescription().setEnabled(false);
// if disabled, scaling can be done on x- and y-axis separately
mChart.setPinchZoom(false);
Legend l = mChart.getLegend();
l.setEnabled(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setEnabled(true);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMaximum(900f);
leftAxis.setAxisMinimum(-250f);
leftAxis.setDrawAxisLine(false);
leftAxis.setDrawZeroLine(false);
leftAxis.setDrawGridLines(false);
mChart.getAxisRight().setEnabled(false);
// add data
setData(8, 120);
mChart.invalidate();
}
private void setData(int count, float range) {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 50;// + (float)
// ((mult *
// 0.1) / 10);
yVals1.add(new Entry(i, val));
}
ArrayList<Entry> yVals2 = new ArrayList<Entry>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range) + 450;// + (float)
// ((mult *
// 0.1) / 10);
yVals2.add(new Entry(i, val));
}
LineDataSet set1, set2;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
set2 = (LineDataSet)mChart.getData().getDataSetByIndex(1);
set1.setValues(yVals1);
set2.setValues(yVals2);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(yVals1, "DataSet 1");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
// set1.setColor(Color.rgb(255, 241, 46));
set1.setDrawCircles(false);
set1.setCircleColor(Color.BLACK);
set1.setLineWidth(2f);
set1.setCircleRadius(3f);
set1.setFillAlpha(255);
set1.setDrawFilled(true);
set1.setFillColor(Color.WHITE);
// TODO copy this
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
int[] colors = { Color.parseColor("#00ffd0"), Color.parseColor("#ff00af")};
set1.setCircleColorHole(Color.WHITE);
set1.setCircleHoleRadius(5f);
set1.setCircleColor(Color.WHITE);
set1.setCircleRadius(6f);
set1.setDrawCircles(true);
Paint paint = mChart.getRenderer().getPaintRender();
int height = 200;
Shader shader = new LinearGradient(0, 0, 0, 200, colors,
null, Shader.TileMode.MIRROR);
Matrix matrix = new Matrix();
matrix.setRotate(90);
shader.setLocalMatrix(matrix);
paint.setShader(shader);
set1.setColors(colors);
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.background_gradient);
set1.setFillDrawable(drawable);
set1.setLineWidth(6f);
set1.setHighLightColor(Color.WHITE);
set1.setDrawHorizontalHighlightIndicator(false);
MyMarkerViewForFun mv = new MyMarkerViewForFun(this, R.layout.custom_marker_view_for_fun);
mv.setChartView(mChart); // For bounds control
mChart.setMarker(mv); // Set the marker to the chart
IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTypeface(mTfLight);
xAxis.setDrawGridLines(false);
xAxis.setGridColor(Color.WHITE);
xAxis.setGranularity(1f); // only intervals of 1 day
xAxis.setLabelCount(7);
xAxis.setValueFormatter(xAxisFormatter);
mChart.setExtraBottomOffset(50);
// TODO copy this
// set1.setDrawCircleHole(false);
set1.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return mChart.getAxisLeft().getAxisMinimum();
}
});
// create a dataset and give it a type
set2 = new LineDataSet(yVals2, "DataSet 2");
set2.setAxisDependency(YAxis.AxisDependency.LEFT);
// set2.setColor(Color.rgb(255, 241, 46));
set2.setDrawCircles(false);
set2.setLineWidth(2f);
set2.setCircleRadius(3f);
set2.setFillAlpha(255);
set2.setDrawFilled(true);
set2.setFillColor(Color.WHITE);
set2.setDrawCircleHole(false);
// set2.setHighLightColor(Color.rgb(244, 117, 117));
set2.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return mChart.getAxisLeft().getAxisMaximum();
}
});
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
// dataSets.add(set2);
// create a data object with the datasets
LineData data = new LineData(dataSets);
data.setDrawValues(false);
// set data
mChart.setData(data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment