Pass RolloverModifier data points to a ViewModel
Overview
This documentation provides a complete working example showing how to:
- Intercept RolloverModifier events
- Extract Series info during hover interactions
- Pass the Series info to another ViewModel
Implementation
1. ViewModel
class ChartViewModel : ViewModel() {
private val _rolloverData = MutableLiveData<String>()
val rolloverData: LiveData<String> = _rolloverData
// Call this method when you detect rollover changes
fun onRolloverChanged(seriesName: String, xValue: String, yValue: String) {
val data = "$seriesName: X=$xValue, Y=$yValue"
_rolloverData.value = data
}
}
2. Activity
// Custom series info provider class to get the rollover modifier data points
private class CustomSeriesInfoProvider(
private val chartViewModel: ChartViewModel
) : DefaultXySeriesInfoProvider() {
override fun getSeriesTooltipInternal(
context: Context,
seriesInfo: XySeriesInfo<*>,
modifierType: Class<*>
): ISeriesTooltip {
return CustomXySeriesTooltip(context, seriesInfo, "RolloverModifier", chartViewModel)
}
private class CustomXySeriesTooltip(
context: Context,
seriesInfo: XySeriesInfo<*>,
private val modifierName: String,
private val chartViewModel: ChartViewModel
) : XySeriesTooltip(context, seriesInfo) {
override fun internalUpdate(seriesInfo: XySeriesInfo<*>) {
val sb = SpannableStringBuilder()
sb.append("X: ").append(seriesInfo.formattedXValue).append(StringUtil.NEW_LINE)
sb.append("Y: ").append(seriesInfo.formattedYValue).append(StringUtil.NEW_LINE)
chartViewModel.onRolloverChanged("Series 1", seriesInfo.formattedXValue.toString(), seriesInfo.formattedYValue.toString())
seriesInfo.seriesName?.let { name ->
val start = sb.length
sb.append(name)
sb.setSpan(
ForegroundColorSpan(ColorUtil.White),
start,
sb.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
sb.append(StringUtil.NEW_LINE)
}
sb.append(modifierName)
text = sb
setSeriesColor(0xff6495ed.toInt())
}
}
}
Use the custom series info provider class in your code as shown below
val lineSeries = FastLineRenderableSeries().apply {
dataSeries = dataS
strokeStyle = SolidPenStyle(0xffFF0010.toInt(), true, 3f, null)
// Set custom series info provider
seriesInfoProvider = CustomSeriesInfoProvider(chartViewModel)
}
surface.renderableSeries.add(lineSeries)
surface.chartModifiers.add(rolloverModifier)
How It Works: Key Concepts Explained
1. SeriesInfo Extraction Process
The solution works by intercepting the tooltip creation and update process:
- Custom SeriesInfoProvider: Extends
DefaultXySeriesInfoProviderto provide custom tooltip - Custom Tooltip: Extends
XySeriesTooltipand overridesinternalUpdate(XySeriesInfo seriesInfo) - Data Extraction: In
internalUpdate(), extract data fromseriesInfoparameter - ViewModel Communication: Call
chartViewModel.onRolloverChanged()with extracted data
2. SeriesInfo Data Available
The XySeriesInfo object provides access to:
seriesInfo.getFormattedXValue()- Formatted X value as shown in tooltipseriesInfo.getFormattedYValue()- Formatted Y value as shown in tooltipseriesInfo.getXValue()- Raw X valueseriesInfo.getYValue()- Raw Y valueseriesInfo.seriesName- Series name if set
3. Event Flow
User hovers over chart
↓
RolloverModifier detects hover
↓
CustomSeriesInfoProvider.getSeriesTooltipInternal() called
↓
CustomXySeriesTooltip.internalUpdate() called
↓
SeriesInfo extracted and passed to ViewModel
↓
ViewModel updates LiveData
Key Implementation Points
ViewModel Integration
ChartViewModelusesMutableLiveData<String>to hold rollover informationonRolloverChanged()method receives formatted SeriesInfo data- Exposes data through
LiveDatafor UI components to observe
Custom SeriesInfoProvider
- Extends
DefaultXySeriesInfoProvider - Overrides
getSeriesTooltipInternal()to return custom tooltip - Passes
ChartViewModelreference to custom tooltip
Custom Tooltip
- Extends
XySeriesTooltip - Overrides
internalUpdate()method where SeriesInfo is available - Calls
chartViewModel.onRolloverChanged()to pass data to ViewModel