gpt4 book ai didi

javascript - Angular2 ng2-图表自定义?

转载 作者:数据小太阳 更新时间:2023-10-29 05:37:44 24 4
gpt4 key购买 nike

我已经开始使用 angular2 ng2-chart。对于我使用 angular2 ng2-chart 创建的下图,我有几个问题,但仍想进行更多自定义:

Sample Chart

问题:

1) 当没有像上图 Nov-7 中那样的值为 0(零)的值时,如何在两点之间画一条虚线?

2) 如何制作阴影效果、不透明度或多种颜色的组合?

3) 当我将鼠标悬停在任何定义的点上时以及如果我想在鼠标悬停时更改 y 轴网格颜色时如何获取 y 轴的值。使用 ng2-chart 悬停功能的最佳方法是什么?

当前示例代码和配置文件:

index.html

<div class="container">
<div class="row">
<div class="overview-page">
<div class="overview-page-title">
<h2>Overview</h2>
</div>
<div class="chart-view">
<canvas baseChart
class="chart"
[datasets]="charts.datasets"
[labels]="charts.labels"
[colors]="charts.chartColors"
[options]="charts.options"
[legend]="false"
[chartType]="charts.type"
(chartHover)="chartHovered($event)">
</canvas>
</div>
</div>
</div>
</div>

index.component.ts

import {Component, Output, EventEmitter, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {Config} from '../../../config/config';

@Component({
templateUrl: 'index.html',
styleUrls: ['../../../../common/stylesheets/pages/index.scss']
})

export class IndexComponent implements OnInit {

protected charts: any;

ngOnInit() {
this.charts = (<any>Config.get('test')).charts;
console.log(this.charts);
}

chartHovered(e:any):void {
console.log(e);
}
}

配置文件:

import * as Immutable from 'immutable';
export const Config = Immutable.Map({
test: {
charts: {
datasets: [{
data: [40, 48.2, 0, 52.6, 51.1, 57.6, 74.8]
}],
labels: ['Nov 5', 'Nov 6', 'Nov 7', 'Nov 8', 'Nov 9', 'Nov 10', 'Nov 11'],
type: 'line',
options: {
scales: {
xAxes: [{
gridLines: {
color: 'rgba(171,171,171,1)',
lineWidth: 1
}
}],
yAxes: [{
ticks: {
beginAtZero: true,
max: 100,
min: 0,
stepSize: 25
},
gridLines: {
color: 'rgba(171,171,171,1)',
lineWidth: 0.5
}
}]
},
responsive: true
},
chartColors: [{
backgroundColor: 'rgba(25,10,24,0.2)',
borderColor: 'rgba(225,10,24,0.2)',
pointBackgroundColor: 'rgba(225,10,24,0.2)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(225,10,24,0.2)'
}]
}
}
});

最佳答案

对于您的第一个问题,我找不到最佳答案。但是,您可以定义多个没有交集的数据集,并为该数据集使用不同的颜色(参见第 2 点)。

http://valor-software.com/ng2-charts/

对于第二个,当您定义颜色时,您已经在代码中这样做了:

chartColors: [{
backgroundColor: 'rgba(25,10,24,0.2)',
borderColor: 'rgba(225,10,24,0.2)',
pointBackgroundColor: 'rgba(225,10,24,0.2)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(225,10,24,0.2)'
}

rgba 中的最后一个数字是不透明度。对于不同的颜色,选项是定义多个数据集,否则它会随机化颜色并且您不会得到混合的颜色。一个笨蛋在这里:

http://plnkr.co/edit/9PckMZiDYZjRz1PA0Suq

关于获取 x 轴值的最后一个问题,请查看在有界事件上记录到控制台的事件。

关于javascript - Angular2 ng2-图表自定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40697214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com