diff --git a/_includes/scripts/chart.liquid b/_includes/scripts/chartjs.liquid similarity index 85% rename from _includes/scripts/chart.liquid rename to _includes/scripts/chartjs.liquid index 6240f9b57ccc..4f43933072f8 100644 --- a/_includes/scripts/chart.liquid +++ b/_includes/scripts/chartjs.liquid @@ -1,4 +1,4 @@ -{% if page.chart %} +{% if page.chart and page.chart.chartjs %} + {% if site.enable_darkmode %} + + {% endif %} + +{% endif %} diff --git a/_layouts/default.liquid b/_layouts/default.liquid index f42bfda8fa0f..0f12156423a6 100644 --- a/_layouts/default.liquid +++ b/_layouts/default.liquid @@ -48,7 +48,8 @@ {% include scripts/bootstrap.liquid %} {% include scripts/masonry.liquid %} {% include scripts/mermaid.liquid %} - {% include scripts/chart.liquid %} + {% include scripts/chartjs.liquid %} + {% include scripts/echarts.liquid %} {% include scripts/misc.liquid %} {% include scripts/badges.liquid %} {% include scripts/mathjax.liquid %} diff --git a/_posts/2024-01-26-chartjs.md b/_posts/2024-01-26-chartjs.md index 49b64f0a9c84..210557d6df1f 100644 --- a/_posts/2024-01-26-chartjs.md +++ b/_posts/2024-01-26-chartjs.md @@ -5,13 +5,14 @@ date: 2024-01-26 01:04:00 description: this is what included chart.js code could look like tags: formatting charts categories: sample-posts -chart: true +chart: + chartjs: true --- -This is an example post with some chart.js code. +This is an example post with some [chart.js](https://www.chartjs.org/) code. ````markdown -```chart +```chartjs { "type": "line", "data": { @@ -64,7 +65,7 @@ This is an example post with some chart.js code. This is how it looks like: -```chart +```chartjs { "type": "line", "data": { @@ -117,7 +118,7 @@ This is how it looks like: Also another example chart. ````markdown -```chart +```chartjs { "type": "doughnut", "data": { @@ -153,7 +154,7 @@ Also another example chart. Which generates: -```chart +```chartjs { "type": "doughnut", "data": { diff --git a/_posts/2024-01-26-echarts.md b/_posts/2024-01-26-echarts.md new file mode 100644 index 000000000000..b496288d05ef --- /dev/null +++ b/_posts/2024-01-26-echarts.md @@ -0,0 +1,68 @@ +--- +layout: post +title: a post with echarts +date: 2024-01-26 16:03:00 +description: this is what included echarts code could look like +tags: formatting charts +categories: sample-posts +chart: + echarts: true +--- + +This is an example post with some [echarts](https://echarts.apache.org/) code. + +````markdown +```echarts +{ + "title": { + "text": "ECharts Getting Started Example" + }, + "responsive": true, + "tooltip": {}, + "legend": { + "top": "30px", + "data": ["sales"] + }, + "xAxis": { + "data": ["Shirts", "Cardigans", "Chiffons", "Pants", "Heels", "Socks"] + }, + "yAxis": {}, + "series": [ + { + "name": "sales", + "type": "bar", + "data": [5, 20, 36, 10, 10, 20] + } + ] +} +``` +```` + +Which generates: + +```echarts +{ + "title": { + "text": "ECharts Getting Started Example" + }, + "responsive": true, + "tooltip": {}, + "legend": { + "top": "30px", + "data": ["sales"] + }, + "xAxis": { + "data": ["Shirts", "Cardigans", "Chiffons", "Pants", "Heels", "Socks"] + }, + "yAxis": {}, + "series": [ + { + "name": "sales", + "type": "bar", + "data": [5, 20, 36, 10, 10, 20] + } + ] +} +``` + +Note that this library offer support for both light and dark themes. You can switch between them using the theme switcher in the top right corner of the page. diff --git a/_sass/_base.scss b/_sass/_base.scss index 5d0c25f36481..7c4d6d920ad8 100644 --- a/_sass/_base.scss +++ b/_sass/_base.scss @@ -1081,3 +1081,8 @@ nav[data-toggle="toc"] { #toc-sidebar { z-index: 1; } + +.echarts { + width: 100%; + height: 400px; +} diff --git a/assets/js/copy_code.js b/assets/js/copy_code.js index 2d107f0bf1a5..4a34e5f52ab3 100644 --- a/assets/js/copy_code.js +++ b/assets/js/copy_code.js @@ -3,7 +3,8 @@ var codeBlocks = document.querySelectorAll("pre"); codeBlocks.forEach(function (codeBlock) { if ( (codeBlock.querySelector("pre:not(.lineno)") || codeBlock.querySelector("code")) && - codeBlock.querySelector("code:not(.language-chart)") && + codeBlock.querySelector("code:not(.language-chartjs)") && + codeBlock.querySelector("code:not(.language-echarts)") && codeBlock.querySelector("code:not(.language-mermaid)") ) { // create copy button diff --git a/assets/js/theme.js b/assets/js/theme.js index efd3ae490505..1f526248da79 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -16,6 +16,10 @@ let setTheme = (theme) => { if (typeof mermaid !== "undefined") { setMermaidTheme(theme); } + // if echarts is not defined, do nothing + if (typeof echarts !== "undefined") { + setEchartsTheme(theme); + } if (theme) { document.documentElement.setAttribute("data-theme", theme); @@ -120,6 +124,22 @@ let setMermaidTheme = (theme) => { } }; +let setEchartsTheme = (theme) => { + document.querySelectorAll(".echarts").forEach((elem) => { + // Get the code block content from previous element, since it is the echarts code itself as defined in Markdown, but it is hidden + let jsonData = elem.previousSibling.childNodes[0].innerHTML; + echarts.dispose(elem); + + if (theme === "dark") { + var chart = echarts.init(elem, "dark-fresh-cut"); + } else { + var chart = echarts.init(elem); + } + + chart.setOption(JSON.parse(jsonData)); + }); +}; + let transTheme = () => { document.documentElement.classList.add("transition"); window.setTimeout(() => {