Android LineChart

Posted by PeEn
2019. 10. 16. 15:37 Programing/Android

 

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<android:lecho.lib.hellocharts.view.LineChartView
    android:id="@+id/chart"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    layout_height="match_parent"
    layout_width="match_parent" />

</LinearLayout>

Main.java

public class MainActivity extends AppCompatActivity {

    LineChartView lineChartView;
    String[] axisData = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept",
            "Oct", "Nov", "Dec"};
    int[] yAxisData = {50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lineChartView = findViewById(R.id.chart);

        List yAxisValues = new ArrayList();
        List axisValues = new ArrayList();


        Line line = new Line(yAxisValues).setColor(Color.parseColor("#9C27B0"));

        for (int i = 0; i < axisData.length; i++) {
            axisValues.add(i, new AxisValue(i).setLabel(axisData[i]));
        }

        for (int i = 0; i < yAxisData.length; i++) {
            yAxisValues.add(new PointValue(i, yAxisData[i]));
        }

        List lines = new ArrayList();
        lines.add(line);

        LineChartData data = new LineChartData();
        data.setLines(lines);

        Axis axis = new Axis();
        axis.setValues(axisValues);
        axis.setTextSize(16);
        axis.setTextColor(Color.parseColor("#03A9F4"));
        data.setAxisXBottom(axis);

        Axis yAxis = new Axis();
        yAxis.setName("Sales in millions");
        yAxis.setTextColor(Color.parseColor("#03A9F4"));
        yAxis.setTextSize(16);
        data.setAxisYLeft(yAxis);

        lineChartView.setLineChartData(data);
        Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
        viewport.top = 110;
        lineChartView.setMaximumViewport(viewport);
        lineChartView.setCurrentViewport(viewport);
    }

}

build.gradle

 allprojects {
        repositories {
            google()
            jcenter()
        }
    }

dependencies {

	implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'
    
}

'Programing > Android' 카테고리의 다른 글

커스텀 토스트, 커스텀 다이얼로그  (0) 2019.10.23
배열과 테이블 위젯 이용한 계산기, 숫자버튼 예제  (0) 2019.10.21
탭호스트  (0) 2019.10.21
날짜 시간 위젯  (0) 2019.10.21
ContextMenu  (0) 2019.10.21