|
| 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 | +} |
0 commit comments