Koala Plot Documentation

repository·main·Indexed 20 days ago

https://github.com/koalaplot/koalaplot-core

A Compose Multiplatform charting and plotting library for Android, iOS, Desktop, and Web. It provides a declarative Composable API to create interactive charts, including Pie, Donut, Line, Stacked Area, Vertical/Horizontal Bar, Bullet, Radar, Polar, Spider plots, and Heatmaps. The library supports deep customization of colors, fonts, and interactions, and offers both DSL-based and custom data entry methods via interfaces like VerticalBarPlotStackedPointEntry and HorizontalBarPlotStackedPointEntry.

Tokens
2.8K
Snippets
7
Records
11
Agent score
73%

What's inside Koala Plot

  1. Overview of Koala Plot features

    main

    Koala Plot is a Compose Multiplatform-based charting library for Android, Desktop, iOS, and Web. Most plot elements are Composables, allowing for deep customization of colors, fonts, borders, and user interactions.

    Supported chart types include:

    • Pie and Donut charts: With hover effects, customizable labels, and donut center content.
    • Line graphs: Supporting linear/log axes, numeric/category axes, zoom/pan, shaded areas (area charts), and various curve types (linear, stairstep, Bezier).
    • Stacked area graphs: For plotting multiple lines with accumulated shaded areas.
    • Vertical bar graphs: Supporting clustered and stacked bars, as well as negative values.
    • Bullet Graphs: For individual or multiple vertically aligned metrics.
    • Radar/Polar/Spider plots: Supporting lines, areas, and symbols with configurable origin and orientation.
    • Heatmaps
    • Layouts and Legends: Includes chart layout positioning (title, plot, legend) and legend layouts (single column or flow-layout).
  2. Install Koala Plot core

    main

    To use Koala Plot in your Compose Multiplatform project, follow these two steps in your build.gradle.kts file:

    1. Ensure mavenCentral() is included in your repositories block.
    2. Add io.github.koalaplot:koalaplot-core:0.12.0 to your dependencies block.

    Note: As this project is in a developmental state (0.x releases), API and binary compatibility might not be maintained between major versions.

    repositories {
      mavenCentral()
    }
    
    dependencies {
      implementation("io.github.koalaplot:koalaplot-core:0.12.0")
    }
  3. Example: Create a Bullet Graph

    main

    You can build complex charts using a declarative Composable API. Below is an example of how to implement a BulletGraph with a custom label, axis, and comparative measures.

    BulletGraphs {
      bullet(FloatLinearAxisModel(0f..300f)) {
        label {
          Column(
            horizontalAlignment = Alignment.End,
            modifier = Modifier.padding(end = KoalaPlotTheme.sizes.gap)
          ) {
            Text("Revenue 2005 YTD", textAlign = TextAlign.End)
            Text(
              "(US $ in thousands)",
              textAlign = TextAlign.End,
              style = MaterialTheme.typography.labelSmall
            )
          }
        }
        axis { labels { Text("${it.toInt()}") } }
        comparativeMeasure(260f)
        featuredMeasureBar(275f)
        ranges(0f, 200f, 250f, 300f)
      }
    }
  4. Create a stacked vertical bar plot using the DSL

    main

    You can create a stacked vertical bar plot within an XYGraphScope using the StackedVerticalBarPlot DSL. This approach allows you to define multiple series, where each series adds a layer to the stack at specific x coordinates.

    To use this, call StackedVerticalBarPlot and use the series function to define each layer. Within each series, use item(x, y) to add data points. If a specific bar Composable is provided to item, it will override the defaultBar for that specific data point.

    XYGraphScope<String, Float>.MyPlot() {
        StackedVerticalBarPlot(modifier = Modifier) {
            // First layer (bottom)
            series(defaultBar = verticalSolidBar(Color.Red)) {
                item("Jan", 10f)
                item("Feb", 15f)
            }
            // Second layer (middle)
            series(defaultBar = verticalSolidBar(Color.Green)) {
                item("Jan", 5f)
                item("Feb", 20f)
            }
        }
    }
  5. StackedVerticalBarPlotScope and series configuration

    main

    The StackedVerticalBarPlotScope<X, Y> is the receiver scope used when using the DSL version of StackedVerticalBarPlot. It provides the series method to define layers of the stack.

    series()

    Starts a new series of bars.

    • Parameters:
      • defaultBar: A DefaultVerticalBarComposable<X, Y> used for all items in this series if no specific bar is provided to item. Defaults to verticalSolidBar(Color.Blue).
      • content: A lambda with StackedVerticalBarPlotSeriesScope<X, Y> receiver.
  6. StackedVerticalBarPlotSeriesScope and item configuration

    main

    The StackedVerticalBarPlotSeriesScope<X, Y> is the receiver scope within a series block. It allows adding individual data points to that specific layer.

    item()

    Adds an item at a specific x-coordinate.

    • Parameters:
      • x: The x-axis coordinate.
      • y: The height (extent) of the bar to be added to the stack at this coordinate.
      • bar: (Optional) A custom DefaultVerticalBarComposable<X, Y> to use for this specific item. If null, the series' defaultBar is used.
  7. Use StackedHorizontalBarPlot with custom data

    main

    For complete control over the data, use the StackedHorizontalBarPlot overload that accepts a List<E> where E implements HorizontalBarPlotStackedPointEntry<X, Y>. This is useful when your data is already structured in a way that defines the stack segments explicitly.

    @Composable
    fun <X, Y, E : HorizontalBarPlotStackedPointEntry<X, Y>> XYGraphScope<X, Y>.StackedHorizontalBarPlot(
        data: List<E>,
        modifier: Modifier = Modifier,
        bar: DefaultHorizontalBarComposable<X, Y> = horizontalSolidBar(MaterialTheme.colorScheme.primary),
        barWidth: Float = 0.9f,
        startAnimationUseCase: StartAnimationUseCase = StartAnimationUseCase(
            executionType = StartAnimationUseCase.ExecutionType.Default,
            KoalaPlotTheme.animationSpec,
        ),
    )
  8. Render a Stacked Horizontal Bar Plot using a DSL

    main

    You can create a stacked horizontal bar plot within an XYGraphScope using the StackedHorizontalBarPlot Composable. This version uses a DSL approach where you define multiple series, and within each series, you add items at specific y coordinates with a given x width.

    Key components:

    • series: Defines a new layer of the stack. You can provide a defaultBar Composable for all items in this series.
    • item: Adds a bar segment at a specific y coordinate with a width x. You can optionally override the bar Composable for a specific item.

    Note: This version is specifically for XYGraphScope<Float, Y> where the x-axis values are Float.

    StackedHorizontalBarPlot<Float, String>(modifier = Modifier) {
        series(horizontalSolidBar(Color.Red)) {
            item(x = 10f, y = "Category A")
            item(x = 5f, y = "Category B")
        }
        series(horizontalSolidBar(Color.Blue)) {
            item(x = 15f, y = "Category A")
        }
    }
  9. Create a stacked vertical bar plot with custom data entries

    main

    If you have pre-calculated data, you can use the StackedVerticalBarPlot overload that accepts a List<E> where E implements VerticalBarPlotStackedPointEntry<X, Y>.

    Each entry requires:

    • x: The x-axis value.
    • yOrigin: The bottom coordinate of the stack (usually 0).
    • y: A List<Y> representing the cumulative top coordinates of each bar in the stack. For example, if the first bar is 10 units high and the second is 5 units high, y should be [10, 15].
    // Define your data
    val data = listOf(
        DefaultVerticalBarPlotStackedPointEntry(
            x = "Jan",
            yOrigin = 0f,
            y = listOf(10f, 25f) // First bar height 10, second bar ends at 25
        )
    )
    
    // Render in XYGraphScope
    StackedVerticalBarPlot(
        data = data,
        bar = { seriesIndex, barIndex, value -> 
            // Custom bar rendering logic
            verticalSolidBar(Color.Blue)(this, seriesIndex, barIndex, value)
        }
    )
  10. VerticalBarPlotStackedPointEntry interface

    main

    The VerticalBarPlotStackedPointEntry<X, Y> interface defines the data structure required for manual data injection into a StackedVerticalBarPlot.

    PropertyTypeDescription
    xXThe x-axis value of the entry
    yOriginYThe y-axis coordinate of the bottom of the lowest bar in the stack
    yList<Y>The y-axis coordinates of the top of each bar in the stack (cumulative)
  11. Implement HorizontalBarPlotStackedPointEntry

    main

    If you are providing your own data structure instead of using the DSL, you must implement the HorizontalBarPlotStackedPointEntry<X, Y> interface. This interface defines how the stacked segments are positioned along the x-axis for a given y-coordinate.

    Properties:

    • y: The y-axis value (the category/label).
    • xOrigin: The starting x-coordinate (the left edge of the first bar in the stack).
    • x: A List<X> representing the right-edge coordinates of each bar in the stack. The first element is the end of the first bar, the second is the end of the second bar, and so on.
    public interface HorizontalBarPlotStackedPointEntry<X, Y> {
        public val y: Y
        public val xOrigin: X
        public val x: List<X>
    }
    
    // Standard implementation
    public data class DefaultHorizontalBarPlotStackedPointEntry<X, Y>(
        override val y: Y,
        override val xOrigin: X,
        override val x: List<X>,
    ) : HorizontalBarPlotStackedPointEntry<X, Y>