Angular2 ng2图表自定义?

Has*_*hif 8 javascript charts chart.js ng2-charts angular

我已经开始使用angular2了ng2-chart.我对使用angular2 ng2-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>
Run Code Online (Sandbox Code Playgroud)

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

Config.ts:

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)'
      }]
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

Yas*_*ser 1

I couldn't find the best answer to your first question. However you can define multiple datasets with no intersection and use different colours (see the point 2) for that one.

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

For the second one when you define colours, as you are already doing it in your code:

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)'
  }
Run Code Online (Sandbox Code Playgroud)

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

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

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