kendo-ui用于Angular自定义LegendItemComponent

时间:2018-07-03 08:15:42

标签: angular kendo-ui kendo-ui-angular2

大家早上好。我需要在甜甜圈图中自定义我的图例项目。 在阅读文档时,我需要定义LegendItemVisualArgs对象并初始化createVisual()方法。

在这里,我展示了我的完成方式:

export class CustomLegendItemViewModel implements LegendItemVisualArgs {
options: any;
pointIndex: any;
sender: ChartComponent;
series: any;

createVisual(): Circle {
    const geometry = new GeomCircle([10, 10], 10);

    return new Circle(geometry);
}

之后,我在Angular组件中创建了该对象:

export class ChartItemComponent implements OnInit, OnDestroy {
  visual: CustomLegendItemViewModel = new CustomLegendItemViewModel();
}

在HTML代码中,我尝试将自定义图例项目视图绑定到我的对象:

<kendo-chart>
            <kendo-chart-series>
                <kendo-chart-series-item [holeSize]="100"
                        type="donut" [data]="data2" [size]="20"
                        categoryField="kind" field="share">
                </kendo-chart-series-item>
            </kendo-chart-series>
            <kendo-chart-legend [visible]="true">
                <kendo-chart-legend-item [visual]="visual"></kendo-chart-legend-item>
            </kendo-chart-legend>
        </kendo-chart>

我遇到了

的错误
Uncaught TypeError: customVisual is not a function
at LegendItem.renderVisual (legend-item.js:112)
at LegendLayout.render (legend-layout.js:33)
at Legend.createItems (legend.js:101)
at new Legend (legend.js:27)
at Chart._getModel (chart.js:463)
at Chart._redraw (chart.js:279)
at Chart._noTransitionsRedraw (chart.js:1284)
at Chart._resize (chart.js:134)
at Chart.resize (chart.js:128)
at ChartComponent.push../node_modules/@progress/kendo-angular-charts/dist/es/chart.component.js.ChartComponent.resize (chart.component.js:389)

有人有什么想法如何解决吗?或只是发送一些参考来定制示例。

谢谢!

2 个答案:

答案 0 :(得分:1)

基于API,您需要向function输入提供visual。 (API Reference

在下面的代码中,我将功能visualFn绑定到输入visual。 (Working example

html

<kendo-chart>
    ...
    <kendo-chart-legend [visible]="true">
        <kendo-chart-legend-item [visual]="visualFn"></kendo-chart-legend-item>
    </kendo-chart-legend>
</kendo-chart>

组件

public visualFn(e: SeriesVisualArgs): Group {
    const geometry = new GeomCircle([10, 10], 10);
    return new Circle(geometry);
}

答案 1 :(得分:0)

我可以像这样在 angular 上实现这一点。基于 jquery 版本 -> http://dojo.telerik.com/iliGOJ/7

组件

  public visualFn(e: SeriesVisualArgs): Layout {

    var color = e.options.markers.background;
    var labelColor = e.options.labels.color;
    var rect = new Rect([0, 0], [100, 50]);
    var layout = new Layout(rect, {
      spacing: 5,
      alignItems: "center"
    });

    var circleGeometry = new GeomCircle([0, 10], 4);
    var circle = new Circle(circleGeometry, {
      stroke: { color: "none" },
      fill: {color: color}
    });
    var marker = circle;

    var label = new Text(e.series.name, [0, 0], {
      fill: { color: labelColor }
    });

    layout.append(marker, label);
    layout.reflow();

    return layout;
  }

html

<kendo-chart-legend position="bottom" [offsetX]="200">
    <kendo-chart-legend-item [visual]="visualFn"></kendo-chart-legend-item>
</kendo-chart-legend>