Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.google.gson.Gson
- import com.google.gson.reflect.TypeToken
- import com.jfoenix.controls.JFXCheckBox
- import com.jfoenix.controls.JFXListView
- import javafx.application.Application
- import javafx.geometry.Pos
- import javafx.scene.Scene
- import javafx.scene.chart.CategoryAxis
- import javafx.scene.chart.LineChart
- import javafx.scene.chart.NumberAxis
- import javafx.scene.chart.XYChart
- import javafx.scene.control.*
- import javafx.stage.Stage
- import javafx.scene.layout.AnchorPane
- import javafx.scene.layout.HBox
- import javafx.scene.layout.VBox
- import javafx.stage.Screen
- import java.beans.EventHandler
- import java.net.URL
- import java.time.LocalDate
- import java.time.LocalTime
- import java.time.Month
- import java.time.ZoneOffset
- class MainController {
- }
- class Coin() {
- var id = ""
- var symbol = ""
- var name = ""
- //var used : SimpleBooleanProperty = SimpleBooleanProperty(false)
- @Override
- override fun toString(): String {
- return name
- }
- }
- class Sender() {
- companion object {
- fun send(url : String) : String {
- return try {
- URL(url).openStream().bufferedReader().use{ it.readText() }
- } catch (e : Exception) {
- "not correct url"
- }
- }
- }
- }
- class PriceData {
- var prices : ArrayList<ArrayList<String>> = arrayListOf()
- var market_caps : ArrayList<ArrayList<String>> = arrayListOf()
- var total_volumes : ArrayList<ArrayList<String>> = arrayListOf()
- }
- class CoinsSeries() {
- companion object {
- fun generation(coin : Coin, from : String, to : String) : XYChart.Series<String, Number> {
- val json = Sender.send("https://api.coingecko.com/api/v3/coins/${coin.id}" +
- "/market_chart/range?vs_currency=usd&from=$from&to=$to")
- val request = Gson().fromJson<PriceData>(json,PriceData::class.java)
- val series = XYChart.Series<String, Number>()
- series.name = coin.name
- request.prices.forEach { list: java.util.ArrayList<String> ->
- series.data.add(XYChart.Data<String, Number>(list[0], list[1].toDouble()))
- }
- return series
- }
- }
- }
- class ListRow(val coin: Coin) {
- val checkBox = JFXCheckBox()
- val label = Label()
- val colorPicker = ColorPicker()
- val hotizontalBox = HBox(checkBox, label)
- var series = XYChart.Series<String, Number>()
- var index = 0
- var checker = false
- }
- class MainStage : Stage(){
- init {
- width = 915.0
- height = 800.0
- title = "Coin"
- val mainPlane = object : AnchorPane(){
- init {
- val mainBox = object : VBox(){
- init {
- val text = object : Label(){
- init {
- prefHeight = 50.0
- prefWidth = 900.0
- text = "Coins"
- alignment = Pos.CENTER
- }
- }
- val CoinsList = object : JFXListView<Coin>(){
- init {
- prefHeight = 100.0
- prefWidth = 900.0
- }
- }
- val dateBox = object : HBox(){
- init {
- prefHeight = 100.0
- prefWidth = 900.0
- val dateFrom = object : DatePicker(){
- init{
- prefWidth = 450.0
- value = LocalDate.of(2019, Month.JANUARY, 1)
- }
- }
- val dateTo = object : DatePicker(){
- init{
- prefWidth = 450.0
- value = LocalDate.now()
- }
- }
- children.addAll(dateFrom,dateTo)
- }
- }
- val chart = object : LineChart<String, Number>(CategoryAxis(),NumberAxis()){
- init{
- prefHeight = 400.0
- prefWidth = 900.0
- }
- }
- children.addAll(text,CoinsList,dateBox,chart)
- }
- }
- children.addAll(mainBox)
- }
- }
- val mainScene = object : Scene(mainPlane){
- }
- scene = mainScene
- val listCoins : ArrayList<Coin> = Gson().fromJson<ArrayList<Coin>>(
- Sender.send("https://api.coingecko.com/api/v3/coins/list"), object : TypeToken<List<Coin>>() {}.type
- )
- val controlMap : MutableMap<Coin, ListRow> = mutableMapOf()
- listCoins.forEach {
- controlMap[it] = ListRow(it)
- }
- val coinsListView = (mainPlane.children[0] as VBox).children[1] as JFXListView<Coin>
- val dateFrom = ((mainPlane.children[0] as VBox).children[2] as HBox).children[0] as DatePicker
- val dateTo = ((mainPlane.children[0] as VBox).children[2] as HBox).children[1] as DatePicker
- val chart = ((mainPlane.children[0] as VBox).children[3] as LineChart<String,Number>)
- coinsListView.items.addAll(listCoins)
- coinsListView.setCellFactory {
- object : ListCell<Coin>(){
- private lateinit var coin_ : Coin
- override fun updateItem(item: Coin?, empty: Boolean) {
- super.updateItem(item, empty)
- var row = ListRow(Coin())
- if (item != null) {
- coin_ = item
- row = controlMap[coin_]!!
- }
- row.label.text = item.toString()
- if(row.checkBox.isSelected) {
- row.colorPicker.opacity = 100.0
- row.colorPicker.isDisable = false
- }
- else {
- row.colorPicker.opacity = 0.0
- row.colorPicker.isDisable = true
- }
- graphic = row.hotizontalBox
- row.checkBox.setOnAction{
- if (row.checkBox.isSelected) {
- row.series = (CoinsSeries.generation(coin_,
- dateFrom.value..toString(),
- dateTo.value.toEpochDay().toString()))
- chart.data.add(row.series)
- if(!row.checker) {
- row.index = chart.data.indexOf(row.series)
- row.checker = true
- }
- } else {
- chart.data.remove(row.series)
- }
- }
- // row.colorPicker.setOnAction {
- // val color = row.colorPicker.value
- // chart.lookupAll(".default-color${row.index}.chart-series-line").forEachIndexed { i, node ->
- // node.style = "-fx-stroke: #${color.toString().substringAfter("x")};"
- // }
- // }
- }
- init {
- contentDisplay = ContentDisplay.GRAPHIC_ONLY
- }
- }
- }
- }
- }
- class Starter : Application() {
- @Throws(Exception::class)
- override fun start(primaryStage: Stage) {
- MainStage().show()
- }
- companion object {
- @JvmStatic
- fun main(args: Array<String>) {
- launch(Starter::class.java)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement