【问题标题】:Angular-chart / line chart with multiple horizontal lines (margins)具有多条水平线(边距)的角图/折线图
【发布时间】:2016-05-26 22:27:14
【问题描述】:

我正在尝试创建一个有四条水平线(边距 - 两个上边距和两个下边距)的斜线图。请看这个小提琴 - https://jsfiddle.net/CypressMountain/arq34fcu/30/

我的目标是在角度控制器内定义这些线的属性(值、颜色、标签),而不是在 JQuery 折线图扩展内定义,因为它目前在小提琴中完成。图形属性,以及边缘线属性将来自后端,并且边缘线将独立于图形绘制。

我不知道如何在控制器中实现 $scope.margins = [] 之类的东西,类似于我们对 $scope.data = [] 或 $scope.labels... 的任何帮助。

这是 HTML:

        <canvas id="line" class="chart chart-linebis" chart-data="data"
            chart-labels="labels" chart-legend="true" chart-series="series"
            chart-click="onClick">
        </canvas> 

当折线图类型被扩展时,边线现在在绘图函数中定义

    draw: function () {
    Chart.types.Line.prototype.draw.apply(this, arguments);

        var lines = 
    [
        {
            label: 'Upper margin 1',
            value: 90,
            color: 'rgba(255, 0, 0, .9)'
        },
        {
            label: 'Upper margin 2',
            value: 75,
            color: 'rgba(255, 165, 0, .8)'
        },
        {
            label: 'Lower margin 1',
            value: -10,
            color: 'rgba(0, 165, 255, .8)'
        },
        {
            label: 'Lower margin 2',
            value: -25,
            color: 'rgba(0, 0, 255, .8)'
        }
    ]

.............................

这是控制器:

angular.module('test', ['chart.js']);

angular.module('test').controller('TestCtrl', function ($scope) {

$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.series = ['Series A'];
$scope.data = [
  [-5, 48, 40, -19, 86, 27, 90]
];
});

引用了之前的两篇文章 angular-chart add horizontal lineChart.js - draw horizontal line

【问题讨论】:

    标签: angularjs chart.js angular-chart


    【解决方案1】:

    我终于解决了,希望这能帮助其他可能有类似任务的人。

    $scope.options 是角度控制器内部的位置,从后端接收边缘线的属性并分配给 $scope.options(您将替换当前的硬编码标签、值和颜色)动态值的水平线)。

        $scope.options = {
    
            limitLines: [
                {
                    label: 'Upper margin 1',
                    value: 90,
                    color: 'rgba(255, 0, 0, .9)'
                },
                {
                    label: 'Upper margin 2',
                    value: 75,
                    color: 'rgba(255, 165, 0, .8)'
                },
                {
                    label: 'Lower margin 1',
                    value: -10,
                    color: 'rgba(0, 165, 255, .8)'
                },
                {
                    label: 'Lower margin 2',
                    value: -25,
                    color: 'rgba(0, 0, 255, .8)'
                }
            ]
    
    }
    

    然后HTML中的canvas标签会添加选项:

    chart-options="options"
    

    最后,在折线图扩展代码中,绘制函数中的“lines”将通过选项桥接到“limitLines”:

        draw: function () {
        Chart.types.Line.prototype.draw.apply(this, arguments);
    
        var lines = this.options.limitLines;
    

    【讨论】:

    • 它确实有效,非常感谢,你帮了我很多:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    相关资源
    最近更新 更多