PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.13
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.13
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / violin-plot / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.13, at blocks/violin-plot/index.js

264 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ( function () {
2 const el = window.wp.element.createElement;
3 const { registerBlockType } = window.wp.blocks;
4 const { InspectorControls, useBlockProps, PanelColorSettings } = window.wp.blockEditor;
5 const { PanelBody, RangeControl, ToggleControl, TextControl, TextareaControl, Button, ColorPicker, Popover } = window.wp.components;
6 const { __ } = window.wp.i18n;
7 const { useState } = window.wp.element;
8
9 // Resolve the shared components lazily without defining globals for each block.
10 function _tc(label, key, attrs, setA) {
11 var Control = window.bkbgTypographyControl;
12 return Control ? el(Control, {
13 label: label,
14 value: attrs[key] || {},
15 onChange: function (value) { var update = {}; update[key] = value; setA(update); }
16 }) : null;
17 }
18 function _tvf(vars, key, attrs, prefix) {
19 var cssVars = window.bkbgTypoCssVars;
20 if (cssVars) Object.assign(vars, cssVars(attrs[key] || {}, prefix));
21 return vars;
22 }
23 function getTypoControl(label, key, attrs, setA) { return _tc(label, key, attrs, setA); }
24 function getTypoCssVars(attrs) {
25 var v = {};
26 _tvf(v, 'titleTypo', attrs, '--bkvip-tt-');
27 return v;
28 }
29
30 // ── Stats helpers ─────────────────────────────────────────────────────────
31 function sorted( arr ) { return [ ...arr ].sort( ( a, b ) => a - b ); }
32 function quantile( s, q ) {
33 const pos = ( s.length - 1 ) * q;
34 const base = Math.floor( pos );
35 const rest = pos - base;
36 return s[ base + 1 ] !== undefined ? s[ base ] + rest * ( s[ base + 1 ] - s[ base ] ) : s[ base ];
37 }
38 // Kernel Density Estimation (Gaussian, bandwidth = Silverman's rule)
39 function kde( sortedVals, bandwidth, steps ) {
40 const mn = sortedVals[ 0 ];
41 const mx = sortedVals[ sortedVals.length - 1 ];
42 const result = [];
43 for ( let i = 0; i <= steps; i++ ) {
44 const x = mn + ( i / steps ) * ( mx - mn );
45 let density = 0;
46 sortedVals.forEach( v => {
47 const u = ( x - v ) / bandwidth;
48 density += Math.exp( -0.5 * u * u );
49 } );
50 density /= ( sortedVals.length * bandwidth * Math.sqrt( 2 * Math.PI ) );
51 result.push( { x, d: density } );
52 }
53 return result;
54 }
55 function bandwidth( n, std ) { return 1.06 * std * Math.pow( n, -0.2 ); }
56 function stddev( vals ) {
57 const m = vals.reduce( ( s, v ) => s + v, 0 ) / vals.length;
58 return Math.sqrt( vals.reduce( ( s, v ) => s + ( v - m ) ** 2, 0 ) / vals.length );
59 }
60
61 // ── Renderer ──────────────────────────────────────────────────────────────
62 function renderViolin( a ) {
63 const groups = a.groups || [];
64 const W = a.svgWidth;
65 const H = a.svgHeight;
66 const PT = a.padTop;
67 const PL = a.padLeft;
68 const PR = a.padRight;
69 const PB = a.padBottom;
70 const chartW = W - PL - PR;
71 const chartH = H - PT - PB;
72 const VW = Math.min( a.violinWidth, ( chartW / groups.length ) * 0.8 );
73 const KDE_STEPS = 40;
74 const opacity = ( a.fillOpacity || 75 ) / 100;
75
76 if ( ! groups.length ) return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%' } );
77
78 // Global Y range across all groups
79 const allVals = groups.flatMap( g => g.values || [] );
80 if ( ! allVals.length ) return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%' } );
81 const globalMin = Math.min( ...allVals );
82 const globalMax = Math.max( ...allVals );
83 const yRange = ( globalMax - globalMin ) || 1;
84 const yPad = yRange * 0.05;
85 function sy( v ) { return PT + chartH - ( ( v - ( globalMin - yPad ) ) / ( yRange + yPad * 2 ) ) * chartH; }
86
87 // Step width per group
88 const step = chartW / groups.length;
89 function cx( gi ) { return PL + gi * step + step / 2; }
90
91 const svgEls = [];
92 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
93
94 // Grid + Y axis ticks
95 if ( a.showGrid ) {
96 const ticks = 5;
97 for ( let t = 0; t <= ticks; t++ ) {
98 const gy = PT + ( t / ticks ) * chartH;
99 const yV = Math.round( ( globalMin - yPad ) + ( 1 - t / ticks ) * ( yRange + yPad * 2 ) );
100 svgEls.push( el( 'line', { key: `gl${ t }`, x1: PL, y1: gy, x2: W - PR, y2: gy, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) );
101 svgEls.push( el( 'text', { key: `gy${ t }`, x: PL - 6, y: gy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, yV ) );
102 }
103 }
104
105 // Y axis label
106 svgEls.push( el( 'text', { key: 'ylabel', x: 14, y: PT + chartH / 2, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit', transform: `rotate(-90, 14, ${ PT + chartH / 2 })` }, a.yAxisLabel ) );
107
108 // Violins
109 groups.forEach( ( g, gi ) => {
110 const vals = g.values || [];
111 if ( ! vals.length ) return;
112 const s = sorted( vals );
113 const std = stddev( s );
114 const bw = bandwidth( s.length, std || 1 );
115 const kde_ = kde( s, bw, KDE_STEPS );
116 const clr = g.color || '#6366f1';
117
118 // Max density → half-width mapping
119 const maxD = Math.max( ...kde_.map( p => p.d ) ) || 1;
120 function dx( d ) { return ( d / maxD ) * ( VW / 2 ); }
121
122 // Build polygon: right side top→bottom, then left side bottom→top
123 const rightPts = kde_.map( p => `${ cx( gi ) + dx( p.d ) },${ sy( p.x ) }` );
124 const leftPts = [ ...kde_ ].reverse().map( p => `${ cx( gi ) - dx( p.d ) },${ sy( p.x ) }` );
125 const polyPts = [ ...rightPts, ...leftPts ].join( ' ' );
126
127 svgEls.push( el( 'polygon', { key: `vio${ gi }`, points: polyPts, fill: clr, fillOpacity: opacity, stroke: clr, strokeWidth: 1.5 } ) );
128
129 // IQR box
130 if ( a.showIQR ) {
131 const q1 = quantile( s, 0.25 );
132 const q3 = quantile( s, 0.75 );
133 const iqrH = sy( q1 ) - sy( q3 );
134 svgEls.push( el( 'rect', { key: `iqr${ gi }`, x: cx( gi ) - VW * 0.12, y: sy( q3 ), width: VW * 0.24, height: Math.max( 2, iqrH ), fill: '#ffffff', fillOpacity: 0.85, stroke: clr, strokeWidth: 1.5, rx: 2 } ) );
135 }
136
137 // Median line
138 if ( a.showMedian ) {
139 const med = quantile( s, 0.5 );
140 svgEls.push( el( 'line', { key: `med${ gi }`, x1: cx( gi ) - VW * 0.18, y1: sy( med ), x2: cx( gi ) + VW * 0.18, y2: sy( med ), stroke: clr, strokeWidth: 2.5 } ) );
141 svgEls.push( el( 'circle', { key: `medpt${ gi }`, cx: cx( gi ), cy: sy( med ), r: 3.5, fill: '#fff', stroke: clr, strokeWidth: 2 } ) );
142 }
143
144 // X label
145 svgEls.push( el( 'text', { key: `xl${ gi }`, x: cx( gi ), y: H - PB + 18, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit', fontWeight: 500 }, g.label ) );
146 } );
147
148 // Axes
149 svgEls.push( el( 'line', { key: 'xax', x1: PL, y1: PT + chartH, x2: W - PR, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.3 } ) );
150 svgEls.push( el( 'line', { key: 'yax', x1: PL, y1: PT, x2: PL, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.3 } ) );
151
152 // Legend
153 if ( a.showLegend ) {
154 const legY = H - PB + 36;
155 const legStep = Math.min( 140, chartW / groups.length );
156 groups.forEach( ( g, gi ) => {
157 const lx = PL + gi * legStep;
158 svgEls.push( el( 'rect', { key: `lgn${ gi }`, x: lx, y: legY - 7, width: 18, height: 10, fill: g.color || '#6366f1', rx: 2, fillOpacity: opacity } ) );
159 svgEls.push( el( 'text', { key: `lgt${ gi }`, x: lx + 22, y: legY, dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, g.label ) );
160 } );
161 }
162
163 return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls );
164 }
165
166 function updGrp( setAttributes, groups, gi, field, val ) {
167 setAttributes( { groups: groups.map( ( g, i ) => i === gi ? { ...g, [field]: val } : g ) } );
168 }
169
170 /* ── colour-swatch + popover ── */
171 function BkbgColorSwatch(p) {
172 var st = useState(false), open = st[0], setOpen = st[1];
173 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
174 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
175 el('div', { style:{ position:'relative', flexShrink:0 } },
176 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
177 style:{ width:'28px', height:'28px', borderRadius:'4px', border: open ? '2px solid #007cba' : '2px solid #ddd', cursor:'pointer', padding:0, display:'block', background: p.value||'#ffffff', flexShrink:0 } }),
178 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
179 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
180 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
181 el('strong', { style:{ fontSize:'12px' } }, p.label),
182 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
183 ),
184 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
185 )
186 )
187 )
188 );
189 }
190
191 registerBlockType( 'blockenberg/violin-plot', {
192 edit: function ( props ) {
193 const { attributes: a, setAttributes } = props;
194 const blockProps = useBlockProps( { className: 'bkbg-violin-wrap', style: getTypoCssVars( a ) } );
195
196 return el( 'div', blockProps,
197 el( InspectorControls, {},
198
199 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
200 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
201 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
202 el( ToggleControl, { label: __( 'Show Grid', 'blockenberg' ), checked: a.showGrid, onChange: v => setAttributes( { showGrid: v } ) } ),
203 el( ToggleControl, { label: __( 'Show Median', 'blockenberg' ), checked: a.showMedian, onChange: v => setAttributes( { showMedian: v } ) } ),
204 el( ToggleControl, { label: __( 'Show IQR Box', 'blockenberg' ), checked: a.showIQR, onChange: v => setAttributes( { showIQR: v } ) } ),
205 el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ),
206 el( RangeControl, { label: __( 'Fill Opacity %', 'blockenberg' ), value: a.fillOpacity, onChange: v => setAttributes( { fillOpacity: v } ), min: 10, max: 100 } ),
207 el( TextControl, { label: __( 'Y Axis Label', 'blockenberg' ), value: a.yAxisLabel, onChange: v => setAttributes( { yAxisLabel: v } ) } ),
208 ),
209
210 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
211 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ),
212 el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 200, max: 700 } ),
213 el( RangeControl, { label: __( 'Violin Width', 'blockenberg' ), value: a.violinWidth, onChange: v => setAttributes( { violinWidth: v } ), min: 20, max: 160 } ),
214 el( RangeControl, { label: __( 'Left Padding', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 20, max: 120 } ),
215 ),
216
217
218 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
219 getTypoControl( __( 'Title', 'blockenberg' ), 'titleTypo', a, setAttributes ),
220 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 20 } )
221 ),
222 el( PanelColorSettings, {
223 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
224 colorSettings: [
225 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
226 { label: __( 'Grid', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) },
227 { label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
228 { label: __( 'Text/Axes', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
229 ]
230 } ),
231
232 el( PanelBody, { title: __( 'Groups', 'blockenberg' ), initialOpen: false },
233 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { groups: [ ...a.groups, { label: 'New Group', color: '#8b5cf6', values: [ 40, 55, 65, 70, 75, 80, 85 ] } ] } ) }, __( '+ Add Group', 'blockenberg' ) ),
234 a.groups.map( ( g, gi ) =>
235 el( PanelBody, { key: gi, title: g.label || `Group ${ gi + 1 }`, initialOpen: false },
236 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: g.label, onChange: v => updGrp( setAttributes, a.groups, gi, 'label', v ) } ),
237 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: g.color, onChange: v => updGrp( setAttributes, a.groups, gi, 'color', v ) } ),
238 el( TextareaControl, {
239 label: __( 'Values (comma-separated)', 'blockenberg' ),
240 value: ( g.values || [] ).join( ', ' ),
241 onChange: v => updGrp( setAttributes, a.groups, gi, 'values', v.split( ',' ).map( n => parseFloat( n.trim() ) ).filter( n => ! isNaN( n ) ) ),
242 rows: 4,
243 } ),
244 el( Button, { isDestructive: true, isSmall: true, style: { marginTop: 6 }, onClick: () => setAttributes( { groups: a.groups.filter( ( _, x ) => x !== gi ) } ) }, __( 'Remove', 'blockenberg' ) ),
245 )
246 ),
247 ),
248 ),
249
250 a.showTitle && a.title && el( 'h3', { className: 'bkbg-violin-title', style: { color: a.titleColor } }, a.title ),
251 el( 'div', { className: 'bkbg-violin-svg' }, renderViolin( a ) ),
252 );
253 },
254
255 save: function ( { attributes: a } ) {
256 const blockProps = useBlockProps.save( { className: 'bkbg-violin-wrap', style: getTypoCssVars( a ) } );
257 return el( 'div', blockProps,
258 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-violin-title', style: { color: a.titleColor } }, a.title ) : null,
259 el( 'div', { className: 'bkbg-violin-svg' }, renderViolin( a ) ),
260 );
261 },
262 } );
263 }() );
264