diff --git a/dist/g2.chart.html.js b/dist/g2.chart.html.js index d8ee33e..944ac59 100644 --- a/dist/g2.chart.html.js +++ b/dist/g2.chart.html.js @@ -2944,8 +2944,6 @@ class G2ChartElement extends HTMLElement { this._ctx = this._root.getElementById('cnv').getContext('2d'); - this._g = g2().del().clr().view({ cartesian: true }); - const t = 35; this._chart = { x: t, @@ -2963,6 +2961,8 @@ class G2ChartElement extends HTMLElement { funcs: () => this.funcs }; + this._g = g2().del().clr().view({ cartesian: true }).chart(this._chart); + try { // If not true, the element should be referenced by another module. if (this.innerHTML !== '') { @@ -2977,7 +2977,7 @@ class G2ChartElement extends HTMLElement { funcs[itr].fn = (() => Function('"use strict"; return (' + a[4] + ')')())(); itr++; } - this.funcs = funcs; + this.funcs = funcs; } } catch (e) { @@ -2985,10 +2985,11 @@ class G2ChartElement extends HTMLElement { this._g.txt({ str: e, y: 5 }); } finally { - this._g.chart(this._chart).nod({ + this._g.nod({ x: () => this.nod && this.nod().x, y: () => this.nod && this.nod().y, - scl: () => this.nod && this.nod().scl || 0}); + scl: () => this.nod && this.nod().scl || 0 + }); this.render(); } } @@ -2997,8 +2998,9 @@ class G2ChartElement extends HTMLElement { this._g.exe(this._ctx); } - setChart(chart) { - this._chart = chart; + setFuncs(funcs) { + this.funcs = funcs; + return this; } disconnectedCallback() { diff --git a/docs/index.html b/docs/index.html index 687e401..fac2c44 100644 --- a/docs/index.html +++ b/docs/index.html @@ -33,7 +33,8 @@
  • Insert
  • Animation
  • Interactivity
  • -
  • Custom HTML Element
  • +
  • Custom HTML element
  • +
  • Custom chart element








    • @@ -156,8 +157,8 @@

      An optional second argument d of stroke, fill or drw can be used to hand over an SVG path data string. In that case previously defined path commands are ignored.

      -
      const star = "M100,10L123.5,82.4L61,37.6L138,37.6L76.5,82.4Z"; -g2().drw({d:star,lw:4,ls:"red",fs:"orange"}) +
      const star = "M100,10L123.5,82.4L61,37.6L138,37.6L76.5,82.4Z"; +g2().drw({d:star,lw:4,ls:"red",fs:"orange"}) .exe(ctx);

      star

      diff --git a/docs/microjam.md/Reuse.html b/docs/microjam.md/Reuse.html index 61856d4..7736c18 100644 --- a/docs/microjam.md/Reuse.html +++ b/docs/microjam.md/Reuse.html @@ -33,7 +33,8 @@
    • Insert
    • Animation
    • Interactivity
    • -
    • Custom HTML Element
    • +
    • Custom HTML element
    • +
    • Custom chart element

      @@ -89,12 +90,12 @@

      use #

      Example #

      const smiley = g2().cir({x:0,y:0,r:5}) // set smiley origin in its center ! .arc({x:0,y:0,r:3,w:0.8,dw:2}) - .beg({fs:"snow"}) // snow colored eyes. + .beg({fs:"snow"}) // snow colored eyes. .cir({x:-2,y:-1,r:1}) .cir({x:2,y:-1,r:1}) .end(); -g2().use({grp:smiley,x:50,y:50,scl:4,lw:0.75,fs:"yellow"}) - .use({grp:smiley,x:150,y:50,scl:5,lw:0.4,fs:"orange",ls:"green"}) +g2().use({grp:smiley,x:50,y:50,scl:4,lw:0.75,fs:"yellow"}) + .use({grp:smiley,x:150,y:50,scl:5,lw:0.4,fs:"orange",ls:"green"}) .exe(ctx);

      smiley example

      @@ -104,14 +105,14 @@

      Example #

      So if you want to reuse geometry more than once, consider using use!

      Example 2 #

      function rollingWheel() { - const r=40,dphi=11/20*Math.PI,style={fs:"transparent",lw:8}, + const r=40,dphi=11/20*Math.PI,style={fs:"transparent",lw:8}, wheel = g2().cir({x:0,y:0,r:r,...style}) .lin({x1:-0.8*r,y1:0,x2:0.8*r,y2:0,...style}) - .cir({x:0,y:0,r:4,fs:"snow",lw:2}), + .cir({x:0,y:0,r:4,fs:"snow",lw:2}), rolling = g2(); for (let alpha=0.3,phi=0; alpha<=1; alpha+=0.1,phi+=dphi) { - rolling.use({grp:wheel,x:r*phi,y:54,w:phi,ls:'rgba(0,0,0,'+alpha*alpha*alpha+')'}); + rolling.use({grp:wheel,x:r*phi,y:54,w:phi,ls:'rgba(0,0,0,'+alpha*alpha*alpha+')'}); } return rolling; } @@ -126,17 +127,17 @@

      Example 2 #

      But we can do even better than that. We slightly modify the previous example and hand over the main g2 object to the service function.

      Example 2 Enhanced #

      -
      <canvas id="c" width="550" height="100"></canvas> -<script src='g2.core.js'></script> +
      <canvas id="c" width="550" height="100"></canvas> +<script src='g2.core.js'></script> <script> - const ctx=document.getElementById("c").getContext("2d"); + const ctx=document.getElementById("c").getContext("2d"); function rollingWheel(g) { - let r=40,dphi=11/20*Math.PI,style={fs:"transparent",lw:8}, + let r=40,dphi=11/20*Math.PI,style={fs:"transparent",lw:8}, wheel = g2().cir({x:0,y:0,r:r,...style}) .lin({x1:-0.8*r,y1:0,x2:0.8*r,y2:0,...style}) - .cir({x:0,y:0,r:4,fs:"snow",lw:2}); + .cir({x:0,y:0,r:4,fs:"snow",lw:2}); for (let alpha=0.3,phi=0; alpha<=1;alpha+=0.1,phi+=dphi) - g.use({grp:wheel,x:r*phi,y:54,w:phi,ls:'rgba(0,0,0,'+alpha*alpha*alpha+')'}); + g.use({grp:wheel,x:r*phi,y:54,w:phi,ls:'rgba(0,0,0,'+alpha*alpha*alpha+')'}); return g; } @@ -157,21 +158,21 @@

      Example 2 with ins

      Example 3 #

      Use is exceptionally useful if the same element is used over and over again

      -
      <canvas id="c1" width=200, height=200 style="border:1px solid black"></canvas> -<canvas id="c2" width=200, height=200 style="border:1px solid black"></canvas> -<script src="./g2.core.js"></script> +
      <canvas id="c1" width=200, height=200 style="border:1px solid black"></canvas> +<canvas id="c2" width=200, height=200 style="border:1px solid black"></canvas> +<script src="./g2.core.js"></script> <script> const pi = Math.PI, - ctx1 = document.getElementById('c1').getContext('2d'), - ctx2 = document.getElementById('c2').getContext('2d'), - world = g2().rec({x:0,y:0,b:200,h:200,fs:"skyblue"}) - .ply({pts:[0,100,40,...],fs:"burlywood"}) - .rec({x:0,y:0,b:200,y:150,fs:"limegreen"}), - tree = g2().rec({x:-5,y:0,b:10,h:40,fs:"brown"}) - .beg({fs:"forestgreen"}) + ctx1 = document.getElementById('c1').getContext('2d'), + ctx2 = document.getElementById('c2').getContext('2d'), + world = g2().rec({x:0,y:0,b:200,h:200,fs:"skyblue"}) + .ply({pts:[0,100,40,...],fs:"burlywood"}) + .rec({x:0,y:0,b:200,y:150,fs:"limegreen"}), + tree = g2().rec({x:-5,y:0,b:10,h:40,fs:"brown"}) + .beg({fs:"forestgreen"}) .cir({x:15,y:-20,r:10}) ... - .cir({x:0,y:-20,r:20,ls:"transparent"}) + .cir({x:0,y:-20,r:20,ls:"transparent"}) .end(); g2().use({grp:world,x:0,y:0}) @@ -196,7 +197,7 @@

      Example 3 #

      Predefined command queues #

      Please note that predefined command queues can also be used without any manipulation of their styling, as shown in previous occasions.

      -
      const block = g2().rec({b:20,h:20,x:50,y:100,fs:"burlywood",ls:"saddlebrown"}); +
      const block = g2().rec({b:20,h:20,x:50,y:100,fs:"burlywood",ls:"saddlebrown"}); block.exe(ctx);
      diff --git a/docs/microjam.md/State-and-Style.html b/docs/microjam.md/State-and-Style.html index e35fe6d..efb21be 100644 --- a/docs/microjam.md/State-and-Style.html +++ b/docs/microjam.md/State-and-Style.html @@ -33,7 +33,8 @@
    • Insert
    • Animation
    • Interactivity
    • -
    • Custom HTML Element
    • +
    • Custom HTML element
    • +
    • Custom chart element

      @@ -138,13 +139,13 @@

      Example 1 #

      -
      <canvas id="c" width="100" height="100"></canvas> -<script src='g2.core.js'></script> +
      <canvas id="c" width="100" height="100"></canvas> +<script src='g2.core.js'></script> <script> - const ctx=document.getElementById("c").getContext("2d"), - style={ls:"red",lw:3,fs:"ddd"}; - g2().rec({x:20,y:10,b:60,h:30,ls:'maroon',fs:'khaki'}) - .cir({x:50,y:75,r:20,lw:2,ld:[6,3],fs:'orange'}) + const ctx=document.getElementById("c").getContext("2d"), + style={ls:"red",lw:3,fs:"ddd"}; + g2().rec({x:20,y:10,b:60,h:30,ls:'maroon',fs:'khaki'}) + .cir({x:50,y:75,r:20,lw:2,ld:[6,3],fs:'orange'}) .exe(ctx) </script>
      @@ -212,41 +213,41 @@

      Example 1 #

      Example 2 #

      -
      <canvas id="c" width="450" height="100"></canvas> -<script src='g2.core.js'></script> -<script> - const ctx=document.getElementById("c").getContext("2d"), - fs1={fs:"darkolivegreen",font:"24px serif"}, - fs2={fs:"steelblue",font:"30px serif"}, - fs3={...fs2,fs:"midnightblue"}; - g2().beg({fs:"gainsboro",ls:"firebrick",lw:3, - thal:"center",tval:"middle"}) - .rec({x:20,y:20,b:80,h:50}) - .txt({str:"one",x:60,y:45,...fs1}) - .beg({y:-10,fs:"darkseagreen",ls:"cadetblue",lw:5}) - .rec({x:120,y:20,b:80,h:50}) - .txt({str:"two",x:160,y:45,...fs2}) - .beg({ls:"@fs",fs:"cadetblue"}) - .rec({x:220,y:20,b:80,h:50}) - .txt({str:"three",x:260,y:45,...fs3}) - .end() - .end() - .rec({x:320,y:20,b:80,h:50}) - .txt({str:"four",x:360,y:45,...fs1}) - .end() +
      <canvas id="c" width="450" height="100"></canvas> +<script src='g2.core.js'></script> +<script> + const ctx=document.getElementById("c").getContext("2d"), + fs1={fs:"darkolivegreen",font:"24px serif"}, + fs2={fs:"steelblue",font:"30px serif"}, + fs3={...fs2,fs:"midnightblue"}; + g2().beg({fs:"gainsboro",ls:"firebrick",lw:3, + thal:"center",tval:"middle"}) + .rec({x:20,y:20,b:80,h:50}) + .txt({str:"one",x:60,y:45,...fs1}) + .beg({y:-10,fs:"darkseagreen",ls:"cadetblue",lw:5}) + .rec({x:120,y:20,b:80,h:50}) + .txt({str:"two",x:160,y:45,...fs2}) + .beg({ls:"@fs",fs:"cadetblue"}) + .rec({x:220,y:20,b:80,h:50}) + .txt({str:"three",x:260,y:45,...fs3}) + .end() + .end() + .rec({x:320,y:20,b:80,h:50}) + .txt({str:"four",x:360,y:45,...fs1}) + .end() .exe(ctx); </script>

      beg Element

      Example Yinyang #

      -
      <canvas id="c" width="100" height="100"></canvas> -<script src='g2.core.js'></script> +
      <canvas id="c" width="100" height="100"></canvas> +<script src='g2.core.js'></script> <script> - const ctx=document.getElementById("c").getContext("2d"), - pi = Math.PI, dark = "#444", light = "#eee"; + const ctx=document.getElementById("c").getContext("2d"), + pi = Math.PI, dark = "#444", light = "#eee"; g2().beg({x:50,y:50,scl:2,fs:light,ls:dark,lw:2}) .cir({x:0,y:0,r:20}) - .beg({fs:"@ls"}) + .beg({fs:"@ls"}) .p() .m({x:0,y:-20}) .a({dw:pi,x:0,y:0}) @@ -255,7 +256,7 @@

      Example Yinyang x:0,y:-10,r:3}) - .cir({x:0,y:10,r:3,ls:light,fs:"@ls"}) + .cir({x:0,y:10,r:3,ls:light,fs:"@ls"}) .end() .end() .exe(ctx); diff --git a/docs/microjam.md/View.html b/docs/microjam.md/View.html index 5716a69..2fccee2 100644 --- a/docs/microjam.md/View.html +++ b/docs/microjam.md/View.html @@ -33,7 +33,8 @@
    • Insert
    • Animation
    • Interactivity
    • -
    • Custom HTML Element
    • +
    • Custom HTML element
    • +
    • Custom chart element

      @@ -82,16 +83,16 @@

      This output is generated by the follwing code addressing different renderer contexts.

      world = g2().view(worldview) - .rec({x:0,y:0,b:200,h:200,fs:"skyblue"}) - .ply({pts:[0,100,40,...],fs:"burlywood"}) - .rec({x:0,y:0,b:200,y:150,fs:"limegreen"}), + .rec({x:0,y:0,b:200,h:200,fs:"skyblue"}) + .ply({pts:[0,100,40,...],fs:"burlywood"}) + .rec({x:0,y:0,b:200,y:150,fs:"limegreen"}), tree = g2().view(treeview) - .rec({x:-5,y:0,b:10,h:40,fs:"brown"}) - .beg({fs:"forestgreen"}) + .rec({x:-5,y:0,b:10,h:40,fs:"brown"}) + .beg({fs:"forestgreen"}) .cir({x:15,y:-20,r:10}) ... - .cir({x:0,y:-20,r:20,ls:"transparent"}) + .cir({x:0,y:-20,r:20,ls:"transparent"}) .end(); g2().use({grp:world,x:0,y:0}) diff --git a/docs/microjam.md/g2.chart.html b/docs/microjam.md/g2.chart.html index 31b3063..a999839 100644 --- a/docs/microjam.md/g2.chart.html +++ b/docs/microjam.md/g2.chart.html @@ -33,7 +33,8 @@
    • Insert
    • Animation
    • Interactivity
    • -
    • Custom HTML Element
    • +
    • Custom HTML element
    • +
    • Custom chart element



      @@ -60,21 +61,16 @@

      The aim of this HTML element is to minimize the implementation overhead of the user, while maintaining the flexibility g2 provides.

      To implement the g2 element into your page just import the g2.html.js.

      -
      -

      Warning: This custom html element is new and not implemented in the current version of g2.
      -The src will be deprecated in v2.6 and has to be updated accordingly.
      -The only thing you have to then is to remove the wip, or replace it with 2.6 or v2.6.

      -
      -
      <g-2 id='g' width="601" height="401" cartesian x0="0" y0="0" grid interactive> -{ "main":[ - {"c":"lin","a":{"x1":"@A.x","y1":"@A.y","x2":"@B.x","y2":"@B.y","lw":3,"ls":"@linkcolor"}}, - {"c":"cir","a":{"id":"A","x":150,"y":50,"r":5,"fs":"orange"}}, - {"c":"cir","a":{"id":"B","x":-50,"y":100,"r":5,"fs":"orange"}}, - {"c":"cir","a":{"x":15,"y":150,"r":30,"fs":"orange"}}, - {"c":"cir","a":{"x":15,"y":150,"r":15,"fs":"snow"}} +
      <g-2 id='g' width="601" height="401" cartesian x0="0" y0="0" grid interactive> +{ "main":[ + {"c":"lin","a":{"x1":"@A.x","y1":"@A.y","x2":"@B.x","y2":"@B.y","lw":3,"ls":"@linkcolor"}}, + {"c":"cir","a":{"id":"A","x":150,"y":50,"r":5,"fs":"orange"}}, + {"c":"cir","a":{"id":"B","x":-50,"y":100,"r":5,"fs":"orange"}}, + {"c":"cir","a":{"x":15,"y":150,"r":30,"fs":"orange"}}, + {"c":"cir","a":{"x":15,"y":150,"r":15,"fs":"snow"}} ]} </g-2> -<script src="https://cdn.jsdelivr.net/gh/goessner/g2@wip/dist/g2.html.js"></script> +<script src="https://cdn.jsdelivr.net/gh/goessner/g2/dist/g2.html.js"></script>
      { "main":[ @@ -85,7 +81,7 @@ {"c":"cir","a":{"x":15,"y":150,"r":15,"fs":"snow"}} ]} - +

      The overall styling is handled by respective properties of the g-2 element:

      • width: Width of the element in px.
      • @@ -104,14 +100,14 @@ each element is an object containing "c" (think command) and "a" (think arguments). So the given "main":

        -
        "main":[ - {"c":"cir","a":{"x":250,"y":100,"r":30,"fs":"orange"}}, - {"c":"cir","a":{"x":250,"y":100,"r":15,"fs":"snow"}} +
        "main":[ + {"c":"cir","a":{"x":250,"y":100,"r":30,"fs":"orange"}}, + {"c":"cir","a":{"x":250,"y":100,"r":15,"fs":"snow"}} ]

        is equal to the command queue given by

        -
        g2().cir({x:250,y:100,r:30,fs:"orange"}) - .cir({x:250,y:100,r:15,fs:"snow"}); +
        g2().cir({x:250,y:100,r:30,fs:"orange"}) + .cir({x:250,y:100,r:15,fs:"snow"});

        Note: Don't forget to use quotations (e.g. "x") for the properties, diff --git a/docs/microjam.md/g2.html.md b/docs/microjam.md/g2.html.md index 85b1693..592ff67 100644 --- a/docs/microjam.md/g2.html.md +++ b/docs/microjam.md/g2.html.md @@ -17,10 +17,6 @@ while maintaining the flexibility _g2_ provides. To implement the _g2_ element into your page just import the `g2.html.js`. -> **Warning**: This custom html element is new and not implemented in the current version of _g2_.
        -> The **src** will be deprecated in v2.6 and has to be updated accordingly.
        -> The only thing you have to then is to remove the **wip**, or replace it with **2.6** or **v2.6**. - ```html { "main":[ @@ -31,7 +27,7 @@ To implement the _g2_ element into your page just import the `g2.html.js`. {"c":"cir","a":{"x":15,"y":150,"r":15,"fs":"snow"}} ]} - + ``` @@ -43,7 +39,7 @@ To implement the _g2_ element into your page just import the `g2.html.js`. {"c":"cir","a":{"x":15,"y":150,"r":15,"fs":"snow"}} ]} - + The overall styling is handled by respective properties of the `g-2` element: diff --git a/docs/microjam.md/g2.mec.html b/docs/microjam.md/g2.mec.html index 168334c..13d1888 100644 --- a/docs/microjam.md/g2.mec.html +++ b/docs/microjam.md/g2.mec.html @@ -4,7 +4,7 @@ - + g2.mec @@ -33,7 +33,8 @@

      • Insert
      • Animation
      • Interactivity
      • -
      • Custom HTML Element
      • +
      • Custom HTML element
      • +
      • Custom chart element

        diff --git a/docs/navigation.md b/docs/navigation.md index faf9568..1255419 100644 --- a/docs/navigation.md +++ b/docs/navigation.md @@ -8,7 +8,8 @@ - [Insert]({base}/microjam.md/Insert.html) - [Animation]({base}/microjam.md/Animation.html) - [Interactivity]({base}/microjam.md/Interactivity.html) - - [Custom HTML Element]({base}/microjam.md/g2.html.html) + - [Custom HTML element]({base}/microjam.md/g2.html.html) + - [Custom chart element]({base}/microjam.md/g2.chart.html.html) --- diff --git a/src/g2.chart.element.js b/src/g2.chart.element.js index 5388eae..18a0bc5 100644 --- a/src/g2.chart.element.js +++ b/src/g2.chart.element.js @@ -40,8 +40,6 @@ class G2ChartElement extends HTMLElement { this._ctx = this._root.getElementById('cnv').getContext('2d'); - this._g = g2().del().clr().view({ cartesian: true }); - const t = 35; this._chart = { x: t, @@ -59,6 +57,8 @@ class G2ChartElement extends HTMLElement { funcs: () => this.funcs }; + this._g = g2().del().clr().view({ cartesian: true }).chart(this._chart); + try { // If not true, the element should be referenced by another module. if (this.innerHTML !== '') { @@ -73,7 +73,7 @@ class G2ChartElement extends HTMLElement { funcs[itr].fn = (() => Function('"use strict"; return (' + a[4] + ')')())(); itr++; } - this.funcs = funcs; + this.funcs = funcs; } } catch (e) { @@ -81,10 +81,11 @@ class G2ChartElement extends HTMLElement { this._g.txt({ str: e, y: 5 }); } finally { - this._g.chart(this._chart).nod({ + this._g.nod({ x: () => this.nod && this.nod().x, y: () => this.nod && this.nod().y, - scl: () => this.nod && this.nod().scl || 0}); + scl: () => this.nod && this.nod().scl || 0 + }); this.render(); } } @@ -93,8 +94,9 @@ class G2ChartElement extends HTMLElement { this._g.exe(this._ctx); } - setChart(chart) { - this._chart = chart; + setFuncs(funcs) { + this.funcs = funcs; + return this; } disconnectedCallback() {