@rokkit/chart 1.0.0-next.13 → 1.0.0-next.131
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -46
- package/dist/Plot/index.d.ts +5 -0
- package/dist/elements/index.d.ts +6 -0
- package/dist/index.d.ts +14 -0
- package/dist/lib/brewing/axes.svelte.d.ts +72 -0
- package/dist/lib/brewing/bars.svelte.d.ts +54 -0
- package/dist/lib/brewing/dimensions.svelte.d.ts +35 -0
- package/dist/lib/brewing/index.svelte.d.ts +118 -0
- package/dist/lib/brewing/legends.svelte.d.ts +54 -0
- package/dist/lib/brewing/scales.svelte.d.ts +29 -0
- package/dist/lib/brewing/types.d.ts +162 -0
- package/dist/lib/context.d.ts +13 -0
- package/dist/lib/scales.svelte.d.ts +35 -0
- package/dist/lib/utils.d.ts +58 -0
- package/dist/old_lib/brewer.d.ts +9 -0
- package/dist/old_lib/chart.d.ts +40 -0
- package/dist/old_lib/grid.d.ts +72 -0
- package/dist/old_lib/index.d.ts +4 -0
- package/dist/old_lib/plots.d.ts +3 -0
- package/dist/old_lib/swatch.d.ts +285 -0
- package/dist/old_lib/ticks.d.ts +36 -0
- package/dist/old_lib/utils.d.ts +1 -0
- package/dist/patterns/index.d.ts +9 -0
- package/dist/patterns/paths/constants.d.ts +1 -0
- package/dist/symbols/constants/index.d.ts +1 -0
- package/dist/symbols/index.d.ts +5 -0
- package/dist/template/constants.d.ts +43 -0
- package/dist/template/shapes/index.d.ts +4 -0
- package/package.json +28 -44
- package/src/Plot/Axis.svelte +95 -0
- package/src/Plot/Bar.svelte +96 -0
- package/src/Plot/Grid.svelte +68 -0
- package/src/Plot/Legend.svelte +127 -0
- package/src/Plot/Root.svelte +107 -0
- package/src/Plot/index.js +5 -0
- package/src/Symbol.svelte +21 -0
- package/src/Texture.svelte +10 -0
- package/src/elements/Bar.svelte +22 -24
- package/src/elements/ColorRamp.svelte +20 -22
- package/src/elements/ContinuousLegend.svelte +20 -17
- package/src/elements/DefinePatterns.svelte +24 -0
- package/src/elements/DiscreteLegend.svelte +15 -15
- package/src/elements/Label.svelte +11 -8
- package/src/elements/SymbolGrid.svelte +27 -0
- package/src/elements/index.js +6 -0
- package/src/examples/BarChartExample.svelte +81 -0
- package/src/index.js +18 -16
- package/src/lib/brewing/axes.svelte.js +177 -0
- package/src/lib/brewing/bars.svelte.js +114 -0
- package/src/lib/brewing/dimensions.svelte.js +56 -0
- package/src/lib/brewing/index.svelte.js +202 -0
- package/src/lib/brewing/legends.svelte.js +94 -0
- package/src/lib/brewing/scales.svelte.js +85 -0
- package/src/lib/brewing/types.js +73 -0
- package/src/lib/context.js +132 -0
- package/src/lib/scales.svelte.js +122 -0
- package/src/lib/utils.js +96 -120
- package/src/old_lib/brewer.js +25 -0
- package/src/old_lib/chart.js +213 -0
- package/src/old_lib/grid.js +85 -0
- package/src/old_lib/index.js +4 -0
- package/src/old_lib/plots.js +27 -0
- package/src/old_lib/swatch.js +16 -0
- package/src/old_lib/ticks.js +46 -0
- package/src/old_lib/utils.js +8 -0
- package/src/patterns/Brick.svelte +15 -0
- package/src/patterns/Circles.svelte +18 -0
- package/src/patterns/CrossHatch.svelte +12 -0
- package/src/patterns/CurvedWave.svelte +7 -0
- package/src/patterns/Dots.svelte +20 -0
- package/src/patterns/OutlineCircles.svelte +13 -0
- package/src/patterns/Tile.svelte +16 -0
- package/src/patterns/Triangles.svelte +13 -0
- package/src/patterns/Waves.svelte +9 -0
- package/src/patterns/index.js +14 -0
- package/src/patterns/paths/NamedPattern.svelte +9 -0
- package/src/patterns/paths/constants.js +4 -0
- package/src/symbols/RoundedSquare.svelte +22 -0
- package/src/symbols/Shape.svelte +24 -0
- package/src/symbols/constants/index.js +4 -0
- package/src/symbols/index.js +9 -0
- package/src/symbols/outline.svelte +60 -0
- package/src/symbols/solid.svelte +60 -0
- package/src/template/Texture.svelte +13 -0
- package/src/template/constants.js +43 -0
- package/src/template/shapes/Circles.svelte +13 -0
- package/src/template/shapes/Lines.svelte +14 -0
- package/src/template/shapes/Path.svelte +9 -0
- package/src/template/shapes/Polygons.svelte +9 -0
- package/src/template/shapes/index.js +4 -0
- package/LICENSE +0 -21
- package/src/chart/FacetGrid.svelte +0 -51
- package/src/chart/Grid.svelte +0 -34
- package/src/chart/Legend.svelte +0 -16
- package/src/chart/PatternDefs.svelte +0 -13
- package/src/chart/Swatch.svelte +0 -93
- package/src/chart/SwatchButton.svelte +0 -29
- package/src/chart/SwatchGrid.svelte +0 -55
- package/src/chart/Symbol.svelte +0 -37
- package/src/chart/Texture.svelte +0 -16
- package/src/chart/TexturedShape.svelte +0 -27
- package/src/chart/TimelapseChart.svelte +0 -97
- package/src/chart/Timer.svelte +0 -27
- package/src/chart.js +0 -9
- package/src/components/charts/Axis.svelte +0 -66
- package/src/components/charts/Chart.svelte +0 -35
- package/src/components/index.js +0 -23
- package/src/components/lib/axis.js +0 -0
- package/src/components/lib/chart.js +0 -187
- package/src/components/lib/color.js +0 -327
- package/src/components/lib/funnel.js +0 -204
- package/src/components/lib/index.js +0 -19
- package/src/components/lib/pattern.js +0 -190
- package/src/components/lib/rollup.js +0 -55
- package/src/components/lib/shape.js +0 -199
- package/src/components/lib/summary.js +0 -145
- package/src/components/lib/theme.js +0 -23
- package/src/components/lib/timer.js +0 -41
- package/src/components/lib/utils.js +0 -165
- package/src/components/plots/BarPlot.svelte +0 -36
- package/src/components/plots/BoxPlot.svelte +0 -54
- package/src/components/plots/ScatterPlot.svelte +0 -30
- package/src/components/store.js +0 -70
- package/src/constants.js +0 -66
- package/src/elements/PatternDefs.svelte +0 -13
- package/src/elements/PatternMask.svelte +0 -20
- package/src/elements/Symbol.svelte +0 -38
- package/src/elements/Tooltip.svelte +0 -23
- package/src/funnel.svelte +0 -35
- package/src/geom.js +0 -105
- package/src/lib/axis.js +0 -75
- package/src/lib/colors.js +0 -32
- package/src/lib/geom.js +0 -4
- package/src/lib/shapes.js +0 -144
- package/src/lib/timer.js +0 -44
- package/src/lookup.js +0 -29
- package/src/plots/BarPlot.svelte +0 -55
- package/src/plots/BoxPlot.svelte +0 -0
- package/src/plots/FunnelPlot.svelte +0 -33
- package/src/plots/HeatMap.svelte +0 -5
- package/src/plots/HeatMapCalendar.svelte +0 -129
- package/src/plots/LinePlot.svelte +0 -55
- package/src/plots/Plot.svelte +0 -25
- package/src/plots/RankBarPlot.svelte +0 -38
- package/src/plots/ScatterPlot.svelte +0 -20
- package/src/plots/ViolinPlot.svelte +0 -11
- package/src/plots/heatmap.js +0 -70
- package/src/plots/index.js +0 -10
- package/src/swatch.js +0 -11
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { setContext } from 'svelte'
|
|
3
|
-
import { writable } from 'svelte/store'
|
|
4
|
-
import { slidingWindow, uniques, brewer } from '../lib'
|
|
5
|
-
|
|
6
|
-
import Grid from './Grid.svelte'
|
|
7
|
-
import Axis from './Axis.svelte'
|
|
8
|
-
import BoxPlot from '../plots/BoxPlot.svelte'
|
|
9
|
-
import ViolinPlot from '../plots/ViolinPlot.svelte'
|
|
10
|
-
import ScatterPlot from '../plots/ScatterPlot.svelte'
|
|
11
|
-
|
|
12
|
-
let chart = writable({})
|
|
13
|
-
let axis
|
|
14
|
-
setContext('chart', chart)
|
|
15
|
-
|
|
16
|
-
export let data
|
|
17
|
-
export let x
|
|
18
|
-
export let y
|
|
19
|
-
export let time
|
|
20
|
-
export let theme
|
|
21
|
-
|
|
22
|
-
export let current
|
|
23
|
-
export let stages
|
|
24
|
-
|
|
25
|
-
function base(data, x, y) {
|
|
26
|
-
return {
|
|
27
|
-
data,
|
|
28
|
-
x,
|
|
29
|
-
y,
|
|
30
|
-
width: 800,
|
|
31
|
-
height: 350,
|
|
32
|
-
values: {
|
|
33
|
-
x: uniques(data, x),
|
|
34
|
-
y: uniques(data, y),
|
|
35
|
-
},
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function sliceBy(data, attr, size, step, offset) {
|
|
40
|
-
const values = uniques(data, attr)
|
|
41
|
-
const groups = slidingWindow(values, size, step, offset).map((x) => ({
|
|
42
|
-
...x,
|
|
43
|
-
data: data.filter(
|
|
44
|
-
(y) => y.Petal_Length >= x.lowerBound && y.Petal_Length < x.upperBound
|
|
45
|
-
),
|
|
46
|
-
}))
|
|
47
|
-
|
|
48
|
-
return groups
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function timed(data, x, y, time) {
|
|
52
|
-
let chart = base(data, x, y)
|
|
53
|
-
let temp = brewer().chart(data, x, y).use(theme).computeAxis()
|
|
54
|
-
axis = temp.axis
|
|
55
|
-
console.log(temp)
|
|
56
|
-
chart.data = sliceBy(chart.data, time, 3, 1)
|
|
57
|
-
stages = chart.data.length - 1
|
|
58
|
-
return chart
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function switchChart(index) {
|
|
62
|
-
let chart = {}
|
|
63
|
-
if (index != null) {
|
|
64
|
-
chart = brewer()
|
|
65
|
-
.chart(phased.data[index].data, x, y)
|
|
66
|
-
.use(theme)
|
|
67
|
-
.computeAxis()
|
|
68
|
-
chart.axis = axis
|
|
69
|
-
// chart.margin = { left: 40, right: 10, top: 10, bottom: 30 }
|
|
70
|
-
}
|
|
71
|
-
return chart
|
|
72
|
-
}
|
|
73
|
-
let phased
|
|
74
|
-
|
|
75
|
-
$: phased = timed(data, x, y, time)
|
|
76
|
-
$: $chart = switchChart(current)
|
|
77
|
-
// $: console.log(current, phased, chart)
|
|
78
|
-
|
|
79
|
-
// setup chart attributes that do not change over time
|
|
80
|
-
// get scales for x & y
|
|
81
|
-
// set margins
|
|
82
|
-
|
|
83
|
-
// nest data by time attribute
|
|
84
|
-
// set up sequence based on ascending values of time
|
|
85
|
-
// set up the timer to switch between data values
|
|
86
|
-
// On change set the context with new data set
|
|
87
|
-
// Old data set needs exit animation, new data set needs entry animation
|
|
88
|
-
</script>
|
|
89
|
-
|
|
90
|
-
<svg viewBox="0 0 {phased.width} {phased.height}" class="flex chart">
|
|
91
|
-
<Grid />
|
|
92
|
-
<Axis orient="bottom" />
|
|
93
|
-
<Axis orient="left" />
|
|
94
|
-
<BoxPlot />
|
|
95
|
-
<ViolinPlot />
|
|
96
|
-
<ScatterPlot jitterWidth={50} offset={50} />
|
|
97
|
-
</svg>
|
package/src/chart/Timer.svelte
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { createEventDispatcher } from 'svelte'
|
|
3
|
-
import { elapsed, timer } from '../lib/timer.js'
|
|
4
|
-
|
|
5
|
-
const dispatch = createEventDispatcher()
|
|
6
|
-
|
|
7
|
-
export let isEnabled = false
|
|
8
|
-
export let currentKeyframe = 0
|
|
9
|
-
export let keyframeCount = 0
|
|
10
|
-
export let duration = 1000
|
|
11
|
-
|
|
12
|
-
$: if (isEnabled) currentKeyframe = Math.floor($elapsed / duration)
|
|
13
|
-
$: if (currentKeyframe === keyframeCount) dispatch('end')
|
|
14
|
-
$: isEnabled && currentKeyframe < keyframeCount ? timer.start() : timer.stop()
|
|
15
|
-
|
|
16
|
-
function onReset() {
|
|
17
|
-
timer.reset()
|
|
18
|
-
isEnabled = false
|
|
19
|
-
currentKeyframe = 0
|
|
20
|
-
}
|
|
21
|
-
</script>
|
|
22
|
-
|
|
23
|
-
<div class="flex flex-row gap-2 timer">
|
|
24
|
-
<button on:click|preventDefault={() => (isEnabled = true)}>play</button>
|
|
25
|
-
<button on:click|preventDefault={() => (isEnabled = false)}>pause</button>
|
|
26
|
-
<button on:click|preventDefault={onReset}>reset</button>
|
|
27
|
-
</div>
|
package/src/chart.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { getContext } from 'svelte'
|
|
3
|
-
export let orient = 'bottom'
|
|
4
|
-
|
|
5
|
-
let chart = getContext('chart')
|
|
6
|
-
|
|
7
|
-
let top =
|
|
8
|
-
orient === 'bottom'
|
|
9
|
-
? $chart.height - $chart.margin.bottom
|
|
10
|
-
: $chart.margin.top
|
|
11
|
-
let left =
|
|
12
|
-
orient === 'right' ? $chart.width - $chart.margin.right : $chart.margin.left
|
|
13
|
-
let tickSizeInner = $chart.theme.tick.size.inner || 6
|
|
14
|
-
let tickSizeOuter = $chart.theme.tick.size.outer || 6
|
|
15
|
-
let tickPadding = $chart.theme.tick.size.padding || 3
|
|
16
|
-
|
|
17
|
-
function axisPath(vertical, scale) {
|
|
18
|
-
const range = scale.range()
|
|
19
|
-
return vertical
|
|
20
|
-
? `M${k * tickSizeOuter},${range[0]}H0V${range[1]}H${k * tickSizeOuter}`
|
|
21
|
-
: `M${range[0]},${k * tickSizeOuter}V0H${range[1]}V${k * tickSizeOuter}`
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
$: anchor =
|
|
25
|
-
orient === 'right' ? 'start' : orient === 'left' ? 'end' : 'middle'
|
|
26
|
-
$: k = orient === 'top' || orient === 'left' ? -1 : 1
|
|
27
|
-
$: dy = orient === 'top' ? '0em' : orient === 'bottom' ? '0.71em' : '0.32em'
|
|
28
|
-
$: vertical = orient === 'left' || orient === 'right'
|
|
29
|
-
// $: range = axis.scale.range()
|
|
30
|
-
$: axis = vertical ? $chart.axis.y : $chart.axis.x
|
|
31
|
-
</script>
|
|
32
|
-
|
|
33
|
-
<g
|
|
34
|
-
transform="translate({vertical ? left : 0},{vertical ? 0 : top})"
|
|
35
|
-
fill="none"
|
|
36
|
-
font-size="10"
|
|
37
|
-
font-family="sans-serif"
|
|
38
|
-
text-anchor={anchor}
|
|
39
|
-
class="axis"
|
|
40
|
-
>
|
|
41
|
-
<path
|
|
42
|
-
class="domain"
|
|
43
|
-
stroke="currentColor"
|
|
44
|
-
d="{axisPath(vertical, axis.scale)}}"
|
|
45
|
-
/>
|
|
46
|
-
{#each axis.ticks as tick}
|
|
47
|
-
<g
|
|
48
|
-
class="tick"
|
|
49
|
-
transform="translate({vertical ? 0 : tick.position},{vertical
|
|
50
|
-
? tick.position
|
|
51
|
-
: 0})"
|
|
52
|
-
>
|
|
53
|
-
<line
|
|
54
|
-
stroke="currentColor"
|
|
55
|
-
y2={vertical ? 0 : k * tickSizeInner}
|
|
56
|
-
x2={vertical ? k * tickSizeInner : 0}
|
|
57
|
-
/>
|
|
58
|
-
<text
|
|
59
|
-
fill="currentColor"
|
|
60
|
-
y={vertical ? 0 : k * (tickSizeInner + tickPadding)}
|
|
61
|
-
x={vertical ? k * (tickSizeInner + tickPadding) : 0}
|
|
62
|
-
{dy}>{tick.label}</text
|
|
63
|
-
>
|
|
64
|
-
</g>
|
|
65
|
-
{/each}
|
|
66
|
-
</g>
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
import { setContext } from 'svelte'
|
|
3
|
-
import { writable } from 'svelte/store'
|
|
4
|
-
import { compact } from '../lib/utils'
|
|
5
|
-
import { builtIn } from '../lib/theme'
|
|
6
|
-
|
|
7
|
-
let chart = writable({})
|
|
8
|
-
|
|
9
|
-
setContext('chart', chart)
|
|
10
|
-
|
|
11
|
-
export let width = 800
|
|
12
|
-
export let height = 450
|
|
13
|
-
export let data
|
|
14
|
-
export let theme = builtIn
|
|
15
|
-
export let x
|
|
16
|
-
export let y
|
|
17
|
-
export let fill
|
|
18
|
-
export let color
|
|
19
|
-
export let padding = 20
|
|
20
|
-
export let curve = 'basis'
|
|
21
|
-
export let stat = 'identity'
|
|
22
|
-
|
|
23
|
-
$: aes = compact({ x, y, fill, color, stat, curve, padding })
|
|
24
|
-
$: chart.set({
|
|
25
|
-
width,
|
|
26
|
-
height,
|
|
27
|
-
data,
|
|
28
|
-
theme,
|
|
29
|
-
aes,
|
|
30
|
-
})
|
|
31
|
-
</script>
|
|
32
|
-
|
|
33
|
-
<svg viewBox="0 0 {width} {height}" class="chart" width="100%">
|
|
34
|
-
<slot />
|
|
35
|
-
</svg>
|
package/src/components/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
export { default as ColorRamp } from './elements/ColorRamp.svelte'
|
|
2
|
-
|
|
3
|
-
export { default as Symbol } from './chart/Symbol.svelte'
|
|
4
|
-
export { default as PatternDefs } from './chart/PatternDefs.svelte'
|
|
5
|
-
export { default as Swatch } from './chart/Swatch.svelte'
|
|
6
|
-
export { default as SwatchButton } from './chart/SwatchButton.svelte'
|
|
7
|
-
export { default as SwatchGrid } from './chart/SwatchGrid.svelte'
|
|
8
|
-
|
|
9
|
-
export { default as Axis } from './chart/Axis.svelte'
|
|
10
|
-
export { default as Grid } from './chart/Grid.svelte'
|
|
11
|
-
|
|
12
|
-
export { default as BoxPlot } from './plots/BoxPlot.svelte'
|
|
13
|
-
export { default as ViolinPlot } from '../plots/ViolinPlot.svelte'
|
|
14
|
-
export { default as ScatterPlot } from './plots/ScatterPlot.svelte'
|
|
15
|
-
export { default as FunnelPlot } from '../plots/FunnelPlot.svelte'
|
|
16
|
-
|
|
17
|
-
export { default as Chart } from './chart/Chart.svelte'
|
|
18
|
-
export { default as TimelapseChart } from './chart/TimelapseChart.svelte'
|
|
19
|
-
export { default as Timer } from './chart/Timer.svelte'
|
|
20
|
-
export { default as BarPlot } from './plots/BarPlot.svelte'
|
|
21
|
-
export { default as HeatMapCalendar } from '../plots/HeatMapCalendar.svelte'
|
|
22
|
-
export { toHexString, initCap, uniqueId, toNested } from './lib/utils'
|
|
23
|
-
export { brewer, uniques, slidingWindow, colors } from './lib/index'
|
|
File without changes
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import { nest } from 'd3-collection'
|
|
2
|
-
import { max, quantile, ascending, histogram } from 'd3-array'
|
|
3
|
-
import { scaleLinear } from 'd3-scale'
|
|
4
|
-
import { area, curveCatmullRom } from 'd3-shape'
|
|
5
|
-
import { getScale } from './utils'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* axis, theme, params, fields
|
|
9
|
-
*/
|
|
10
|
-
export class ChartBrewer {
|
|
11
|
-
constructor(data, x, y) {
|
|
12
|
-
this.data = data
|
|
13
|
-
this.x = x
|
|
14
|
-
this.y = y
|
|
15
|
-
this.fill = x
|
|
16
|
-
this.axis = null
|
|
17
|
-
this.stats = {}
|
|
18
|
-
// this.yOffset = 20
|
|
19
|
-
this.padding = 10
|
|
20
|
-
this.margin = {
|
|
21
|
-
left: 10,
|
|
22
|
-
top: 10,
|
|
23
|
-
right: 10,
|
|
24
|
-
bottom: 10
|
|
25
|
-
}
|
|
26
|
-
this.params = {
|
|
27
|
-
ticks: {}
|
|
28
|
-
}
|
|
29
|
-
this.labels = []
|
|
30
|
-
this.width = 800
|
|
31
|
-
this.height = (this.width * 7) / 16
|
|
32
|
-
this.scaleValues = null
|
|
33
|
-
this.theme = {}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
computeMargin(xAxisOrientation, yAxisOrientation) {
|
|
37
|
-
this.scaleValues = {
|
|
38
|
-
x: [...new Set(this.data.map((item) => item[this.x]))],
|
|
39
|
-
y: [...new Set(this.data.map((item) => item[this.y]))],
|
|
40
|
-
fill: [...new Set(this.data.map((item) => item[this.fill]))]
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
let xOffset =
|
|
44
|
-
max(this.scaleValues.y.map((value) => value.toString().length)) * 10
|
|
45
|
-
let yOffset = 20
|
|
46
|
-
|
|
47
|
-
this.margin = {
|
|
48
|
-
left: this.padding + (yAxisOrientation === 'left' ? xOffset : 0),
|
|
49
|
-
right: this.padding + (yAxisOrientation === 'left' ? 0 : xOffset),
|
|
50
|
-
top: this.padding + (xAxisOrientation === 'bottom' ? 0 : yOffset),
|
|
51
|
-
bottom: this.padding + (xAxisOrientation === 'bottom' ? yOffset : 0)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
computeAxis(buffer = 0, inverse = false) {
|
|
56
|
-
let x = {}
|
|
57
|
-
let y = {}
|
|
58
|
-
let fill = {}
|
|
59
|
-
|
|
60
|
-
if (!this.scaleValues) {
|
|
61
|
-
this.computeMargin('bottom', 'left')
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
x.scale = getScale(
|
|
65
|
-
this.scaleValues.x,
|
|
66
|
-
[this.margin.left, this.width - this.margin.right],
|
|
67
|
-
buffer
|
|
68
|
-
)
|
|
69
|
-
const domainY = inverse
|
|
70
|
-
? [this.margin.top, this.height - this.margin.bottom]
|
|
71
|
-
: [this.height - this.margin.bottom, this.margin.top]
|
|
72
|
-
y.scale = getScale(this.scaleValues.y, domainY, buffer)
|
|
73
|
-
|
|
74
|
-
x.ticks = tickValues(x.scale, 'x', this.params)
|
|
75
|
-
y.ticks = tickValues(y.scale, 'y', this.params)
|
|
76
|
-
|
|
77
|
-
this.axis = { x, y, fill }
|
|
78
|
-
return this
|
|
79
|
-
}
|
|
80
|
-
use(theme) {
|
|
81
|
-
this.theme = theme
|
|
82
|
-
return this
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
params(margin, ticks) {
|
|
86
|
-
this.margin = margin
|
|
87
|
-
this.ticks = ticks
|
|
88
|
-
return this
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
fillWith(fill) {
|
|
92
|
-
this.fill = fill
|
|
93
|
-
return this
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
highlight(values) {
|
|
97
|
-
this.highlight = values
|
|
98
|
-
return this
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
animate() {
|
|
102
|
-
return this
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
summary() {
|
|
106
|
-
const result = nest()
|
|
107
|
-
.key((d) => d[this.x])
|
|
108
|
-
.rollup((d) => {
|
|
109
|
-
let values = d.map((g) => g[this.y]).sort(ascending)
|
|
110
|
-
let q1 = quantile(values, 0.25)
|
|
111
|
-
let q3 = quantile(values, 0.75)
|
|
112
|
-
let median = quantile(values, 0.5)
|
|
113
|
-
let interQuantileRange = q3 - q1
|
|
114
|
-
let min = q1 - 1.5 * interQuantileRange
|
|
115
|
-
let max = q3 + 1.5 * interQuantileRange
|
|
116
|
-
return { q1, q3, median, interQuantileRange, min, max }
|
|
117
|
-
})
|
|
118
|
-
.entries(this.data)
|
|
119
|
-
return result
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// assumes axis has been computes
|
|
123
|
-
violin() {
|
|
124
|
-
if (!this.axis) this.computeAxis()
|
|
125
|
-
// Features of the histogram
|
|
126
|
-
var histogramBins = bin()
|
|
127
|
-
.domain(this.axis.y.scale.domain())
|
|
128
|
-
.thresholds(this.axis.y.scale.ticks(20)) // Important: how many bins approx are going to be made? It is the 'resolution' of the violin plot
|
|
129
|
-
.value((d) => d)
|
|
130
|
-
|
|
131
|
-
// Compute the binning for each group of the dataset
|
|
132
|
-
var sumstat = nest()
|
|
133
|
-
.key((d) => d[this.x])
|
|
134
|
-
.rollup((d) => histogramBins(d.map((g) => +g[this.y])))
|
|
135
|
-
.entries(this.data)
|
|
136
|
-
|
|
137
|
-
// console.log(sumstat)
|
|
138
|
-
// What is the biggest number of value in a bin? We need it cause this value will have a width of 100% of the bandwidth.
|
|
139
|
-
var maxNum = 0
|
|
140
|
-
for (let i in sumstat) {
|
|
141
|
-
let allBins = sumstat[i].value
|
|
142
|
-
let lengths = allBins.map((a) => a.length)
|
|
143
|
-
let longest = max(lengths)
|
|
144
|
-
if (longest > maxNum) {
|
|
145
|
-
maxNum = longest
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
// console.log(
|
|
149
|
-
// maxNum,
|
|
150
|
-
// this.axis.x.scale.bandwidth(),
|
|
151
|
-
// this.axis.x.scale('setosa')
|
|
152
|
-
// )
|
|
153
|
-
|
|
154
|
-
// The maximum width of a violin must be x.bandwidth = the width dedicated to a group
|
|
155
|
-
var xNum = scaleLinear()
|
|
156
|
-
.range([0, this.axis.x.scale.bandwidth()])
|
|
157
|
-
.domain([0, maxNum])
|
|
158
|
-
|
|
159
|
-
let result = area()
|
|
160
|
-
.x0(xNum(0))
|
|
161
|
-
.x1(function (d) {
|
|
162
|
-
return xNum(d.length)
|
|
163
|
-
})
|
|
164
|
-
.y((d) => this.axis.y.scale(d.x0))
|
|
165
|
-
.curve(curveCatmullRom)
|
|
166
|
-
|
|
167
|
-
let areas = sumstat.map((d) => ({
|
|
168
|
-
curve: result(d.value),
|
|
169
|
-
x: this.axis.x.scale(d.key)
|
|
170
|
-
}))
|
|
171
|
-
return areas
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
function tickValues(scale, whichAxis, params) {
|
|
176
|
-
let { values, count } =
|
|
177
|
-
whichAxis in params.ticks ? params.ticks[whichAxis] : {}
|
|
178
|
-
values =
|
|
179
|
-
Array.isArray(values) && values.length > 2
|
|
180
|
-
? values
|
|
181
|
-
: scale.ticks
|
|
182
|
-
? scale.ticks.apply(scale, [count])
|
|
183
|
-
: scale.domain()
|
|
184
|
-
const ticks = values.map((label) => ({ label, position: scale(label) }))
|
|
185
|
-
|
|
186
|
-
return ticks
|
|
187
|
-
}
|