Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 8.06 KB | None | 0 0
  1. import com.google.gson.Gson
  2. import com.google.gson.reflect.TypeToken
  3. import com.jfoenix.controls.JFXCheckBox
  4. import com.jfoenix.controls.JFXListView
  5. import javafx.application.Application
  6. import javafx.geometry.Pos
  7. import javafx.scene.Scene
  8. import javafx.scene.chart.CategoryAxis
  9. import javafx.scene.chart.LineChart
  10. import javafx.scene.chart.NumberAxis
  11. import javafx.scene.chart.XYChart
  12. import javafx.scene.control.*
  13. import javafx.stage.Stage
  14. import javafx.scene.layout.AnchorPane
  15. import javafx.scene.layout.HBox
  16. import javafx.scene.layout.VBox
  17. import javafx.stage.Screen
  18. import java.beans.EventHandler
  19. import java.net.URL
  20. import java.time.LocalDate
  21. import java.time.LocalTime
  22. import java.time.Month
  23. import java.time.ZoneOffset
  24.  
  25.  
  26. class MainController {
  27.  
  28. }
  29.  
  30. class Coin() {
  31.     var id = ""
  32.     var symbol = ""
  33.     var name = ""
  34.     //var used : SimpleBooleanProperty = SimpleBooleanProperty(false)
  35.  
  36.     @Override
  37.     override fun toString(): String {
  38.         return name
  39.     }
  40. }
  41.  
  42. class Sender() {
  43.     companion object {
  44.         fun send(url : String) : String {
  45.             return try {
  46.                 URL(url).openStream().bufferedReader().use{ it.readText() }
  47.             } catch (e : Exception) {
  48.                 "not correct url"
  49.             }
  50.         }
  51.     }
  52. }
  53.  
  54. class PriceData {
  55.     var prices : ArrayList<ArrayList<String>> = arrayListOf()
  56.     var market_caps : ArrayList<ArrayList<String>> = arrayListOf()
  57.     var total_volumes : ArrayList<ArrayList<String>> = arrayListOf()
  58. }
  59.  
  60. class CoinsSeries() {
  61.     companion object {
  62.         fun generation(coin : Coin, from : String, to : String) : XYChart.Series<String, Number> {
  63.             val json = Sender.send("https://api.coingecko.com/api/v3/coins/${coin.id}" +
  64.                     "/market_chart/range?vs_currency=usd&from=$from&to=$to")
  65.             val request = Gson().fromJson<PriceData>(json,PriceData::class.java)
  66.             val series = XYChart.Series<String, Number>()
  67.             series.name = coin.name
  68.             request.prices.forEach { list: java.util.ArrayList<String> ->
  69.                 series.data.add(XYChart.Data<String, Number>(list[0], list[1].toDouble()))
  70.             }
  71.             return series
  72.         }
  73.     }
  74. }
  75.  
  76. class ListRow(val coin: Coin) {
  77.     val checkBox = JFXCheckBox()
  78.     val label = Label()
  79.     val colorPicker = ColorPicker()
  80.     val hotizontalBox = HBox(checkBox, label)
  81.     var series = XYChart.Series<String, Number>()
  82.     var index = 0
  83.     var checker = false
  84. }
  85.  
  86. class MainStage : Stage(){
  87.  
  88.  
  89.     init {
  90.         width = 915.0
  91.         height = 800.0
  92.         title = "Coin"
  93.  
  94.         val mainPlane = object : AnchorPane(){
  95.             init {
  96.                 val mainBox = object : VBox(){
  97.                     init {
  98.                         val text = object : Label(){
  99.                             init {
  100.                                 prefHeight = 50.0
  101.                                 prefWidth = 900.0
  102.                                 text = "Coins"
  103.                                 alignment = Pos.CENTER
  104.                             }
  105.                         }
  106.                         val CoinsList = object : JFXListView<Coin>(){
  107.                             init {
  108.                                 prefHeight = 100.0
  109.                                 prefWidth = 900.0
  110.                             }
  111.                         }
  112.                         val dateBox = object : HBox(){
  113.                             init {
  114.                                 prefHeight = 100.0
  115.                                 prefWidth = 900.0
  116.                                 val dateFrom = object : DatePicker(){
  117.                                     init{
  118.                                         prefWidth = 450.0
  119.                                         value = LocalDate.of(2019, Month.JANUARY, 1)
  120.                                     }
  121.                                 }
  122.                                 val dateTo = object : DatePicker(){
  123.                                     init{
  124.                                         prefWidth = 450.0
  125.                                         value = LocalDate.now()
  126.                                     }
  127.                                 }
  128.                                 children.addAll(dateFrom,dateTo)
  129.                             }
  130.                         }
  131.                         val chart = object  : LineChart<String, Number>(CategoryAxis(),NumberAxis()){
  132.                             init{
  133.                                 prefHeight = 400.0
  134.                                 prefWidth = 900.0
  135.                             }
  136.                         }
  137.                         children.addAll(text,CoinsList,dateBox,chart)
  138.  
  139.                     }
  140.                 }
  141.                 children.addAll(mainBox)
  142.             }
  143.         }
  144.         val mainScene = object : Scene(mainPlane){
  145.  
  146.         }
  147.  
  148.         scene = mainScene
  149.  
  150.         val listCoins : ArrayList<Coin> = Gson().fromJson<ArrayList<Coin>>(
  151.             Sender.send("https://api.coingecko.com/api/v3/coins/list"), object : TypeToken<List<Coin>>() {}.type
  152.         )
  153.  
  154.         val controlMap : MutableMap<Coin, ListRow> = mutableMapOf()
  155.  
  156.         listCoins.forEach {
  157.             controlMap[it] = ListRow(it)
  158.         }
  159.  
  160.         val coinsListView = (mainPlane.children[0] as VBox).children[1] as JFXListView<Coin>
  161.         val dateFrom = ((mainPlane.children[0] as VBox).children[2] as HBox).children[0] as DatePicker
  162.         val dateTo = ((mainPlane.children[0] as VBox).children[2] as HBox).children[1] as DatePicker
  163.         val chart = ((mainPlane.children[0] as VBox).children[3] as LineChart<String,Number>)
  164.         coinsListView.items.addAll(listCoins)
  165.  
  166.         coinsListView.setCellFactory {
  167.             object : ListCell<Coin>(){
  168.                 private lateinit var coin_ : Coin
  169.  
  170.                 override fun updateItem(item: Coin?, empty: Boolean) {
  171.                     super.updateItem(item, empty)
  172.  
  173.                     var row = ListRow(Coin())
  174.  
  175.                     if (item != null) {
  176.                         coin_ = item
  177.                         row = controlMap[coin_]!!
  178.                     }
  179.  
  180.                     row.label.text = item.toString()
  181.  
  182.                     if(row.checkBox.isSelected) {
  183.                         row.colorPicker.opacity = 100.0
  184.                         row.colorPicker.isDisable = false
  185.                     }
  186.                     else {
  187.                         row.colorPicker.opacity = 0.0
  188.                         row.colorPicker.isDisable = true
  189.                     }
  190.                     graphic = row.hotizontalBox
  191.                     row.checkBox.setOnAction{
  192.                         if (row.checkBox.isSelected) {
  193.                             row.series = (CoinsSeries.generation(coin_,
  194.                                 dateFrom.value..toString(),
  195.                                 dateTo.value.toEpochDay().toString()))
  196.                             chart.data.add(row.series)
  197.                             if(!row.checker) {
  198.                                 row.index = chart.data.indexOf(row.series)
  199.                                 row.checker = true
  200.                             }
  201.                         } else {
  202.                             chart.data.remove(row.series)
  203.                         }
  204.                     }
  205. //                    row.colorPicker.setOnAction {
  206. //                        val color = row.colorPicker.value
  207. //                        chart.lookupAll(".default-color${row.index}.chart-series-line").forEachIndexed { i, node ->
  208. //                            node.style = "-fx-stroke: #${color.toString().substringAfter("x")};"
  209. //                        }
  210. //                    }
  211.                 }
  212.                 init {
  213.                     contentDisplay = ContentDisplay.GRAPHIC_ONLY
  214.                 }
  215.             }
  216.         }
  217.     }
  218.  
  219.  
  220. }
  221.  
  222.  
  223. class Starter : Application() {
  224.     @Throws(Exception::class)
  225.     override fun start(primaryStage: Stage) {
  226.         MainStage().show()
  227.     }
  228.  
  229.     companion object {
  230.         @JvmStatic
  231.         fun main(args: Array<String>) {
  232.             launch(Starter::class.java)
  233.         }
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement