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 / box-plot / index.js

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

254 lines 18.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, Button, ColorPicker, Popover } = window.wp.components;
6 const { __ } = window.wp.i18n;
7 const { useState } = window.wp.element;
8
9 function getTypographyControl() {
10 return (window.bkbgTypographyControl || function () { return null; });
11 }
12
13 function _tv(typo, prefix) {
14 if (!typo) return {};
15 var s = {};
16 if (typo.family) s[prefix + 'font-family'] = "'" + typo.family + "', sans-serif";
17 if (typo.weight) s[prefix + 'font-weight'] = typo.weight;
18 if (typo.transform) s[prefix + 'text-transform'] = typo.transform;
19 if (typo.style) s[prefix + 'font-style'] = typo.style;
20 if (typo.decoration) s[prefix + 'text-decoration'] = typo.decoration;
21 var su = typo.sizeUnit || 'px';
22 if (typo.sizeDesktop !== '' && typo.sizeDesktop != null) s[prefix + 'font-size-d'] = typo.sizeDesktop + su;
23 if (typo.sizeTablet !== '' && typo.sizeTablet != null) s[prefix + 'font-size-t'] = typo.sizeTablet + su;
24 if (typo.sizeMobile !== '' && typo.sizeMobile != null) s[prefix + 'font-size-m'] = typo.sizeMobile + su;
25 var lhu = typo.lineHeightUnit || '';
26 if (typo.lineHeightDesktop !== '' && typo.lineHeightDesktop != null) s[prefix + 'line-height-d'] = typo.lineHeightDesktop + lhu;
27 if (typo.lineHeightTablet !== '' && typo.lineHeightTablet != null) s[prefix + 'line-height-t'] = typo.lineHeightTablet + lhu;
28 if (typo.lineHeightMobile !== '' && typo.lineHeightMobile != null) s[prefix + 'line-height-m'] = typo.lineHeightMobile + lhu;
29 var lsu = typo.letterSpacingUnit || 'px';
30 if (typo.letterSpacingDesktop !== '' && typo.letterSpacingDesktop != null) s[prefix + 'letter-spacing-d'] = typo.letterSpacingDesktop + lsu;
31 if (typo.letterSpacingTablet !== '' && typo.letterSpacingTablet != null) s[prefix + 'letter-spacing-t'] = typo.letterSpacingTablet + lsu;
32 if (typo.letterSpacingMobile !== '' && typo.letterSpacingMobile != null) s[prefix + 'letter-spacing-m'] = typo.letterSpacingMobile + lsu;
33 var wsu = typo.wordSpacingUnit || 'px';
34 if (typo.wordSpacingDesktop !== '' && typo.wordSpacingDesktop != null) s[prefix + 'word-spacing-d'] = typo.wordSpacingDesktop + wsu;
35 if (typo.wordSpacingTablet !== '' && typo.wordSpacingTablet != null) s[prefix + 'word-spacing-t'] = typo.wordSpacingTablet + wsu;
36 if (typo.wordSpacingMobile !== '' && typo.wordSpacingMobile != null) s[prefix + 'word-spacing-m'] = typo.wordSpacingMobile + wsu;
37 return s;
38 }
39
40 // ── Renderer ──────────────────────────────────────────────────────────────
41 function renderBoxPlot( a ) {
42 const series = a.series || [];
43 const W = a.svgWidth;
44 const H = a.svgHeight;
45 const PT = a.padTop;
46 const PL = a.padLeft;
47 const PR = a.padRight;
48 const PB = a.padBottom;
49 const BW = a.boxWidth;
50 const WW = a.whiskerWidth;
51
52 const allVals = series.flatMap( s => [
53 s.min, s.q1, s.median, s.mean, s.q3, s.max,
54 ...( s.outliers || [] )
55 ].filter( v => v !== undefined ) );
56 const minVal = Math.min( ...allVals );
57 const maxVal = Math.max( ...allVals );
58 const range = maxVal - minVal || 1;
59 const chartH = H - PT - PB;
60 const chartW = W - PL - PR;
61
62 function yAt( v ) { return H - PB - ( ( v - minVal ) / range ) * chartH; }
63 function xAt( i ) { return PL + ( i + 0.5 ) * ( chartW / series.length ); }
64
65 const svgEls = [];
66 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
67
68 // Horizontal grid lines
69 if ( a.showGridLines ) {
70 const ticks = 5;
71 for ( let i = 0; i <= ticks; i++ ) {
72 const v = minVal + range * i / ticks;
73 const gy = yAt( v );
74 svgEls.push( el( 'line', { key: `gy${ i }`, x1: PL, y1: gy, x2: W - PR, y2: gy, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) );
75 svgEls.push( el( 'text', { key: `gyt${ i }`, x: PL - 6, y: gy, textAnchor: 'end', dominantBaseline: 'middle', fill: '#9ca3af', fontSize: a.valueFontSize, fontFamily: 'inherit' }, String( Math.round( v ) ) ) );
76 }
77 }
78
79 // Axes
80 svgEls.push( el( 'line', { key: 'axX', x1: PL, y1: H - PB, x2: W - PR, y2: H - PB, stroke: '#d1d5db', strokeWidth: 1 } ) );
81 svgEls.push( el( 'line', { key: 'axY', x1: PL, y1: PT, x2: PL, y2: H - PB, stroke: '#d1d5db', strokeWidth: 1 } ) );
82
83 // Boxes
84 series.forEach( ( s, i ) => {
85 const cx = xAt( i );
86 const clr = s.color || '#6366f1';
87 const yQ1 = yAt( s.q1 );
88 const yQ3 = yAt( s.q3 );
89 const yMed = yAt( s.median );
90 const yMin = yAt( s.min );
91 const yMax = yAt( s.max );
92 const yMean = a.showMean ? yAt( s.mean ) : null;
93 const boxX = cx - BW / 2;
94
95 // Whisker: min to Q1
96 svgEls.push( el( 'line', { key: `wlo${ i }`, x1: cx, y1: yMin, x2: cx, y2: yQ1, stroke: clr, strokeWidth: 1.5, strokeDasharray: '4 2' } ) );
97 // Whisker cap min
98 svgEls.push( el( 'line', { key: `wlc${ i }`, x1: cx - WW / 2, y1: yMin, x2: cx + WW / 2, y2: yMin, stroke: clr, strokeWidth: 2 } ) );
99 // Whisker: Q3 to max
100 svgEls.push( el( 'line', { key: `wup${ i }`, x1: cx, y1: yQ3, x2: cx, y2: yMax, stroke: clr, strokeWidth: 1.5, strokeDasharray: '4 2' } ) );
101 // Whisker cap max
102 svgEls.push( el( 'line', { key: `wuc${ i }`, x1: cx - WW / 2, y1: yMax, x2: cx + WW / 2, y2: yMax, stroke: clr, strokeWidth: 2 } ) );
103
104 // IQR box
105 svgEls.push( el( 'rect', { key: `box${ i }`, x: boxX, y: yQ3, width: BW, height: Math.abs( yQ1 - yQ3 ), fill: clr, fillOpacity: 0.2, stroke: clr, strokeWidth: 2, rx: 3 } ) );
106 // Median line
107 svgEls.push( el( 'line', { key: `med${ i }`, x1: boxX, y1: yMed, x2: boxX + BW, y2: yMed, stroke: clr, strokeWidth: 3 } ) );
108
109 // Mean diamond
110 if ( yMean !== null ) {
111 const dm = 5;
112 svgEls.push( el( 'polygon', { key: `mean${ i }`, points: `${ cx },${ yMean - dm } ${ cx + dm },${ yMean } ${ cx },${ yMean + dm } ${ cx - dm },${ yMean }`, fill: '#ffffff', stroke: clr, strokeWidth: 2 } ) );
113 }
114
115 // Outliers
116 if ( a.showOutliers ) {
117 ( s.outliers || [] ).forEach( ( ov, oi ) => {
118 svgEls.push( el( 'circle', { key: `out${ i }${ oi }`, cx, cy: yAt( ov ), r: 4, fill: 'none', stroke: clr, strokeWidth: 2 } ) );
119 } );
120 }
121
122 // X label
123 svgEls.push( el( 'text', { key: `lbl${ i }`, x: cx, y: H - PB + 16, textAnchor: 'middle', fill: a.textColor || '#374151', fontSize: a.labelFontSize, fontFamily: 'inherit' }, s.label || '' ) );
124 } );
125
126 // Legend
127 if ( a.showLegend ) {
128 const items = [
129 { label: 'Median', shape: 'line' },
130 { label: 'Mean', shape: 'diamond' },
131 { label: 'IQR Box', shape: 'rect' },
132 { label: 'Outlier', shape: 'circle' },
133 ];
134 items.forEach( ( it, li ) => {
135 const lx = PL + li * 120;
136 const ly = PT - 18;
137 if ( it.shape === 'line' ) {
138 svgEls.push( el( 'line', { key: `leg${ li }`, x1: lx, y1: ly, x2: lx + 18, y2: ly, stroke: '#6b7280', strokeWidth: 3 } ) );
139 } else if ( it.shape === 'diamond' ) {
140 svgEls.push( el( 'polygon', { key: `leg${ li }`, points: `${ lx + 9 },${ ly - 5 } ${ lx + 14 },${ ly } ${ lx + 9 },${ ly + 5 } ${ lx + 4 },${ ly }`, fill: '#fff', stroke: '#6b7280', strokeWidth: 2 } ) );
141 } else if ( it.shape === 'rect' ) {
142 svgEls.push( el( 'rect', { key: `leg${ li }`, x: lx, y: ly - 5, width: 18, height: 10, fill: '#6b728033', stroke: '#6b7280', strokeWidth: 2, rx: 2 } ) );
143 } else {
144 svgEls.push( el( 'circle', { key: `leg${ li }`, cx: lx + 9, cy: ly, r: 4, fill: 'none', stroke: '#6b7280', strokeWidth: 2 } ) );
145 }
146 svgEls.push( el( 'text', { key: `legt${ li }`, x: lx + 22, y: ly, dominantBaseline: 'middle', fill: a.textColor, fontSize: 11, fontFamily: 'inherit' }, it.label ) );
147 } );
148 }
149
150 return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls );
151 }
152
153 function updSer( setAttributes, series, si, field, val ) {
154 setAttributes( { series: series.map( ( s, i ) => i === si ? { ...s, [field]: val } : s ) } );
155 }
156 function parseOutliers( raw ) {
157 return raw.split( /[\s,]+/ ).map( Number ).filter( v => !isNaN( v ) );
158 }
159
160 /* ── colour-swatch + popover ── */
161 function BkbgColorSwatch(p) {
162 var st = useState(false), open = st[0], setOpen = st[1];
163 return el('div', { style:{ display:'flex', alignItems:'center', justifyContent:'space-between', padding:'4px 0', gap:'8px' } },
164 el('span', { style:{ fontSize:'12px', color:'#1e1e1e', flex:1, lineHeight:1.4 } }, p.label),
165 el('div', { style:{ position:'relative', flexShrink:0 } },
166 el('button', { type:'button', title: p.value||'none', onClick: function(){ setOpen(!open); },
167 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 } }),
168 open && el(Popover, { position:'bottom left', onClose: function(){ setOpen(false); } },
169 el('div', { style:{ padding:'8px' }, onMouseDown: function(e){ e.stopPropagation(); } },
170 el('div', { style:{ display:'flex', justifyContent:'space-between', alignItems:'center', marginBottom:'6px' } },
171 el('strong', { style:{ fontSize:'12px' } }, p.label),
172 el(Button, { icon:'no-alt', isSmall:true, onClick: function(){ setOpen(false); } })
173 ),
174 el(ColorPicker, { color: p.value, enableAlpha:true, onChange: p.onChange })
175 )
176 )
177 )
178 );
179 }
180
181 registerBlockType( 'blockenberg/box-plot', {
182 edit: function ( props ) {
183 const { attributes: a, setAttributes } = props;
184 const blockProps = useBlockProps( { className: 'bkbg-boxplot-wrap', style: Object.assign({}, _tv(a.typoTitle, '--bkbg-boxplot-title-')) } );
185
186 return el( 'div', blockProps,
187 el( InspectorControls, {},
188
189 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
190 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
191 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
192 el( ToggleControl, { label: __( 'Show Grid Lines', 'blockenberg' ), checked: a.showGridLines, onChange: v => setAttributes( { showGridLines: v } ) } ),
193 el( ToggleControl, { label: __( 'Show Mean Diamond', 'blockenberg' ), checked: a.showMean, onChange: v => setAttributes( { showMean: v } ) } ),
194 el( ToggleControl, { label: __( 'Show Outliers', 'blockenberg' ), checked: a.showOutliers, onChange: v => setAttributes( { showOutliers: v } ) } ),
195 el( ToggleControl, { label: __( 'Show Legend', 'blockenberg' ), checked: a.showLegend, onChange: v => setAttributes( { showLegend: v } ) } ),
196 ),
197
198 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
199 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ),
200 el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 150, max: 700 } ),
201 el( RangeControl, { label: __( 'Box Width', 'blockenberg' ), value: a.boxWidth, onChange: v => setAttributes( { boxWidth: v } ), min: 16, max: 100 } ),
202 el( RangeControl, { label: __( 'Whisker Cap Width', 'blockenberg' ), value: a.whiskerWidth, onChange: v => setAttributes( { whiskerWidth: v } ), min: 8, max: 80 } ),
203 ),
204
205
206 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
207 el(getTypographyControl(), { label: __('Title', 'blockenberg'), value: a.typoTitle, onChange: v => setAttributes({ typoTitle: v }) }),
208 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 9, max: 22 } ),
209 el(RangeControl, { label: __('Value Font Size (px)', 'blockenberg'), value: a.valueFontSize, min: 8, max: 18, onChange: v => setAttributes({ valueFontSize: v }) })
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 Lines', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) },
216 { label: __( 'Title Color', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
217 { label: __( 'Text Color', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
218 ]
219 } ),
220
221 el( PanelBody, { title: __( 'Series', 'blockenberg' ), initialOpen: false },
222 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { series: [ ...a.series, { label: 'New Group', color: '#6366f1', min: 30, q1: 45, median: 60, mean: 58, q3: 72, max: 90, outliers: [] } ] } ) }, __( '+ Add Group', 'blockenberg' ) ),
223 a.series.map( ( s, si ) =>
224 el( PanelBody, { key: si, title: s.label || `Group ${ si + 1 }`, initialOpen: false },
225 el( TextControl, { label: __( 'Label', 'blockenberg' ), value: s.label, onChange: v => updSer( setAttributes, a.series, si, 'label', v ) } ),
226 el( BkbgColorSwatch, { label: __( 'Color', 'blockenberg' ), value: s.color, onChange: v => updSer( setAttributes, a.series, si, 'color', v ) } ),
227 el( RangeControl, { label: __( 'Min', 'blockenberg' ), value: s.min, onChange: v => updSer( setAttributes, a.series, si, 'min', v ), min: -1000, max: 10000, step: 1 } ),
228 el( RangeControl, { label: __( 'Q1 (25th pct)', 'blockenberg' ), value: s.q1, onChange: v => updSer( setAttributes, a.series, si, 'q1', v ), min: -1000, max: 10000, step: 1 } ),
229 el( RangeControl, { label: __( 'Median (Q2)', 'blockenberg' ), value: s.median, onChange: v => updSer( setAttributes, a.series, si, 'median', v ), min: -1000, max: 10000, step: 1 } ),
230 el( RangeControl, { label: __( 'Mean', 'blockenberg' ), value: s.mean, onChange: v => updSer( setAttributes, a.series, si, 'mean', v ), min: -1000, max: 10000, step: 1 } ),
231 el( RangeControl, { label: __( 'Q3 (75th pct)', 'blockenberg' ), value: s.q3, onChange: v => updSer( setAttributes, a.series, si, 'q3', v ), min: -1000, max: 10000, step: 1 } ),
232 el( RangeControl, { label: __( 'Max', 'blockenberg' ), value: s.max, onChange: v => updSer( setAttributes, a.series, si, 'max', v ), min: -1000, max: 10000, step: 1 } ),
233 el( TextControl, { label: __( 'Outliers (comma separated)', 'blockenberg' ), value: ( s.outliers || [] ).join( ', ' ), onChange: v => updSer( setAttributes, a.series, si, 'outliers', parseOutliers( v ) ) } ),
234 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { series: a.series.filter( ( _, x ) => x !== si ) } ) }, __( 'Remove', 'blockenberg' ) ),
235 )
236 ),
237 ),
238 ),
239
240 a.showTitle && a.title && el( 'h3', { className: 'bkbg-boxplot-title', style: { color: a.titleColor } }, a.title ),
241 el( 'div', { className: 'bkbg-boxplot-svg' }, renderBoxPlot( a ) ),
242 );
243 },
244
245 save: function ( { attributes: a } ) {
246 const blockProps = useBlockProps.save( { className: 'bkbg-boxplot-wrap', style: Object.assign({}, _tv(a.typoTitle, '--bkbg-boxplot-title-')) } );
247 return el( 'div', blockProps,
248 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-boxplot-title', style: { color: a.titleColor } }, a.title ) : null,
249 el( 'div', { className: 'bkbg-boxplot-svg' }, renderBoxPlot( a ) ),
250 );
251 },
252 } );
253 }() );
254