Angular2 ng2-chart customizations?

Hassan Kashif picture Hassan Kashif · Nov 19, 2016 · Viewed 10k times · Source

I have started to use angular2 ng2-chart. I have few questions regarding the below image which I created using angular2 ng2-chart but still want to do more customization:

Sample Chart

Questions:

1) How can I draw a dotted-line between two points when there are no values like in above image Nov-7 has value 0 (zero)?

2) How can I make a shadow effect, opacity or a combination of more than one colors?

3) How can I get the value of y-axis when I hover on any of the defined point and also if I want to change the y-axis grid color on mouse hover. What is the best way to do it using ng2-chart hover function?

Current sample code and config file:

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);
  }
}

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)'
      }]
    }
  }
});

Answer

Yaser picture Yaser · Nov 28, 2016

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)'
  }

The last number in rgba is the opacity. For having different colours the option is to define multiple datasets, otherwise it randomises the colours and you won't get mixed ones. A plunker here:

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

For the last question regarding getting the value of x-axis, look at the event which is logged to console on bounded events.