SqPlot.js (6156B)
1 import { SampleSetDist } from "../../dist/SampleSetDist/index.js"; 2 import * as Result from "../../utility/result.js"; 3 import { vPlot } from "../../value/index.js"; 4 import { SqOtherError } from "../SqError.js"; 5 import { SqSampleSetDistribution, wrapDistribution, } from "./SqDistribution/index.js"; 6 import { SqLambda } from "./SqLambda.js"; 7 import { wrapScale } from "./SqScale.js"; 8 import { SqPlotValue } from "./index.js"; 9 export function wrapPlot(value, context) { 10 switch (value.type) { 11 case "distributions": 12 return new SqDistributionsPlot(value, context); 13 case "numericFn": 14 return new SqNumericFnPlot(value, context); 15 case "distFn": 16 return new SqDistFnPlot(value, context); 17 case "scatter": 18 return new SqScatterPlot(value, context); 19 case "relativeValues": 20 return new SqRelativeValuesPlot(value, context); 21 } 22 } 23 class SqAbstractPlot { 24 constructor(_value, context) { 25 this._value = _value; 26 this.context = context; 27 } 28 toString() { 29 return vPlot(this._value).toString(); 30 } 31 get asValue() { 32 return new SqPlotValue(vPlot(this._value), this.context); 33 } 34 get title() { 35 return this._value.title; 36 } 37 } 38 export class SqDistributionsPlot extends SqAbstractPlot { 39 constructor() { 40 super(...arguments); 41 this.tag = "distributions"; 42 } 43 static create({ distribution, xScale, yScale, showSummary, title, }) { 44 return new SqDistributionsPlot({ 45 type: "distributions", 46 distributions: [{ distribution: distribution._value }], 47 xScale: xScale._value, 48 yScale: yScale._value, 49 showSummary, 50 title, 51 }); 52 } 53 get distributions() { 54 return this._value.distributions.map(({ name, distribution }) => ({ 55 name, 56 distribution: wrapDistribution(distribution), 57 })); 58 } 59 get showSummary() { 60 return this._value.showSummary; 61 } 62 get xScale() { 63 return wrapScale(this._value.xScale); 64 } 65 get yScale() { 66 return wrapScale(this._value.yScale); 67 } 68 } 69 export class SqNumericFnPlot extends SqAbstractPlot { 70 constructor() { 71 super(...arguments); 72 this.tag = "numericFn"; 73 this.createdProgrammatically = false; 74 } 75 static create({ fn, xScale, yScale, points, title, }) { 76 const result = new SqNumericFnPlot({ 77 type: "numericFn", 78 fn: fn._value, 79 xScale: xScale._value, 80 yScale: yScale._value, 81 title: title, 82 points, 83 }, fn.context); 84 result.createdProgrammatically = true; 85 return result; 86 } 87 get fn() { 88 return new SqLambda(this._value.fn, this.context 89 ? this.createdProgrammatically 90 ? this.context 91 : this.context.extend({ type: "string", value: "fn" }) 92 : undefined); 93 } 94 get xScale() { 95 return wrapScale(this._value.xScale); 96 } 97 get yScale() { 98 return wrapScale(this._value.yScale); 99 } 100 get points() { 101 return this._value.points; 102 } 103 toString() { 104 return this.fn.toString(); 105 } 106 } 107 export class SqDistFnPlot extends SqAbstractPlot { 108 constructor() { 109 super(...arguments); 110 this.tag = "distFn"; 111 this.createdProgrammatically = false; 112 } 113 static create({ fn, xScale, yScale, distXScale, title, points, }) { 114 const result = new SqDistFnPlot({ 115 type: "distFn", 116 fn: fn._value, 117 xScale: xScale._value, 118 yScale: yScale._value, 119 distXScale: distXScale._value, 120 title: title, 121 points, 122 }, fn.context); 123 result.createdProgrammatically = true; 124 return result; 125 } 126 get fn() { 127 return new SqLambda(this._value.fn, this.context 128 ? this.createdProgrammatically 129 ? this.context 130 : this.context.extend({ type: "string", value: "fn" }) 131 : undefined); 132 } 133 get xScale() { 134 return wrapScale(this._value.xScale); 135 } 136 get yScale() { 137 return wrapScale(this._value.yScale); 138 } 139 get distXScale() { 140 return wrapScale(this._value.distXScale); 141 } 142 get points() { 143 return this._value.points; 144 } 145 toString() { 146 return this.fn.toString(); 147 } 148 } 149 export class SqScatterPlot extends SqAbstractPlot { 150 constructor() { 151 super(...arguments); 152 this.tag = "scatter"; 153 } 154 buildSampleSetDist(dist, env) { 155 const sampleSetResult = SampleSetDist.fromDist(dist, env); 156 if (!sampleSetResult.ok) { 157 return Result.Err(new SqOtherError("Conversion to SampleSet failed")); 158 } 159 return Result.Ok(new SqSampleSetDistribution(sampleSetResult.value)); 160 } 161 xDist(env) { 162 return this.buildSampleSetDist(this._value.xDist, env); 163 } 164 yDist(env) { 165 return this.buildSampleSetDist(this._value.yDist, env); 166 } 167 get xScale() { 168 const scale = this._value.xScale; 169 return scale ? wrapScale(scale) : undefined; 170 } 171 get yScale() { 172 const scale = this._value.yScale; 173 return scale ? wrapScale(scale) : undefined; 174 } 175 static zipToPoints(xDist, yDist) { 176 const xSamples = xDist.getSamples(); 177 const ySamples = yDist.getSamples(); 178 if (xSamples.length !== ySamples.length) { 179 throw new Error("Sample count mismatch"); 180 } 181 const points = []; 182 for (let i = 0; i < xSamples.length; i++) { 183 points.push({ x: xSamples[i], y: ySamples[i] }); 184 } 185 return points; 186 } 187 } 188 export class SqRelativeValuesPlot extends SqAbstractPlot { 189 constructor() { 190 super(...arguments); 191 this.tag = "relativeValues"; 192 } 193 get ids() { 194 return this._value.ids; 195 } 196 get fn() { 197 return new SqLambda(this._value.fn, this.context 198 ? this.context.extend({ type: "string", value: "fn" }) 199 : undefined); 200 } 201 } 202 //# sourceMappingURL=SqPlot.js.map