All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.insanusmokrassar.AutoPostRatingsAnalyzePlugin.ChartsBuilders.RatingCountChartBuilder.kt Maven / Gradle / Ivy

package com.github.insanusmokrassar.AutoPostRatingsAnalyzePlugin.ChartsBuilders

import com.github.insanusmokrassar.AutoPostRatingsAnalyzePlugin.database.CountOfRatedPostsTable
import org.joda.time.DateTime
import org.knowm.xchart.*

class RatingCountChartBuilder(
    private val period: Long,
    private val countOfRatedPostsTable: CountOfRatedPostsTable,
    private val chartWidth: Int = 1600,
    private val chartHeight: Int = 900,
    private val chartTitle: String = "Ratings count chart"
) : ChartBuilder {
    override suspend fun build(): ByteArray? {
        return buildPeriod(period)
    }

    override suspend fun buildPeriod(period: Long): ByteArray? {
        return countOfRatedPostsTable.getRates(
            DateTime.now().minus(period)
        ).let {
            pairs ->
            val pairs = pairs.sortedBy { it.first }
            val dates = DoubleArray(pairs.size) { pairs[it].first.millis.toDouble() }.toList()
            XYChartBuilder().apply {
                width(chartWidth)
                height(chartHeight)
                title(chartTitle)
                xAxisTitle("Date-Time")
                yAxisTitle("Ratings")
            }.build().apply {
                styler.defaultSeriesRenderStyle = XYSeries.XYSeriesRenderStyle.Area
                addSeries(
                    "Count",
                    dates.toDoubleArray(),
                    DoubleArray(pairs.size) { pairs[it].second.toDouble() }
                )
            }.run {
                styler.xAxisLabelRotation = 90
                val labelsStep = dates.chartLabelsStep
                val overrides = dates.mapIndexed {
                    index, millis ->
                    millis to if (index % labelsStep != 0) {
                        " "
                    } else {
                        pairs[index].first.toString()
                    }
                }
                setXAxisLabelOverrideMap(overrides.toMap())
                BitmapEncoder.getBitmapBytes(this, BitmapEncoder.BitmapFormat.JPG)
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy