Skip to content

Comments

Refactor: Introduce customizable tooltips for LineChart#136

Open
abdo-essam wants to merge 2 commits intoTheChance101:developfrom
abdo-essam:refactor/Add-Customizable-Tooltips-to-LineChart
Open

Refactor: Introduce customizable tooltips for LineChart#136
abdo-essam wants to merge 2 commits intoTheChance101:developfrom
abdo-essam:refactor/Add-Customizable-Tooltips-to-LineChart

Conversation

@abdo-essam
Copy link

@abdo-essam abdo-essam commented Jan 19, 2026

Add Customizable Tooltips to LineChart

Summary

This PR adds comprehensive tooltip customization support to the LineChart component while maintaining full backward compatibility. Users can now localize tooltips, display X/Y coordinates, customize appearance, and control the marker circle style.

Problem Solved

Previously, the LineChart tooltip had several limitations:

  • ❌ Hardcoded "Value:" label that couldn't be localized
  • ❌ Could only show Y value, not X coordinate or both
  • ❌ Fixed size (50dp x 30dp) and appearance
  • ❌ No control over the marker circle (always stroke)
  • ❌ No way to customize text formatting

Solution

Added a flexible TooltipConfig system that allows:

  • ✅ Localize tooltip labels (e.g., "القيمة" instead of "Value")
  • ✅ Show Y value, X value, XY values, or custom content
  • ✅ Customize colors, size, corner radius, and text style
  • ✅ Control marker circle: stroke, solid, or none
  • ✅ Provide custom value formatters
  • ✅ Disable tooltip entirely if needed

Changes Made

New Files

  • TooltipConfig.kt - Configuration model with sealed classes for type-safe options
  • LineChartTooltipExamples.kt - 9 comprehensive usage examples

Modified Files

  • LineParameters.kt - Added tooltipConfig parameter with default
  • CircleWithRectAndText.kt - Complete refactor with better naming:
    • circleWithRectAndText()drawTooltipWithMarker()
    • chartRectangleWithText()drawTooltipBox()
    • Added drawMarkerCircle() and generateTooltipText()
  • DefaultLines.kt - Updated to use new API with xAxisData
  • QuadraticLines.kt - Updated to use new API with xAxisData
  • ChartContent.kt - Passes xAxisData to drawing functions

API Design

TooltipConfig

data class TooltipConfig(
    val enabled: Boolean = true,
    val backgroundColor: Color = Color.White,
    val borderColor: Color? = null,
    val textColor: Color = Color.Black,
    val textSize: TextUnit = 8.sp,
    val cornerRadius: Dp = 16.dp,
    val size: TooltipSize = TooltipSize.Auto,
    val content: TooltipContent = TooltipContent.YValue(),
    val padding: Dp = 8.dp,
    val markerStyle: MarkerStyle = MarkerStyle.Stroke(),
)

Content Types

  • TooltipContent.YValue(label, formatter) - Y value only (default)
  • TooltipContent.XValue(label, xAxisData) - X value only
  • TooltipContent.XYValue(xLabel, yLabel, xAxisData, yFormatter) - Both X and Y
  • TooltipContent.Custom(formatter) - Complete custom control

Marker Styles

  • MarkerStyle.Stroke(strokeWidth) - Outline circle (default)
  • MarkerStyle.Solid - Filled circle
  • MarkerStyle.None - No marker

Usage Examples

Localized Tooltip

tooltipConfig = TooltipConfig(
    content = TooltipContent.YValue(label = "القيمة") // Arabic
)

Show X and Y Coordinates

tooltipConfig = TooltipConfig(
    content = TooltipContent.XYValue(
        xLabel = "Month",
        yLabel = "Sales"
    )
)

Custom Styling with Solid Marker

tooltipConfig = TooltipConfig(
    markerStyle = MarkerStyle.Solid,
    backgroundColor = Color(0xFF1E1E1E),
    textColor = Color.White,
    textSize = 12.sp,
    cornerRadius = 8.dp
)

Backward Compatibility

100% backward compatible - All existing code works without changes. The default TooltipConfig() produces the same behavior as before.

Testing

  • ✅ Build successful: ./gradlew :chart:build
  • ✅ No compilation errors
  • ✅ 9 comprehensive examples created and verified
  • ✅ Backward compatibility verified

Code Quality

Improved Naming

  • circleWithRectAndText()drawTooltipWithMarker() (clearer intent)
  • chartRectangleWithText()drawTooltipBox() (more descriptive)
  • Added drawMarkerCircle() (separated concern)
  • Added generateTooltipText() (content generation)

Better Organization

  • Separated marker rendering from tooltip rendering
  • Extracted text generation into dedicated function
  • Added comprehensive KDoc documentation
  • Used sealed classes for type-safe configuration

Documentation

  • ✅ 9 usage examples in LineChartTooltipExamples.kt
  • ✅ Comprehensive inline documentation
  • ✅ Implementation walkthrough

Breaking Changes

None - This is a purely additive change with full backward compatibility.

Checklist

  • Code compiles successfully
  • Backward compatibility maintained
  • Examples created
  • Documentation added
  • Clean code with improved naming
  • Type-safe API design
  • No breaking changes

This commit introduces a major enhancement to the LineChart, allowing for extensive customization of tooltips that appear on data point clicks. The previous hardcoded tooltip has been replaced with a flexible `TooltipConfig` model.

Key changes include:
- **Added `TooltipConfig`:** A new data class to control tooltip appearance and behavior, including colors, size, text style, and content.
- **Customizable Content:** Tooltips can now display Y-values, X-values, both, or fully custom-formatted text. This supports localization and varied data representation.
- **Marker Styles:** The circular marker at the selected point can now be a stroke (outline), solid (filled), or disabled entirely.
- **Refactored Drawing Logic:** Replaced `CircleWithRectAndText` with a new `TooltipWithMarker` component that handles the new customizable drawing.
- **Enabled Multi-Line Tooltips:** Removed the restriction that disabled tooltips on multi-line charts.
- **Added Documentation:** Included a new `TOOLTIP_CUSTOMIZATION.md` file and comprehensive usage examples in `LineChartTooltipExamples.kt`.
@AndrewAboalhana
Copy link
Collaborator

Great work, I will review it with other pull requests to take them into the new version release, so it may take some time. Don't hesitate to reach out to me to ask about anything, and don't feel bad if it take sometime

This commit introduces advanced customization options for line chart tooltips:

-   **Individual Corner Radii**: Allows setting unique corner radii for each corner of the tooltip box via the new `TooltipCornerRadii` data class.
-   **Tooltip Positioning**: Adds a `TooltipPosition` sealed class (Center, Left, Right) to control the horizontal alignment of the tooltip relative to the data point.

The implementation handles drawing tooltips with either uniform or individual corner radii and updates the tooltip's position based on the new alignment setting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants