2014-03-06 2 views
1

this과 같은 작업을하려고하지만 모양이 약간 융통성이 있습니다. 본질적으로 파이의 일부만 채워진 파이 차트 (그리고 나머지는 비어 있음) 또는 일종의 다이얼 차트입니다.Android Polar Graphing

극좌표를 사용하여 두 개의 화살표 (0도 1도 및 1도 -92도)를 그리는 것도 상대적으로 쉽지만 Android에서이 작업을 수행 할 수있는 라이브러리는 찾을 수 없습니다. 0 도가 실제로 극좌표 0도처럼 보일 때가 필요합니다.

저는 AChartEngine DialChart를 사용하여 가까이에서 뭔가를 얻을 수 있었지만 각 화살표에 표시되는 레이블을 얻는 방법을 알 수는 없습니다. renderer.setDisplayValues(true);series.setDisplayChartValues(true); 을 시도했지만 두 개의 화살표에 값이 표시되지 않으므로 DialChart에서도 가능한지 확실하지 않습니다. 배경에 다이얼의 레이블을 표시하면 사용자가 화살표에 레이블을 지정할 필요가 없지만 0을 0으로 보이게하려면 DialChart가 추가 된 LinearLayout을 회전시키고 있습니다. 극지 그래프에서. 또한 renderer.setShowLabels(false);을 사용하고 허위로 표시 할 수있는 모든 다른 것들을 설정 했음에도 불구하고 백그라운드에서 다이얼의 레이블을 숨기기 위해 고심하고 있습니다. 내 해킹은 라벨 색상을 배경 색상으로 설정하는 것이지만 더 좋은 방법이 있다면 알려 주시기 바랍니다.

다음은 DialChart 용 코드입니다.

CategorySeries category = new CategorySeries("Angle"); 
category.add("Extension", 0); 
category.add("Flexion", 90); 

renderer = new DialRenderer(); 
renderer.setLabelsColor(getActivity().getResources().getColor(R.color.background)); 

renderer.setInScroll(true); 
renderer.setDisplayValues(true); 

renderer.setShowLegend(false); 
renderer.setShowAxes(false); 
renderer.setShowLabels(false); 
renderer.setShowGrid(false); 
renderer.setMargins(new int[] {20, 30, 15, 0}); 
renderer.setVisualTypes(new DialRenderer.Type[] {Type.ARROW, Type.ARROW}); 
renderer.setMinValue(-20); 
renderer.setMaxValue(280); 
renderer.setPanEnabled(false); 
renderer.setZoomEnabled(false); 

SimpleSeriesRenderer r = new SimpleSeriesRenderer(); 
series.setColor(getActivity().getResources().getColor(R.color.green)); 
series.setDisplayChartValues(true); 
series.setChartValuesTextSize(30); 
visualizationRenderer.addSeriesRenderer(r); 

r = new SimpleSeriesRenderer(); 
series.setColor(getActivity().getResources().getColor(R.color.green)); 
series.setDisplayChartValues(true); 
series.setChartValuesTextSize(30); 
renderer.addSeriesRenderer(r); 

visualization = ChartFactory.getDialChartView(getActivity(), category, renderer); 

LinearLayout layout = (LinearLayout) this.getView().findViewById(R.id.sessions_visualization); 
layout.addView(visualization); 

layout.setRotation(220.0f); 

나는이 코드를 수정하여 작동하는 것을 얻거나, 내가하려고하는 것을 달성하는 데 도움이되는 다른 라이브러리를 열어 둔다. 감사!

+0

해결책을 찾은 것 같습니다. 질문의 마지막 단락입니다. –

+0

다른 사람들이 비슷한 것을했는지 (몇 시간 동안 검색 한 후에도 아무 것도 찾을 수 없었 음) 알지 못했기 때문에 AChartEngine을 얻을 수 없어 라이브러리가 더 잘 작동하는지 알고 있는지 마지막 단락에 질문했습니다. 내가 원하는 걸 해줄거야. 나는 사용자 정의보기를 만들었습니다 (지금까지 안드로이드에서 할 수있는 것을 몰랐습니다). – laberle

답변

0

나는 나중에 이와 같은 일을하고 싶은 사람을 위해 내 자신의 질문에 대답하고 있습니다.

Android에서 맞춤보기를 만들고 표시 할 항목을 그릴 수 있습니다. 좋은 문서 here이 있습니다.

다음은 관련 코드 스 니펫입니다. 그것은 완벽하지는 않지만 일을합니다.

public class AngleVisualization extends View { 
    private Paint textPaint; 
    private Paint arcPaint; 
    private Paint linePaint; 
    RectF oval; 
    private float extension; 
    private float flexion; 
    private int textColor; 
    private int arcColor; 
    private float extensionLabelX; 
    private float extensionLabelY; 
    private float flexionLabelX; 
    private float flexionLabelY; 

    private Rect extensionBounds = new Rect(); 


    public AngleVisualization(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray a = context.getTheme().obtainStyledAttributes(
      attrs, 
      R.styleable.AngleVisualization, 
      0, 0); 

    try { 
     extension = a.getFloat(R.styleable.AngleVisualization_extensionValue, 0); 
     flexion = a.getFloat(R.styleable.AngleVisualization_flexionValue, 0); 
     textColor = a.getColor(R.styleable.AngleVisualization_textColor, Color.BLACK); 
     arcColor = a.getColor(R.styleable.AngleVisualization_arcColor, context.getResources().getColor(R.color.green)); 
     extensionLabelX = a.getDimension(R.styleable.AngleVisualization_extensionLabelX, 190); 
     extensionLabelY = a.getDimension(R.styleable.AngleVisualization_extensionLabelY, 150); 
     flexionLabelX = a.getDimension(R.styleable.AngleVisualization_flexionLabelX, 50); 
     extensionLabelY = a.getDimension(R.styleable.AngleVisualization_flexionLabelY, 190); 

    } finally { 
     a.recycle(); 
    } 

    oval = new RectF(); 

    init(); 
} 

private void init() { 
    textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    textPaint.setColor(textColor); 
    textPaint.setTextSize(30); 

    arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    arcPaint.setColor(arcColor); 

    linePaint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    linePaint.setColor(arcColor); 
    linePaint.setStrokeWidth(3); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    String extensionString = decimalFormat.format(extension) + "˚"; 
    textPaint.getTextBounds(extensionString, 0, extensionString.length(), extensionBounds); 

    canvas.drawArc(oval, extension, flexion - extension, true, arcPaint); 
    canvas.drawLine(0.0f, extensionBounds.height(), oval.right/2, extensionBounds.height(), linePaint); 
    canvas.drawText(extensionString, extensionLabelX, extensionLabelY, textPaint); 
    canvas.drawText(decimalFormat.format(flexion) + "˚", flexionLabelX, flexionLabelY, textPaint); 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    // Account for padding 
    float xpad = (float)(getPaddingLeft() + getPaddingRight()); 
    float ypad = (float)(getPaddingTop() + getPaddingBottom()); 

    float ww = (float)w - xpad; 
    float hh = (float)h - ypad; 

    String extensionString = decimalFormat.format(extension) + "˚"; 
    textPaint.getTextBounds(extensionString, 0, extensionString.length(), extensionBounds); 
    float diameter = Math.min(ww, (hh - extensionBounds.height()) * 2.0f) - extensionBounds.height(); 

    oval = new RectF(
      0, 
      diameter/-2.0f, 
      diameter, 
      diameter/2.0f); 
    oval.offsetTo(getPaddingLeft(), getPaddingTop() - diameter/2.0f + extensionBounds.height()); 
    flexionLabelY = diameter/2.0f + extensionBounds.height(); 
    flexionLabelX = 0; 
    extensionLabelY = extensionBounds.height(); 
    extensionLabelX = ww/2; 
} 
} 
관련 문제