简体   繁体   English

在Chart.js中使用ng-class

[英]Use of ng-class with Chart.js

I'm using the angular-charts module, which is based on Charts.js. 我正在使用基于Charts.js的angular-charts模块。 It works pretty great, but now I'm trying to change my app in a way I can select many options from a combobox, so the chart gets recompiled and refreshed to the new type of chart I want. 它的效果非常好,但是现在我试图以一种可以从组合框选择许多选项的方式来更改我的应用程序,以便重新编译图表并将其刷新为所需的新型图表。

I tried using ng-class for this purpose, but it somehow doesn't work (the chart just never gets compiled; the DOM element is there but you don't actually see anything). 我为此目的尝试使用ng-class,但是它某种程度上是行不通的(图表从未被编译过; DOM元素在那里,但您实际上什么也看不到)。

This is my chart and my selector: 这是我的图表和选择器:

<div class="main-chart" ng-show="showChart" ng-cloak>
    <span class="label title">{{title}}</span>
    <canvas id="grafico" ng-class="chartclass"
    chart-data="data" chart-labels="labels">
    </canvas>
    <select name="typeSelector" id="chartType" class="form-control" ng-model="type" ng-change="changeChart()">
        <option ng-selected="true" value="doughnut">Circle</option>
        <option value="bars">Bars</option>
    </select>
</div>

These are the parts involved from my controller, the two variables (one for the selector and one for the chart class), and the function that changes the class after a click is performed: 这些是控制器中涉及的部分,两个变量(一个用于选择器,一个用于图表类),以及在单击后更改类的函数:

$scope.type = "";
$scope.chartclass = "chart chart-doughnut";

$scope.changeChart = function(){

    $scope.chartclass = "chart chart-"+$scope.type;
}

The class name gets properly changed, but I just don't see anything. 类名正确更改,但是我什么都没看到。 Maybe I have to call to a $compile function, use a custom directive with a link, or something? 也许我必须调用$ compile函数,使用带有链接的自定义指令,还是其他? I'm pretty much new in Angular so I would love to see a good suggestion! 我是Angular的新手,所以我很乐意看到一个不错的建议!

Thanks! 谢谢!

EDIT: Let me repeat: the class name gets properly changed! 编辑:让我重复一遍:类名正确更改! The problem isn't about changing the class name, which works, the problem is with the actual chart showing off. 问题不在于更改类名有效,而是与实际的图表显示有关。 It keeps hidden, like if it never was compiled. 它保持隐藏状态,就像从未编译过一样。

EDIT 2: Check it out in these images. 编辑2:在这些图像中出。 First image shows the normal working with "class" attribute, second one shows what we're trying, which is selecting the class name dynamically (with ng-class). 第一张图片显示了正常使用“ class”属性,第二张图片显示了我们正在尝试的内容,它是动态选择类名称(使用ng-class)。 As I said, the DOM gets correctly changed, so the class name is ok! 就像我说的那样,DOM得到了正确的更改,因此类名还可以! The problem is, that somehow it just doesn't show the chart: 问题是,某种程度上它没有显示图表:

http://imgur.com/a/SJbzg http://imgur.com/a/SJbzg

EDIT 3: This is what I currently have: 编辑3:这是我目前拥有的:

Simple variable declaration on the controller, initialized for showing the Doughnut chart type: 控制器上的简单变量声明,已初始化以显示甜甜圈图类型:

$scope.chartType = "Doughnut";

Here, the DOM: 在这里,DOM:

<div class="main-chart" ng-show="showChart" ng-cloak>
    <span class="label title">{{title}}</span>
<canvas id="grafico" chart-type="chartType"
    chart-data="data" chart-labels="labels">
</canvas>
    <select name="typeSelector" id="typeOfChart" class="form-control" ng-model="chartType">
        <option ng-selected="true" value="Doughnut">Circle</option>
        <option value="Bar">Bars</option>
    </select>
</div>

What am I doing wrong? 我究竟做错了什么? Why the chart never gets rendered? 为什么图表永远不会渲染?

The way to fix this, is to choose the class: 解决此问题的方法是选择类:

class="chart-base"

And then, pass the type you want in a variable, to the attribute "chart-type": 然后,将所需的类型输入变量“ chart-type”:

chart-type="type"

You can choose one of the following types: Line, Bar, Radar, PolarArea, Pie, Doughnut. 您可以选择以下类型之一:线,条,雷达,PolarArea,饼图,甜甜圈。

Thanks to @PankajParkar for some of the help! 感谢@PankajParkar提供的一些帮助!

If you current element is inside ng-repeat / ng-if then type inside your content & type inside controller will be different. 如果您目前的元素里面ng-repeat / ng-if然后type您的内容与内部type的内部控制会有所不同。

I'd say do it on HTML it self, if its simple operation 我想说的是,如果操作简单,就可以在HTML上自行完成

ng-class="'chart chart-'+ type"

Update 更新

In order to use chart in which you can change the type dynamically, you must set the class to this: 为了使用可以在其中动态更改类型的图表,必须将类设置为此:

class="chart-base"

After this, you need to specify a chart-type attribute with scope value/expression( this line will read up value & place watch over that value by this line , so that it will ensure chart will get re-draw on type change). 之后,您需要指定一个具有作用域值/表达式的chart-type属性( 此行将读取值并通过此行监视该值,以确保图表在类型更改时能够重新绘制)。 When type value get changed it will automatically re render chart with correct type. 当类型值更改时,它将自动使用正确的类型重新呈现图表。 Chart re-rendering needs it and the class-base. 图表重新呈现需要它和基于类的。 This would be a working example: 这将是一个工作示例:

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

Below are the valid chart type which you can use (took it from here ) 以下是您可以使用的有效图表类型( 从此处获取

.directive('chartBase', function (ChartJsFactory) { return new ChartJsFactory(); })
.directive('chartLine', function (ChartJsFactory) { return new ChartJsFactory('Line'); })
.directive('chartBar', function (ChartJsFactory) { return new ChartJsFactory('Bar'); })
.directive('chartRadar', function (ChartJsFactory) { return new ChartJsFactory('Radar'); })
.directive('chartDoughnut', function (ChartJsFactory) { return new ChartJsFactory('Doughnut'); })
.directive('chartPie', function (ChartJsFactory) { return new ChartJsFactory('Pie'); })
.directive('chartPolarArea', function (ChartJsFactory) { return new ChartJsFactory('PolarArea'); });

So type value can be Line , Bar , Radar , PolarArea , Pie , Doughnut . 因此,类型值可以是LineBarRadarPolarAreaPieDoughnut

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM