### Add Koala Plot Core Dependency (Kotlin) Source: https://koalaplot.github.io/docs/getting-started This snippet shows how to include the Koala Plot core library as a dependency in a Kotlin Multiplatform project using Gradle. ```kotlin implementation(io.github.koalaplot:koalaplot-core:) ``` -------------------------------- ### Generate a Line Plot with Koala Plot (Kotlin) Source: https://koalaplot.github.io/docs/getting-started This example demonstrates how to create a simple line plot displaying the function y=x^2 for x values from 1 to 10 using Koala Plot in a Kotlin application. It utilizes `XYGraph` and `LinePlot` components. ```kotlin fun main() = singleWindowApplication { val data = buildList { for (i in 1..10) { add(DefaultPoint(i.toFloat(), i * i.toFloat())) } } XYGraph( rememberFloatLinearAxisModel(data.autoScaleXRange()), rememberFloatLinearAxisModel(data.autoScaleYRange()) ) { LinePlot( data, lineStyle = LineStyle(SolidColor(Color.Blue)) ) } } ``` -------------------------------- ### Create a Basic Stair-Step Plot in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/line_plots Demonstrates the creation of a basic stair-step plot using the `StairstepPlot` composable. This plot type draws series as points and connects them with horizontal and vertical lines, resembling steps. The example plots a quadratic function. ```kotlin val data = buildList { for (i in 1..10) { add(DefaultPoint(i.toFloat(), i * i.toFloat())) } } XYGraph( rememberFloatLinearAxisModel(data.autoScaleXRange()), rememberFloatLinearAxisModel(data.autoScaleYRange()) ) { StairstepPlot( data, lineStyle = LineStyle(SolidColor(Color.Blue)) ) } ``` -------------------------------- ### Combine Lines and Symbols on XYGraph in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/line_plots Demonstrates how to plot both lines and symbols on the same XYGraph by using multiple LinePlot composables. The z-order of plots is determined by their declaration order. This example shows a scatter plot with a line overlay. ```kotlin val data = buildList { val random = Random(10) for (i in 1..50) { val x = random.nextDouble(1.0, 10.0).toFloat() val y = (x + (random.nextDouble() - 0.5) * 2.0).toFloat() add(DefaultPoint(x, y)) } } val line = buildList { add(DefaultPoint(-5f, -5f)) add(DefaultPoint(15f, 15f)) } XYGraph( rememberFloatLinearAxisModel(0f..12f), rememberFloatLinearAxisModel(0f..12f) ) { LinePlot(line, lineStyle = LineStyle(SolidColor(Color.Blue))) LinePlot( data, symbol = { Symbol(fillBrush = SolidColor(Color.Blue), outlineBrush = SolidColor(Color.Black)) } ) } ``` -------------------------------- ### Create a Basic Pie Chart with Labels (Kotlin) Source: https://koalaplot.github.io/docs/pie Demonstrates the creation of a basic Pie Chart using Koala Plot. It takes a List as data and displays the value of each slice as a label. The slices are arranged clockwise starting from the first element. ```kotlin val random = Random(10) val data: List = buildList { for (i in 1..10) { add(random.nextFloat() * 10f) } } PieChart( data, label = { i -> Text(data[i].toString()) } ) ``` -------------------------------- ### Create Column and Flow Legends Source: https://koalaplot.github.io/docs/legends Provides examples for ColumnLegend, which arranges items in rows and columns, and FlowLegend, which wraps items based on available width. ```kotlin ColumnLegend( modifier = Modifier.padding(16.dp).border(1.dp, Color.Black).padding(16.dp), itemCount = itemCount, symbol = { Symbol(shape = RectangleShape, fillBrush = SolidColor(palette[it])) }, label = { Text("Item $it") }, value = { Text("${it * 10}%") } ) ``` ```kotlin FlowLegend( modifier = Modifier.padding(16.dp).border(1.dp, Color.Black).padding(16.dp), itemCount = itemCount, symbol = { Symbol(shape = RectangleShape, fillBrush = SolidColor(palette[it])) }, label = { Text("Item $it") }, ) ``` -------------------------------- ### Plot Lines and Symbols with a Single LinePlot in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/line_plots Illustrates how to plot both a line and its corresponding symbols using a single LinePlot composable. This is achieved by providing both `lineStyle` and `symbol` parameters to the LinePlot. This example modifies a previous scatter plot to include a line. ```kotlin LinePlot( data, lineStyle = LineStyle(SolidColor(Color.Blue)), symbol = { Symbol(fillBrush = SolidColor(Color.Blue), outlineBrush = SolidColor(Color.Black)) } ) ``` -------------------------------- ### Add Annotations to XYGraph in Kotlin Source: https://koalaplot.github.io/docs/xygraphs Demonstrates adding HorizontalLineAnnotation, VerticalLineAnnotation, and XYAnnotation to an XYGraph. The XYAnnotation requires a coordinate and an anchor point to position a Composable. This example uses Kotlin and the Koala Plot library. ```kotlin import androidx.compose.foundation.text.Text import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.graphics.drawscope.LineStyle import io.github.koalaplot.core.charts.XYGraph import io.github.koalaplot.core.data.FloatLinearAxisModel import io.github.koalaplot.core.geometry.Point import io.github.koalaplot.core.xygraph.AnchorPoint import io.github.koalaplot.core.xygraph.HorizontalLineAnnotation import io.github.koalaplot.core.xygraph.VerticalLineAnnotation import io.github.koalaplot.core.xygraph.XYAnnotation XYGraph( xAxisModel = FloatLinearAxisModel(0f..10f), yAxisModel = FloatLinearAxisModel(0f..20f), ) { HorizontalLineAnnotation(10.4f, LineStyle(SolidColor(Color.Blue))) VerticalLineAnnotation(3.2f, LineStyle(SolidColor(Color.Red))) XYAnnotation(Point(3.2f, 10.4f), AnchorPoint.BottomLeft) { Text("Text annotation") } } ``` -------------------------------- ### Customize Axis Titles and Labels with Composables Source: https://koalaplot.github.io/docs/xygraphs Demonstrates using Composable functions for axis labels and titles, including the use of AnnotatedString for styled text and custom icons for categories. The example shows how to align and rotate titles within the allocated axis space. ```Kotlin fun main() = singleWindowApplication { val yAxisTitle = buildAnnotatedString { pushStyle(ParagraphStyle(textAlign = TextAlign.Center)) pushStyle(SpanStyle(fontWeight = FontWeight.Bold, fontSize = MaterialTheme.typography.titleLarge.fontSize)) append("Votes\n") pop() pushStyle(SpanStyle(color = Color.Gray, fontSize = MaterialTheme.typography.labelSmall.fontSize)) append("(Millions)") } XYGraph( xAxisModel = CategoryAxisModel(IconCategories.entries), yAxisModel = rememberFloatLinearAxisModel(0f..4f, minorTickCount = 0), xAxisLabels = { it.icon() }, xAxisTitle = {}, yAxisLabels = { Text(it.toString()) }, yAxisTitle = { Box(modifier = Modifier.fillMaxHeight(), contentAlignment = Alignment.BottomCenter) { Text( yAxisTitle, modifier = Modifier.rotateVertically(VerticalRotation.COUNTER_CLOCKWISE) .padding(bottom = KoalaPlotTheme.sizes.gap) ) } }, modifier = Modifier.padding(16.dp) ) { } } enum class IconCategories(private val icon: ImageVector, private val color: Color) { MONEY(Icons.Default.Money, Color(0, 150, 0)), HOME(Icons.Default.Home, Color.Black), DELETE(Icons.Default.Delete, Color.Red), SETTINGS(Icons.Default.Settings, Color.Gray), CHECK(Icons.Default.CheckCircle, Color.Blue) ; @Composable fun icon() { Icon(icon, contentDescription = null, tint = color) } } ``` -------------------------------- ### Stair-Step Plot with Varied Level Line Styles in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/line_plots Shows an advanced usage of `StairstepPlot` where different line styles can be applied to each level based on the y-axis value. This example varies the color and stroke width of the line segments according to the y-axis value, creating a gradient effect. ```kotlin val data = buildList { for (i in 1..10) { add(DefaultPoint(i.toFloat(), i * i.toFloat())) } } XYGraph( rememberFloatLinearAxisModel(data.autoScaleXRange()), rememberFloatLinearAxisModel(data.autoScaleYRange()) ) { StairstepPlot( data, lineStyle = LineStyle(SolidColor(Color.Transparent)), levelLineStyle = { y -> LineStyle(SolidColor(Color(red = y / 100f, green = 0f, blue = 0f)), strokeWidth = 4.dp) } ) } ``` -------------------------------- ### Create Polar Line and Area Plot (Kotlin) Source: https://koalaplot.github.io/docs/polar Shows how to plot lines and areas on a polar graph using `PolarPlotSeries`. This example defines a cardioid function and plots it with specified line and area styles. Requires KoalaPlot library. ```kotlin fun cardioid(theta: Double): Double = 2F * (1F + cos(theta)) val data1: List> = buildList { for (theta in 0..360) { add(PolarPoint(cardioid(theta * PI / 180F).toFloat(), theta.toFloat().toDegrees())) } } PolarGraph( rememberFloatRadialAxisModel(List(6) { it.toFloat() }), rememberAngularValueAxisModel(), angularAxisLabelText = { "${it.toDegrees().value}°" } ) { PolarPlotSeries( data1, lineStyle = LineStyle(SolidColor(Color.Blue), strokeWidth = 2.dp), areaStyle = AreaStyle(SolidColor(Color.Blue.copy(alpha = 0.2f))) ) } ``` -------------------------------- ### Create Vertical Bar Plot with Custom Start/End Points in KoalaPlot Source: https://koalaplot.github.io/docs/xygraphs/bar_plots Illustrates creating a vertical bar plot where each bar can have a specified starting and ending y-coordinate, enabling visualizations like waterfall charts. This example uses a list of data entries with explicit start and end values and custom colors for each bar. It requires KoalaPlot and Compose UI dependencies. ```kotlin @OptIn(ExperimentalKoalaPlotApi::class) fun main() = singleWindowApplication { val categories = listOf("Initial Cash", "Q1", "Q2", "Q3", "Q4", "Final Cash") val colors = listOf( Color.DarkGray, Color(0xFF00498F), Color(0xFFED7D31), Color(0xFF00498F), Color(0xFF00498F), Color.DarkGray, ) val data = listOf( verticalBarPlotEntry(categories[0], 0f, 100f), verticalBarPlotEntry(categories[1], 100f, 120f), verticalBarPlotEntry(categories[2], 120f, 90f), verticalBarPlotEntry(categories[3], 90f, 110f), verticalBarPlotEntry(categories[4], 110f, 130f), verticalBarPlotEntry(categories[5], 0f, 130f), ) XYGraph( xAxisModel = remember { CategoryAxisModel(categories) }, yAxisModel = rememberFloatLinearAxisModel(0f..150f, minorTickCount = 0), ) { VerticalBarPlot( data, bar = { index, _, _ -> DefaultBar( brush = SolidColor(colors[index]), modifier = Modifier.fillMaxWidth(), ) } ) } } ``` -------------------------------- ### Create Vertical Bar Plot using KoalaPlot Builder DSL Source: https://koalaplot.github.io/docs/xygraphs/bar_plots Demonstrates an alternative, more concise syntax for creating vertical bar plots using KoalaPlot's builder DSL. This method allows defining each bar's category, start, end, and appearance directly within the `VerticalBarPlot` block. It's a syntactic sugar over the previous method and requires KoalaPlot dependencies. ```kotlin XYGraph( xAxisModel = remember { CategoryAxisModel(categories) }, yAxisModel = rememberFloatLinearAxisModel(0f..150f, minorTickCount = 0), ) { VerticalBarPlot { item("Initial Cash", 0f, 100f, solidBar(Color.DarkGray)) item("Q1", 100f, 120f, solidBar(Color(0xFF00498F))) item("Q2", 120f, 90f, solidBar(Color(0xFFED7D31))) item("Q3", 90f, 110f, solidBar(Color(0xFF00498F))) item("Q4", 110f, 130f, solidBar(Color(0xFF00498F))) item("Final Cash", 0f, 130f, solidBar(Color.DarkGray)) } } ``` -------------------------------- ### Scatter Plot with Custom Symbols in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/line_plots Creates a scatter plot where each data point is represented by a symbol instead of a connecting line. This example uses the Symbol composable to define custom fill and outline brushes for the default RectangleShape. ```kotlin val data = buildList { val random = Random(10) for (i in 1..50) { val x = random.nextDouble(1.0, 10.0).toFloat() val y = (x + (random.nextDouble() - 0.5) * 2.0).toFloat() add(DefaultPoint(x, y)) } } XYGraph( rememberFloatLinearAxisModel(0f..12f), rememberFloatLinearAxisModel(0f..12f) ) { LinePlot( data, symbol = { Symbol(fillBrush = SolidColor(Color.Blue), outlineBrush = SolidColor(Color.Black)) } ) } ``` -------------------------------- ### Create Single Vertical Bar Plot with KoalaPlot Source: https://koalaplot.github.io/docs/xygraphs/bar_plots Demonstrates plotting a single series of vertical bars using KoalaPlot's VerticalBarPlot. This example plots the population of New York City boroughs, with bars extending from the y-axis origin (0). It requires KoalaPlot dependencies and uses CategoryAxisModel for the x-axis and LinearAxisModel for the y-axis. ```kotlin @OptIn(ExperimentalKoalaPlotApi::class) fun main() = singleWindowApplication { val boroughs = listOf("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island") val population = listOf(1.446788f, 2.648452f, 1.638281f, 2.330295f, 0.487155f) XYGraph( xAxisModel = remember { CategoryAxisModel(boroughs) }, yAxisModel = rememberFloatLinearAxisModel(0f..3f, minorTickCount = 0), yAxisTitle = "Population (Millions)" ) { VerticalBarPlot( xData = boroughs, yData = population, bar = { _, _, _ -> DefaultBar( brush = SolidColor(Color.Blue), modifier = Modifier.fillMaxWidth(), ) } ) } } ``` -------------------------------- ### Create a Donut Chart with Hole Content (Kotlin) Source: https://koalaplot.github.io/docs/pie Illustrates how to create a Donut Chart with a customizable hole. The 'holeSize' parameter controls the size of the hole, and 'holeContent' allows embedding a Composable, such as a summary of the total values, within the center. ```kotlin val random = Random(10) val data: List = buildList { for (i in 1..10) { add(random.nextFloat() * 10f) } } PieChart( data, label = { i -> Text(data[i].toString()) }, holeSize = 0.75F, holeContent = { Box( modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center, ) { Column { Text("Total", style = MaterialTheme.typography.titleLarge) Text( ((data.sum() * 100F).roundToInt() / 100F).toString(), style = MaterialTheme.typography.displaySmall ) } } } ) ``` -------------------------------- ### Customize KoalaPlot Theme with KoalaPlotTheme Source: https://koalaplot.github.io/docs/theming Demonstrates how to wrap XYGraph components with KoalaPlotTheme to modify axis colors and remove minor gridlines. This approach allows for global visual styling of plot elements within a Compose hierarchy. ```kotlin KoalaPlotTheme(axis = KoalaPlotTheme.axis.copy(color = Color.Black, minorGridlineStyle = null)) { XYGraph( rememberFloatLinearAxisModel(data.autoScaleXRange()), rememberFloatLinearAxisModel(data.autoScaleYRange()), modifier = Modifier.weight(0.5f) ) { LinePlot( data, lineStyle = LineStyle(SolidColor(Color.Blue)) ) } } ``` -------------------------------- ### Layout Charts with ChartLayout Source: https://koalaplot.github.io/docs/legends Demonstrates how to use ChartLayout to position a title, a legend, and a plot area. The legend can be placed in various locations relative to the main content. ```kotlin val itemCount = 5 val palette = generateHueColorPalette(itemCount) ChartLayout( modifier = Modifier.border(0.dp, Color.Green).padding(16.dp), title = { Text("Chart Title", style = MaterialTheme.typography.titleLarge) }, legend = { ColumnLegend( modifier = Modifier.padding(16.dp).border(1.dp, Color.Black).padding(16.dp), itemCount = itemCount, symbol = { Symbol(shape = RectangleShape, fillBrush = SolidColor(palette[it])) }, label = { Text("Item $it") } ) }, legendLocation = LegendLocation.LEFT ) { Box(modifier = Modifier.fillMaxSize().border(1.dp, Color.Blue), contentAlignment = Alignment.Center) { Text("Graph Area") } } ``` -------------------------------- ### Configure LogAxisModel for logarithmic scaling in Kotlin Source: https://koalaplot.github.io/docs/xygraphs Illustrates the use of LogAxisModel to transform coordinates logarithmically, with ranges specified as powers of 10. ```kotlin XYGraph( xAxisModel = rememberFloatLinearAxisModel(data.autoScaleXRange()), yAxisModel = LogAxisModel(-1..2) ) { LinePlot( data, lineStyle = LineStyle(SolidColor(Color.Blue)) ) } ``` -------------------------------- ### Create a single bullet graph with Kotlin Compose Source: https://koalaplot.github.io/docs/bullet Demonstrates how to render a single bullet graph using the BulletGraphs composable. It configures the axis model, labels, comparative measures, and range thresholds. ```kotlin BulletGraphs(modifier = Modifier.padding(16.dp).height(100.dp)) { 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) } } ``` -------------------------------- ### Basic Line Plot in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/line_plots Generates a basic line plot using a list of (x, y) data points. It requires an XYGraph composable with FloatLinearAxisModels for scaling. The LinePlot composable renders the data with a specified line style. ```kotlin val data = buildList { for (i in 1..10) { add(DefaultPoint(i.toFloat(), i * i.toFloat())) } } XYGraph( rememberFloatLinearAxisModel(data.autoScaleXRange()), rememberFloatLinearAxisModel(data.autoScaleYRange()) ) { LinePlot( data, lineStyle = LineStyle(SolidColor(Color.Blue)) ) } ``` -------------------------------- ### Implement Stacked Area Plot in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/area_plots This snippet demonstrates how to generate random data, prepare it using StackedAreaPlotDataAdapter, and render a stacked area plot within an XYGraph. It requires the Koala Plot library and uses standard Compose for Desktop/JVM patterns. ```kotlin fun makeRandomLine(): List = buildList { var last = 0f for (i in 1..10) { last += random.nextFloat() add(last) } } val randomLines = List(4) { makeRandomLine() } val stackData = StackedAreaPlotDataAdapter(randomLines[0].indices.toList().map { (it + 1).toFloat() }, randomLines) val styles = List(4) { StackedAreaStyle( LineStyle(SolidColor(Color.Black), strokeWidth = 2.dp), AreaStyle(SolidColor(Color.Blue.copy(alpha = 0.20f * (it + 1)))) ) } XYGraph( rememberFloatLinearAxisModel(0f..12f), rememberFloatLinearAxisModel(0f..25f), xAxisTitle = "", yAxisTitle = "", xAxisLabels = { it.toString(1) }, yAxisLabels = { it.toString(1) } ) { StackedAreaPlot( stackData, styles, AreaBaseline.ConstantLine(0f) ) } ``` -------------------------------- ### Adding a Second Line to a Plot in Kotlin Source: https://koalaplot.github.io/docs/xygraphs/line_plots Demonstrates how to add a second line to an existing plot within the same XYGraph. This involves defining a new dataset and calling LinePlot again with potentially different styling, such as color, thickness, and dash effects. ```kotlin XYGraph( rememberFloatLinearAxisModel(dataSquared.autoScaleXRange()), rememberFloatLinearAxisModel(dataSquared.autoScaleYRange()) ) { LinePlot( dataSquared, lineStyle = LineStyle(SolidColor(Color.Blue)) ) LinePlot( dataExp, lineStyle = LineStyle( SolidColor(Color(0, 200, 0)), 2.dp, dashPathEffect(floatArrayOf(20f, 10f)) ) ) } ``` -------------------------------- ### Create Basic Polar Graph with Value Axis Source: https://koalaplot.github.io/docs/polar Initializes a PolarGraph using a radial axis with Float values and an angular axis with default value-based ticks. It demonstrates the basic structure required to render a polar plot. ```kotlin PolarGraph( rememberFloatRadialAxisModel(List(6) { it.toFloat() }), rememberAngularValueAxisModel(), angularAxisLabelText = { "${it.toDegrees().value}\u00B0" } ) { } ``` -------------------------------- ### Implement CategoryAxisModel for discrete data in Kotlin Source: https://koalaplot.github.io/docs/xygraphs Shows the usage of CategoryAxisModel to map discrete string values to an axis, utilizing the object's toString() method for labels. ```kotlin val burroughs = listOf("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island") XYGraph( xAxisModel = CategoryAxisModel(burroughs), yAxisModel = rememberFloatLinearAxisModel(0f..4f, minorTickCount = 0), yAxisTitle = "Population (Millions)" ) { } ``` -------------------------------- ### Configure Individual XYGraph Gridlines Source: https://koalaplot.github.io/docs/xygraphs Shows how to apply specific LineStyle configurations to horizontal and vertical gridlines directly within the XYGraph component, including disabling specific lines by setting them to null. ```Kotlin XYGraph( xAxisModel = FloatLinearAxisModel(0f..10f), yAxisModel = FloatLinearAxisModel(0f..20f), horizontalMajorGridLineStyle = LineStyle(SolidColor(Color.Blue), strokeWidth = 3.dp), horizontalMinorGridLineStyle = LineStyle( SolidColor(Color.Blue), pathEffect = PathEffect.dashPathEffect(floatArrayOf(5f, 5f)), ), verticalMajorGridLineStyle = LineStyle(SolidColor(Color.DarkGray)), verticalMinorGridLineStyle = null, ) { } ``` -------------------------------- ### Configure Axis Ticks and Gridline Styles Source: https://koalaplot.github.io/docs/xygraphs Demonstrates how to customize axis colors, tick sizes, and gridline path effects using KoalaPlotTheme and AxisStyle configurations. ```Kotlin KoalaPlotTheme( axis = KoalaPlotTheme.axis.copy( minorGridlineStyle = KoalaPlotTheme.axis.minorGridlineStyle!!.copy( pathEffect = PathEffect.dashPathEffect(floatArrayOf(3f, 3f)) ) ) ) { XYGraph( xAxisModel = FloatLinearAxisModel(0f..10f), yAxisModel = FloatLinearAxisModel(0f..20f), xAxisTitle = "X Axis Title", yAxisTitle = "Y Axis Title", xAxisStyle = rememberAxisStyle( Color.Blue, majorTickSize = 5.dp, minorTickSize = 5.dp, ), yAxisStyle = rememberAxisStyle( Color.Blue, majorTickSize = 5.dp, minorTickSize = 5.dp, ) ) { } } ``` -------------------------------- ### Create multiple aligned bullet graphs with Kotlin Compose Source: https://koalaplot.github.io/docs/bullet Shows how to group multiple bullet graphs together with consistent alignment using a reusable label component. This is useful for dashboard-style data visualization. ```kotlin @Composable fun TwoLineLabel(line1: String, line2: String) { Column( horizontalAlignment = Alignment.End, modifier = Modifier.padding(end = KoalaPlotTheme.sizes.gap) ) { Text(line1, textAlign = TextAlign.End) Text(line2, textAlign = TextAlign.End, style = MaterialTheme.typography.labelSmall) } } BulletGraphs(modifier = Modifier.padding(16.dp)) { bullet(FloatLinearAxisModel(0f..300f)) { label { TwoLineLabel("Revenue", "(US $ in thousands)") } axis { labels { Text("${it.toInt()}", style = MaterialTheme.typography.labelSmall) } } comparativeMeasure(250f) featuredMeasureBar(275f) ranges(0f, 150f, 225f, 300f) } bullet(FloatLinearAxisModel(0f..30f)) { label { TwoLineLabel("Profit", "%") } axis { labels { Text("${it.toInt()}%", style = MaterialTheme.typography.labelSmall) } } comparativeMeasure(26f) featuredMeasureBar(22.5f) ranges(0f, 20f, 25f, 30f) } bullet(FloatLinearAxisModel(0f..600f)) { label { TwoLineLabel("Avg Order Size", "U.S. $") } axis { labels { Text("${it.toInt()}", style = MaterialTheme.typography.labelSmall) } } comparativeMeasure(550f) featuredMeasureBar(325f) ranges(0f, 350f, 500f, 600f) } } ``` -------------------------------- ### Customize XYGraph Gridlines via Theme Source: https://koalaplot.github.io/docs/xygraphs Demonstrates how to globally modify gridline styles, such as applying a dash path effect to minor gridlines, by wrapping the XYGraph in a KoalaPlotTheme. ```Kotlin KoalaPlotTheme( axis = KoalaPlotTheme.axis.copy( minorGridlineStyle = KoalaPlotTheme.axis.minorGridlineStyle!!.copy( pathEffect = PathEffect.dashPathEffect(floatArrayOf(3f, 3f)) ) ) ) { XYGraph( xAxisModel = FloatLinearAxisModel(0f..10f), yAxisModel = FloatLinearAxisModel(0f..20f), xAxisTitle = "X Axis Title", yAxisTitle = "Y Axis Title", ) { } } ``` -------------------------------- ### Configure LinearAxisModel range and ticks in Kotlin Source: https://koalaplot.github.io/docs/xygraphs Demonstrates how to define a custom range for a LinearAxisModel and control minor tick density using the rememberFloatLinearAxisModel function within an XYGraph. ```kotlin rememberFloatLinearAxisModel(0f..12f, minorTickCount = 0), rememberFloatLinearAxisModel(-10f..110f, minorTickCount = 0), ) { LinePlot( data, lineStyle = LineStyle(SolidColor(Color.Blue)) ) } ``` -------------------------------- ### Create Polar Scatter Plot with Symbols (Kotlin) Source: https://koalaplot.github.io/docs/polar Demonstrates creating polar scatter plots using `PolarPlotSeries` with custom symbols. It takes lists of `PolarPoint` objects and applies symbol styling. Dependencies include KoalaPlot charting library. ```kotlin val data1: List> = buildList { for (i in 1..20) { add(PolarPoint(random.nextFloat() * 5f, (15.2f + random.nextFloat() * 15f).toDegrees())) } } val data2: List> = buildList { for (i in 1..20) { add(PolarPoint(random.nextFloat() * 5f, (95f + random.nextFloat() * 35f).toDegrees())) } } PolarGraph( rememberFloatRadialAxisModel(List(6) { it.toFloat() }), rememberAngularValueAxisModel(), angularAxisLabelText = { "${it.toDegrees().value}°" } ) { PolarPlotSeries(data1, symbols = { Symbol(shape = CircleShape, fillBrush = SolidColor(Color.Blue)) }) PolarPlotSeries(data2, symbols = { Symbol(shape = CircleShape, fillBrush = SolidColor(Color.Red)) }) } ``` -------------------------------- ### Create Polar Graph with Category Angular Axis Source: https://koalaplot.github.io/docs/polar Demonstrates using a CategoryAngularAxisModel to place categorical string data evenly around the circumference of the polar plot. ```kotlin PolarGraph( rememberFloatRadialAxisModel(List(6) { it.toFloat() }), rememberCategoryAngularAxisModel(listOf("A", "B", "C", "D", "E")), ) { } ``` -------------------------------- ### Create Stacked Vertical Bar Plot with Kotlin Source: https://koalaplot.github.io/docs/xygraphs/bar_plots This snippet demonstrates how to create a stacked vertical bar plot using the KoalaPlot library in Kotlin. It visualizes population data for NYC boroughs by year, utilizing a builder API for series and item specification. The plot automatically accumulates values for stacking, and is suitable for float-valued vertical axes. ```kotlin val boroughs = listOf("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island") val years = listOf(1980, 1990, 2000, 2010, 2020) val colors = listOf( Color.hsv(0f, 1f, 0.7f), Color.hsv(72f, 1f, 0.7f), Color.hsv(144f, 1f, 0.7f), Color.hsv(216f, 1f, 0.7f), Color.hsv(288f, 1f, 0.7f) ) // 1 color per borough // Population data is in order of borough, then year val population = listOf( listOf(1168972, 1203789, 1332650, 1385108, 1446788), // Bronx listOf(2230936, 2300664, 2465326, 2552911, 2648452), // Brooklyn listOf(1428285, 1487536, 1537195, 1585873, 1638281), // Manhattan listOf(1891325, 1951598, 2229379, 2250002, 2330295), // Queens listOf(352121, 378977, 443728, 468730, 487155), // Staten Island ) XYGraph( xAxisModel = remember { CategoryAxisModel(years) }, yAxisModel = rememberFloatLinearAxisModel(0f..10f, minorTickCount = 0), yAxisTitle = "Population (Millions)" ) { StackedVerticalBarPlot { boroughs.forEachIndexed { index, _ -> series(solidBar(colors[index])) { years.forEachIndexed { yearIndex, year -> item(year, population[index][yearIndex].toFloat() / 1E6f) } } } } } ``` -------------------------------- ### Customize Polar Graph Appearance Source: https://koalaplot.github.io/docs/polar Demonstrates advanced styling of the PolarGraph, including custom colors for radial and angular grid lines and a background fill color using AreaStyle. ```kotlin PolarGraph( rememberFloatRadialAxisModel(List(6) { it.toFloat() }), rememberAngularValueAxisModel(), angularAxisLabelText = { "${it.toDegrees().value}\u00B0" }, polarGraphProperties = PolarGraphDefaults.PolarGraphPropertyDefaults().copy( radialAxisGridLineStyle = LineStyle(SolidColor(Color.DarkGray)), angularAxisGridLineStyle = LineStyle(SolidColor(Color.Blue)), background = AreaStyle(SolidColor(Color.LightGray)) ) ) { } ``` -------------------------------- ### Plot individual area plots with constant baseline using Kotlin Source: https://koalaplot.github.io/docs/xygraphs/area_plots Demonstrates the use of AreaPlot to render lines with a shaded area relative to a constant horizontal baseline (y=0). This approach is suitable for visualizing distributions or single-series trends. ```kotlin AreaPlot( data = distribution1, lineStyle = LineStyle(brush = SolidColor(Color(0xFF00498F)), strokeWidth = 2.dp), areaStyle = AreaStyle( brush = SolidColor(Color(0xFF00498F)), alpha = 0.5f, ), areaBaseline = AreaBaseline.ConstantLine(0f) ) AreaPlot( data = distribution2, lineStyle = LineStyle(brush = SolidColor(Color(0xFF37A78F)), strokeWidth = 2.dp), areaStyle = AreaStyle( brush = SolidColor(Color(0xFF37A78F)), alpha = 0.5f, ), areaBaseline = AreaBaseline.ConstantLine(0f) ) ``` -------------------------------- ### Plot area between two arbitrary lines using Kotlin Source: https://koalaplot.github.io/docs/xygraphs/area_plots Shows how to shade the region between two distinct data series by setting the areaBaseline to an arbitrary line. This is useful for highlighting the difference or range between two related datasets. ```kotlin val data1 = buildList { for (i in 1..10) { add(DefaultPoint(i.toFloat(), 10f * (1.04).pow(i).toFloat())) } } val data2 = buildList { for (i in 1..10) { add(DefaultPoint(i.toFloat(), 10f * (1.06).pow(i).toFloat())) } } XYGraph( rememberFloatLinearAxisModel(0f..12f), rememberFloatLinearAxisModel(0f..20f), xAxisTitle = "", yAxisTitle = "", xAxisLabels = { it.toString(1) }, yAxisLabels = { it.toString(1) } ) { LinePlot( data = data1, lineStyle = LineStyle(brush = SolidColor(Color(0xFF00498F)), strokeWidth = 2.dp), ) AreaPlot( data = data2, lineStyle = LineStyle(brush = SolidColor(Color(0xFF37A78F)), strokeWidth = 2.dp), areaStyle = AreaStyle( brush = SolidColor(Color(0xFF37A78F)), alpha = 0.5f, ), areaBaseline = AreaBaseline.ArbitraryLine(data1) ) } ``` -------------------------------- ### Rotate Axis Labels Source: https://koalaplot.github.io/docs/xygraphs Shows how to rotate x-axis labels to accommodate long text by using the labelRotation property within the AxisStyle. ```Kotlin XYGraph( xAxisModel = CategoryAxisModel(burroughs), yAxisModel = rememberFloatLinearAxisModel(0f..4f, minorTickCount = 0), yAxisTitle = "Population (Millions)", xAxisStyle = rememberAxisStyle(labelRotation = 45) ) { } ``` -------------------------------- ### Create Grouped Vertical Bar Plot with Koala Plot Source: https://koalaplot.github.io/docs/xygraphs/bar_plots Demonstrates how to define categorical data and render a grouped vertical bar plot. It utilizes the GroupedVerticalBarPlot builder API to map population data across multiple series (years) and categories (boroughs). ```kotlin val boroughs = listOf("Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island") val years = listOf(1980, 1990, 2000, 2010, 2020) val colors = listOf( Color.hsv(0f, 1f, 0.7f), Color.hsv(72f, 1f, 0.7f), Color.hsv(144f, 1f, 0.7f), Color.hsv(216f, 1f, 0.7f), Color.hsv(288f, 1f, 0.7f) ) val population = listOf( listOf(1168972, 1203789, 1332650, 1385108, 1446788), listOf(2230936, 2300664, 2465326, 2552911, 2648452), listOf(1428285, 1487536, 1537195, 1585873, 1638281), listOf(1891325, 1951598, 2229379, 2250002, 2330295), listOf(352121, 378977, 443728, 468730, 487155) ) XYGraph( xAxisModel = remember { CategoryAxisModel(boroughs) }, yAxisModel = rememberFloatLinearAxisModel(0f..3f, minorTickCount = 0), yAxisTitle = "Population (Millions)" ) { GroupedVerticalBarPlot { years.indices.forEach { series(solidBar(colors[it])) { boroughs.forEachIndexed { index, borough -> item(borough, 0f, population[index][it].toFloat() / 1E6f) } } } } } ``` -------------------------------- ### Style Polar Graph Grid Lines Source: https://koalaplot.github.io/docs/polar Configures the PolarGraph to use straight radial grid lines instead of concentric circles, creating a spider-web effect. This is achieved by modifying the PolarGraphPropertyDefaults. ```kotlin PolarGraph( rememberFloatRadialAxisModel(List(6) { it.toFloat() }), rememberAngularValueAxisModel(), angularAxisLabelText = { "${it.toDegrees().value}\u00B0" }, polarGraphProperties = PolarGraphDefaults.PolarGraphPropertyDefaults().copy(RadialGridType.LINES) ) { } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.