jjoe64 / GraphView

Android Graph Library for creating zoomable and scrollable line and bar graphs.
https://github.com/jjoe64/GraphView/wiki
Other
2.75k stars 811 forks source link

Kotlin #646

Closed maffbm closed 5 years ago

maffbm commented 5 years ago

I've just started writing Android apps, so I'm very new, but my latest project requires 3 graphs, but I'm learning Kotlin, not Java, and all the examples and tutorials online for GraphView are using Java, can anyone show me the HelloWorld Graph example in Kotlin so I can go from there?

Thanks

holluc15 commented 5 years ago
   val graph = view.findViewById(R.id.graph) as GraphView
    graph.title = "title"
    graph.titleTextSize = 55f
    graph.titleColor = ContextCompat.getColor(context!!,R.color.colorPrimary)

    val series = LineGraphSeries<DataPoint>(
        arrayOf(
            DataPoint(0.toDouble(), 9.toDouble()),
            DataPoint(10.toDouble(), 5.toDouble()),
            DataPoint(20.toDouble(), 8.toDouble()),
            DataPoint(30.toDouble(), 9.toDouble())
        )
    )
    series.color = ContextCompat.getColor(context!!, R.color.colorPrimary)
    series.isDrawDataPoints = true
    series.thickness = 10
    series.dataPointsRadius = 5f

    var renderer = graph.gridLabelRenderer

    graph.gridLabelRenderer.labelFormatter = object : DefaultLabelFormatter() {
        override fun formatLabel(value: Double, isValueX: Boolean): String {
            if (isValueX) {

                val v = value.toLong()
                val date = Date(v)
                var label: String? = null

                    label = DateFormat.format("MM.dd", date).toString()

                return label
            }else{
                return super.formatLabel(value, isValueX)
            }

        }
    }

    graph.addSeries(series)
maffbm commented 5 years ago

thank you very much!