使用 Android Highchart Wrapper 更新 xaxis

问题描述 投票:0回答:1

我正在使用这个包装器https://github.com/highcharts/highcharts-android来创建图表。 它工作正常,但是当我使用事件单击时,图表不会重绘。

这是我的简化代码:

object ChartOptionProvider {
fun provideOptionsForChartType(
    options: MutableMap<String?, String>,
    incomeSeries: ArrayList<*>?,
    outcomeSeries: ArrayList<*>?,
    categories: ArrayList<String>?
): HIOptions? {
    val step: Double = 1.0
    val yaxis = HIYAxis()
    val xaxis = HIXAxis()

    if (options["chartType"] == "column") {
        val hiOptions = HIOptions()
        val chart = HIChart()
        chart.borderRadius = 6
        chart.type = options["chartType"]
        hiOptions.chart = chart
        val exporting = HIExporting()
        exporting.enabled = false
        hiOptions.exporting = exporting
        val navigation = HINavigation()
        navigation.buttonOptions = HIButtonOptions()
        navigation.buttonOptions.enabled = false

        val plotOptions = HIPlotOptions()
        plotOptions.column = HIColumn()
        plotOptions.column.groupPadding = 0.25
        plotOptions.column.grouping = false


        plotOptions.series = HISeries()
        plotOptions.series.allowPointSelect = false
        hiOptions.plotOptions = plotOptions
        val credits = HICredits()
        credits.enabled = false
        hiOptions.credits = credits
        val title = HITitle()
        title.text = ""
        hiOptions.title = title

        xaxis.tickColor = HIColor.initWithRGBA(176, 207, 209, 1.0)
        xaxis.lineColor = HIColor.initWithRGBA(176, 207, 209, 1.0)
        xaxis.gridLineWidth = 1
        xaxis.gridLineColor = HIColor.initWithRGBA(176, 207, 209, 1.0)

        xaxis.labels = HILabels()
        val xLabelsStyle = HILabels()
        xLabelsStyle.style = HICSSObject()
        xLabelsStyle.style.color = "rgba(93, 111, 112, 1)"
        xLabelsStyle.style.fontSize = "12px"
        xLabelsStyle.style.fontFamily = "SF UI Text"
        xLabelsStyle.style.fontWeight = "400"
        xLabelsStyle.style.padding = "8px"
        xLabelsStyle.style.borderRadius = 10
        xLabelsStyle.style.backgroundColor = "rgba(176, 207, 209, 0.8)"
        xLabelsStyle.style.border = "2px solid rgba(0, 0, 0, 0.5)"
        xaxis.labels = xLabelsStyle
        xaxis.labels.step = step
        xaxis.categories = categories
        hiOptions.xAxis = object : ArrayList<HIXAxis?>() {
            init {
                add(xaxis)
            }
        }

        plotOptions.column.point = customClickEvent {clickedPosition ->
            clickedPosition?.let {
                **//here I need to update graph**
                val plotBand = HIPlotBands()
                plotBand.apply {
                    color = HIColor.initWithHexValue("#FCFFC5")
                    from = clickedPosition
                    to = (clickedPosition.toInt() - 1)
                }
                xaxis.plotBands = arrayListOf(plotBand)
            }

        }

        yaxis.lineWidth = 0
        yaxis.gridLineWidth = 1
        yaxis.tickAmount = 6
        yaxis.gridLineDashStyle = "longdash"
        yaxis.lineColor = HIColor.initWithRGBA(176, 207, 209, 1.0)
        yaxis.labels = HILabels()
        val yLabelsStyle = HICSSObject()
        yLabelsStyle.color = "rgba(0, 81, 87, 1)"
        yLabelsStyle.fontSize = "10px"
        yLabelsStyle.fontFamily = "SF UI Text"
        yLabelsStyle.fontWeight = "700"
        yaxis.labels.style = yLabelsStyle
        yaxis.labels.x = -5
        yaxis.title = HITitle()
        yaxis.title.text = ""
        hiOptions.yAxis = object : ArrayList<HIYAxis?>() {
            init {
                add(yaxis)
            }
        }

        val incomeColumn = HIColumn()
        incomeColumn.showInLegend = false

        incomeColumn.data = incomeSeries

        incomeColumn.color = HIColor.initWithRGBA(0, 174, 101, 1.0)
        incomeColumn.pointPlacement = -0.25

        val outcomeColumn = HIColumn()

        hiOptions.tooltip = customTooltip()
        outcomeColumn.showInLegend = false
        outcomeColumn.name = "Uscite\n"
        outcomeColumn.color = HIColor.initWithRGBA(0, 81, 87, 1.0)
        outcomeColumn.data = outcomeSeries
        outcomeColumn.pointPlacement = 0.25
        hiOptions.series = arrayListOf(incomeColumn, outcomeColumn)
        return hiOptions
    }
    return null
}
 

private fun customClickEvent(function: (Number?) -> Unit): HIPoint {
    val hiConsumer = HIConsumer<HIChartContext> { chartContext ->

        val prop = chartContext.getProperty("x")
        function.invoke(prop as? Number)
        // Implementazione del callback
    }

    val properties = arrayOf("x", "y")
    val clickEvent = HIFunction(hiConsumer, properties)

    val columnsPoint = HIPoint()
    val columnsEvents = HIEvents()
    columnsPoint.events = columnsEvents
    columnsPoint.events.click = clickEvent
    return columnsPoint
 }
 }

然后,我在我的经理中这样称呼这个提供者:

fun setChart(
    chartTitle: String,
    chartView: HIChartView,
    c: Context,
    chartType: String,
    enableExport: Boolean,
    incomeSeries: ArrayList<Double>,
    outcomeSeries: ArrayList<Double>,
    categories: ArrayList<String>
): HIChartView {
    title = chartTitle // we pass scale of the chart (day, week, etc...)
    val series = StringBuilder()
    for (d in incomeSeries) {
        series.append(java.lang.String.valueOf(d.toInt()))
        series.append(",")
    }
    series.deleteCharAt(series.length - 1)
    val options: MutableMap<String?, String> = HashMap()
    

    chartView.options = ChartOptionProvider.provideOptionsForChartType(options, incomeSeries as ArrayList<*>?,outcomeSeries as ArrayList<*>?, categories)
    return chartView
}

然后,我在片段中调用 setChart。一切正常,除了单击应该向所选列添加背景颜色

android kotlin highcharts
1个回答
0
投票

我解决了使用:

val handler = Handler(Looper.getMainLooper())
                handler.post {
                    clickedPosition?.let {
                        val plotBand = customGradientWhenClick(plotOptions, clickedPosition)
                        xaxis.plotBands = arrayListOf(plotBand)
                    }
                }
© www.soinside.com 2019 - 2024. All rights reserved.