Search code examples
androidpaintandroid-graphview

How to create line graph dynamically like this image


Line Graph

I have 5 dynamic data which is giving +1 or -1 as result, if the result is +1 then graph direction is towards up and if the result is -1 then graph direction is towards downward.


Solution

  • Image in your question is not available to view but i can put a sample for how to draw a line chart in your android app easily.

    First -as mentioned in comment- you can use MPAndroidChart. You can add it to your project like following:

     implementation "com.github.PhilJay:MPAndroidChart:v3.0.3"
    

    Here's a sample default line chart implementation which i use in my personal projects.

    open class DefaultLineChart @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyle: Int = 0)
        : LineChart(context, attrs, defStyle) {
    
        init {
            setupChart()
        }
    
        private fun setupChart() {
    
            // Init Description
            val description = Description().apply {
                isEnabled = false
            }
            setDescription(description)
    
            // Init GridBackground
            setGridBackgroundColor(android.R.color.white)
            setDrawGridBackground(true)
    
            // Init Borders
            setDrawBorders(true)
            setBorderColor(android.R.color.black)
            setBorderWidth(1f)
    
            // Init Other Properties
            setPinchZoom(false)
            isDoubleTapToZoomEnabled = false
            isDragEnabled = true
            setNoDataText(context.getString(R.string.info_text_no_content))
            setScaleEnabled(true)
    
            // Init Legend
            val legend = legend.apply {
                isEnabled = false
            }
    
            // Init xAxis
            val xAxis = xAxis.apply {
                isEnabled = true
                setCenterAxisLabels(false)
                gridColor = android.R.color.white
                setAvoidFirstLastClipping(false)
                setDrawLimitLinesBehindData(true)
                position = XAxis.XAxisPosition.BOTTOM
            }
    
    
            // Init leftAxis
            val leftAxis = axisLeft.apply {
                axisMinimum = 0f
                setDrawAxisLine(false)
                setDrawZeroLine(true)
                setDrawGridLines(true)
                gridColor = android.R.color.black
                axisLineColor = android.R.color.black
            }
    
            val rightAxis = axisRight.apply {
                isEnabled = false
            }
        }
    
        fun setChartTitle(title: String) {
            val description = Description().apply {
                text = title
                isEnabled = true
            }
            setDescription(description)
        }
    }
    

    You can add it to your xml layout like below:

    <path_to_your_class.DefaultLineChart
                        android:layout_width="match_parent"
                        android:layout_height="400dp"
                        android:layout_marginTop="16dp"/>
    

    Finally you can draw your chart like following code:

    fun bindDefaultLineChart(chartView: LineChart,
                                     marketValues: List<MarketVal>?,
                                     label: String?) {
                if (marketValues == null) {
                    return
                }
    
                val yValues = ArrayList<Entry>()
                var maxCount: Long = 0
                for (marketValue in marketValues) {
                    val entry = Entry(marketValue.dateTime.toFloat(),
                            marketValue.transactionNumber.toFloat())
                    yValues.add(entry)
                    if (marketValue.transactionNumber > maxCount) {
                        maxCount = marketValue.transactionNumber.toLong()
                    }
                }
    
                val xAxis = chartView.xAxis
                val xAxisFormatter = ChartValueDateFormatter(Constants.DEFAULT_DATE_FORMAT)
                xAxis.valueFormatter = xAxisFormatter
    
                chartView.axisLeft.apply {
                    axisMaximum = (maxCount + MAX_CHAR_Y_AXIS_ADDITION).toFloat()
                    valueFormatter = NonFloatValueFormatter()
                }
    
                val dataSet = LineDataSet(yValues, label).apply {
                    axisDependency = YAxis.AxisDependency.LEFT
                    color = ContextCompat.getColor(chartView.context, R.color.colorAccent)
                    formLineWidth = 2f
                    setDrawIcons(true)
                    setDrawValues(false)
                }
    
                val lineData = LineData(dataSet)
                chartView.data = lineData
            }
    }
    

    You can find example implementation from here.