public Axis yAxis() { Axis yAxis = new Axis(); yAxis.setAutoGenerated(false); List<AxisValue> axisValues = new ArrayList<AxisValue>(); // for(int j = 1; j <= 12; j += 1) { // if (doMgdl) { // axisValues.add(new AxisValue(j * 50)); // } else { // axisValues.add(new AxisValue(j*2)); // } // } axisValues.add(new AxisValue(240)); axisValues.add(new AxisValue((int) this.lowMark)); axisValues.add(new AxisValue((int) this.highMark)); yAxis.setValues(axisValues); yAxis.setHasLines(true); yAxis.setLineColor(Color.parseColor("#555555")); yAxis.setMaxLabelChars(5); yAxis.setInside(true); yAxis.setTextSize(axisTextSize); yAxis.setTextColor(Color.parseColor("#8D8D8D")); yAxis.setHasTiltedLabels(true); return yAxis; }
private void updateGraph(Cursor cursor) { // set up the goal line SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int goalCalories = Integer.parseInt(prefs.getString("preference_calorie_goal", "2000")); List<PointValue> goalValues = new ArrayList<>(); // set up the data List<PointValue> values = new ArrayList<>(); cursor.moveToFirst(); if (!cursor.isAfterLast()) { goalValues.add(new PointValue(getTimeFromRow(cursor), goalCalories)); } while (cursor.isAfterLast() == false) { float value = cursor.getFloat(cursor.getColumnIndex("total")); // Log.d("graph", "adding " + value + " on " + sdf.format(getTimeFromRow(cursor))); values.add(new PointValue(getTimeFromRow(cursor), value)); cursor.moveToNext(); } cursor.moveToLast(); goalValues.add(new PointValue(getTimeFromRow(cursor), goalCalories)); List<Line> lines = new ArrayList<Line>(); Line goalLine = new Line(goalValues).setColor(Color.parseColor("#ff0000")); lines.add(goalLine); Line line = new Line(values).setColor(Color.parseColor("#44a134")).setCubic(true); // line.setFilled(true); // line.setHasLabels(true); lines.add(line); LineChartData data = new LineChartData(); data.setLines(lines); Axis timeAxis = new Axis(); // timeAxis.setName(""); // timeAxis.setTextColor(ChartUtils.COLOR_ORANGE); timeAxis.setMaxLabelChars(5); timeAxis.setFormatter(new TimestampAxisFormatter()); timeAxis.setHasLines(true); timeAxis.setHasTiltedLabels(true); data.setAxisXBottom(timeAxis); Axis calorieAxis = new Axis(); // weightAxis.setName(""); // weightAxis.setTextColor(ChartUtils.COLOR_GREEN); calorieAxis.setMaxLabelChars(7); calorieAxis.setFormatter(new SimpleAxisValueFormatter().setAppendedText(" C".toCharArray())); calorieAxis.setHasLines(true); calorieAxis.setHasTiltedLabels(true); data.setAxisYLeft(calorieAxis); hellochart.setLineChartData(data); hellochart.setZoomType(ZoomType.HORIZONTAL); hellochart.setOnValueTouchListener(new ValueTouchListener()); // set up zoom so we can see 0 calories Viewport viewport = new Viewport(hellochart.getMaximumViewport()); viewport.bottom = 0; hellochart.setMaximumViewport(viewport); hellochart.setCurrentViewport(viewport); hellochart.invalidate(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.graph); // generate Dates Calendar calendar = Calendar.getInstance(); Date d1 = calendar.getTime(); calendar.add(Calendar.DATE, 1); Date d2 = calendar.getTime(); calendar.add(Calendar.DATE, 1); Date d3 = calendar.getTime(); Date d4 = calendar.getTime(); calendar.add(Calendar.DATE, 1); Date d5 = calendar.getTime(); calendar.add(Calendar.DATE, 1); Date d6 = calendar.getTime(); Date d7 = calendar.getTime(); calendar.add(Calendar.DATE, 1); Date d8 = calendar.getTime(); calendar.add(Calendar.DATE, 1); Date d9 = calendar.getTime(); GraphView graph = (GraphView) findViewById(R.id.graph); // you can directly pass Date objects to DataPoint-Constructor // this will convert the Date to double via Date#getTime() LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>( new DataPoint[] { new DataPoint(d1, 1), new DataPoint(d2, 5), new DataPoint(d3, 3), new DataPoint(d4, 4), new DataPoint(d5, 6), new DataPoint(d6, 5), new DataPoint(d7, 7), new DataPoint(d8, 4), new DataPoint(d9, 6), }); series.setColor(getResources().getColor(android.R.color.holo_green_dark)); LineGraphSeries<DataPoint> series2 = new LineGraphSeries<DataPoint>( new DataPoint[] { new DataPoint(d1, 2), new DataPoint(d2, 3), new DataPoint(d3, 3), new DataPoint(d4, 4), new DataPoint(d5, 5), new DataPoint(d6, 5), new DataPoint(d7, 7), new DataPoint(d8, 4), new DataPoint(d9, 6), }); series.setColor(getResources().getColor(android.R.color.holo_red_dark)); graph.addSeries(series); graph.addSeries(series2); // set date label formatter graph .getGridLabelRenderer() .setLabelFormatter(new DateAsXAxisLabelFormatter(NativeGraphActivity.this)); graph.getGridLabelRenderer().setNumHorizontalLabels(8); // only 4 because of the space // set manual x bounds to have nice steps graph.getViewport().setMinX(d1.getTime()); graph.getViewport().setMaxX(d9.getTime()); graph.getViewport().setXAxisBoundsManual(true); lecho.lib.hellocharts.view.LineChartView chartView = (LineChartView) findViewById(R.id.chart); List<PointValue> values = new ArrayList<PointValue>(); values.add(new PointValue(0, 2)); values.add(new PointValue(1, 4)); values.add(new PointValue(2, 3)); values.add(new PointValue(3, 4)); // In most cased you can call data model methods in builder-pattern-like manner. Line line = new Line(values) .setColor(getResources().getColor(android.R.color.holo_red_dark)) .setCubic(false); List<Line> lines = new ArrayList<Line>(); lines.add(line); LineChartData data = new LineChartData(); data.setLines(lines); Axis axis = new Axis(); axis.setHasTiltedLabels(true); data.setAxisXBottom(new Axis().setAutoGenerated(true)); data.setAxisYLeft(new Axis().setAutoGenerated(true)); data.setBaseValue(20); chartView.setLineChartData(data); }