|
| 1 | +--- |
| 2 | +title: "Creating interactive Diagrams with eCharts" |
| 3 | +slug: creating-interactive-diagrams-with-echarts |
| 4 | +date: 2024-08-21 |
| 5 | +draft: false |
| 6 | +author: André Dietrich |
| 7 | +image: "/images/post/echarts-diagrams.gif" |
| 8 | +categories: |
| 9 | + - Article |
| 10 | +tags: |
| 11 | + - LiaScript |
| 12 | + - Diagrams |
| 13 | + - eCharts |
| 14 | + - Interactive |
| 15 | + - WebGL |
| 16 | + - Macros |
| 17 | +--- |
| 18 | + |
| 19 | +LiaScript uses the powerful eCharts library to create interactive diagrams, whether you're visualizing ASCII art or presenting data from tables. In our previous blog post, we introduced the `<lia-chart>` tag—a custom web component that uses eCharts under the hood to easily generate custom diagrams. |
| 20 | + |
| 21 | +{{<preview "blog/074_creating-interactive-diagrams-with-chatgpt">}} |
| 22 | + |
| 23 | +For more complex visualizations, you might want to go beyond the basic capabilities of the `<lia-chart>` tag and tap directly into the eCharts library, particularly when combined with "echarts-gl," a WebGL extension for eCharts that enables even more advanced graphical features. This can be done directly within your script tags. Here’s how you can import the necessary libraries in your course setup: |
| 24 | + |
| 25 | +```markdown |
| 26 | +<!-- |
| 27 | +script: https://cdn.jsdelivr.net/npm/echarts@5.4.2/dist/echarts.min.js |
| 28 | + https://cdn.jsdelivr.net/npm/echarts-gl@2.0.9/dist/echarts-gl.min.js |
| 29 | +--> |
| 30 | + |
| 31 | +# Your Course |
| 32 | +``` |
| 33 | + |
| 34 | +### Examples |
| 35 | + |
| 36 | +Below are some examples illustrating how to combine eCharts with LiaScript inputs. The process is similar to using the `<lia-chart>` tag, but with a few key differences. For instance, when working with eCharts directly, you'll need to define additional functions that cannot be handled by the `<lia-chart>` tag alone. Additionally, you'll need to create a `<div>` element in your document where the script will render the diagram. |
| 37 | + |
| 38 | +The script functions silently in the background—it doesn't produce visible output within the document itself but rather makes the necessary modifications to render the diagram. Whenever the input values associated with the script change, the main script is automatically re-executed, and the diagram updates in real-time. |
| 39 | + |
| 40 | +> Why is the script re-executed? This happens because of the @input(`topic`) markers. These markers "subscribe" to any changes in the scripts that define the specified topic as their output. When those scripts change, the main script is triggered again, ensuring your diagram remains up-to-date. |
| 41 | +
|
| 42 | +<iframe loading="lazy" style="height: 80vh; min-width: 100%; border: 1px black solid" src="https://liascript.github.io/LiveEditor/?/embed/code/H4sIAAAAAAAAA5VWS3PbNhC+81eg0tSSW5Oino4oS3Vr++CDJ5m6t05mApFLEQ34CADqkYz+excgKZK2PHV5kLDfLvZb7gPgzU+2bUlfsEx5JFIqk95g4AeJ848MgLOtcBJQgySLB+BHVCh5O3UmzmgQMKkqyImZtrdI+bzHjb3htyPHdeYtT4hWzmx7ZVld8pRynkufPEfAObHJPUiVCsXSxLJuirhJACHNuVp2hs5w1iFbynOoBJZkOWoETTbQIWmujJilOxAdglTLjusMO0QqyPTSxXVM98vOuNxqU76jB2lTX7EtrG4NeDMomFfnYqgDeIudBkHJbY/PUr+POWBbwoJlJ6Ys0W4OHEl3LFCRR4au+/OCRMA2Edb1g7uNFp3VDeZ6uzqFXO7A/GecHjwMlrME7DVP/a8L0nDUWVlbKogp0X0akyUJUj+PIVHOBtQDB7384/AY9Hs6lt7lwtjHhzu9A82rPmEJU/3KTWmVZrqYC8sIpi64oXjZ/hcjf7k0OsxaQ4MS4laxG/Ef2H0qTblimUd+HK9Q3DKZU/5ENWB6U0bpziMh5RKuDBAwjFyiA4+MCgSr4hF7Wgp07xG3WLPkT13EyhUhfspT4ZG/T13f646H49l82rtqQJPp9XQ9aUHXE4x92ILoOpjDvAWBG47DDy0oxGcdtiEAd+62oYDCrO0+nMyCybgFBddjd3TdDmLquqNZr0Q+m/8j/ppc7n/fMzm+r1J7aIvf2+JGsMBIjawrkZdJz1KpHsIQfFUnExK65lBYFcSFrYI4SwXlz3kG4pnGGXbo5r+38aLtKzPdlLWka6l02RX2/LjOgIxoUEVagsdKS+M1wx5/w4n7hvldvoa4bj8T8R5fPxdwajj9BCwMcwmPtcdhI6wM/JxT0dRWfO0aSRAMZNWSFac6ZMjWQ9KQ+nCqeEYFjUEJ5jdrQ3DsBYRa1Yy6MTgvX1VnzdSkJ4ByPMqZf+I4IU9UYWyUN32KNN9ECUgM2HUm9ftiUJSX+EuyOuiHbzlVZnBrj3lTqEYZqSPn0+NVU6HH+gyuD+KTggzIxD0pj7Xd9hzLcPqKYPbateuMhudconWYJ745yfr5FdletigEqFwkRWB4IPbNoWisfilAP5V9I/X1GflrDeaXeMieITy8i9B+m1Gy5P8xfn8f46gieMna5NHkbZ5X8/DZOi5Ol8PFRXUVORLURwP2Cx06adyo3W6X3AkImLKsvyImcVr1kQNkRyUOqcAjix+Iol8hIaHAi1BFQMB4lmQHa5xOIFR5llV9AVUXH82oH4GTis2g9CkHkAw0VSqcSMX8N39ZjqgdF588ttSfPBcbvhz+C6/Ha/GlCQAA"></iframe> |
| 43 | + |
| 44 | +<br> |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +<br> |
| 49 | + |
| 50 | +<iframe loading="lazy" style="height: 80vh; min-width: 100%; border: 1px black solid" src="https://liascript.github.io/LiveEditor/?/embed/code/preview/H4sIAAAAAAAAA91W227jNhB911ewzjaWG1uWZTvZyJem3bRFgN226BboQ2B0aZGymEiUKtIXdeF/71CUbNHOdgvsWwXIpg5nzhwOh5fpV72eJYKcZdJHkZSZ8Pv9gHDnSRAas03ucCr7PEv6NIhwLsXd2Bk5Xp8wIWvISZiyt1D1/Bea3iq+8xzXuTWYAP0UWYiFjItzPsGSLKa7Hk+ZoEA5clwTU2S93tyyLtCPcbpFKUcyoiiAeFQwzC3r13RLc/SK/Ll7hXw01dlAhIZ4HctZa+i20AbHa6qbjGdrQHPMV7SF0rUsPzPFMWghUD9rgZWQNJu1FIB38O/Wjj0cb3EhejiQbEPndyU47euY84aW4gu1eF+uZUrYBjEyayWYcUVTxBB3y4iMfAQ0X09QRNkqgsp57W6iSWs+hdnczK1aduUBM5zFuPAhasw47S3jNHieoAZRa25tcI7KIrhPEzRDJA3WCeXS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fXNy8Mg4iok+arfsUp+pT3Vag0dyKZxN8GM72+9b38chXPBpfAn9CZuqb9A1b0LUqKDAAA"></iframe> |
| 51 | + |
| 52 | +<br> |
| 53 | + |
| 54 | +### Learn More |
| 55 | + |
| 56 | +To dive deeper into using the eCharts library directly and explore the comprehensive examples provided in the link below: |
| 57 | + |
| 58 | +{{< button label="eCharts Examples" link="https://echarts.apache.org/examples/en/index.html" >}} |
| 59 | + |
| 60 | +Now that you’re familiar with using ChatGPT to create interactive diagrams, you’re ready to take things to the next level. By combining LiaScript snippets with the more advanced examples from the eCharts library, you can create dynamic visualizations that bring your ideas to life. The possibilities are endless — start experimenting and visualize everything you can imagine! |
| 61 | + |
| 62 | +### Automate with Macros |
| 63 | + |
| 64 | +To streamline your workflow, especially when working with repetitive eCharts diagrams, you can create custom macros. Here's a quick example to demonstrate how you can use macros to simplify your coding process. |
| 65 | + |
| 66 | +When incorporating code into a macro, the following syntax is recommended: |
| 67 | + |
| 68 | +````markdown |
| 69 | +```language @macro(params) |
| 70 | +console.log("The code block is passed as the last parameter") |
| 71 | +
|
| 72 | + ... |
| 73 | +
|
| 74 | +``` |
| 75 | +```` |
| 76 | + |
| 77 | +This syntax is utilized in the examples below. The `@echarts` macro is defined in the document's main header and is designed to accept two parameters. The first parameter is the background theme, which can be set to either `light` or `dark`. The second parameter is the code block that generates the diagram. |
| 78 | + |
| 79 | +Rather than directly injecting LiaScript code, this macro uses a helper function `@echarts_`. It first calls the `@uid` macro to generate a unique ID, which is then assigned to the `<div>` element and used in the `document.getElementById` query. The macro then passes the background parameter and the code block, which is enclosed in triple backticks to denote a multi-line string. If you're working with a single line of code, you can use single backticks to indicate the string, especially if it includes additional commas. |
| 80 | + |
| 81 | +<br> |
| 82 | + |
| 83 | +<iframe loading="lazy" style="height: 80vh; min-width: 100%; border: 1px black solid" src="https://liascript.github.io/LiveEditor/?/embed/code/edit/H4sIAAAAAAAAA+1YbW/jNhL+rl/Bc3prubFkSbazjR2nvttsiwDda9EU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StInF+VfL9LCT1MWmLc7ZEnIEVxKwK0lEeD9bq8jDG6/22mr2+mRbtVu3KZ6Vf5l8fXbUcNFIiNdbg0pd2dlvLfO3bCjKWqNu6fZeTMZ6mGPSu7etcTPnRD2a0o+MtD/ln96eV1ALlj6SPQ67Opa1ei13TWcdd4t0pK0Dv4/WGu5Zss/IhVZfQP4MVlIrvU/B4pJdHsVAAA="></iframe> |
| 84 | + |
| 85 | +<br> |
| 86 | + |
| 87 | +Your standard Markdown viewer will still highlight the code blocks as usual, but the LiaScript interpreter will go a step further by transforming them into fully interactive diagrams. |
0 commit comments