Comments (2)
Hello, please try this:
- Add the dependency in your
build.gradle.kts
:
sourceSets["main"].dependencies {
implementation(npm("vega", "5.24.0"))
// ...
}
- Create some external declarations (add more functions if you need them):
@JsModule("vega")
@JsNonModule
external object vega {
fun parse(spec: dynamic): dynamic
class View(spec: dynamic, options: dynamic) {
fun runAsync()
}
}
- Create the spec with a few dynamic
obj {}
calls:
val spec = obj {
this["\$schema"] = "https://vega.github.io/schema/vega/v5.json"
this.description = "A basic bar chart example, with value labels shown upon mouse hover."
this.width = 400
this.height = 200
this.padding = 5
this.data = arrayOf(
obj {
this.name = "table"
this.values = arrayOf(
obj {
this.category = "A"
this.amount = 28
},
obj {
this.category = "B"
this.amount = 55
},
obj {
this.category = "C"
this.amount = 43
},
obj {
this.category = "D"
this.amount = 91
},
obj {
this.category = "E"
this.amount = 81
},
obj {
this.category = "F"
this.amount = 53
},
obj {
this.category = "G"
this.amount = 19
},
obj {
this.category = "H"
this.amount = 87
}
)
}
)
this.signals = arrayOf(
obj {
this.name = "tooltip"
this.value = obj {
}
this.on = arrayOf(
obj {
this.events = "rect:mouseover"
this.update = "datum"
},
obj {
this.events = "rect:mouseout"
this.update = "{}"
}
)
}
)
this.scales = arrayOf(
obj {
this.name = "xscale"
this.type = "band"
this.domain = obj {
this.data = "table"
this.field = "category"
}
this.range = "width"
this.padding = 0.05
this.round = true
},
obj {
this.name = "yscale"
this.domain = obj {
this.data = "table"
this.field = "amount"
}
this.nice = true
this.range = "height"
}
)
this.axes = arrayOf(
obj {
this.orient = "bottom"
this.scale = "xscale"
},
obj {
this.orient = "left"
this.scale = "yscale"
}
)
this.marks = arrayOf(
obj {
this.type = "rect"
this.from = obj {
this.data = "table"
}
this.encode = obj {
this.enter = obj {
this.x = obj {
this.scale = "xscale"
this.field = "category"
}
this.width = obj {
this.scale = "xscale"
this.band = 1
}
this.y = obj {
this.scale = "yscale"
this.field = "amount"
}
this.y2 = obj {
this.scale = "yscale"
this.value = 0
}
}
this.update = obj {
this.fill = obj {
this.value = "steelblue"
}
}
this.hover = obj {
this.fill = obj {
this.value = "red"
}
}
}
},
obj {
this.type = "text"
this.encode = obj {
this.enter = obj {
this.align = obj {
this.value = "center"
}
this.baseline = obj {
this.value = "bottom"
}
this.fill = obj {
this.value = "#333"
}
}
this.update = obj {
this.x = obj {
this.scale = "xscale"
this.signal = "tooltip.category"
this.band = 0.5
}
this.y = obj {
this.scale = "yscale"
this.signal = "tooltip.amount"
this.offset = -2
}
this.text = obj {
this.signal = "tooltip.amount"
}
this.fillOpacity = arrayOf(
obj {
this.test = "datum === tooltip"
this.value = 0
},
obj {
this.value = 1
}
)
}
}
}
)
}
- Use
addAfterInsertHook
to access DOM element and pass it to Vega API (you can also use "svg" renderer):
class App : Application() {
override fun start() {
root("kvapp") {
div {
addAfterInsertHook {
val view = vega.View(vega.parse(spec), obj {
this.renderer = "canvas"
this.container = getElement()
this.hover = true
})
view.runAsync()
}
}
}
}
}
from kvision.
Worked like a charm.
(Sorry for the late response)
from kvision.
Related Issues (20)
- Initial value for DateTime control HOT 1
- Request - support for crossorigin attribute in Image (and others) HOT 1
- Ability to intercept image load errors HOT 2
- Component lifecycle HOT 6
- StackPanel seems to not indexing components properly HOT 4
- Wrong module in kvision.gitbook.io. HOT 1
- trying to generate .pot files HOT 12
- overrides of `SimplePanel.AddAll` call refresh outside of a `singleRender` wrapper. HOT 1
- Allow KVService to have arguments with GET HOT 6
- Allow modifying response headers for Spring Boot and Micronaut apps HOT 1
- remove id parameter on get requests HOT 1
- Can't get data from Tabulator.RowComponent HOT 2
- Expandable/collapsible fieldset or panel HOT 2
- How to attch files using the Richtext Component HOT 7
- duplicated different word in kvision docs HOT 1
- did you consider to integrate kvision with ktor client? HOT 5
- can't make tabulator selectable with kvision v7.4.4 HOT 2
- My tabulator not selecting data HOT 5
- How to add more than one bottomCalc rows? HOT 2
- kvision gradle plugin not works well with Spring-Web-Flux HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kvision.