StockChart 一款适用于Android的高扩展性、高性能股票图/K线图开发库,支持多图组合,除了使用内置子图还支持定制子图满足复杂的业务需求。
StockChart 一款适用于Android的高扩展性、高性能股票图/K线图开发库,支持多图组合,除了使用内置子图还支持定制子图满足复杂的业务需求。
1、示例截图:
2、gif演示:
3、自定义子图实现案例:
(若图片未显示,可能需要科学上网。)
StockChart
的子图。内置的子图:KChart
(K线图)、TimeBarChart
(时间条图)、VolumeChart
(成交量图)、MacdChart
(MACD指标图)、KdjChart
(KDJ指标图)StockChartConfig
,每个子图也有自己的配置如:KChartConfig
、KDJChartConfig
。allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.wangyiqian:StockChart:1.1.15'
}
<com.github.wangyiqian.stockchart.StockChart android:id="@+id/stock_chart"
android:layout_width="match_parent" android:layout_height="wrap_content" />
StockChart
的配置val stockChartConfig = StockChartConfig()
stock_chart.setConfig(stockChartConfig)
// K线图的配置与工厂
val kChartConfig = KChartConfig()
val kChartFactory = KChartFactory(stockChart = stock_chart, childChartConfig = kChartConfig)
// 时间条图的配置与工厂
val timeBarConfig = TimeBarConfig()
val timeBarFactory = TimeBarFactory(stockChart = stock_chart, childChartConfig = timeBarConfig)
// MACD指标图的配置与工厂
val macdChartConfig = MacdChartConfig()
val macdChartFactory =
MacdChartFactory(stockChart = stock_chart, childChartConfig = macdChartConfig)
// 将需要显示的子图的工厂加入全局配置
stockChartConfig.addChildCharts(kChartFactory, timeBarFactory, macdChartFactory)
// 加载模拟数据
Data.loadDayData(this, 0) { kEntities: List<IKEntity> ->
// 初始显示最后50条数据
val pageSize = 50
// 设置加载到的数据
stockChartConfig.setKEntities(
kEntities,
showStartIndex = kEntities.size - pageSize,
showEndIndex = kEntities.size - 1
)
// 通知更新K线图
stock_chart.notifyChanged()
}
注意:任何配置的修改都需要调用
StockChart
的notifyChanged()
方法去更新视图展示
详细请参考提供的示例(samples模块)
StockChartConfig
字段 | 描述 |
---|---|
kEntities | K线数据 |
showStartIndex | 初始显示区域的起始坐标 |
showEndIndex | 初始显示区域的结束坐标 |
scrollAble | 是否支持滑动 |
overScrollAble | 是否支持"滑过头回弹"效果 |
overScrollDistance | "滑过头回弹"最大距离 |
overScrollOnLoadMoreDistance | "滑过头回弹"过程中触发加载更多需要的距离 |
scaleAble | 是否支持双指缩放 |
scrollSmoothly | 是否需要"平滑的"滑动。如果false,滑动时一个下标对应的内容要么全显示,要么不显示。 |
frictionScrollExceedLimit | 超出滑动限制范围时拖动的"摩擦力" |
scaleFactorMax | 双指缩放最大缩放比例 |
scaleFactorMin | 双指缩放最小缩放比例 |
showHighlightHorizontalLine | 是否支持长按高亮横线 |
highlightHorizontalLineWidth | 长按高亮横线宽度 |
highlightHorizontalLineColor | 长按高亮横线颜色 |
highlightHorizontalLinePathEffect | 长按高亮横线虚线 |
showHighlightVerticalLine | 是否支持长按高亮竖线 |
highlightVerticalLineWidth | 长按高亮竖线宽度 |
highlightVerticalLineColor | 长按高亮竖线颜色 |
highlightVerticalLinePathEffect | 长按高亮竖线虚线 |
riseColor | 涨色值 |
downColor | 跌色值 |
backgroundColor | 背景色 |
gridHorizontalLineCount | 背景网格横线数 |
gridVerticalLineCount | 背景网格竖线数 |
gridLineColor | 背景网格线条色 |
gridLineStrokeWidth | 背景网格线条宽度 |
chartMainDisplayAreaPaddingLeft | 主数据显示区域的左内间距,主数据一般指数据线 |
chartMainDisplayAreaPaddingRight | 主数据显示区域的右内间距,主数据一般指数据线 |
onGestureListener | 手势监听 |
KChartConfig
字段 | 描述 |
---|---|
height | 高度 |
marginTop | 顶部外间距 |
marginBottom | 底部外间距 |
onHighlightListener | 长按回调 |
chartMainDisplayAreaPaddingTop | 主数据显示区域的顶部内间距 |
chartMainDisplayAreaPaddingBottom | 主数据显示区域的底部内间距 |
kChartType | K线图类型 |
highlightLabelLeft | 长按时高亮线左侧标签配置 |
highlightLabelTop | 长按时高亮线顶部标签配置 |
highlightLabelRight | 长按时高亮线右侧标签配置 |
highlightLabelBottom | 长按时高亮线底部标签配置 |
lineChartColor | 线形图的线条颜色 |
lineChartStrokeWidth | 线形图的线条宽度 |
mountainChartColor | 山峰图线条颜色 |
mountainChartStrokeWidth | 山峰图的线条宽度 |
mountainChartLinearGradientColors | 山峰图的封闭渐变色 |
candleChartLineStrokeWidth | 蜡烛图的中间线宽度 |
hollowChartLineStrokeWidth | 空心蜡烛图线条宽度 |
barChartLineStrokeWidth | 美国线图(竹节图)线条宽度 |
costPrice | 成本线价格 |
costPriceLineColor | 成本线颜色 |
costPriceLineWidth | 成本线宽度 |
indexStrokeWidth | 指标线条宽度 |
barSpaceRatio | 柱子之间的空间占比柱子宽度 |
index | 需要展示的指标类型,目前只有MA、EMA、BOLL |
indexColors | 指标线的颜色 |
leftLabelConfig | 左侧标签配置 |
rightLabelConfig | 右侧标签配置 |
showAvgLine | 是否显示分时均线。若需要显示,K线数据需带有分时均线价格。 |
avgLineColor | 分时均线颜色 |
avgLineStrokeWidth | 分时均线宽度 |
yValueMin | y轴范围最小值,在增加或修改K线数据之前指定才有效 |
yValueMax | y轴范围最大值,在增加或修改K线数据之前指定才有效 |
TimeBarConfig
字段 | 描述 |
---|---|
height | 高度 |
marginTop | 顶部外间距 |
marginBottom | 底部外间距 |
onHighlightListener | 长按回调 |
chartMainDisplayAreaPaddingTop | 主数据显示区域的顶部内间距 |
chartMainDisplayAreaPaddingBottom | 主数据显示区域的底部内间距 |
backGroundColor | 背景色 |
labelTextSize | 标签文本大小 |
labelTextColor | 标签文本色 |
highlightLabelTextSize | 长按显示的标签文本大小 |
highlightLabelTextColor | 长按显示的标签文本色 |
highlightLabelBgColor | 长按显示的标签背景色 |
type | 时间条样式 |
VolumeChartConfig
字段 | 描述 |
---|---|
height | 高度 |
marginTop | 顶部外间距 |
marginBottom | 底部外间距 |
onHighlightListener | 长按回调 |
chartMainDisplayAreaPaddingTop | 主数据显示区域的顶部内间距 |
chartMainDisplayAreaPaddingBottom | 主数据显示区域的底部内间距 |
highlightLabelLeft | 长按时高亮线左侧标签配置 |
highlightLabelRight | 长按时高亮线右侧标签配置 |
barSpaceRatio | 柱子之间的空间占比柱子宽度 |
volumeChartType | 柱子样式 |
hollowChartLineStrokeWidth | 柱子空心时的线条宽度 |
MacdChartConfig
字段 | 描述 |
---|---|
height | 高度 |
marginTop | 顶部外间距 |
marginBottom | 底部外间距 |
onHighlightListener | 长按回调 |
chartMainDisplayAreaPaddingTop | 主数据显示区域的顶部内间距 |
chartMainDisplayAreaPaddingBottom | 主数据显示区域的底部内间距 |
highlightLabelLeft | 长按时高亮线左侧标签配置 |
highlightLabelRight | 长按时高亮线右侧标签配置 |
difLineColor | dif线颜色 |
difLineStrokeWidth | dif线宽度 |
deaLineColor | dea线颜色 |
deaLineStrokeWidth | dea线宽度 |
macdTextColor | macd文字颜色 |
barSpaceRatio | 柱子之间的空间占比柱子宽度 |
index | 需要展示的指标配置 |
KdjChartConfig
字段 | 描述 |
---|---|
height | 高度 |
marginTop | 顶部外间距 |
marginBottom | 底部外间距 |
onHighlightListener | 长按回调 |
chartMainDisplayAreaPaddingTop | 主数据显示区域的顶部内间距 |
chartMainDisplayAreaPaddingBottom | 主数据显示区域的底部内间距 |
highlightLabelLeft | 长按时高亮线左侧标签配置 |
highlightLabelRight | 长按时高亮线右侧标签配置 |
kLineColor | k线颜色 |
kLineStrokeWidth | k线宽度 |
dLineColor | d线颜色 |
dLineStrokeWidth | d线宽度 |
jLineColor | j线颜色 |
jLineStrokeWidth | j线宽度 |
index | 需要展示的指标配置 |
RsiChartConfig
字段 | 描述 |
---|---|
height | 高度 |
marginTop | 顶部外间距 |
marginBottom | 底部外间距 |
onHighlightListener | 长按回调 |
chartMainDisplayAreaPaddingTop | 主数据显示区域的顶部内间距 |
chartMainDisplayAreaPaddingBottom | 主数据显示区域的底部内间距 |
highlightLabelLeft | 长按时高亮线左侧标签配置 |
highlightLabelRight | 长按时高亮线右侧标签配置 |
indexColors | 指标线的颜色 |
lineStrokeWidth | 指标线宽度 |
dashLineColor | 虚线颜色 |
index | 需要展示的指标配置 |
indexStarterBgColor | 指标头文字背景色 |
indexStarterBgPaddingHorizontal | 指标头文字背景水平内间距 |
indexStarterRightIcon | 指标头文字右侧图标 |
indexStarterClickListener | 指标头文字点击事件 |
HighlightLabelConfig
字段 | 描述 |
---|---|
bgColor | 背景色 |
bgCorner | 背景圆角 |
padding | 内间距 |
textSize | 文字大小 |
textColor | 文字颜色 |
textFormat | 显示的内容格式化 |
LabelConfig
字段 | 描述 |
---|---|
count | 标签数 |
formatter | 显示内容格式化 |
textSize | 文字大小 |
textColor | 文字颜色 |
horizontalMargin | 水平外间距 |
marginTop | 顶部外间距 |
marginBottom | 底部外间距 |
BaseChildChartConfig
BaseChildChart
AbsChildChartFactory
class CustomChartConfig(
height: Int = DEFAULT_CHILD_CHART_HEIGHT,
marginTop: Int = DEFAULT_CHILD_CHART_MARGIN_TOP,
marginBottom: Int = DEFAULT_CHILD_CHART_MARGIN_BOTTOM,
onHighlightListener: OnHighlightListener? = null,
chartMainDisplayAreaPaddingTop: Float = DEFAULT_CHART_MAIN_DISPLAY_AREA_PADDING_TOP,
chartMainDisplayAreaPaddingBottom: Float = DEFAULT_CHART_MAIN_DISPLAY_AREA_PADDING_BOTTOM,
var bigLabel: String? = null
) : BaseChildChartConfig(
height,
marginTop,
marginBottom,
onHighlightListener,
chartMainDisplayAreaPaddingTop,
chartMainDisplayAreaPaddingBottom
)
class CustomChart(
stockChart: IStockChart,
chartConfig: CustomChartConfig
) : BaseChildChart<CustomChartConfig>(stockChart, chartConfig) {
override fun onKEntitiesChanged() {
// 如果需要的话,在这里处理K先数据变化后要做的事
}
override fun getYValueRange(startIndex: Int, endIndex: Int, result: FloatArray) {
// 提供指定下标范围内[startIndex ~ endIndex],y轴逻辑坐标的范围值
}
override fun preDrawBackground(canvas: Canvas) {
// ... 绘制细节
}
override fun drawBackground(canvas: Canvas) {
// ... 绘制细节
}
override fun preDrawData(canvas: Canvas) {
// ... 绘制细节
}
override fun drawData(canvas: Canvas) {
// ... 绘制细节
}
override fun preDrawHighlight(canvas: Canvas) {
// ... 绘制细节
}
override fun drawHighlight(canvas: Canvas) {
// ... 绘制细节
}
override fun drawAddition(canvas: Canvas) {
// ... 绘制细节
}
}
class CustomChartFactory(stockChart: IStockChart, childChartConfig: CustomChartConfig) :
AbsChildChartFactory<CustomChartConfig>(stockChart, childChartConfig) {
override fun createChart() = CustomChart(stockChart, childChartConfig)
}
// 自定义子图的使用
// 自定义子图的配置与工厂
val customChartConfig = CustomChartConfig()
val customChartFactory = CustomChartFactory(stock_chart, customChartConfig)
// 添加子图
stockChartConfig.addChildCharts(customChartFactory)
// 更新UI
stock_chart.notifyChanged()
使用手机浏览器扫码下载安装,如需下载到本地请点击下载
感觉好用就点个Star呗~感激万分。 任何问题欢迎在Issues区提问,或者有更多需求请联系我。 我的邮箱:[email protected]
您的支持是我最大的动力!
Copyright 2021 WangYiqian
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.