Giter Site home page Giter Site logo

ellsworthrw / diamondcharts Goto Github PK

View Code? Open in Web Editor NEW
16.0 3.0 2.0 236 KB

Diamond Charts

License: Apache License 2.0

Kotlin 98.48% Java 1.52%
android chart charting-library charts graph jetpack-compose jetpack-compose-charts kotlin area-chart bar-chart

diamondcharts's Introduction

Diamond Charts

ver Kotlin Kotlin License

Diamond Charts is a charting library for Android Jetpack Compose.

Setup

The library is available from the Maven Central repository with the current version of ver

build.gradle(.kts):

    dependencies {
        api("com.diamondedge:charts-android:$charts_version")
    }

Sample Usage

Builtin Composable

You can use a builtin composable like this:

@Preview
@Composable
private fun LineGraphMultiPreview() {
    LineGraph(
        RandomData(DefaultData.SIMPLE_SERIES, 2),
        modifier = Modifier
            .fillMaxWidth()
            .height(200.dp),
        margins = Margins.wideRight
    )
}

line

More builtin samples can be found here

Custom Composable

It is expected in most cases that you will create your own Composable that will create the chart. The following is an example of creating a line graph using a function to compute the data points.

@Composable
fun FunctionGraph(
    minX: Double,
    maxX: Double,
    modifier: Modifier = Modifier,
    margins: Margins = Margins.default,
    fn: (Double) -> Double,
) {
    val density = LocalDensity.current
    Canvas(modifier = modifier) {
        // create an instance of the Charts container
        val charts = Charts(size.width, size.height, margins, Charts.LEGEND_NONE)
        
        // add the desired charts and graphs to the container
        // if multiple are added they will be drawn on top of each other
        charts.add(XYGraph(createData(fn, minX, maxX)))

        // make any desired changes to the vertical and horizontal axis
        charts.vertAxis?.apply {
            majorTickLabelPosition = TickLabelPosition.BelowTick
        }
        charts.horizontalAxis?.apply {
            majorTickLabelPosition = TickLabelPosition.RightOfTick
        }

        // do the drawing of chart
        // you can add custom drawing on top of the charts to provide even more customizations
        drawIntoCanvas { canvas ->
            charts.draw(ComposeGC(canvas, density))
        }
    }
}

private fun createData(fn: (Double) -> Double, minX: Double, maxX: Double): ChartData {
    val data = DefaultXYData("fn")
    data.dataCount = 100
    val xInc = (maxX - minX) / data.dataCount
    for (i in 0 until data.dataCount) {
        val x = minX + (i + 1) * xInc
        data.setValue(i, x, fn(x))
    }
    return data
}

@Preview
@Composable
private fun FunctionGraphPreview() {
    FunctionGraph(
        -1.5,
        3.5,
        modifier = Modifier
            .fillMaxWidth()
            .height(300.dp),
        margins = Margins.wideRight
    ) { x ->
        (x + 1) * (x - 2) * (x - 2)
    }
}

function

diamondcharts's People

Contributors

ellsworthrw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

diamondcharts's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.