2013-03-16 88 views
5

我是新圖書館。我試圖創建下面的情節:Androidplot - 背景和範圍

enter image description here

同時,我有下一個:

enter image description here

我的問題:注:顏色不事項

  1. d怎麼樣o我擺脫了黑色區域(這裏是範圍域和標題的標籤) 並居中情節(如第一張圖)

  2. 如何添加範圍如第一張圖片? (X:[1-7] Y:[0-4500])

  3. 使同一網格在第一圖片

我的代碼:

public class MainActivity extends Activity { 

private XYPlot mySimpleXYPlot; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

// Create a couple arrays of y-values to plot: 
    Number[] days = { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; 
    Number[] values = { 380, 1433, 1965, 3200, 3651, 3215, 3217 }; 

    // initialize our XYPlot reference: 
    mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot); 
    mySimpleXYPlot.getBackgroundPaint().setColor(Color.WHITE); 
    mySimpleXYPlot.setBorderStyle(XYPlot.BorderStyle.NONE, null, null); 
    mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setColor(Color.WHITE); 
    mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE); 



    // Domain 
    mySimpleXYPlot.getGraphWidget().setDomainLabelPaint(null); 
    mySimpleXYPlot.getGraphWidget().setDomainOriginLinePaint(null); 
    mySimpleXYPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, days.length);  
    mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("0")); 


    //Range 
    mySimpleXYPlot.getGraphWidget().setRangeOriginLinePaint(null); 
    mySimpleXYPlot.setRangeStep(XYStepMode.SUBDIVIDE, values.length); 
    mySimpleXYPlot.setRangeValueFormat(new DecimalFormat("0")); 


    //Remove legend 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getLegendWidget()); 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getDomainLabelWidget()); 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getRangeLabelWidget()); 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getTitleWidget()); 

    // Turn the above arrays into XYSeries': 
    XYSeries series1 = new SimpleXYSeries(
      Arrays.asList(days),   
      Arrays.asList(values), 
      "Series1");        // Set the display title of the series 

    // Create a formatter to use for drawing a series using LineAndPointRenderer: 
    LineAndPointFormatter series1Format = new LineAndPointFormatter(
      Color.rgb(0, 200, 0),     // line color 
      Color.rgb(0, 100, 0),     // point color 
      Color.CYAN);       // 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)); 

    series1Format.setFillPaint(lineFill); 

    // add a new series' to the xyplot: 
    mySimpleXYPlot.addSeries(series1, series1Format); 

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

} 
+0

我試圖找出如何刪除X在不完整的網格中的軸線。 – prometheuspk 2014-02-10 15:35:43

回答

14

我希望這是有幫助的。

我的代碼:

private XYPlot mySimpleXYPlot; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Create a couple arrays of y-values to plot: 
    Number[] days = { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; 
    Number[] values = { 380, 1433, 1965, 3200, 3651, 3215, 3217 }; 

    // initialize our XYPlot reference: 
    mySimpleXYPlot = (XYPlot) findViewById(R.id.mySimpleXYPlot); 

    mySimpleXYPlot.setBorderStyle(Plot.BorderStyle.NONE, null, null); 
    mySimpleXYPlot.setPlotMargins(0, 0, 0, 0); 
    mySimpleXYPlot.setPlotPadding(0, 0, 0, 0); 
    mySimpleXYPlot.setGridPadding(0, 10, 5, 0); 

    mySimpleXYPlot.setBackgroundColor(Color.WHITE); 

    mySimpleXYPlot.position(
      mySimpleXYPlot.getGraphWidget(), 
      0, 
      XLayoutStyle.ABSOLUTE_FROM_LEFT, 
      0, 
      YLayoutStyle.RELATIVE_TO_CENTER, 
      AnchorPosition.LEFT_MIDDLE); 

    mySimpleXYPlot.getGraphWidget().getBackgroundPaint().setColor(Color.WHITE); 
    mySimpleXYPlot.getGraphWidget().getGridBackgroundPaint().setColor(Color.WHITE); 

    mySimpleXYPlot.getGraphWidget().getDomainLabelPaint().setColor(Color.BLACK); 
    mySimpleXYPlot.getGraphWidget().getRangeLabelPaint().setColor(Color.BLACK); 

    mySimpleXYPlot.getGraphWidget().getDomainOriginLabelPaint().setColor(Color.BLACK); 
    mySimpleXYPlot.getGraphWidget().getDomainOriginLinePaint().setColor(Color.BLACK); 
    mySimpleXYPlot.getGraphWidget().getRangeOriginLinePaint().setColor(Color.BLACK); 

    // Domain 
    mySimpleXYPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, days.length);  
    mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("0")); 
    mySimpleXYPlot.setDomainStepValue(1); 

    //Range 
    mySimpleXYPlot.setRangeBoundaries(0, 4500, BoundaryMode.FIXED); 
    mySimpleXYPlot.setRangeStepValue(10); 
    //mySimpleXYPlot.setRangeStep(XYStepMode.SUBDIVIDE, values.length); 
    mySimpleXYPlot.setRangeValueFormat(new DecimalFormat("0")); 

    //Remove legend 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getLegendWidget()); 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getDomainLabelWidget()); 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getRangeLabelWidget()); 
    mySimpleXYPlot.getLayoutManager().remove(mySimpleXYPlot.getTitleWidget()); 

    // Turn the above arrays into XYSeries': 
    XYSeries series1 = new SimpleXYSeries(
      Arrays.asList(days),   
      Arrays.asList(values), 
      "Series1");        // Set the display title of the series 

    // Create a formatter to use for drawing a series using LineAndPointRenderer: 
    LineAndPointFormatter series1Format = new LineAndPointFormatter(
      Color.rgb(0, 200, 0),     // line color 
      Color.rgb(0, 100, 0),     // point color 
      Color.CYAN);       // 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)); 

    series1Format.setFillPaint(lineFill); 

    // add a new series' to the xyplot: 
    mySimpleXYPlot.addSeries(series1, series1Format); 

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

}

它看起來是這樣的:

graph

+0

嘿,我想繪製看起來像這樣 – 2015-07-21 10:15:09

+0

圖這是可能的這樣的數據:圖像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAA4YAAAJ1CAYAAACFAu4OAAAgAElEQVR4XuydB5gUVdaGv6runhwYGDKKcRXFrKAoSDAhrrImxBww7SqmXVF0/UUwrQkj6wKmXVExuyoqill0RcwKqEiOQ5g8Pd1V93/OuVXdNT3dMz2BSX1qF6dnuuqG996q5351zj3HyMvLU + hgx477TU2uR9Rzg061AZiRa2wDMJX + qgYcwwZU9LzkKpGzhIAQaH8E6JlATwB + QDgHPRSU81Cg50DN50ZyfTSg + H8GDKVgchUGLHoCKQXD0HUqRWXTV/Sd4mp1S2zPg8l9Qpn8nKIy9Se6MNlnla4nWmi0zOT6U9dZulXeZ6t7tvuMbXodwO9fX9kcxUgZQkAICAEhIARSnoCR0sKwjuG3DRsmi0C9 + ItoyJSfMgJACHRsAjZsmLVEoZY4UZlT67WRAyWRWNQCzFYGiz3TMGDbNpTSQs406Rnj/O6co0wTsGyYrB6j5fJ5/FSi51PNJ1OkVY4w1N8maqsuleuP6Z13hL09qinotHi2nRr – Prasad 2017-03-20 11:49:03

12

在最新的版本,而是採用

mySimpleXYPlot.position(
     mySimpleXYPlot.getGraphWidget(), 
     0, 
     XLayoutStyle.ABSOLUTE_FROM_LEFT, 
     0, 
     YLayoutStyle.RELATIVE_TO_CENTER, 
     AnchorPosition.LEFT_MIDDLE); 

它被使用:

mySimpleXYPlot.getGraphWidget().setSize(new SizeMetrics(
      0, SizeLayoutType.FILL, 
      0, SizeLayoutType.FILL)); 

這將消除黑色區域。

1

這將會對新設備的工作:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
    plot.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
} 
1
// Create a formatter to use for drawing a series using LineAndPointRenderer: 
LineAndPointFormatter series1Format = new LineAndPointFormatter(
     Color.rgb(0, 200, 0),     // line color 
     Color.rgb(0, 100, 0),     // point color 
     Color.CYAN); 

參數丟失

它應該是這樣的

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