angular-chart.js中的水平条形图

时间:2015-11-10 16:21:38

标签: javascript angularjs chart.js angular-chart angular-chartist.js

我已经在angular-chart.js中成功创建了条形图,但现在我想将其更改为水平条形图。另外,我希望将字段放在水平条本身内:

 angular.module("app", ["chart.js"])
    .controller("LineCtrl", function ($scope) {

    var x =
    {
        "labels": [
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"
        ],
        "data": [
            99,
            59,
            80,
            81,
            56,
            55,
            40
        ],
        "type": "line",
        "series": "eventTypePerDay"
    }

    console.log(x.series);
    //ses all of the variables to build the chart
    $scope.labels = x.labels;
    $scope.colours = ['#71d078'];
    $scope.type = x.type;
    $scope.data = [x.data];
    $scope.series = [x.series];


});

我想要的例子

enter image description here

3 个答案:

答案 0 :(得分:4)

引擎盖下angular-chart.js使用chart.js,它没有水平条形图的原生支持。也就是说,可以添加一个水平条形图插件来支持这种类型的图表:https://github.com/tomsouthall/Chart.HorizontalBar.js

我相信要做到这一点,你需要使用动态图表指令

<canvas id="base" class="chart-base" chart-type="type"
  chart-data="data" chart-labels="labels" chart-legend="true">
</canvas>

然后将类型指定为HorizontalBar

答案 1 :(得分:3)

对于那些后来想知道的人;有新版本的chart.js和angular-chart.js可用。 Chart.js 2.1及更高版本支持水平图表,angular-chart.js有一个新分支可以使用最新的chart.js版本see the github repo here

使用此版本不需要Tom Southall提到的上述Horizo​​ntalBar插件。

使用上面angular-chart.js网站提供的示例,并确保将class属性的值设置为&#34; chart-horizo​​ntalBar&#34;。

<canvas id="HorizontalBar" class="chart chart-horizontalBar" chart-data="data" chart-labels="labels" chart-series="series"></canvas>

答案 2 :(得分:0)

我认为现在不需要包含 Chart.Horizo​​ntalBar.js 。这是如何使用chart.js -

在Angular中获得简单的水平条形图

HTML -

<div>
    <canvas id="bar-chart-horizontal" width="800" height="450"></canvas>
</div>

JS控制器 -

var myChart = new Chart(document.getElementById("bar-chart-horizontal"), {
        type: 'horizontalBar',
        data: {
          labels: ["Africa", "Asia", "Europe", "Latin America", "North America"],
          datasets: [
            {
              label: "Population (millions)",
              backgroundColor: ["#ff0000", "#8e5ea2","#3cba9f","#454545","#c45850"],
              data: [2478,5267,734,784,433]
            }
          ]
        },
        options: {
          legend: { display: false },
          title: {
            display: true,
            text: 'Predicted world population (millions) in 2050'
          }
        }
    });