2014-10-29 1 views
2

내가 mpAndroidChart 도서관에서 찾고, 그리고 난 (A 막대 그래프에 대한)이 코드를했습니다 : 차트를 보여 주었다 때 MPAndroidChart 자동 줌 막대 그래프 x 축에는

chart.setDescription(""); 
chart.setDrawVerticalGrid(false); 
chart.setDrawGridBackground(false); 
chart.setDrawBarShadow(false); 
chart.setDrawLegend(false); 

XLabels xl = holder.chart.getXLabels(); 
xl.setCenterXLabelText(true); 
xl.setPosition(XLabelPosition.BOTTOM); 
xl.setTextSize(20f); 

YLabels yl = holder.chart.getYLabels(); 
yl.setLabelCount(5); 
yl.setTextSize(20f); 

// set data 
chart.setData((BarData) mChartData); 
holder.chart.setScaleMinima(2f, 0f); 

chart.animateY(700); 

지금, 바

있습니다 확대되었지만 너무 많이 확대되었습니다. 사실 x를 맞추기 위해 setScaleMinima 값을 줄일 때까지 축소 할 수 있습니다. 차트가 표시 될 때 확대/축소가 setScaleMinima 내부에서 선언 한 것입니다. 내가 어떻게 할 수 있니?

+0

스케일 최소값을 0f로 설정하면 너무 많이 축소 할 수 있습니다. 1f로 설정하면 차트 전체를 정확히 볼 수 있습니다. –

답변

4

스케일 최소값을 0f로 설정하면 무한대로 축소 할 수 있습니다. 1f로 설정하면 차트 전체를 정확히 볼 수 있습니다.

그래서 당신은 호출해야합니다 : 만 줌 경계를 설정하지만 실제 줌을하지 않습니다

setScaleMinima(2f, 1f)

합니다.

그런 다음

zoom(...) 당신이 원하는 목적지까지 확대 할 수있는 방법을 사용합니다.

+0

안녕하세요, Android 용 라이브러리를 사용 중입니다. http://stackoverflow.com/q/39589620/1503130에 게시 된 문제를 받고 있습니다. 너는 나를 똑같이 안내해 줄 수 있니? – Prateek

0

저는 BarChart 라이브러리에 대해 잘 모릅니다 만 직접 구현할 수 있습니다. 내가 최근에 만든 하나의 스 니펫을 붙여 놓을 예정입니다.

package com.example.preferencestest.util; 

import com.example.preferencestest.R; 

import android.content.Context; 
import android.content.res.Resources; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.util.AttributeSet; 
import android.view.View; 

public class MyCustomChartView extends View { 
    private int width=0, height=0; 

    public MyCustomChartView(Context context) { 
     super(context); 
     setup(null); 
    } 

    public MyCustomChartView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setup(attrs); 
    } 
    public MyCustomChartView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setup(attrs); 
    } 

    private int firstValue=0, secondValue=0, thirdValue=0, horizontalPadding=20, verticalPadding=40; 
    private String title=""; 
    private Paint outerLine, grid, other; 
    private Resources res; 

    private void setup(AttributeSet attrs) { 
     if (attrs != null) { 
      Context ctx = getContext(); 
      TypedArray ta = ctx.obtainStyledAttributes(attrs, R.styleable.MyCustomCharView); 
      firstValue = ta.getInt(R.styleable.MyCustomCharView_Plot_First_Value, firstValue); 
      secondValue = ta.getInt(R.styleable.MyCustomCharView_Plot_Second_Value, secondValue); 
      thirdValue = ta.getInt(R.styleable.MyCustomCharView_Plot_Third_Value, thirdValue); 
      ta.recycle(); 
     } 
     res = getResources(); 
     outerLine = new Paint(Paint.ANTI_ALIAS_FLAG); 
     outerLine.setColor(res.getColor(R.color.plot_outer_line)); 
     outerLine.setStrokeWidth(3); 
     grid = new Paint(Paint.ANTI_ALIAS_FLAG); 
     grid.setColor(res.getColor(R.color.plot_grid)); 
     grid.setStrokeWidth(2); 
     other = new Paint(Paint.ANTI_ALIAS_FLAG); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     width = w; height = h; 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     //float width = canvas.getWidth(), height = canvas.getHeight(); 
     int limit = Math.max(6, Math.max(firstValue, Math.max(secondValue, thirdValue))); 
     canvas.drawLine(horizontalPadding, verticalPadding, 
       horizontalPadding, height-verticalPadding, outerLine); 
     canvas.drawLine(horizontalPadding, height-verticalPadding, 
       width-horizontalPadding, height-verticalPadding, outerLine); 

     int gridVertMargin = Math.round((height-(verticalPadding*2))/7); 
     for (int i=0; i<7; i++) { 
      canvas.drawLine(horizontalPadding+1, 
        verticalPadding+(i*gridVertMargin), 
        width-horizontalPadding, 
        verticalPadding+(i*gridVertMargin), grid); 
     } 

     int gridHoriMargin = Math.round((width-(horizontalPadding*2))/limit); 
     for (int i = 0; i < limit; i++) { 
      canvas.drawLine(horizontalPadding+((i+1)*gridHoriMargin), 
        verticalPadding+1, 
        horizontalPadding+((i+1)*gridHoriMargin), 
        height-verticalPadding, grid); 
      canvas.drawText(String.valueOf(i+1), horizontalPadding+((i+1)*gridHoriMargin)-2, height-(verticalPadding/2), outerLine); 
     } 

     other.setColor(res.getColor(R.color.plot_first_color)); 
     if (this.firstValue > 0) { 
      canvas.drawRect(horizontalPadding+1, 
        verticalPadding+(gridVertMargin*1)+1, 
        this.firstValue*gridHoriMargin+horizontalPadding-1, 
        verticalPadding+(gridVertMargin*2)-1, other); 
     } 

     other.setColor(res.getColor(R.color.plot_second_color)); 
     if (this.secondValue > 0) { 
      canvas.drawRect(horizontalPadding+1, 
        verticalPadding+(gridVertMargin*3)+1, 
        this.secondValue*gridHoriMargin+horizontalPadding-1, 
        verticalPadding+(gridVertMargin*4)-1, other); 
     } 

     other.setColor(res.getColor(R.color.plot_third_color)); 
     if (this.thirdValue > 0) { 
      canvas.drawRect(horizontalPadding+1, 
        verticalPadding+(gridVertMargin*5)+1, 
        this.thirdValue*gridHoriMargin+horizontalPadding-1, 
        verticalPadding+(gridVertMargin*6)-1, other); 
     } 
    } 

    public int getFirstValue() { 
     return firstValue; 
    } 

    public void setFirstValue(int first) { 
     if (this.firstValue != first) { 
      this.firstValue = first; 
      invalidate(); 
     } 
    } 

    public int getSecondValue() { 
     return secondValue; 
    } 

    public void setSecondValue(int second) { 
     if (this.secondValue != second) { 
      this.secondValue = second; 
      invalidate(); 
     } 
    } 

    public int getThirdValue() { 
     return thirdValue; 
    } 

    public void setThirdValue(int third) { 
     if (this.thirdValue != third) { 
      this.thirdValue = third; 
      invalidate(); 
     } 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     if (! this.title.equals(title)) { 
      this.title = title; 
      invalidate(); 
     } 
    } 
} 
이 같은 XML styleable 값 (첫째,이 경우 두 번째와 세 번째 줄)에 대한 XML 파일이 필요합니다

:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyCustomCharView"> 
     <attr name="Plot_First_Value" format="integer" /> 
     <attr name="Plot_Second_Value" format="integer" /> 
     <attr name="Plot_Third_Value" format="integer" /> 
    </declare-styleable> 

    <color name="plot_outer_line">#330000</color> 
    <color name="plot_grid">#dd0000</color> 
    <color name="plot_first_color">#99aa00</color> 
    <color name="plot_second_color">#770000</color> 
    <color name="plot_third_color">#00ee77</color> 
</resources> 

을 그리고 마지막으로 당신이이 같은 새 막대 그래프를 추가 할 수 있습니다 레이아웃 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res/[yourpackage]"> 

     <[yourpackage].MyCustomChartView 
      android:id="@+id/row_plot" 
      style="@style/ListItem" 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      title="" 
      app:Plot_First_Value="15" 
      app:Plot_Second_Value="1" 
      app:Plot_Third_Value="3" /> 
{...} 
+0

barchart가 라이브러리가 아닙니다. mpAndroidchart 라이브러리의 barChart를 생성하는 객체입니다. – giozh

+0

이 뷰를 작성하면 mpAndroidchart를 알지 못했거나 사용할 수 없습니다. 그러나이 예제에서는 많은 줌 유형과 이벤트를 처리하는 것으로 보입니다. 프로젝트 github 페이지에서 볼 수 있듯이 :'데모 어플리케이션의 해당 코드는 MPChartExample 폴더 안에있는이 저장소에 포함되어 있습니다 .' 줌 레벨을 자동으로 처리하는 소스가 있습니다. – blender