app.component.ts
从'@angular/core'导入{组件};从ng2-charts/ng2-charts"导入{ BaseChartDirective };@零件({选择器:'应用程序根',templateUrl: './app.component.html',styleUrls: ['./app.component.css']})导出类 AppComponent {私人号码:任何[] = [{标签:测试",data: [10,2,6,7]//应该在图表中造成 4 个点}];私人_chartOpt:任何= {响应式:真};公共 _chartColours:any[] = [];私有_标签:字符串[] = [];私人_chartLegend:布尔=假;私人 _chartType: string = "line";}
chart.js 库是通过 angular-cli.json 添加的:
脚本":[../node_modules/chart.js/dist/Chart.bundle.min.js"],
我真的不知道我哪里错了.我尝试了许多不同的选项(responsive: true/false、maintainAspectRatio : true/false、将 <canvas> 包装在 ><div> 并且没有,CSS 样式,宽度/高度属性,...),但似乎无法让这个工作.我什至将 ng2-charts 版本降级为我曾经使用过的版本,但没有运气.
如果您需要更多代码,请告诉我.
解决方案
labels 属性必须与值一起定义.根据 properties 的文档:
<块引用>
labels (?Array) - x 轴标签.图表是必需的:折线、条形和雷达.
因此,为标签指定实际值就可以了.
I'm starting new Angular application which I initialised with Angular-CLI (beta 22.1). But when I add test-data (5 values) to my chart it seems to scale wrong and only shows the first two values and streched them over the whole length of the graph (see picture).
The project is otherwise blank and does not contain any CSS whatsoever.
My app.component.html:
<div>
<canvas baseChart [datasets]="_numbers" [labels]="_labels" [options]="_chartOpt" [colors]="_chartColours" [legend]="_chartLegend" [chartType]="_chartType" width="600px">
</canvas>
<div>
The app.component.ts
import { Component } from '@angular/core';
import { BaseChartDirective } from "ng2-charts/ng2-charts";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
private _numbers: any[] = [
{
label: "Test",
data: [10,2,6,7] // should cause 4 points in the chart
}
];
private _chartOpt: any = {
responsive: true
};
public _chartColours:any[] = [];
private _labels: string[] = [];
private _chartLegend: boolean = false;
private _chartType: string = "line";
}
The chart.js library is added via the angular-cli.json:
"scripts": [
"../node_modules/chart.js/dist/Chart.bundle.min.js"
],
I really don't know where I'm going wrong. I've tried many different options (responsive: true/false, maintainAspectRatio : true/false, wrapping the <canvas> in <div> and without, CSS Styles, width/height attributes, ...) but can't seem to get this one to work. I even downgraded the ng2-charts version to one that I reviously worked with, but with no luck.
Let me know if you need anymore code.
解决方案
labels property must be defined along with values. According to documentation for properties:
labels (?Array) - x axis labels. It's necessary for charts: line, bar and radar.
So specifying actual values for labels will do the trick.
这篇关于Ng2-Charts Linechart 仅显示前 2 个两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
相关推荐
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...