Skip to content

EnthuDai/SimpleChart-Kotlin

Repository files navigation

一款轻量级android图表组件SimpleChart-Kotlin

效果演示

基础效果演示

引入方式

gradle导入

  1. 在更目录中的build.gradle添加 JitPack仓库

    allprojects {
    	repositories {
    		...
    		maven { url 'https://jitpack.io' }
    	}
    }

    以上是官方提供的添加仓库方法,实测在较新的gradle版本中此方式无法成功生效,而是需要在settings.gradle中添加:

    dependencyResolutionManagement {
    	repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    	repositories {
        	...
        	maven { url 'https://jitpack.io' }
     }
    }
  2. 添加依赖

    dependencies {
        implementation 'com.github.EnthuDai:SimpleChart-Kotlin:1.0.8'
    }	

PS: 发布一个kotlin的android library到jitpack的过程极为艰难,尤其是在使用较新版本的gradle时,大家如果遇到问题,可以参考一下这篇文章

kotlin android library 发布至jitpack问题及解决方式记载

源码导入

  1. 下载该项目源码

    git clone https://github.com/EnthuDai/SimpleChart-Kotlin.git
  2. 在需要使用该库的项目中通过文件导入此模块 导入模块步骤1 导入模块步骤二,选择上一步中下载项目的chart文件夹

  3. 在app的build.gradle的依赖中添加此库

    dependencies {
    
    	...
    
     implementation project(':chart')
    }

使用方式

布局文件

<com.poemdistance.chart.BarChart
        android:id="@+id/barChart"
        android:layout_width="0dp"
        android:layout_height="300dp"
        app:title="柱状图"
        app:xAxis="staDate"
        app:yAxis="profit"
        app:yAxisDesc="入账收益"/>

设置数据

数据设置的方式非常简单,支持直接传入JSON数组字符串,或传入List< Any>形式的对象集合。所有类型图表均支持这两种方式的数据传入。JSON数组字符串的导入代码如下:

val jsonData = """
        [{"staDate":"01-09","profit":235,"activeOrderCount":36},
        {"staDate":"01-08","profit":244,"activeOrderCount":33},
        {"staDate":"01-07","profit":159,"activeOrderCount":29},
        {"staDate":"01-06","profit":452,"activeOrderCount":48},
        {"staDate":"01-05","profit":116,"activeOrderCount":19},
        {"staDate":"01-04","profit":131,"activeOrderCount":22},
        {"staDate":"01-03","profit":345,"activeOrderCount":32},
        {"staDate":"01-02","profit":277,"activeOrderCount":35},
        {"staDate":"01-01","profit":206,"activeOrderCount":28}]
    """.trimIndent()
barChart.updateData(jsonData) // barChart为该图表的实例

事件监听

监听用户当前触摸的数据项

// 监听触摸事件,将获得Json字符串形式的当前触控数据,当触控点从图表移开时返回null
barChart.listener =  {
	it?.let{
		Log.d(LOG_TAG, it)
	}
}

配置项

所有图表均具备的配置项

属性名 类型 默认值 含义
title String 图表标题
animation Boolean True 是否开启加载动画
animationDuration Int 500 加载动画时长
showLegend Boolean 自动(单维度数据时不显示) 是否显示图例

柱状图 com.poemdistance.chart.BarChart

属性名 类型 默认值 含义 是否必填
xAxis String x轴数据的参数名
yAxis String y轴数据的参数名
yAxisDesc String yAxis值 描述y轴数据项的参数名

折线图 com.poemdistance.chart.LineChart

属性名 类型 默认值 含义 是否必填
xAxis String x轴数据的参数名
yAxis String y轴数据的参数名,支持多个,用“,”分隔
yAxisDesc String yAxis值 描述y轴数据项的参数名,支持多个,用“,”分隔

饼图 com.poemdistance.chart.PieChart

属性名 类型 默认值 含义 是否必填
name String 数据项的展示参数名
value String 数据项的值参数名

最后

欢迎使用和issue

About

一款用Kotlin写的基础图表组件。支持动画加载、深色模式、JSON数据导入、触摸事件监听。

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages