PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.11
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.11
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.11, at blocks/violin-plot/index.js

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