Skip to content

Commit

Permalink
馃悰 fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
theapache64 committed Mar 31, 2020
1 parent 30c9c2f commit 20decc7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
Binary file added charts/India_2020_03_31.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified charts/demo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
}


Expand All @@ -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(
Expand All @@ -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`")
Expand Down
Expand Up @@ -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 {
Expand All @@ -18,25 +21,18 @@ class GraphologistTest {
BitmapEncoder.saveBitmap(chart, "../charts/chart.png", BitmapEncoder.BitmapFormat.PNG);
}

@Test
fun testIfCanGraphologistCanDrawSuccess() {
val graphologist = Graphologist()
}

@Test
fun testDeathChartSuccess() {

val deaths = listOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 4, 7, 10, 10, 12, 20, 20, 24)
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);

}

Expand Down
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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 {
""
}
Expand Down

0 comments on commit 20decc7

Please sign in to comment.