2

我正在尝试将 ZingChart 集成为 GrapesJs 中的自定义组件类型。我遵循了一些示例并实现了以下插件。

块.js

import { lineChartRef, chartType } from './consts';
export default (editor, opt = {}) => {
   const c = opt;
   const bm = editor.BlockManager;

   if (c.blocks.indexOf(lineChartRef) >= 0) {
       bm.add(lineChartRef, {
           label: c.labelLineChart,
           content: `
               <div data-gjs-type="${chartType}" id="myChart"></div>
           `
       });
   }
};


组件.js

import { chartType } from './consts';

export default (editor, opt = {}) => {
    const domc = editor.DomComponents;
    const defaultType = domc.getType('default');
    const defaultModel = defaultType.model;

    domc.addType(chartType, {
        model: defaultModel.extend(
            {
                defaults: {
                    ...defaultModel.prototype.defaults,
                    script: function() {
                        if (typeof zingchart == 'undefined') {
                            var script = document.createElement('script');
                            script.src =
                                'https://cdn.zingchart.com/zingchart.min.js';
                            document.body.appendChild(script);
                        }
                    }
                }
            },
            {
                isComponent: el => {
                    if (
                        el.getAttribute &&
                        el.getAttribute('data-gjs-type') === chartType
                    ) {
                        return {
                            type: chartType
                        };
                    }
                }
            }
        ),
        view: {
            onRender() {
                renderZingChart.bind(this)();
            }
        }
    });

    function renderZingChart() {
        const data = {
            type: 'bar',
            title: {
                text: 'Data Basics',
                fontSize: 24
            },
            legend: {
                draggable: true
            },
            scaleX: {
                // Set scale label
                label: { text: 'Days' },
                // Convert text on scale indices
                labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            scaleY: {
                label: { text: 'Temperature (°F)' }
            },
            plot: {
                animation: {
                    effect: 'ANIMATION_EXPAND_BOTTOM',
                    method: 'ANIMATION_STRONG_EASE_OUT',
                    sequence: 'ANIMATION_BY_NODE',
                    speed: 275
                }
            },
            series: [
                {
                    // plot 1 values, linear data
                    values: [23, 20, 27, 29, 25, 17, 15],
                    text: 'Week 1'
                },
                {
                    // plot 2 values, linear data
                    values: [35, 42, 33, 49, 35, 47, 35],
                    text: 'Week 2'
                },
                {
                    // plot 2 values, linear data
                    values: [15, 22, 13, 33, 44, 27, 31],
                    text: 'Week 3'
                }
            ]
        };
        const chart = {
            id: 'myChart',
            data
        };

        zingchart.render(chart);
    }
};

index.js

import grapesjs from 'grapesjs';
import loadBlocks from './blocks';
import loadComponents from './components';
import { lineChartRef } from './consts';

export default grapesjs.plugins.add('fndy-charts', (editor, opts = {}) => {
    let c = opts;

    let defaults = {
        blocks: [lineChartRef],
        defaultStyle: 1,
        labelLineChart: 'Line Chart'
    };

    // Load defaults
    for (let name in defaults) {
        if (!(name in c)) c[name] = defaults[name];
    }

    loadBlocks(editor, c);
    loadComponents(editor, c);
});

常量.js

export const lineChartRef = 'line-chart';
export const chartType = 'chart';

当我将块添加到画布时,它会呈现,但里面的 ZingChart 不会。我已经尝试过的一些事情:

  1. 验证是否正在调用 ZingChart 渲染函数。
  2. 尝试将 renderZingChart 函数调用移动到不同的组件挂钩。具体来说,component:mount、view.init() 和 view.onRender()。
  3. 将 renderZingChart 函数调用作为 script.onload 回调移动到脚本函数。可以在此处找到类似的示例:https ://grapesjs.com/docs/modules/Components-js.html#basic-scripts 。这确实正确呈现了 ZingChart,但感觉不正确,并且由于脚本函数在 GrapesJs 范围之外运行,因此不允许我传入参数。

我的想法已经用完了,所以任何指导都会很棒!谢谢!

4

2 回答 2

2

我正在用echarts做一个图表组件库,渲染图表的方法也差不多。我看到的唯一缺少的是元素的id. 这是 zing 用于呈现图表的属性。

我做了一个小例子,它显然没有准备好生产,因为块的 id 是静态的。这专门解决了渲染问题,使 id 动态化,您可以监听component:add事件并将模型 id 添加为属性。

const plugin = editor => {
  const {
    BlockManager: bm
  } = editor;
  bm.add("mychart", {
    label: "Chart",
    content: {
      tagName: "div",
      attributes: {
        id: 'myChart'
      },
      style: {
        width: "300px",
        height: "300px"
      },
      script: function() {
        const init = () => {
          const data = {
            type: "bar",
            title: {
              text: "Data Basics",
              fontSize: 24
            },
            legend: {
              draggable: true
            },
            scaleX: {
              // Set scale label
              label: {
                text: "Days"
              },
              // Convert text on scale indices
              labels: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
            },
            scaleY: {
              label: {
                text: "Temperature (°F)"
              }
            },
            plot: {
              animation: {
                effect: "ANIMATION_EXPAND_BOTTOM",
                method: "ANIMATION_STRONG_EASE_OUT",
                sequence: "ANIMATION_BY_NODE",
                speed: 275
              }
            },
            series: [{
                // plot 1 values, linear data
                values: [23, 20, 27, 29, 25, 17, 15],
                text: "Week 1"
              },
              {
                // plot 2 values, linear data
                values: [35, 42, 33, 49, 35, 47, 35],
                text: "Week 2"
              },
              {
                // plot 2 values, linear data
                values: [15, 22, 13, 33, 44, 27, 31],
                text: "Week 3"
              }
            ]
          };
          const chart = {
            id: this.id,
            data
          };

          zingchart.render(chart);
        };
        if (typeof zingchart == "undefined") {
          var script = document.createElement("script");
          script.onload = init;
          script.src = "https://cdn.zingchart.com/zingchart.min.js";
          document.body.appendChild(script);
        } else {
          init();
        }
      }
    }
  });
};
const editor = grapesjs.init({
  container: "#gjs",
  fromElement: true,
  height: "100vh",
  width: "auto",
  storageManager: false,
  panels: {
    defaults: []
  },
  plugins: ["gjs-preset-webpage", plugin]
});

您可以在此处检查图表正在呈现。 密码笔

希望这就够了,干杯!

于 2020-02-04T09:59:27.660 回答
-1

我认为您不需要编写非常复杂的代码来使用 Zing 图表。我将添加一个小示例代码来制作图表块元素,因此当您拖放块元素时,它将使图表成为gjs div 的grapesjs 。我正在使用Highcharts。

editor.BlockManager.add('Blockhighcharts', {
    label: 'Highchart',
    category: 'CHART',
    attributes: { class:'gjs-fonts gjs-f-b1' },
    content: {
    script: function () {
var container = "container"+Math.floor(Math.random() * 100);
$(this).attr("id",container);    
$('#gridly_div').append($(this));
    var myChart = Highcharts.chart(container, {
        chart: {
            type: 'bar',
        },
        title: {
            text: 'Fruit Consumption'
        },
        xAxis: {
            categories: ['Apples', 'Bananas', 'Oranges']
        },
        yAxis: {
            title: {
                text: 'Fruit eaten'
            }
        },
        series: [{
            name: 'Jane',
            data: [1, 0, 4]
        }, {
            name: 'John',
            data: [5, 7, 3]
        }]
    });

将显示图表的 HTML 代码如下。

 <div id="gjs" style="height:0px; overflow:hidden;">
  <style>
    #gjs{
      height:  100%;
      width: 100%;
      margin: 0;
    }
  </style>
<div id='gridly_div' class="gridly">
</div>

于 2020-03-23T08:49:47.877 回答