Skip to content

Commit

Permalink
Add bar spacing customization
Browse files Browse the repository at this point in the history
  • Loading branch information
mfracx committed Aug 11, 2023
1 parent d31a700 commit 4015264
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion JetChart/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ afterEvaluate {
from components.release
groupId = 'io.jetchart'
artifactId = 'JetChart'
version = '1.3.1'
version = '1.3.2'
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions JetChart/src/main/java/io/jetchart/bar/BarChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import io.jetchart.bar.BarChartUtils.axisAreas
import io.jetchart.bar.BarChartUtils.barDrawableArea
import io.jetchart.bar.BarChartUtils.forEachWithArea
Expand All @@ -35,7 +37,8 @@ fun BarChart(
xAxisDrawer: XAxisDrawer = BarXAxisDrawer(),
yAxisDrawer: YAxisDrawer = BarYAxisWithValueDrawer(),
labelDrawer: BarLabelDrawer = SimpleBarLabelDrawer(),
valueDrawer: BarValueDrawer = SimpleBarValueDrawer()
valueDrawer: BarValueDrawer = SimpleBarValueDrawer(),
barHorizontalMargin: Dp = 3.dp
) {
val transitionAnimation = remember(chars.bars) { Animatable(initialValue = 0f) }
val rectangles = remember { mutableStateMapOf<Bar, Rect>() }
Expand Down Expand Up @@ -63,7 +66,7 @@ fun BarChart(

xAxisDrawer.drawAxisLine(this, canvas, xAxisArea)

chars.forEachWithArea(this, barDrawableArea, transitionAnimation.value, valueDrawer) { barArea, bar ->
chars.forEachWithArea(this, barDrawableArea, transitionAnimation.value, valueDrawer, barHorizontalMargin) { barArea, bar ->
barDrawer.drawBar(this, canvas, barArea, bar)
labelDrawer.draw(this, canvas, bar.label, barArea, xAxisArea)
valueDrawer.draw(this, canvas, bar.value, barArea, xAxisArea)
Expand Down
17 changes: 10 additions & 7 deletions JetChart/src/main/java/io/jetchart/bar/BarChartUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package io.jetchart.bar
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import io.jetchart.bar.renderer.label.BarValueDrawer
import io.jetchart.bar.renderer.xaxis.XAxisDrawer
import io.jetchart.bar.renderer.yaxis.YAxisDrawer
Expand Down Expand Up @@ -39,15 +41,16 @@ internal object BarChartUtils {
}

fun Bars.forEachWithArea(
drawScope: DrawScope,
barDrawableArea: Rect,
progress: Float,
valueDrawer: BarValueDrawer,
block: (barArea: Rect, bar: Bar) -> Unit
) {
drawScope: DrawScope,
barDrawableArea: Rect,
progress: Float,
valueDrawer: BarValueDrawer,
barHorizontalMargin: Dp,
block: (barArea: Rect, bar: Bar) -> Unit
) = with(drawScope){
val totalBars = bars.size
val widthOfBarArea = barDrawableArea.width / totalBars
val offsetOfBar = widthOfBarArea * 0.2f
val offsetOfBar = widthOfBarArea - barHorizontalMargin.toPx()

bars.forEachIndexed { index, bar ->
val left = barDrawableArea.left + (index * widthOfBarArea)
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ allprojects {
2. Add the dependency
```groovy
dependencies {
implementation 'com.github.fracassi-marco:JetChart:1.3.1'
implementation 'com.github.fracassi-marco:JetChart:1.3.2'
}
```

Expand Down Expand Up @@ -74,7 +74,8 @@ fun BarChartComposable(text: MutableState<String>) {
xAxisDrawer = BarXAxisDrawer(),
yAxisDrawer = BarYAxisWithValueDrawer(),
labelDrawer = SimpleBarLabelDrawer(),
valueDrawer = SimpleBarValueDrawer(drawLocation = Inside)
valueDrawer = SimpleBarValueDrawer(drawLocation = Inside),
barHorizontalMargin = 5.dp
)
}
```
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/io/jetchart/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ fun BarChartComposable(text: MutableState<String>) {
xAxisDrawer = BarXAxisDrawer(),
yAxisDrawer = BarYAxisWithValueDrawer(),
labelDrawer = SimpleBarLabelDrawer(),
valueDrawer = SimpleBarValueDrawer(drawLocation = Inside)
valueDrawer = SimpleBarValueDrawer(drawLocation = Inside),
barHorizontalMargin = 5.dp
)
}

Expand Down

0 comments on commit 4015264

Please sign in to comment.