changes to blocks and styles
This commit is contained in:
19
includes/blocks/lcp-visualizer/src/block.json
Normal file
19
includes/blocks/lcp-visualizer/src/block.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"$schema": "https://schemas.wp.org/trunk/block.json",
|
||||
"apiVersion": 3,
|
||||
"name": "lcp/visualizer",
|
||||
"version": "0.1.0",
|
||||
"title": "Visualizer",
|
||||
"category": "widgets",
|
||||
"icon": "smiley",
|
||||
"description": "Block for charts and data visualization",
|
||||
"example": {},
|
||||
"supports": {
|
||||
"html": false
|
||||
},
|
||||
"textdomain": "lcp",
|
||||
"editorScript": "file:./index.js",
|
||||
"editorStyle": "file:./index.css",
|
||||
"style": "file:./style-index.css",
|
||||
"viewScript": "file:./view.js"
|
||||
}
|
||||
38
includes/blocks/lcp-visualizer/src/edit.js
Normal file
38
includes/blocks/lcp-visualizer/src/edit.js
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Retrieves the translation of text.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
|
||||
*/
|
||||
import { __ } from '@wordpress/i18n';
|
||||
|
||||
/**
|
||||
* React hook that is used to mark the block wrapper element.
|
||||
* It provides all the necessary props like the class name.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
|
||||
*/
|
||||
import { useBlockProps } from '@wordpress/block-editor';
|
||||
|
||||
/**
|
||||
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
|
||||
* Those files can contain any CSS code that gets applied to the editor.
|
||||
*
|
||||
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
|
||||
*/
|
||||
import './editor.scss';
|
||||
|
||||
/**
|
||||
* The edit function describes the structure of your block in the context of the
|
||||
* editor. This represents what the editor will render when the block is used.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
|
||||
*
|
||||
* @return {Element} Element to render.
|
||||
*/
|
||||
export default function Edit() {
|
||||
return (
|
||||
<p { ...useBlockProps() }>
|
||||
{ __( 'Todo List – hello from the editor!', 'todo-list' ) }
|
||||
</p>
|
||||
);
|
||||
}
|
||||
9
includes/blocks/lcp-visualizer/src/editor.scss
Normal file
9
includes/blocks/lcp-visualizer/src/editor.scss
Normal file
@ -0,0 +1,9 @@
|
||||
/**
|
||||
* The following styles get applied inside the editor only.
|
||||
*
|
||||
* Replace them with your own styles or remove the file completely.
|
||||
*/
|
||||
|
||||
.wp-block-create-block-todo-list {
|
||||
border: 1px dotted #f00;
|
||||
}
|
||||
39
includes/blocks/lcp-visualizer/src/index.js
Normal file
39
includes/blocks/lcp-visualizer/src/index.js
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Registers a new block provided a unique name and an object defining its behavior.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
|
||||
*/
|
||||
import { registerBlockType } from '@wordpress/blocks';
|
||||
|
||||
/**
|
||||
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
|
||||
* All files containing `style` keyword are bundled together. The code used
|
||||
* gets applied both to the front of your site and to the editor.
|
||||
*
|
||||
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
|
||||
*/
|
||||
import './style.scss';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import Edit from './edit';
|
||||
import save from './save';
|
||||
import metadata from './block.json';
|
||||
|
||||
/**
|
||||
* Every block starts by registering a new block type definition.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
|
||||
*/
|
||||
registerBlockType( metadata.name, {
|
||||
/**
|
||||
* @see ./edit.js
|
||||
*/
|
||||
edit: Edit,
|
||||
|
||||
/**
|
||||
* @see ./save.js
|
||||
*/
|
||||
save,
|
||||
} );
|
||||
22
includes/blocks/lcp-visualizer/src/save.js
Normal file
22
includes/blocks/lcp-visualizer/src/save.js
Normal file
@ -0,0 +1,22 @@
|
||||
/**
|
||||
* React hook that is used to mark the block wrapper element.
|
||||
* It provides all the necessary props like the class name.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* The save function defines the way in which the different attributes should
|
||||
* be combined into the final markup, which is then serialized by the block
|
||||
* editor into `post_content`.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#save
|
||||
*
|
||||
* @return {Element} Element to render.
|
||||
*/
|
||||
export default function save() {
|
||||
return (
|
||||
null
|
||||
);
|
||||
}
|
||||
24
includes/blocks/lcp-visualizer/src/style.scss
Normal file
24
includes/blocks/lcp-visualizer/src/style.scss
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* The following styles get applied both on the front of your site
|
||||
* and in the editor.
|
||||
*
|
||||
* Replace them with your own styles or remove the file completely.
|
||||
*/
|
||||
|
||||
.wp-block-create-block-todo-list {
|
||||
background-color: #21759b;
|
||||
color: #fff;
|
||||
padding: 2px;
|
||||
}
|
||||
|
||||
|
||||
.line {
|
||||
fill: none;
|
||||
stroke-width: 2px;
|
||||
}
|
||||
.hover-line {
|
||||
stroke-width: 4px;
|
||||
}
|
||||
.axis-label {
|
||||
font-size: 12px;
|
||||
}
|
||||
127
includes/blocks/lcp-visualizer/src/view.js
Normal file
127
includes/blocks/lcp-visualizer/src/view.js
Normal file
@ -0,0 +1,127 @@
|
||||
/**
|
||||
* Use this file for JavaScript code that you want to run in the front-end
|
||||
* on posts/pages that contain this block.
|
||||
*
|
||||
* When this file is defined as the value of the `viewScript` property
|
||||
* in `block.json` it will be enqueued on the front end of the site.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* ```js
|
||||
* {
|
||||
* "viewScript": "file:./view.js"
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* If you're not making any changes to this file because your project doesn't need any
|
||||
* JavaScript running in the front-end, then you should delete this file and remove
|
||||
* the `viewScript` property from `block.json`.
|
||||
*
|
||||
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/#view-script
|
||||
*/
|
||||
|
||||
/* eslint-disable no-console */
|
||||
console.log( 'Hello World! (from lcp-visualizer)' );
|
||||
/* eslint-enable no-console */
|
||||
|
||||
|
||||
// JavaScript to initialize D3.js for all elements with the class .lcp-visualizer
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const data = [
|
||||
{ date: '2023-01-01', team: 'Manchester City', goals: 2 },
|
||||
{ date: '2023-01-01', team: 'Liverpool', goals: 1 },
|
||||
{ date: '2023-02-01', team: 'Manchester City', goals: 3 },
|
||||
{ date: '2023-02-01', team: 'Liverpool', goals: 2 },
|
||||
{ date: '2023-03-01', team: 'Manchester City', goals: 4 },
|
||||
{ date: '2023-03-01', team: 'Liverpool', goals: 3 },
|
||||
{ date: '2023-04-01', team: 'Manchester City', goals: 3 },
|
||||
{ date: '2023-04-01', team: 'Liverpool', goals: 4 },
|
||||
{ date: '2023-05-01', team: 'Manchester City', goals: 5 },
|
||||
{ date: '2023-05-01', team: 'Liverpool', goals: 4 }
|
||||
];
|
||||
|
||||
const parseDate = d3.timeParse("%Y-%m-%d");
|
||||
|
||||
data.forEach(d => {
|
||||
d.date = parseDate(d.date);
|
||||
d.goals = +d.goals;
|
||||
});
|
||||
|
||||
const teams = Array.from(new Set(data.map(d => d.team)));
|
||||
const color = d3.scaleOrdinal()
|
||||
.domain(teams)
|
||||
.range(d3.schemeCategory10);
|
||||
|
||||
const margin = { top: 20, right: 30, bottom: 40, left: 40 };
|
||||
const width = 800 - margin.left - margin.right;
|
||||
const height = 400 - margin.top - margin.bottom;
|
||||
|
||||
const svg = d3.select(".lcp-visualizer")
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", `translate(${margin.left},${margin.top})`);
|
||||
|
||||
const x = d3.scaleTime()
|
||||
.domain(d3.extent(data, d => d.date))
|
||||
.range([0, width]);
|
||||
|
||||
const y = d3.scaleLinear()
|
||||
.domain([0, d3.max(data, d => d.goals)])
|
||||
.nice()
|
||||
.range([height, 0]);
|
||||
|
||||
const line = d3.line()
|
||||
.x(d => x(d.date))
|
||||
.y(d => y(d.goals));
|
||||
|
||||
svg.append("g")
|
||||
.attr("transform", `translate(0,${height})`)
|
||||
.call(d3.axisBottom(x));
|
||||
|
||||
svg.append("g")
|
||||
.call(d3.axisLeft(y));
|
||||
|
||||
const teamData = teams.map(team => {
|
||||
return {
|
||||
team: team,
|
||||
values: data.filter(d => d.team === team)
|
||||
};
|
||||
});
|
||||
|
||||
const path = svg.selectAll(".line")
|
||||
.data(teamData)
|
||||
.enter().append("path")
|
||||
.attr("class", "line")
|
||||
.attr("d", d => line(d.values))
|
||||
.attr("stroke", d => color(d.team));
|
||||
|
||||
// Hover effect
|
||||
teamData.forEach(team => {
|
||||
svg.selectAll(`circle.${team.team}`)
|
||||
.data(team.values)
|
||||
.enter().append("circle")
|
||||
.attr("class", team.team)
|
||||
.attr("cx", d => x(d.date))
|
||||
.attr("cy", d => y(d.goals))
|
||||
.attr("r", 4)
|
||||
.attr("fill", color(team.team))
|
||||
.on("mouseover", function(event, d) {
|
||||
d3.select(this)
|
||||
.transition()
|
||||
.duration(200)
|
||||
.attr("r", 6)
|
||||
.attr("fill", "orange");
|
||||
d3.select(`path.${d.team}`).classed("hover-line", true);
|
||||
})
|
||||
.on("mouseout", function(event, d) {
|
||||
d3.select(this)
|
||||
.transition()
|
||||
.duration(200)
|
||||
.attr("r", 4)
|
||||
.attr("fill", color(d.team));
|
||||
d3.select(`path.${d.team}`).classed("hover-line", false);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user