Kendo 图表与 Vue

Kendo diagram with Vue

我正在按照说明进行操作 https://www.telerik.com/kendo-vue-ui/components/diagram/ 但我是 运行 带有 .vue 文件的独立 Vue 应用程序。

开始,它在 kendo.dataviz 处失败,表示 kendo 未定义(在 visualTemplate 函数处)。

我看了STACKBLITZ中的代码,示例代码好像是匆忙拼凑起来的。有点 "let's import everything and not worry about it too much"。 index.js 中的导入也是如此。性能很重要,我不想在我的组件中导入超出我需要的内容。

https://stackblitz.com/edit/jhqgbd?file=index.js

那么,我实际上需要什么才能让它在 Vue 中工作?

import Vue from 'vue'
import '@progress/kendo-ui'
import '@progress/kendo-theme-default/dist/all.css'
import { DataSourceInstaller, DiagramInstaller } from '@progress/kendo-diagram-vue-wrapper'

Vue.use(DiagramInstaller);
Vue.use(DataSourceInstaller);

new Vue({
    el: '#vueapp',
    data: function() {
        return {
            diagramModel: {
                children: 'items'
            },
            diagramData: myData
        }
    },
    methods: {
        kendowidgetready: function (widget) {
            widget.bringIntoView(widget.shapes)
        },
        visualTemplate: function (options) {
            var dataviz = kendo.dataviz
            var g = new dataviz.diagram.Group()
            var dataItem = options.dataItem

            g.append(new dataviz.diagram.Rectangle({
                width: 210,
                height: 75,
                stroke: {
                    width: 0
                },
                fill: {
                    gradient: {
                    type: 'linear',
                    stops: [{
                        color: dataItem.colorScheme,
                        offset: 0,
                        opacity: 0.5
                    }, {
                        color: dataItem.colorScheme,
                        offset: 1,
                        opacity: 1
                    }]
                    }
                }
            }))

            g.append(new dataviz.diagram.TextBlock({
                text: dataItem.firstName + ' ' + dataItem.lastName,
                x: 85,
                y: 20,
                fill: '#fff'
            }))

            g.append(new dataviz.diagram.TextBlock({
                text: dataItem.title,
                x: 85,
                y: 40,
                fill: '#fff'
            }))

            g.append(new dataviz.diagram.Image({
                source: 'https://demos.telerik.com/kendo-ui/content/dataviz/diagram/people/' + dataItem.image,
                x: 3,
                y: 3,
                width: 68,
                height: 68
            }))

            return g
        }
    }
})

我创建了一个工作示例 here。我发现的唯一问题是 @progress/kendo-diagram-vue-wrapper 中不存在 DataSourceInstaller。我不得不从 @progress/kendo-datasource-vue-wrapper 导入它。

作为diagramData,我从link你的postet中获取了数据。

作为 index.html 我也从你的 link 那里拿走了一个,但做了一点清理。

完整示例

index.js

import Vue from 'vue';
import '@progress/kendo-ui';
import '@progress/kendo-theme-default/dist/all.css';
import { DataSourceInstaller } from '@progress/kendo-datasource-vue-wrapper';
import { DiagramInstaller } from '@progress/kendo-diagram-vue-wrapper';

import diagramData from './data';

Vue.use(DiagramInstaller);
Vue.use(DataSourceInstaller);

new Vue({
  el: '#vueapp',
  data() {
    return {
      diagramModel: {
        children: 'items',
      },
      diagramData,
    }
  },
  methods: {
    kendowidgetready(widget) {
      widget.bringIntoView(widget.shapes);
    },
    visualTemplate(options) {
      var dataviz = kendo.dataviz;
      var g = new dataviz.diagram.Group();
      var dataItem = options.dataItem;

      g.append(new dataviz.diagram.Rectangle({
        width: 210,
        height: 75,
        stroke: {
          width: 0,
        },
        fill: {
          gradient: {
            type: 'linear',
            stops: [{
              color: dataItem.colorScheme,
              offset: 0,
              opacity: 0.5,
            }, {
              color: dataItem.colorScheme,
              offset: 1,
              opacity: 1,
            }],
          },
        },
      }));

      g.append(new dataviz.diagram.TextBlock({
        text: dataItem.firstName + ' ' + dataItem.lastName,
        x: 85,
        y: 20,
        fill: '#fff',
      }));

      g.append(new dataviz.diagram.TextBlock({
        text: dataItem.title,
        x: 85,
        y: 40,
        fill: '#fff',
      }));

      g.append(new dataviz.diagram.Image({
        source: 'https://demos.telerik.com/kendo-ui/content/dataviz/diagram/people/' + dataItem.image,
        x: 3,
        y: 3,
        width: 68,
        height: 68,
      }));

      return g;
    },
  },
});

index.html

<!DOCTYPE html>
<html>

<head>
<title>Kendo UI for Vue Example Application</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
  <div id="vueapp">
    <kendo-hierarchicaldatasource 
      ref="remoteDataSourceComponent" 
      :data="diagramData" 
      schema-model-children="items">
    </kendo-hierarchicaldatasource>

    <kendo-diagram 
      data-source-ref="remoteDataSourceComponent" 
      layout-type="layered" 
      :shape-defaults-visual="visualTemplate"
      connection-defaults-stroke-color="#979797" 
      :connection-defaults-stroke-width="2" 
      @kendowidgetready="kendowidgetready">
    </kendo-diagram>
  </div>
</body>

</html>

data.js

export default [{
  firstName: 'Antonio',
  lastName: 'Moreno',
  image: 'antonio.jpg',
  title: 'Team Lead',
  colorScheme: '#1696d3',
  items: [{
    firstName: 'Elizabeth',
    image: 'elizabeth.jpg',
    lastName: 'Brown',
    title: 'Design Lead',
    colorScheme: '#ef6944',
    items: [{
      firstName: 'Ann',
      lastName: 'Devon',
      image: 'ann.jpg',
      title: 'UI Designer',
      colorScheme: '#ef6944'
    }]
  }, {
    firstName: 'Diego',
    lastName: 'Roel',
    image: 'diego.jpg',
    title: 'QA Engineer',
    colorScheme: '#ee587b',
    items: [{
      firstName: 'Fran',
      lastName: 'Wilson',
      image: 'fran.jpg',
      title: 'QA Intern',
      colorScheme: '#ee587b'
    }]
  }, {
    firstName: 'Felipe',
    lastName: 'Izquiedro',
    image: 'felipe.jpg',
    title: 'Senior Developer',
    colorScheme: '#75be16',
    items: [{
      firstName: 'Daniel',
      lastName: 'Tonini',
      image: 'daniel.jpg',
      title: 'Developer',
      colorScheme: '#75be16'
    }]
  }]
}]