|
| 1 | + |
| 2 | +package com.xxmassdeveloper.mpchartexample; |
| 3 | + |
| 4 | +import android.graphics.Color; |
| 5 | +import android.graphics.Typeface; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.util.Log; |
| 8 | +import android.view.Menu; |
| 9 | +import android.view.MenuItem; |
| 10 | +import android.view.WindowManager; |
| 11 | + |
| 12 | +import com.github.mikephil.charting.charts.BarLineChartBase.BorderPosition; |
| 13 | +import com.github.mikephil.charting.charts.LineChart; |
| 14 | +import com.github.mikephil.charting.components.Legend; |
| 15 | +import com.github.mikephil.charting.components.Legend.LegendForm; |
| 16 | +import com.github.mikephil.charting.components.XAxis; |
| 17 | +import com.github.mikephil.charting.components.YAxis; |
| 18 | +import com.github.mikephil.charting.components.YAxis.AxisDependency; |
| 19 | +import com.github.mikephil.charting.data.Entry; |
| 20 | +import com.github.mikephil.charting.data.LineData; |
| 21 | +import com.github.mikephil.charting.data.LineDataSet; |
| 22 | +import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener; |
| 23 | +import com.github.mikephil.charting.utils.ColorTemplate; |
| 24 | +import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase; |
| 25 | + |
| 26 | +public class RealtimeLineChartActivity extends DemoBase implements |
| 27 | + OnChartValueSelectedListener { |
| 28 | + |
| 29 | + private LineChart mChart; |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void onCreate(Bundle savedInstanceState) { |
| 33 | + super.onCreate(savedInstanceState); |
| 34 | + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 35 | + WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 36 | + setContentView(R.layout.activity_realtime_linechart); |
| 37 | + |
| 38 | + mChart = (LineChart) findViewById(R.id.chart1); |
| 39 | + mChart.setOnChartValueSelectedListener(this); |
| 40 | + mChart.setValueTextColor(Color.WHITE); |
| 41 | + |
| 42 | + mChart.setDrawBorder(true); |
| 43 | + mChart.setBorderPositions(new BorderPosition[] { |
| 44 | + BorderPosition.BOTTOM |
| 45 | + }); |
| 46 | + |
| 47 | + // no description text |
| 48 | + mChart.setDescription(""); |
| 49 | + mChart.setNoDataTextDescription("You need to provide data for the chart."); |
| 50 | + |
| 51 | + // enable value highlighting |
| 52 | + mChart.setHighlightEnabled(true); |
| 53 | + |
| 54 | + // enable touch gestures |
| 55 | + mChart.setTouchEnabled(true); |
| 56 | + |
| 57 | + // enable scaling and dragging |
| 58 | + mChart.setDragEnabled(true); |
| 59 | + mChart.setScaleEnabled(true); |
| 60 | + mChart.setDrawGridBackground(false); |
| 61 | + |
| 62 | + // if disabled, scaling can be done on x- and y-axis separately |
| 63 | + mChart.setPinchZoom(true); |
| 64 | + |
| 65 | + // set an alternative background color |
| 66 | + mChart.setBackgroundColor(Color.LTGRAY); |
| 67 | + |
| 68 | + // add empty data |
| 69 | + mChart.setData(new LineData()); |
| 70 | + |
| 71 | + Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf"); |
| 72 | + |
| 73 | + // get the legend (only possible after setting data) |
| 74 | + Legend l = mChart.getLegend(); |
| 75 | + |
| 76 | + // modify the legend ... |
| 77 | + // l.setPosition(LegendPosition.LEFT_OF_CHART); |
| 78 | + l.setForm(LegendForm.LINE); |
| 79 | + l.setTypeface(tf); |
| 80 | + l.setTextColor(Color.WHITE); |
| 81 | + |
| 82 | + XAxis xl = mChart.getXAxis(); |
| 83 | + xl.setTypeface(tf); |
| 84 | + xl.setTextColor(Color.WHITE); |
| 85 | + xl.setDrawGridLines(false); |
| 86 | + xl.setAvoidFirstLastClipping(true); |
| 87 | + |
| 88 | + YAxis leftAxis = mChart.getAxisLeft(); |
| 89 | + leftAxis.setTypeface(tf); |
| 90 | + leftAxis.setTextColor(Color.WHITE); |
| 91 | + leftAxis.setAxisMaxValue(120f); |
| 92 | + leftAxis.setDrawGridLines(true); |
| 93 | + |
| 94 | + YAxis rightAxis = mChart.getAxisRight(); |
| 95 | + rightAxis.setEnabled(false); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 100 | + getMenuInflater().inflate(R.menu.realtime, menu); |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 106 | + |
| 107 | + switch (item.getItemId()) { |
| 108 | + case R.id.actionAdd: { |
| 109 | + addEntry(); |
| 110 | + break; |
| 111 | + } |
| 112 | + } |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + private int year = 15; |
| 117 | + |
| 118 | + private void addEntry() { |
| 119 | + |
| 120 | + LineData data = mChart.getData(); |
| 121 | + |
| 122 | + if (data != null) { |
| 123 | + |
| 124 | + LineDataSet set = data.getDataSetByIndex(0); |
| 125 | + // set.addEntry(...); // can be called as well |
| 126 | + |
| 127 | + if (set == null) { |
| 128 | + set = createSet(); |
| 129 | + data.addDataSet(set); |
| 130 | + } |
| 131 | + |
| 132 | + // add a new x-value first |
| 133 | + data.addXValue(mMonths[data.getXValCount() % 12] + " " + (year + data.getXValCount() / 12)); |
| 134 | + data.addEntry(new Entry((float) (Math.random() * 40) + 40f, set.getEntryCount()), 0); |
| 135 | + |
| 136 | + // let the chart know it's data has changed |
| 137 | + mChart.notifyDataSetChanged(); |
| 138 | + |
| 139 | + // limit the number of visible entries |
| 140 | + mChart.setVisibleXRange(6); |
| 141 | + // mChart.setVisibleYRange(30, AxisDependency.LEFT); |
| 142 | + |
| 143 | + // move to the latest entry |
| 144 | + mChart.moveViewToX(data.getXValCount()-7); |
| 145 | + |
| 146 | + // this automatically refreshes the chart (calls invalidate()) |
| 147 | +// mChart.moveViewTo(data.getXValCount()-7, 55f, AxisDependency.LEFT); |
| 148 | + |
| 149 | + // redraw the chart |
| 150 | +// mChart.invalidate(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private LineDataSet createSet() { |
| 155 | + |
| 156 | + LineDataSet set = new LineDataSet(null, "Dynamic Data"); |
| 157 | + set.setAxisDependency(AxisDependency.LEFT); |
| 158 | + set.setColor(ColorTemplate.getHoloBlue()); |
| 159 | + set.setCircleColor(ColorTemplate.getHoloBlue()); |
| 160 | + set.setLineWidth(2f); |
| 161 | + set.setCircleSize(4f); |
| 162 | + set.setFillAlpha(65); |
| 163 | + set.setFillColor(ColorTemplate.getHoloBlue()); |
| 164 | + set.setHighLightColor(Color.rgb(244, 117, 117)); |
| 165 | + return set; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void onValueSelected(Entry e, int dataSetIndex) { |
| 170 | + Log.i("Entry selected", e.toString()); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void onNothingSelected() { |
| 175 | + Log.i("Nothing selected", "Nothing selected."); |
| 176 | + } |
| 177 | +} |
0 commit comments