How to make Chart.js Bubble chart with ng2-chart?

后端 未结 2 1543
温柔的废话
温柔的废话 2020-12-22 00:57

Chart.js supports bubble charts, but the ng2-chart docs don\'t mention them. Is it possible to build a bubble chart with ng2-charts? Any pointer to a working ex

2条回答
  •  再見小時候
    2020-12-22 01:17

    The npm package ng2-charts now supports bubble charts (as of version 2.0.0-beta.10)

    Here's the snippet from the demo app:

    Code:

    import { Component, OnInit } from '@angular/core';
    import { ChartOptions, ChartType, ChartDataSets } from 'chart.js';
    import { Color } from 'ng2-charts';
    
    @Component({
      selector: 'app-bubble-chart',
      templateUrl: './bubble-chart.component.html',
      styleUrls: ['./bubble-chart.component.scss']
    })
    export class BubbleChartComponent implements OnInit {
      public bubbleChartOptions: ChartOptions = {
        responsive: true,
        scales: {
          xAxes: [{ ticks: { min: 0, max: 30, } }],
          yAxes: [{ ticks: { min: 0, max: 30, } }]
        }
      };
      public bubbleChartType: ChartType = 'bubble';
      public bubbleChartLegend = true;
    
      public bubbleChartData: ChartDataSets[] = [
        {
          data: [
            { x: 10, y: 10, r: 10 },
            { x: 15, y: 5, r: 15 },
            { x: 26, y: 12, r: 23 },
            { x: 7, y: 8, r: 8 },
          ],
          label: 'Series A',
        },
      ];
    
      public bubbleChartColors: Color[] = [
        {
          backgroundColor: [
            'red',
            'green',
            'blue',
            'purple',
          ]
        }
      ];
    
      constructor() { }
    
      ngOnInit() {
      }
    }
    

    (I am a collaborator on that package).

提交回复
热议问题