Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 6,178 Bytes
63858e7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
import {VComponent} from './VisComponent'
import {spacyColors} from '../etc/SpacyInfo'
import {SVG} from '../etc/SVGplus'
import * as d3 from 'd3'
import * as R from 'ramda'
import { D3Sel } from '../etc/Util';
import { SimpleEventHandler } from '../etc/SimpleEventHandler';
interface MarginInfo {
top: number,
bottom: number,
right: number,
left: number
}
// Dependent on the options in the response
type MatchedMetaSelections = "pos" | "dep" | "ent"
interface MatchedMetaCount {
pos: number
dep: number
is_ent: number
}
interface MaxAttMetaCount {
offset: number
}
type MatchedDataInterface = MatchedMetaCount
type MaxAttDataInterface = MaxAttMetaCount
type DataInterface = MatchedDataInterface | MaxAttDataInterface
interface CountedHist {
label: string,
count: number
}
type RenderDataInterface = CountedHist[]
/**
* Data formatting functions
*/
const toRenderData = (obj: {[s: string]: number}): RenderDataInterface => Object.keys(obj).map((k, i) => {
return {label: k, count: obj[k]}
})
const toStringOrNum = (a:string) => {
const na = +a
if (isNaN(na)) {
return a
}
return na
}
const sortByLabel = R.sortBy(R.compose(toStringOrNum, R.prop('label')))
const sortByCount = R.sortBy(R.prop('count'))
const toOrderedRender = R.compose(
R.reverse,
// @ts-ignore -- TODO: fix
sortByCount,
toRenderData
)
export class CorpusHistogram<T> extends VComponent<T> {
css_name = ''
static events = {}
_current = {
chart: {
height: null,
width: null
}
}
// D3 COMPONENTS
svg: D3Sel
options: {
margin: MarginInfo
barWidth: number
width: number
height: number
val: string
xLabelRot: number
xLabelOffset: number
yLabelOffset: number
}
axes = {
x: d3.scaleBand(),
y: d3.scaleLinear(),
}
constructor(d3parent: D3Sel, eventHandler?: SimpleEventHandler, options={}) {
super(d3parent, eventHandler)
this.options = {
margin: {
top: 10,
right: 30,
bottom: 50,
left: 40
},
barWidth: 25,
width: 185,
height: 230,
val: "pos", // Change Default, pass through constructor
xLabelRot: 45,
xLabelOffset: 15,
yLabelOffset: 5,
}
this.superInitSVG()
}
meta():MatchedMetaSelections
meta(val:MatchedMetaSelections): this
meta(val?) {
if (val == null) {
return this.options.val;
}
this.options.val = val;
this.update(this._data)
return this;
}
_init() {}
private createXAxis() {
const self = this;
const op = this.options;
const width = op.width - op.margin.left - op.margin.right
this.axes.x
.domain(R.map(R.prop('label'), self.renderData))
.rangeRound([0, width])
.padding(0.1)
this._current.chart.width = width;
}
private createYAxis() {
const self = this;
const op = this.options;
const height = op.height - op.margin.top - op.margin.bottom
this.axes.y
.domain([0, +d3.max(R.map(R.prop('count'), self.renderData))])
.rangeRound([height, 0])
this._current.chart.height = height;
}
private createAxes() {
this.createXAxis()
this.createYAxis()
}
_wrangle(data: DataInterface) {
const out = data[this.options.val]
return toOrderedRender(out)
}
width():number
width(val:number):this
width(val?) {
if (val == null) {
return this.options.width;
}
this.options.width = val;
this.updateWidth();
this.createXAxis();
return this;
}
height():number
height(val:number):this
height(val?) {
if (val == null) {
return this.options.height;
}
this.options.height = val;
this.updateHeight();
this.createYAxis();
return this;
}
private updateWidth() {
this.svg.attr('width', this.options.width)
}
private updateHeight() {
this.svg.attr('height', this.options.height)
}
private figWidth(data: RenderDataInterface) {
const op = this.options;
return (data.length * op.barWidth) + op.margin.left + op.margin.right
}
_render(data:RenderDataInterface) {
const self = this;
const op = this.options;
const curr = this._current;
this.parent.html('')
this.svg = this.parent
this.createAxes();
this.width(this.figWidth(data));
this.updateHeight();
// Initialize axes
const g = self.svg.append("g")
.attr("transform", SVG.translate({x: op.margin.left, y:op.margin.top}))
// Hack to allow clearing this histograms to work
self.base = g
// Fix below for positional changing
const axisBottom = g.append("g")
.attr("transform", SVG.translate({x: 0, y:curr.chart.height}))
.call(d3.axisBottom(self.axes.x))
if (op.val != "offset") {
axisBottom
.selectAll("text")
.attr("y", op.yLabelOffset) // Move below the axis
.attr("x", op.xLabelOffset) // Offset to the right a bit
.attr("transform", SVG.rotate(op.xLabelRot))
}
g.append("g")
.call(d3.axisLeft(self.axes.y))
g.selectAll(".bar")
.data(data)
.join('rect')
.attr("class", "bar")
.attr("x", function(d) { return self.axes.x(d.label); })
.attr("y", function(d) { return self.axes.y(d.count); })
.attr("width", self.axes.x.bandwidth())
.attr("height", function(d) { return curr.chart.height - self.axes.y(d.count); })
.style('fill', k => spacyColors.colorScale[op.val](k.label))
}
}
|