PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
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 / histogram-chart / index.js

index.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/histogram-chart/index.js

168 lines 12.5 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 } = window.wp.components;
6 const { __ } = window.wp.i18n;
7
8 var _TypographyControl, _typoCssVars;
9 function getTypographyControl() { return _TypographyControl || (_TypographyControl = window.bkbgTypographyControl); }
10 function getTypoCssVars() { return _typoCssVars || (_typoCssVars = window.bkbgTypoCssVars); }
11
12 // ── Renderer ──────────────────────────────────────────────────────────────
13 function renderHistogram( a ) {
14 const bins = a.bins || [];
15 const W = a.svgWidth;
16 const H = a.svgHeight;
17 const PT = a.padTop;
18 const PL = a.padLeft;
19 const PR = a.padRight;
20 const PB = a.padBottom;
21 const chartW = W - PL - PR;
22 const chartH = H - PT - PB;
23 const n = bins.length;
24 if ( ! n ) return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%' } );
25
26 const maxCount = Math.max( ...bins.map( b => b.count || 0 ) ) || 1;
27 const barW = chartW / n;
28 const gap = a.barGap || 2;
29
30 function sy( v ) { return PT + chartH - ( v / maxCount ) * chartH; }
31 function sh( v ) { return ( v / maxCount ) * chartH; }
32
33 const svgEls = [];
34 svgEls.push( el( 'rect', { key: 'bg', x: 0, y: 0, width: W, height: H, fill: a.bgColor || '#ffffff', rx: 8 } ) );
35
36 // Grid lines + Y ticks
37 if ( a.showGrid ) {
38 const ticks = 5;
39 for ( let t = 0; t <= ticks; t++ ) {
40 const gy = PT + ( t / ticks ) * chartH;
41 const yV = Math.round( maxCount * ( 1 - t / ticks ) );
42 svgEls.push( el( 'line', { key: `gl${ t }`, x1: PL, y1: gy, x2: W - PR, y2: gy, stroke: a.gridColor || '#f3f4f6', strokeWidth: 1 } ) );
43 svgEls.push( el( 'text', { key: `gy${ t }`, x: PL - 8, y: gy, textAnchor: 'end', dominantBaseline: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 1, fontFamily: 'inherit' }, yV ) );
44 }
45 }
46
47 // Bars
48 bins.forEach( ( bin, i ) => {
49 const bx = PL + i * barW + gap / 2;
50 const bw = barW - gap;
51 const bh = sh( bin.count || 0 );
52 const by = sy( bin.count || 0 );
53 svgEls.push( el( 'rect', { key: `bar${ i }`, x: bx, y: by, width: bw, height: bh, fill: a.barColor || '#6366f1', rx: 3, fillOpacity: 0.88 } ) );
54
55 // Value label above bar
56 if ( a.showValues && bh > 12 ) {
57 svgEls.push( el( 'text', { key: `val${ i }`, x: bx + bw / 2, y: by - 4, textAnchor: 'middle', fill: a.barColor || '#6366f1', fontSize: a.valueFontSize, fontWeight: 700, fontFamily: 'inherit' }, bin.count ) );
58 }
59
60 // X label
61 svgEls.push( el( 'text', { key: `xl${ i }`, x: bx + bw / 2, y: PT + chartH + 18, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize - 2, fontFamily: 'inherit', transform: n > 6 ? `rotate(-35,${ bx + bw / 2 },${ PT + chartH + 18 })` : undefined }, bin.label ) );
62 } );
63
64 // Density curve (optional) — quadratic smooth through bar midpoints
65 if ( a.showCurve && bins.length > 2 ) {
66 const pts = bins.map( ( b, i ) => [ PL + i * barW + barW / 2, sy( b.count || 0 ) ] );
67 let d = `M ${ pts[0][0] } ${ pts[0][1] }`;
68 for ( let i = 1; i < pts.length; i++ ) {
69 const mx = ( pts[ i - 1 ][0] + pts[ i ][0] ) / 2;
70 d += ` Q ${ pts[ i - 1 ][0] } ${ pts[ i - 1 ][1] } ${ mx } ${ ( pts[ i - 1 ][1] + pts[ i ][1] ) / 2 }`;
71 }
72 d += ` Q ${ pts[ pts.length - 2 ][0] } ${ pts[ pts.length - 2 ][1] } ${ pts[ pts.length - 1 ][0] } ${ pts[ pts.length - 1 ][1] }`;
73 svgEls.push( el( 'path', { key: 'curve', d, fill: 'none', stroke: a.curveColor || '#ef4444', strokeWidth: 2.5, strokeLinejoin: 'round' } ) );
74 }
75
76 // Axes
77 svgEls.push( el( 'line', { key: 'xax', x1: PL, y1: PT + chartH, x2: W - PR, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.4 } ) );
78 svgEls.push( el( 'line', { key: 'yax', x1: PL, y1: PT, x2: PL, y2: PT + chartH, stroke: a.textColor, strokeWidth: 1, strokeOpacity: 0.4 } ) );
79
80 // Axis labels
81 svgEls.push( el( 'text', { key: 'xlab', x: PL + chartW / 2, y: H - 6, textAnchor: 'middle', fill: a.textColor, fontSize: a.labelFontSize, fontFamily: 'inherit' }, a.xAxisLabel ) );
82 svgEls.push( el( 'text', { key: 'ylab', 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 ) );
83
84 return el( 'svg', { viewBox: `0 0 ${ W } ${ H }`, width: '100%', style: { display: 'block', maxWidth: W + 'px', margin: '0 auto' } }, ...svgEls );
85 }
86
87 registerBlockType( 'blockenberg/histogram-chart', {
88 edit: function ( props ) {
89 const { attributes: a, setAttributes } = props;
90 var _tv = getTypoCssVars();
91 var wrapStyle = {};
92 Object.assign(wrapStyle, _tv(a.titleTypo || {}, '--bkbg-hist-tt-'));
93 if (a.titleFontSize && a.titleFontSize !== 18) wrapStyle['--bkbg-hist-tt-sz'] = a.titleFontSize + 'px';
94 if (a.titleFontWeight && a.titleFontWeight !== '700') wrapStyle['--bkbg-hist-tt-fw'] = a.titleFontWeight;
95 if (a.titleLineHeight && a.titleLineHeight !== 1.2) wrapStyle['--bkbg-hist-tt-lh'] = String(a.titleLineHeight);
96 const blockProps = useBlockProps( { className: 'bkbg-histogram-wrap', style: wrapStyle } );
97
98 return el( 'div', blockProps,
99 el( InspectorControls, {},
100
101 el( PanelBody, { title: __( 'Chart Settings', 'blockenberg' ), initialOpen: true },
102 el( TextControl, { label: __( 'Title', 'blockenberg' ), value: a.title, onChange: v => setAttributes( { title: v } ) } ),
103 el( ToggleControl, { label: __( 'Show Title', 'blockenberg' ), checked: a.showTitle, onChange: v => setAttributes( { showTitle: v } ) } ),
104 el( ToggleControl, { label: __( 'Show Grid', 'blockenberg' ), checked: a.showGrid, onChange: v => setAttributes( { showGrid: v } ) } ),
105 el( ToggleControl, { label: __( 'Show Values', 'blockenberg' ), checked: a.showValues, onChange: v => setAttributes( { showValues: v } ) } ),
106 el( ToggleControl, { label: __( 'Show Density Curve', 'blockenberg' ), checked: a.showCurve, onChange: v => setAttributes( { showCurve: v } ) } ),
107 el( TextControl, { label: __( 'X Axis Label', 'blockenberg' ), value: a.xAxisLabel, onChange: v => setAttributes( { xAxisLabel: v } ) } ),
108 el( TextControl, { label: __( 'Y Axis Label', 'blockenberg' ), value: a.yAxisLabel, onChange: v => setAttributes( { yAxisLabel: v } ) } ),
109 ),
110
111 el( PanelBody, { title: __( 'Layout', 'blockenberg' ), initialOpen: false },
112 el( RangeControl, { label: __( 'Canvas Width', 'blockenberg' ), value: a.svgWidth, onChange: v => setAttributes( { svgWidth: v } ), min: 300, max: 1200, step: 20 } ),
113 el( RangeControl, { label: __( 'Canvas Height', 'blockenberg' ), value: a.svgHeight, onChange: v => setAttributes( { svgHeight: v } ), min: 160, max: 700 } ),
114 el( RangeControl, { label: __( 'Bar Gap (px)', 'blockenberg' ), value: a.barGap, onChange: v => setAttributes( { barGap: v } ), min: 0, max: 20 } ),
115 el( RangeControl, { label: __( 'Left Padding', 'blockenberg' ), value: a.padLeft, onChange: v => setAttributes( { padLeft: v } ), min: 20, max: 120 } ),
116 el( RangeControl, { label: __( 'Bottom Padding', 'blockenberg' ),value: a.padBottom, onChange: v => setAttributes( { padBottom: v } ), min: 30, max: 120 } ),
117 ),
118
119 el( PanelBody, { title: __( 'Typography', 'blockenberg' ), initialOpen: false },
120 el(getTypographyControl(), { label: __('Title Typography', 'blockenberg'), value: a.titleTypo || {}, onChange: v => setAttributes({ titleTypo: v }) }),
121 el( RangeControl, { label: __( 'Label Font Size', 'blockenberg' ), value: a.labelFontSize, onChange: v => setAttributes( { labelFontSize: v } ), min: 8, max: 20 } ),
122 el( RangeControl, { label: __( 'Value Font Size', 'blockenberg' ), value: a.valueFontSize, onChange: v => setAttributes( { valueFontSize: v } ), min: 7, max: 16 } )
123 ),
124 el( PanelColorSettings, {
125 title: __( 'Colors', 'blockenberg' ), initialOpen: false,
126 colorSettings: [
127 { label: __( 'Bar fill', 'blockenberg' ), value: a.barColor, onChange: v => setAttributes( { barColor: v || '#6366f1' } ) },
128 { label: __( 'Density curve', 'blockenberg' ), value: a.curveColor, onChange: v => setAttributes( { curveColor: v || '#ef4444' } ) },
129 { label: __( 'Background', 'blockenberg' ), value: a.bgColor, onChange: v => setAttributes( { bgColor: v || '#ffffff' } ) },
130 { label: __( 'Grid', 'blockenberg' ), value: a.gridColor, onChange: v => setAttributes( { gridColor: v || '#f3f4f6' } ) },
131 { label: __( 'Title', 'blockenberg' ), value: a.titleColor, onChange: v => setAttributes( { titleColor: v || '#111827' } ) },
132 { label: __( 'Text/Axes', 'blockenberg' ), value: a.textColor, onChange: v => setAttributes( { textColor: v || '#374151' } ) },
133 ]
134 } ),
135
136 el( PanelBody, { title: __( 'Bins', 'blockenberg' ), initialOpen: false },
137 el( Button, { variant: 'secondary', style: { marginBottom: 10 }, onClick: () => setAttributes( { bins: [ ...a.bins, { label: 'New bin', count: 10 } ] } ) }, __( '+ Add Bin', 'blockenberg' ) ),
138 a.bins.map( ( bin, bi ) =>
139 el( 'div', { key: bi, style: { display: 'flex', gap: 6, alignItems: 'flex-end', marginBottom: 6 } },
140 el( TextControl, { label: bi === 0 ? __( 'Label', 'blockenberg' ) : undefined, value: bin.label, onChange: v => setAttributes( { bins: a.bins.map( ( b, j ) => j === bi ? { ...b, label: v } : b ) } ), style: { flex: 2 } } ),
141 el( TextControl, { label: bi === 0 ? __( 'Count', 'blockenberg' ) : undefined, value: String( bin.count ), type: 'number', onChange: v => setAttributes( { bins: a.bins.map( ( b, j ) => j === bi ? { ...b, count: parseInt( v ) || 0 } : b ) } ), style: { flex: 1 } } ),
142 el( Button, { isDestructive: true, isSmall: true, onClick: () => setAttributes( { bins: a.bins.filter( ( _, j ) => j !== bi ) } ) }, '' ),
143 )
144 ),
145 ),
146 ),
147
148 a.showTitle && a.title && el( 'h3', { className: 'bkbg-histogram-title', style: { color: a.titleColor } }, a.title ),
149 el( 'div', { className: 'bkbg-histogram-svg' }, renderHistogram( a ) ),
150 );
151 },
152
153 save: function ( { attributes: a } ) {
154 var _tv = getTypoCssVars();
155 var saveStyle = {};
156 Object.assign(saveStyle, _tv(a.titleTypo || {}, '--bkbg-hist-tt-'));
157 if (a.titleFontSize && a.titleFontSize !== 18) saveStyle['--bkbg-hist-tt-sz'] = a.titleFontSize + 'px';
158 if (a.titleFontWeight && a.titleFontWeight !== '700') saveStyle['--bkbg-hist-tt-fw'] = a.titleFontWeight;
159 if (a.titleLineHeight && a.titleLineHeight !== 1.2) saveStyle['--bkbg-hist-tt-lh'] = String(a.titleLineHeight);
160 const blockProps = useBlockProps.save( { className: 'bkbg-histogram-wrap', style: saveStyle } );
161 return el( 'div', blockProps,
162 a.showTitle && a.title ? el( 'h3', { className: 'bkbg-histogram-title', style: { color: a.titleColor } }, a.title ) : null,
163 el( 'div', { className: 'bkbg-histogram-svg' }, renderHistogram( a ) ),
164 );
165 },
166 } );
167 }() );
168