Пример #1
0
  private void ShowPlot() {
    mySimpleXYPlot = (XYPlot) findViewById(R.id.chartPlot);
    mySimpleXYPlot.setTitle("Weight");

    if (plotLine != null) mySimpleXYPlot.removeSeries(plotLine);

    mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE);
    mySimpleXYPlot.getGraphWidget().getGridLinePaint().setColor(Color.BLACK);
    mySimpleXYPlot
        .getGraphWidget()
        .getGridLinePaint()
        .setPathEffect(new DashPathEffect(new float[] {1, 1}, 1));
    mySimpleXYPlot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK);
    mySimpleXYPlot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK);

    mySimpleXYPlot.setBorderStyle(Plot.BorderStyle.SQUARE, null, null);
    mySimpleXYPlot.getBorderPaint().setStrokeWidth(1);
    mySimpleXYPlot.getBorderPaint().setAntiAlias(false);
    mySimpleXYPlot.getBorderPaint().setColor(Color.WHITE);
    mySimpleXYPlot.getGraphWidget().setPaddingRight(5);

    // Create a formatter to use for drawing a series using LineAndPointRenderer:
    LineAndPointFormatter series1Format =
        new LineAndPointFormatter(
            Color.rgb(0, 100, 0), // line color
            Color.rgb(0, 100, 0), // point color
            Color.rgb(100, 200, 0)); // fill color

    // setup our line fill paint to be a slightly transparent gradient:
    Paint lineFill = new Paint();
    lineFill.setAlpha(200);
    lineFill.setShader(
        new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.GREEN, Shader.TileMode.MIRROR));

    LineAndPointFormatter formatter =
        new LineAndPointFormatter(Color.rgb(0, 0, 0), Color.BLUE, Color.RED);
    formatter.setFillPaint(lineFill);

    plotLine = new SimpleXYSeries(Arrays.asList(dates), Arrays.asList(weights), "Weight over time");
    mySimpleXYPlot.addSeries(plotLine, formatter);
    mySimpleXYPlot.redraw();

    // draw a domain tick for each year:
    int min = DomainMin();
    int max = DomainMax();
    int domainStep = domainStep(min, max);
    mySimpleXYPlot.setDomainStep(XYStepMode.SUBDIVIDE, domainStep);
    // mySimpleXYPlot.setDomainBoundaries(min, max, BoundaryMode.AUTO);

    // customize our domain/range labels
    mySimpleXYPlot.setDomainLabel("Date");
    mySimpleXYPlot.setRangeLabel("Weight");

    Double minimum = FindMinimum();
    Double maximum = FindMaximum();
    Double stepSize = FindStep(maximum, minimum);
    mySimpleXYPlot.setRangeBoundaries(minimum, maximum, BoundaryMode.FIXED);
    mySimpleXYPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, stepSize);

    mySimpleXYPlot.setDomainValueFormat(
        new Format() {
          private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM");

          @Override
          public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {

            // because our timestamps are in seconds and SimpleDateFormat expects milliseconds
            // we multiply our timestamp by 1000:
            long timestamp = ((Number) obj).longValue() * 1000;
            Date date = new Date(timestamp);
            return dateFormat.format(date, toAppendTo, pos);
          }

          @Override
          public Object parseObject(String source, ParsePosition pos) {
            return null;
          }
        });

    // by default, AndroidPlot displays developer guides to aid in laying out your plot.
    // To get rid of them call disableAllMarkup():
    mySimpleXYPlot.disableAllMarkup();
  }