Search Results for

    Show / Hide Table of Contents

    Pass RolloverModifier data points to a ViewModel

    Overview

    This documentation provides a complete working example showing how to:

    1. Intercept RolloverModifier events
    2. Extract Series info during hover interactions
    3. Pass the Series info to another ViewModel

    Implementation

    1. ViewModel

    • Kotlin
    • Java
    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
        }
    }
    
    public class ChartViewModel extends ViewModel {
        private final MutableLiveData<String> rolloverData = new MutableLiveData<>();
    
        public LiveData<String> getRolloverData() {
            return rolloverData;
        }
    
        // Call this method when you detect rollover changes
        public void onRolloverChanged(String seriesName, String xValue, String yValue) {
            String data = seriesName + ": X=" + xValue + ", Y=" + yValue;
            rolloverData.setValue(data);
        }
    }
    

    2. Activity

    • Kotlin
    • Java
    // 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())
            }
        }
    }
    
    
    // Custom series info provider class to get the rollover modifier data points
    private static class CustomSeriesInfoProvider extends DefaultXySeriesInfoProvider {
    
        private final ChartViewModel chartViewModel;
    
        public CustomSeriesInfoProvider(ChartViewModel chartViewModel) {
            this.chartViewModel = chartViewModel;
        }
    
        @Override
        protected ISeriesTooltip getSeriesTooltipInternal(Context context, XySeriesInfo<?> seriesInfo, Class<?> modifierType) {
            return new CustomXySeriesTooltip(context, seriesInfo, "RolloverModifier",chartViewModel);
        }
    
        private static class CustomXySeriesTooltip extends XySeriesTooltip {
            private final String modifierName;
            private final ChartViewModel chartViewModel;
    
            public CustomXySeriesTooltip(Context context, XySeriesInfo<?> seriesInfo, String modifierName, ChartViewModel chartViewModel) {
                super(context, seriesInfo);
                this.modifierName = modifierName;
                this.chartViewModel = chartViewModel;
            }
    
            @Override
            protected void internalUpdate(XySeriesInfo seriesInfo) {
                final SpannableStringBuilder sb = new SpannableStringBuilder();
                sb.append("X: ").append(seriesInfo.getFormattedXValue()).append(StringUtil.NEW_LINE);
                sb.append("Y: ").append(seriesInfo.getFormattedYValue()).append(StringUtil.NEW_LINE);
                chartViewModel.onRolloverChanged("Series 1", seriesInfo.getFormattedXValue().toString(), seriesInfo.getFormattedYValue().toString());
    
                if (seriesInfo.seriesName != null) {
                    final int start = sb.length();
    
                    sb.append(seriesInfo.seriesName);
                    sb.setSpan(new ForegroundColorSpan(ColorUtil.White), start, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    sb.append(StringUtil.NEW_LINE);
                }
                sb.append(modifierName);
                setText(sb);
    
                setSeriesColor(0xff6495ed);
            }
        }
    }
    

    Use the custom series info provider class in your code as shown below

    • Kotlin
    • Java
    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)
    
    
    // Use the custom series info provider class in your code as shown below
    FastLineRenderableSeries lineSeries = new FastLineRenderableSeries();
    lineSeries.setDataSeries(dataSeries);
    lineSeries.setStrokeStyle(new SolidPenStyle(0xffFF0010, true, 3, null));
    
    // Set custom series info provider
    lineSeries.setSeriesInfoProvider(new CustomSeriesInfoProvider(chartViewModel));
    
    Collections.addAll(surface.getRenderableSeries(), lineSeries);
    Collections.addAll(surface.getChartModifiers(), rolloverModifier);
    

    How It Works: Key Concepts Explained

    1. SeriesInfo Extraction Process

    The solution works by intercepting the tooltip creation and update process:

    1. Custom SeriesInfoProvider: Extends DefaultXySeriesInfoProvider to provide custom tooltip
    2. Custom Tooltip: Extends XySeriesTooltip and overrides internalUpdate(XySeriesInfo seriesInfo)
    3. Data Extraction: In internalUpdate(), extract data from seriesInfo parameter
    4. 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 tooltip
    • seriesInfo.getFormattedYValue() - Formatted Y value as shown in tooltip
    • seriesInfo.getXValue() - Raw X value
    • seriesInfo.getYValue() - Raw Y value
    • seriesInfo.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

    • ChartViewModel uses MutableLiveData<String> to hold rollover information
    • onRolloverChanged() method receives formatted SeriesInfo data
    • Exposes data through LiveData for UI components to observe

    Custom SeriesInfoProvider

    • Extends DefaultXySeriesInfoProvider
    • Overrides getSeriesTooltipInternal() to return custom tooltip
    • Passes ChartViewModel reference to custom tooltip

    Custom Tooltip

    • Extends XySeriesTooltip
    • Overrides internalUpdate() method where SeriesInfo is available
    • Calls chartViewModel.onRolloverChanged() to pass data to ViewModel
    Back to top © 2011-2025 SciChart. All rights reserved. | sitemap.xml