private void setData(int start, int count, float range) { ArrayList<String> xVals = new ArrayList<String>(); for (int i = 0; i < count; i++) { xVals.add((start + i) + "日"); } ArrayList<Entry> vals1 = new ArrayList<Entry>(); for (int i = 0; i < count; i++) { vals1.add(new Entry(TurnControl.PunchPerDay[9 - i], i)); } // create a dataset and give it a type LineDataSet set1 = new LineDataSet(vals1, "近期打卡情况"); set1.setDrawCubic(true); set1.setCubicIntensity(0.2f); set1.setDrawFilled(true); set1.setDrawCircles(true); set1.setLineWidth(2f); set1.setCircleSize(5f); set1.setHighLightColor(Color.rgb(244, 117, 117)); set1.setColor(Color.rgb(104, 241, 175)); set1.setFillColor(ColorTemplate.getHoloBlue()); // create a data object with the datasets LineData data = new LineData(xVals, set1); data.setValueTypeface(tf); data.setValueTextSize(9f); data.setDrawValues(false); // set data mChart.setData(data); }
private void addEntry() { LineData data = mChart.getData(); int year = 2015; if (data != null) { LineDataSet set = data.getDataSetByIndex(0); if (set == null) { set = createSet(); data.addDataSet(set); } // data.addXValue(mMonths[data.getXValCount() % 12] + " " + (year + // data.getXValCount() /12)); // data.addEntry(new Entry((float) (Math.random() * 40) + 30f, set.getEntryCount(), // 0)); mChart.notifyDataSetChanged(); mChart.setVisibleXRangeMaximum(120); mChart.moveViewToX(data.getXValCount() - 121); } }
@Override public float getFillLinePosition( LineDataSet dataSet, LineData data, float chartMaxY, float chartMinY) { float fillMin = 0f; if (dataSet.getYMax() > 0 && dataSet.getYMin() < 0) { fillMin = 0f; } else { if (!mStartAtZero) { float max, min; if (data.getYMax() > 0) max = 0f; else max = chartMaxY; if (data.getYMin() < 0) min = 0f; else min = chartMinY; fillMin = dataSet.getYMin() >= 0 ? min : max; } else { fillMin = 0f; } } return fillMin; }
private void addEntry() { LineData data = mLineChart.getData(); if (data != null) { LineDataSet set = data.getDataSetByIndex(0); if (set == null) { set = createSet(); data.addDataSet(set); } // add a new random value float a = r.nextFloat() * (2.00f + 2.00f) - 2.0f; System.out.println(a); data.addXValue(""); data.addEntry(new Entry(a, set.getEntryCount()), 0); mLineChart.notifyDataSetChanged(); mLineChart.setVisibleXRange(6, 0); mLineChart.moveViewToX(data.getXValCount() - 7); } }
private void setData(int count, float range) { // only using first meter for now. // TODO: graph multiple meters? Meter meter = mMeters.get(0); List<MeterReading> readings = meter.getReadings(); ArrayList<Entry> averageEntries = new ArrayList<Entry>(); // build up X-axis labels; e.g., dates. // build up Y-axis with values; // skip first date as we can't determine consumption. ArrayList<String> xVals = new ArrayList<String>(); ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>(); float runningTotal = 0; float dailyAverage; for (int lv = 1; lv < count; lv++) { MeterReading prevReading = readings.get(lv - 1); MeterReading currReading = readings.get(lv); xVals.add(removeYearFromDate(currReading.getTimeStamp())); float val = (currReading.getConsumption() - prevReading.getConsumption()) * 10; yVals1.add(new BarEntry(val, lv - 1)); // calc & store daily average. runningTotal += val; dailyAverage = runningTotal / lv; averageEntries.add(new Entry(dailyAverage, lv - 1)); } // TODO: allow name to be updated. BarDataSet set1 = new BarDataSet(yVals1, "mine"); set1.setBarSpacePercent(35f); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); dataSets.add(set1); // bar chart data BarData barData = new BarData(xVals, dataSets); barData.setValueFormatter(new MyValueFormatter()); barData.setValueTextSize(10f); // line chart data LineData lineData = new LineData(); LineDataSet set = new LineDataSet(averageEntries, "average"); int lineColor = Color.rgb(251, 169, 165); set.setColor(lineColor); set.setLineWidth(1.5f); set.setDrawCircles(false); set.setFillColor(lineColor); set.setDrawCubic(true); set.setDrawValues(false); set.setAxisDependency(YAxis.AxisDependency.LEFT); lineData.addDataSet(set); CombinedData combinedData = new CombinedData(getDates(meter.getReadings())); combinedData.setData(barData); combinedData.setData(lineData); mChart.setData(combinedData); mChart.invalidate(); }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.kommfort, container, false); RelativeLayout relativ = (RelativeLayout) rootView.findViewById(R.id.bak); mLineChart = (LineChart) relativ.findViewById(R.id.linechartkommfort); mLineChart.setOnChartValueSelectedListener(this); // no description text mLineChart.setDescription("Beschleunigungskraefte"); mLineChart.setNoDataTextDescription("You need to provide data for the chart."); // enable highlighting mLineChart.setHighlightEnabled(true); // enable touch gestures mLineChart.setTouchEnabled(true); // enable scaling and dragging mLineChart.setDragEnabled(true); mLineChart.setScaleEnabled(true); mLineChart.setDrawGridBackground(false); // if disabled, scaling can be done on x- and y-axis separately mLineChart.setPinchZoom(true); // set an alternative background color mLineChart.setBackgroundColor(Color.WHITE); LineData data = new LineData(); data.setValueTextColor(Color.BLACK); // add empty data mLineChart.setData(data); // get the legend (only possible after setting data) Legend l = mLineChart.getLegend(); // modify the legend ... // l.setPosition(LegendPosition.LEFT_OF_CHART); l.setForm(Legend.LegendForm.LINE); l.setTextColor(Color.BLACK); XAxis xl = mLineChart.getXAxis(); xl.setTextColor(Color.BLACK); xl.setDrawGridLines(false); xl.setAvoidFirstLastClipping(true); YAxis leftAxis = mLineChart.getAxisLeft(); leftAxis.setTextColor(Color.BLACK); leftAxis.setAxisMaxValue(2f); leftAxis.setAxisMinValue(-2f); leftAxis.setStartAtZero(false); leftAxis.setDrawGridLines(true); YAxis rightAxis = mLineChart.getAxisRight(); rightAxis.setEnabled(false); Button zukamm = (Button) rootView.findViewById(R.id.butonzumkreis); // TextView zubesh=(TextView) rootView.findViewById(R.id.zubeschleunigungskraefte); // Layout button=(Layout) rootView.findViewById(R.id.); zukamm.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), KammsherKreis.class); Kommfort.this.startActivity(intent); } }); /** * zubesh.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { * Intent myIntent = new Intent(getActivity(), Beschleunigungskraefte.class); * getActivity().startActivityForResult(myIntent,100); } }); */ return rootView; }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_status, container, false); // TODO Need lot of design changes in chart // TODO Have to update LineChart with CombineChart to display LineChart with BarChart mChart = (LineChart) view.findViewById(R.id.chartInsight); mChart.setDescription(""); mChart.setBackgroundColor(Color.WHITE); mChart.setDrawGridBackground(false); LineData d = new LineData(); ArrayList<Entry> entries = new ArrayList<>(); for (int index = 0; index < 12; index++) entries.add(new Entry(getRandom(25, 10), index)); LineDataSet set = new LineDataSet(entries, "DataSet1"); set.setColor(Color.rgb(240, 238, 70)); set.setLineWidth(2.5f); set.setCircleColor(Color.rgb(240, 238, 70)); set.setCircleSize(5f); set.setFillColor(Color.rgb(240, 238, 70)); set.setDrawCubic(true); set.setDrawValues(true); set.setValueTextSize(10f); set.setValueTextColor(Color.rgb(240, 238, 70)); for (int index = 0; index < 12; index++) entries.add(new Entry(getRandom(25, 10), index)); LineDataSet set2 = new LineDataSet(entries, "DataSet2"); set.setColor(Color.rgb(240, 238, 70)); set.setLineWidth(2.5f); set.setCircleColor(Color.rgb(240, 238, 70)); set.setCircleSize(5f); set.setFillColor(Color.rgb(240, 238, 70)); set.setDrawCubic(true); set.setDrawValues(true); set.setValueTextSize(10f); set.setValueTextColor(Color.rgb(240, 238, 70)); set.setAxisDependency(YAxis.AxisDependency.LEFT); d.addDataSet(set); d.addDataSet(set2); mChart.setData(d); mChart.setDescription("Fuel Insight"); mChart.animateXY(2000, 2000); mChart.invalidate(); // String[] projection = { FuelContract.COLUMN_AMOUNT, FuelContract.COLUMN_ID }; // Cursor fuelData = // inflater.getContext().getContentResolver().query(FuelProvider.FUEL_CONTENT_URI, projection, // null, null, null); // while (fuelData.getCount()!=0) { // fuelData.moveToNext(); // Log.i("FuelData", "ID: " + fuelData.getString(1) + " Amount: " + // fuelData.getString(0)); // } return view; }