Skip to content

Commit d892655

Browse files
committed
Grantt chart
Inspired by PhilJay/MPAndroidChart#5519
1 parent afd5397 commit d892655

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<activity android:name="info.appdev.chartexample.HalfPieChartActivity" />
6161
<activity android:name="info.appdev.chartexample.SpecificPositionsLineChartActivity" />
6262
<activity android:name="info.appdev.chartexample.TimeLineActivity" />
63+
<activity android:name="info.appdev.chartexample.TimeIntervalBarChartActivity" />
6364
</application>
6465

6566
</manifest>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package info.appdev.chartexample
2+
3+
import info.appdev.charting.data.BarEntry
4+
5+
/**
6+
* Utility class for creating time interval entries for Gantt-style charts.
7+
* Provides helper methods to create BarEntry objects with time interval data.
8+
*/
9+
object GanttUtils {
10+
/**
11+
* Create a time interval entry for a single task.
12+
*
13+
* @param taskIndex Y-axis position (task row)
14+
* @param startTime Start time value
15+
* @param duration Duration value
16+
* @return BarEntry configured for time interval rendering
17+
*/
18+
fun createTimeIntervalEntry(taskIndex: Float, startTime: Float, duration: Float): BarEntry {
19+
return BarEntry(taskIndex, floatArrayOf(startTime, duration))
20+
}
21+
22+
/**
23+
* Create a time interval entry with multiple segments.
24+
* Useful for showing multiple time ranges for a single task.
25+
*
26+
* @param taskIndex Y-axis position (task row)
27+
* @param timeIntervals Array of [start1, duration1, start2, duration2, ...]
28+
* @return BarEntry configured for time interval rendering
29+
*/
30+
fun createMultiSegmentEntry(taskIndex: Float, timeIntervals: FloatArray): BarEntry {
31+
require(timeIntervals.size >= 2) { "timeIntervals must have at least 2 elements" }
32+
return BarEntry(taskIndex, timeIntervals)
33+
}
34+
35+
/**
36+
* Create a time interval entry with custom data.
37+
*
38+
* @param taskIndex Y-axis position (task row)
39+
* @param startTime Start time value
40+
* @param duration Duration value
41+
* @param data Custom data object
42+
* @return BarEntry configured for time interval rendering with data
43+
*/
44+
fun createTimeIntervalEntry(
45+
taskIndex: Float, startTime: Float,
46+
duration: Float, data: Any?
47+
): BarEntry {
48+
return BarEntry(taskIndex, floatArrayOf(startTime, duration), data)
49+
}
50+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package info.appdev.chartexample
2+
3+
import android.graphics.Color
4+
import android.os.Bundle
5+
import android.widget.SeekBar
6+
import android.widget.SeekBar.OnSeekBarChangeListener
7+
import info.appdev.chartexample.databinding.ActivityHorizontalbarchartBinding
8+
import info.appdev.chartexample.notimportant.DemoBase
9+
import info.appdev.charting.components.YAxis
10+
import info.appdev.charting.data.BarData
11+
import info.appdev.charting.data.BarDataSet
12+
import info.appdev.charting.data.BarEntry
13+
import info.appdev.charting.data.Entry
14+
import info.appdev.charting.highlight.Highlight
15+
import info.appdev.charting.listener.OnChartValueSelectedListener
16+
import info.appdev.chartexample.GanttUtils
17+
import timber.log.Timber
18+
19+
/**
20+
* using HorizontalBarChart for Gantt-style time interval visualization.
21+
* Shows how to display tasks as horizontal bars with start time and duration.
22+
*/
23+
class TimeIntervalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener {
24+
25+
private lateinit var binding: ActivityHorizontalbarchartBinding
26+
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
binding = ActivityHorizontalbarchartBinding.inflate(layoutInflater)
30+
setContentView(binding.root)
31+
32+
// Create sample tasks with time intervals
33+
val entries: MutableList<BarEntry> = ArrayList()
34+
35+
entries.add(GanttUtils.createMultiSegmentEntry(-1f, floatArrayOf(-50f, -20f, 20f, 50f)))
36+
// starts at 0, duration 100
37+
entries.add(GanttUtils.createTimeIntervalEntry(0f, 0f, 100f))
38+
// starts at 50, duration 150
39+
entries.add(GanttUtils.createTimeIntervalEntry(1f, 50f, 150f))
40+
// starts at 150, duration 100
41+
entries.add(GanttUtils.createTimeIntervalEntry(2f, 150f, 100f))
42+
43+
Timber.d(entries.joinToString(separator = "\n"))
44+
45+
// Create dataset with time interval data
46+
val dataSet = BarDataSet(entries, "Tasks")
47+
dataSet.setColors(
48+
Color.GREEN,
49+
Color.BLUE,
50+
Color.YELLOW
51+
)
52+
dataSet.barBorderWidth = 1f
53+
dataSet.barBorderColor = Color.BLACK
54+
55+
// Create and set data
56+
val barData = BarData(dataSet)
57+
barData.barWidth = 0.8f
58+
binding.chart1.data = barData
59+
60+
// Configure chart
61+
binding.chart1.setFitBars(true)
62+
binding.chart1.isDrawValueAboveBar = true
63+
binding.chart1.xAxis.setDrawLabels(true)
64+
65+
val yl = binding.chart1.axisLeft
66+
yl.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
67+
68+
// Refresh
69+
binding.chart1.invalidate()
70+
}
71+
72+
override fun saveToGallery() = Unit
73+
74+
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) = Unit
75+
76+
override fun onStartTrackingTouch(p0: SeekBar?) = Unit
77+
78+
override fun onStopTrackingTouch(p0: SeekBar?) = Unit
79+
80+
override fun onValueSelected(entry: Entry, highlight: Highlight) = Unit
81+
82+
override fun onNothingSelected() = Unit
83+
}

app/src/main/kotlin/info/appdev/chartexample/notimportant/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import info.appdev.chartexample.ScrollViewActivity
7575
import info.appdev.chartexample.SpecificPositionsLineChartActivity
7676
import info.appdev.chartexample.StackedBarActivity
7777
import info.appdev.chartexample.StackedBarActivityNegative
78+
import info.appdev.chartexample.TimeIntervalBarChartActivity
7879
import info.appdev.chartexample.TimeLineActivity
7980
import info.appdev.chartexample.compose.HorizontalBarComposeActivity
8081
import info.appdev.chartexample.compose.HorizontalBarFullComposeActivity
@@ -219,6 +220,7 @@ class MainActivity : ComponentActivity() {
219220
add(ContentItem("Demonstrate and fix issues"))
220221
add(ContentItem("Gradient", "Show a gradient edge case", GradientActivity::class.java))
221222
add(ContentItem("Timeline", "Show a time line with Unix timestamp", TimeLineActivity::class.java))
223+
add(ContentItem("Timeinterval", "Grantt chart", TimeIntervalBarChartActivity::class.java))
222224
}
223225
}
224226
}

0 commit comments

Comments
 (0)