diff --git a/charts/India_2020_03_31.png b/charts/India_2020_03_31.png new file mode 100644 index 0000000..d4c5eb0 Binary files /dev/null and b/charts/India_2020_03_31.png differ diff --git a/charts/demo.png b/charts/demo.png index d34d2ff..8ff094b 100644 Binary files a/charts/demo.png and b/charts/demo.png differ diff --git a/covid19-api/src/main/kotlin/com/teamxenox/covid19api/chart/Graphologist.kt b/covid19-api/src/main/kotlin/com/teamxenox/covid19api/chart/Graphologist.kt index fee2744..8ec1f29 100644 --- a/covid19-api/src/main/kotlin/com/teamxenox/covid19api/chart/Graphologist.kt +++ b/covid19-api/src/main/kotlin/com/teamxenox/covid19api/chart/Graphologist.kt @@ -7,9 +7,11 @@ import org.knowm.xchart.BitmapEncoder import org.knowm.xchart.XYChart import org.knowm.xchart.XYChartBuilder import java.awt.Color +import java.awt.Font import java.io.File import java.lang.IllegalArgumentException import java.text.SimpleDateFormat +import javax.imageio.ImageIO class Graphologist { @@ -39,7 +41,11 @@ class Graphologist { private val inputDateFormat = SimpleDateFormat("yyyy-MM-dd") private val outputDateFormat = SimpleDateFormat("dd/MM/yyyy") - + private const val WATERMARK_TEXT = "Generated with @CoDoc19Bot" + private const val WM_FONT_SIZE = 15 + private const val WM_X = CHART_WIDTH - ((WATERMARK_TEXT.length / 1.7) * WM_FONT_SIZE).toInt() + private const val WM_Y = CHART_HEIGHT - (WM_FONT_SIZE * 0.75).toInt() + private val WM_FONT = Font("Ubuntu", Font.BOLD, WM_FONT_SIZE) } @@ -51,12 +57,23 @@ class Graphologist { val chart = getChart(chartType, date, data) val fileName = "${data.countryName.replace(" ", "_")}_${date.replace("-", "_")}.png" + + // Adding watermark + val buffImage = BitmapEncoder.getBufferedImage(chart); + val graphics = buffImage.graphics + graphics.apply { + font = WM_FONT + color = plotBgColor + drawString(WATERMARK_TEXT, WM_X, WM_Y) + }.dispose() + val chartFile = File("${JarUtils.getJarDir()}charts/$fileName") - BitmapEncoder.saveBitmap(chart, chartFile.absolutePath, BitmapEncoder.BitmapFormat.PNG); + ImageIO.write(buffImage, "png", chartFile) + return chartFile } - fun getChart(chartType: Int, date: String, _data: JhuData): XYChart { + private fun getChart(chartType: Int, date: String, _data: JhuData): XYChart { val data = if (chartType == CHART_CASE_DAILY || chartType == CHART_DEATH_DAILY) { JhuData( @@ -70,21 +87,21 @@ class Graphologist { val seriesTitle: String val chartLineColor: Color - val yAxisTitle : String - val chartTitle : String + val yAxisTitle: String + val chartTitle: String when (chartType) { CHART_DEATH, CHART_DEATH_DAILY -> { seriesTitle = CHART_DEATH_LEGEND chartLineColor = Color.RED - yAxisTitle =CHART_DEATH_Y_AXIS_TITLE - chartTitle =CHART_DEATH_TITLE + yAxisTitle = CHART_DEATH_Y_AXIS_TITLE + chartTitle = CHART_DEATH_TITLE } CHART_CASE, CHART_CASE_DAILY -> { seriesTitle = CHART_CASE_LEGEND chartLineColor = Color.ORANGE - yAxisTitle =CHART_CASE_Y_AXIS_TITLE - chartTitle =CHART_CASE_TITLE + yAxisTitle = CHART_CASE_Y_AXIS_TITLE + chartTitle = CHART_CASE_TITLE } else -> throw IllegalArgumentException("Undefined chartType `$chartType`") diff --git a/covid19-api/src/test/kotlin/com/teamxenox/covid19api/chart/GraphologistTest.kt b/covid19-api/src/test/kotlin/com/teamxenox/covid19api/chart/GraphologistTest.kt index 5707ef4..8529ad0 100644 --- a/covid19-api/src/test/kotlin/com/teamxenox/covid19api/chart/GraphologistTest.kt +++ b/covid19-api/src/test/kotlin/com/teamxenox/covid19api/chart/GraphologistTest.kt @@ -5,7 +5,10 @@ import com.teamxenox.covid19api.utils.JarUtils import com.winterbe.expekt.should import org.junit.Test import org.knowm.xchart.* +import java.awt.Color +import java.awt.Font import java.io.File +import javax.imageio.ImageIO class GraphologistTest { @@ -18,11 +21,6 @@ class GraphologistTest { BitmapEncoder.saveBitmap(chart, "../charts/chart.png", BitmapEncoder.BitmapFormat.PNG); } - @Test - fun testIfCanGraphologistCanDrawSuccess() { - val graphologist = Graphologist() - } - @Test fun testDeathChartSuccess() { @@ -30,13 +28,11 @@ class GraphologistTest { val cases = listOf(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 28, 30, 31, 34, 39, 43, 56, 62, 73, 82, 102, 113, 119, 142, 156, 194, 244, 330, 396, 499, 536, 657, 727, 887, 987) deaths.size.should.equal(cases.size) - val chart = Graphologist().getChart( + val chart = Graphologist().prepareChart( Graphologist.CHART_DEATH, - "2020-03-30", + "2020-03-31", JhuData("India", deaths) ) - val chartFile = File("../charts/demo.png") - BitmapEncoder.saveBitmap(chart, chartFile.absolutePath, BitmapEncoder.BitmapFormat.PNG); } diff --git a/src/main/kotlin/com/teamxenox/codoc19/core/features/quiz/QuizBoss.kt b/src/main/kotlin/com/teamxenox/codoc19/core/features/quiz/QuizBoss.kt index f6c6727..f84415d 100644 --- a/src/main/kotlin/com/teamxenox/codoc19/core/features/quiz/QuizBoss.kt +++ b/src/main/kotlin/com/teamxenox/codoc19/core/features/quiz/QuizBoss.kt @@ -13,7 +13,7 @@ import java.lang.IllegalArgumentException class QuizBoss( private val telegramApi: Telegram, private val chatId: Long, - private val messageId: Long + messageId: Long ) : FeatureProxy(telegramApi, chatId, messageId) { companion object { diff --git a/src/main/kotlin/com/teamxenox/codoc19/core/features/stats/CovidAnalyst.kt b/src/main/kotlin/com/teamxenox/codoc19/core/features/stats/CovidAnalyst.kt index 8e3459b..fa3aaff 100644 --- a/src/main/kotlin/com/teamxenox/codoc19/core/features/stats/CovidAnalyst.kt +++ b/src/main/kotlin/com/teamxenox/codoc19/core/features/stats/CovidAnalyst.kt @@ -72,8 +72,8 @@ class CovidAnalyst(private val telegramApi: Telegram, private val chatId: Long, private fun toText(header: String, stats: Statistics, isGlobal: Boolean): String { - val globalText = if (isGlobal) { - "🌐 Global statistics are based on GMT +00:00" + val globalText = if (stats.countryName != "India") { + "🌐 Statistics are based on GMT +00:00" } else { "" }