Skip to content

Commit 73a3c78

Browse files
committed
Optional hatch fill
1 parent 2b4288e commit 73a3c78

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

app/src/main/kotlin/info/appdev/chartexample/TimeIntervalChartActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TimeIntervalChartActivity : DemoBase(), OnChartValueSelectedListener {
2929
// Add sample project tasks
3030
ganttData.addTask(GanttTask("Design", 0f, 50f, Color.rgb(255, 107, 107))) // Red: 0-50
3131
ganttData.addTask(GanttTask("Dev", 40f, 100f, Color.rgb(66, 165, 245))) // Blue: 40-140
32-
ganttData.addTask(GanttTask("Testing", 120f, 40f, Color.rgb(76, 175, 80))) // Green: 120-160
32+
ganttData.addTask(GanttTask("Testing", 120f, 40f, Color.rgb(76, 175, 80), hatched = true)) // Green: 120-160
3333
ganttData.addTask(GanttTask("Launch", 150f, 20f, Color.rgb(255, 193, 7))) // Yellow: 150-170
3434

3535
// Set data and render

chartLib/src/main/kotlin/info/appdev/charting/charts/GanttChart.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import android.util.AttributeSet
88
import android.view.View
99
import info.appdev.charting.data.GanttChartData
1010
import java.util.Locale
11+
import androidx.core.graphics.withClip
1112

1213
class GanttChart : View {
1314
private var data: GanttChartData? = null
1415
private var taskPaint: Paint? = null
16+
private var hatchPaint: Paint? = null
1517
private var gridPaint: Paint? = null
1618
private var textPaint: Paint? = null
1719

@@ -40,6 +42,12 @@ class GanttChart : View {
4042
taskPaint = Paint().apply {
4143
isAntiAlias = true
4244
}
45+
hatchPaint = Paint().apply {
46+
color = 0x7DFFFFFF // more transparent (0x7D ≈ 66 % opacity vs previous 0x55 ≈ 33 %)
47+
strokeWidth = 20f // thicker lines (was 2f)
48+
isAntiAlias = true
49+
style = Paint.Style.STROKE
50+
}
4351
gridPaint = Paint().apply {
4452
color = -0x333334
4553
strokeWidth = 1f
@@ -179,6 +187,18 @@ class GanttChart : View {
179187
val rect = RectF(startX, taskY, endX, taskY + taskHeight)
180188
taskPaint!!.color = task.color
181189
canvas.drawRect(rect, taskPaint!!)
190+
191+
if (task.hatched) {
192+
// Hatch lines (45° diagonal, bottom-left → top-right)
193+
canvas.withClip(rect) {
194+
val hatchSpacing = 10f
195+
var hx = startX - taskHeight
196+
while (hx < endX + taskHeight) {
197+
drawLine(hx, taskY + taskHeight, hx + taskHeight, taskY, hatchPaint!!)
198+
hx += hatchSpacing
199+
}
200+
}
201+
}
182202
canvas.drawRect(rect, borderPaint)
183203
}
184204
}

chartLib/src/main/kotlin/info/appdev/charting/data/GanttTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package info.appdev.charting.data
99
* @param duration How long the task lasts
1010
* @param color Display color (Android color int)
1111
*/
12-
class GanttTask(val name: String?, val startTime: Float, val duration: Float, val color: Int) {
12+
class GanttTask(val name: String?, val startTime: Float, val duration: Float, val color: Int, val hatched: Boolean = false) {
1313
val endTime: Float
1414
get() = startTime + duration
1515
}

0 commit comments

Comments
 (0)